hash_drbgtest.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <stdint.h>
  13. #include <gmssl/hex.h>
  14. #include <gmssl/digest.h>
  15. #include <gmssl/hash_drbg.h>
  16. #include <gmssl/error.h>
  17. #define EntropyInput "212956390783381dbfc6362dd0da9a09"
  18. #define Nonce "5280987fc5e27a49"
  19. #define PersonalizationString ""
  20. #define AdditionalInput ""
  21. #define V0 "02b84eba8121ca090b6b66d3371609eaf76405a5c2807d80035c1a13dfed5aa18e536af599a7b3c68b2c56240ed11997f4048910d84604"
  22. #define C0 "a677e4921587563eebe55d1b25e59c3f3d200bc61aaee665e7a6858c2857c45dba4bce8182252962ae86de491046a5e3450eec44938a0a"
  23. #define AdditionalInput1 ""
  24. #define EntropyInputPR1 "2edb396eeb8960f77943c2a59075a786"
  25. #define V1 "f9afadfbbf2c3d1004f9baca38be247342e5fbb83281915d5de18beb963712a344e89bb0e6b925a7bbc32eadb8b441efc1fa0c649df42a"
  26. #define C1 "1d41cbbd634909e4761c232fcfd6a6c2edf0a7f4d3d3c164f74a88955f355efce2d86c1e9fa897b7005ef9d4d3a51bf4fc0b805ab896c9"
  27. #define PR1 "2edb396eeb8960f77943c2a59075a786"
  28. #define PR2 "30b565b63a5012676940d3ef17d9e996"
  29. int main(void)
  30. {
  31. // currently we only has SHA-1 test suites
  32. #ifdef ENABLE_BROKEN_CRYPTO
  33. HASH_DRBG drbg;
  34. uint8_t entropy[sizeof(EntropyInput)/2];
  35. uint8_t nonce[sizeof(Nonce)/2];
  36. uint8_t personalstr[1 + sizeof(PersonalizationString)/2];
  37. uint8_t v[sizeof(V0)/2];
  38. uint8_t c[sizeof(C0)/2];
  39. uint8_t entropy_pr1[sizeof(EntropyInputPR1)/2];
  40. uint8_t pr1[sizeof(PR1)/2];
  41. uint8_t pr2[sizeof(PR2)/2];
  42. size_t entropy_len, nonce_len, personalstr_len, vlen, clen;
  43. size_t entropy_pr1len;
  44. size_t pr1_len, pr2_len;
  45. unsigned char out[640/8];
  46. int i;
  47. hex_to_bytes(EntropyInput, strlen(EntropyInput), entropy, &entropy_len);
  48. hex_to_bytes(Nonce, strlen(Nonce), nonce, &nonce_len);
  49. hex_to_bytes(PersonalizationString, strlen(PersonalizationString), personalstr, &personalstr_len);
  50. hex_to_bytes(V0, strlen(V0), v, &vlen);
  51. hex_to_bytes(C0, strlen(C0), c, &clen);
  52. hex_to_bytes(EntropyInputPR1, strlen(EntropyInputPR1), entropy_pr1, &entropy_pr1len);
  53. hex_to_bytes(PR1, strlen(PR1), pr1, &pr1_len);
  54. hex_to_bytes(PR2, strlen(PR2), pr2, &pr2_len);
  55. hash_drbg_init(&drbg, DIGEST_sha1(),
  56. entropy, entropy_len,
  57. nonce, nonce_len,
  58. personalstr, personalstr_len);
  59. printf("sha1_drbg test 1 ");
  60. if (drbg.seedlen != vlen
  61. || memcmp(drbg.V, v, vlen) != 0
  62. || memcmp(drbg.C, c, clen) != 0
  63. || drbg.reseed_counter != 1) {
  64. printf("failed\n");
  65. return 1;
  66. } else {
  67. printf("ok\n");
  68. }
  69. hash_drbg_reseed(&drbg, pr1, pr1_len, NULL, 0);
  70. hash_drbg_generate(&drbg, NULL, 0, 640/8, out);
  71. hash_drbg_reseed(&drbg, pr2, pr2_len, NULL, 0);
  72. hash_drbg_generate(&drbg, NULL, 0, 640/8, out);
  73. for (i = 0; i < sizeof(out); i++) {
  74. printf("%02x", out[i]);
  75. }
  76. printf("\n");
  77. #endif
  78. return 0;
  79. }