Reference implementations of PQC
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

222 lines
6.6 KiB

  1. #include "sha2.h"
  2. #include <stdio.h>
  3. #include <stdint.h>
  4. #include <string.h>
  5. #include <inttypes.h>
  6. const unsigned char plaintext[113] = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  7. const unsigned char plaintext2[128] = {
  8. 0xfd, 0x22, 0x03, 0xe4, 0x67, 0x57, 0x4e, 0x83, 0x4a, 0xb0, 0x7c, 0x90,
  9. 0x97, 0xae, 0x16, 0x45, 0x32, 0xf2, 0x4b, 0xe1, 0xeb, 0x5d, 0x88, 0xf1,
  10. 0xaf, 0x77, 0x48, 0xce, 0xff, 0x0d, 0x2c, 0x67, 0xa2, 0x1f, 0x4e, 0x40,
  11. 0x97, 0xf9, 0xd3, 0xbb, 0x4e, 0x9f, 0xbf, 0x97, 0x18, 0x6e, 0x0d, 0xb6,
  12. 0xdb, 0x01, 0x00, 0x23, 0x0a, 0x52, 0xb4, 0x53, 0xd4, 0x21, 0xf8, 0xab,
  13. 0x9c, 0x9a, 0x60, 0x43, 0xaa, 0x32, 0x95, 0xea, 0x20, 0xd2, 0xf0, 0x6a,
  14. 0x2f, 0x37, 0x47, 0x0d, 0x8a, 0x99, 0x07, 0x5f, 0x1b, 0x8a, 0x83, 0x36,
  15. 0xf6, 0x22, 0x8c, 0xf0, 0x8b, 0x59, 0x42, 0xfc, 0x1f, 0xb4, 0x29, 0x9c,
  16. 0x7d, 0x24, 0x80, 0xe8, 0xe8, 0x2b, 0xce, 0x17, 0x55, 0x40, 0xbd, 0xfa,
  17. 0xd7, 0x75, 0x2b, 0xc9, 0x5b, 0x57, 0x7f, 0x22, 0x95, 0x15, 0x39, 0x4f,
  18. 0x3a, 0xe5, 0xce, 0xc8, 0x70, 0xa4, 0xb2, 0xf8
  19. };
  20. const unsigned char expected_224[28] = {
  21. 0xc9, 0x7c, 0xa9, 0xa5, 0x59, 0x85, 0x0c, 0xe9, 0x7a, 0x04, 0xa9, 0x6d,
  22. 0xef, 0x6d, 0x99, 0xa9, 0xe0, 0xe0, 0xe2, 0xab, 0x14, 0xe6, 0xb8, 0xdf,
  23. 0x26, 0x5f, 0xc0, 0xb3
  24. };
  25. const unsigned char expected_256[32] = {
  26. 0xcf, 0x5b, 0x16, 0xa7, 0x78, 0xaf, 0x83, 0x80, 0x03, 0x6c, 0xe5, 0x9e,
  27. 0x7b, 0x04, 0x92, 0x37, 0x0b, 0x24, 0x9b, 0x11, 0xe8, 0xf0, 0x7a, 0x51,
  28. 0xaf, 0xac, 0x45, 0x03, 0x7a, 0xfe, 0xe9, 0xd1
  29. };
  30. const unsigned char expected_384[48] = {
  31. 0x09, 0x33, 0x0c, 0x33, 0xf7, 0x11, 0x47, 0xe8, 0x3d, 0x19, 0x2f, 0xc7,
  32. 0x82, 0xcd, 0x1b, 0x47, 0x53, 0x11, 0x1b, 0x17, 0x3b, 0x3b, 0x05, 0xd2,
  33. 0x2f, 0xa0, 0x80, 0x86, 0xe3, 0xb0, 0xf7, 0x12, 0xfc, 0xc7, 0xc7, 0x1a,
  34. 0x55, 0x7e, 0x2d, 0xb9, 0x66, 0xc3, 0xe9, 0xfa, 0x91, 0x74, 0x60, 0x39
  35. };
  36. const unsigned char expected_512[64] = {
  37. 0x8e, 0x95, 0x9b, 0x75, 0xda, 0xe3, 0x13, 0xda, 0x8c, 0xf4, 0xf7, 0x28,
  38. 0x14, 0xfc, 0x14, 0x3f, 0x8f, 0x77, 0x79, 0xc6, 0xeb, 0x9f, 0x7f, 0xa1,
  39. 0x72, 0x99, 0xae, 0xad, 0xb6, 0x88, 0x90, 0x18, 0x50, 0x1d, 0x28, 0x9e,
  40. 0x49, 0x00, 0xf7, 0xe4, 0x33, 0x1b, 0x99, 0xde, 0xc4, 0xb5, 0x43, 0x3a,
  41. 0xc7, 0xd3, 0x29, 0xee, 0xb6, 0xdd, 0x26, 0x54, 0x5e, 0x96, 0xe5, 0x5b,
  42. 0x87, 0x4b, 0xe9, 0x09
  43. };
  44. const unsigned char expected_512_2[64] = {
  45. 0xa2, 0x1b, 0x10, 0x77, 0xd5, 0x2b, 0x27, 0xac, 0x54, 0x5a, 0xf6, 0x3b,
  46. 0x32, 0x74, 0x6c, 0x6e, 0x3c, 0x51, 0xcb, 0x0c, 0xb9, 0xf2, 0x81, 0xeb,
  47. 0x9f, 0x35, 0x80, 0xa6, 0xd4, 0x99, 0x6d, 0x5c, 0x99, 0x17, 0xd2, 0xa6,
  48. 0xe4, 0x84, 0x62, 0x7a, 0x9d, 0x5a, 0x06, 0xfa, 0x1b, 0x25, 0x32, 0x7a,
  49. 0x9d, 0x71, 0x0e, 0x02, 0x73, 0x87, 0xfc, 0x3e, 0x07, 0xd7, 0xc4, 0xd1,
  50. 0x4c, 0x60, 0x86, 0xcc
  51. };
  52. static int test_sha256_incremental(void) {
  53. unsigned char output[32];
  54. sha256ctx state;
  55. int i = 0;
  56. sha256_inc_init(&state);
  57. sha256_inc_blocks(&state, plaintext, 1);
  58. sha256_inc_finalize(output, &state, plaintext + 64, 112 - 64);
  59. if (memcmp(expected_256, output, 32) != 0) {
  60. printf("ERROR sha256 incremental did not match sha256.\n");
  61. printf(" Expected: ");
  62. for (i = 0; i < 32; i++) {
  63. printf("%02X", expected_256[i]);
  64. }
  65. printf("\n");
  66. printf(" Received: ");
  67. for (i = 0; i < 32; i++) {
  68. printf("%02X", output[i]);
  69. }
  70. printf("\n");
  71. return 1;
  72. }
  73. return 0;
  74. }
  75. static int test_sha512_incremental(void) {
  76. unsigned char output[64];
  77. sha512ctx state;
  78. int i = 0;
  79. sha512_inc_init(&state);
  80. sha512_inc_blocks(&state, plaintext2, 1);
  81. sha512_inc_finalize(output, &state, NULL, 0);
  82. if (memcmp(expected_512_2, output, 64) != 0) {
  83. printf("ERROR sha512 incremental output did not match test vector.\n");
  84. printf("Expected: ");
  85. for (i = 0; i < 64; i++) {
  86. printf("%02X", expected_512[i]);
  87. }
  88. printf("\n");
  89. printf("Received: ");
  90. for (i = 0; i < 64; i++) {
  91. printf("%02X", output[i]);
  92. }
  93. printf("\n");
  94. return 1;
  95. }
  96. return 0;
  97. }
  98. static int test_sha224(void) {
  99. unsigned char output[28];
  100. int i = 0;
  101. sha224(output, plaintext, 112);
  102. if (memcmp(expected_224, output, 28) != 0) {
  103. printf("ERROR sha224 output did not match test vector.\n");
  104. printf("Expected: ");
  105. for (i = 0; i < 28; i++) {
  106. printf("%02X", expected_224[i]);
  107. }
  108. printf("\n");
  109. printf("Received: ");
  110. for (i = 0; i < 28; i++) {
  111. printf("%02X", output[i]);
  112. }
  113. printf("\n");
  114. return 1;
  115. }
  116. return 0;
  117. }
  118. static int test_sha256(void) {
  119. unsigned char output[32];
  120. int i = 0;
  121. sha256(output, plaintext, 112);
  122. if (memcmp(expected_256, output, 32) != 0) {
  123. printf("ERROR sha256 output did not match test vector.\n");
  124. printf("Expected: ");
  125. for (i = 0; i < 32; i++) {
  126. printf("%02X", expected_256[i]);
  127. }
  128. printf("\n");
  129. printf("Received: ");
  130. for (i = 0; i < 32; i++) {
  131. printf("%02X", output[i]);
  132. }
  133. printf("\n");
  134. return 1;
  135. }
  136. return 0;
  137. }
  138. static int test_sha384(void) {
  139. unsigned char output[48];
  140. int i = 0;
  141. sha384(output, plaintext, 112);
  142. if (memcmp(expected_384, output, 48) != 0) {
  143. printf("ERROR sha384 output did not match test vector.\n");
  144. printf("Expected: ");
  145. for (i = 0; i < 48; i++) {
  146. printf("%02X", expected_384[i]);
  147. }
  148. printf("\n");
  149. printf("Received: ");
  150. for (i = 0; i < 48; i++) {
  151. printf("%02X", output[i]);
  152. }
  153. printf("\n");
  154. return 1;
  155. }
  156. return 0;
  157. }
  158. static int test_sha512(void) {
  159. unsigned char output[64];
  160. int i = 0;
  161. sha512(output, plaintext, 112);
  162. if (memcmp(expected_512, output, 64) != 0) {
  163. printf("ERROR sha512 output did not match test vector.\n");
  164. printf("Expected: ");
  165. for (i = 0; i < 64; i++) {
  166. printf("%02X", expected_512[i]);
  167. }
  168. printf("\n");
  169. printf("Received: ");
  170. for (i = 0; i < 64; i++) {
  171. printf("%02X", output[i]);
  172. }
  173. printf("\n");
  174. return 1;
  175. }
  176. return 0;
  177. }
  178. int main(void) {
  179. int result = 0;
  180. result += test_sha224();
  181. result += test_sha256();
  182. result += test_sha256_incremental();
  183. result += test_sha384();
  184. result += test_sha512();
  185. result += test_sha512_incremental();
  186. if (result != 0) {
  187. puts("Errors occurred");
  188. }
  189. return result;
  190. }