選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

174 行
4.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 expected_224[28] = {
  8. 0xc9, 0x7c, 0xa9, 0xa5, 0x59, 0x85, 0x0c, 0xe9, 0x7a, 0x04, 0xa9, 0x6d,
  9. 0xef, 0x6d, 0x99, 0xa9, 0xe0, 0xe0, 0xe2, 0xab, 0x14, 0xe6, 0xb8, 0xdf,
  10. 0x26, 0x5f, 0xc0, 0xb3
  11. };
  12. const unsigned char expected_256[32] = {
  13. 0xcf, 0x5b, 0x16, 0xa7, 0x78, 0xaf, 0x83, 0x80, 0x03, 0x6c, 0xe5, 0x9e,
  14. 0x7b, 0x04, 0x92, 0x37, 0x0b, 0x24, 0x9b, 0x11, 0xe8, 0xf0, 0x7a, 0x51,
  15. 0xaf, 0xac, 0x45, 0x03, 0x7a, 0xfe, 0xe9, 0xd1
  16. };
  17. const unsigned char expected_384[48] = {
  18. 0x09, 0x33, 0x0c, 0x33, 0xf7, 0x11, 0x47, 0xe8, 0x3d, 0x19, 0x2f, 0xc7,
  19. 0x82, 0xcd, 0x1b, 0x47, 0x53, 0x11, 0x1b, 0x17, 0x3b, 0x3b, 0x05, 0xd2,
  20. 0x2f, 0xa0, 0x80, 0x86, 0xe3, 0xb0, 0xf7, 0x12, 0xfc, 0xc7, 0xc7, 0x1a,
  21. 0x55, 0x7e, 0x2d, 0xb9, 0x66, 0xc3, 0xe9, 0xfa, 0x91, 0x74, 0x60, 0x39
  22. };
  23. const unsigned char expected_512[64] = {
  24. 0x8e, 0x95, 0x9b, 0x75, 0xda, 0xe3, 0x13, 0xda, 0x8c, 0xf4, 0xf7, 0x28,
  25. 0x14, 0xfc, 0x14, 0x3f, 0x8f, 0x77, 0x79, 0xc6, 0xeb, 0x9f, 0x7f, 0xa1,
  26. 0x72, 0x99, 0xae, 0xad, 0xb6, 0x88, 0x90, 0x18, 0x50, 0x1d, 0x28, 0x9e,
  27. 0x49, 0x00, 0xf7, 0xe4, 0x33, 0x1b, 0x99, 0xde, 0xc4, 0xb5, 0x43, 0x3a,
  28. 0xc7, 0xd3, 0x29, 0xee, 0xb6, 0xdd, 0x26, 0x54, 0x5e, 0x96, 0xe5, 0x5b,
  29. 0x87, 0x4b, 0xe9, 0x09
  30. };
  31. static int test_sha256_incremental(void) {
  32. unsigned char output[32];
  33. uint8_t state[40];
  34. int i = 0;
  35. sha256_inc_init(state);
  36. sha256_inc_blocks(state, plaintext, 1);
  37. sha256_inc_finalize(output, state, plaintext + 64, 112 - 64);
  38. if (memcmp(expected_256, output, 32)) {
  39. printf("ERROR sha256 incremental did not match sha256.\n");
  40. printf(" Expected: ");
  41. for (i = 0; i < 32; i++) {
  42. printf("%02X", expected_256[i]);
  43. }
  44. printf("\n");
  45. printf(" Received: ");
  46. for (i = 0; i < 32; i++) {
  47. printf("%02X", output[i]);
  48. }
  49. printf("\n");
  50. return 1;
  51. }
  52. return 0;
  53. }
  54. static int test_sha224(void) {
  55. unsigned char output[28];
  56. int i = 0;
  57. sha224(output, plaintext, 112);
  58. if (memcmp(expected_224, output, 28)) {
  59. printf("ERROR sha224 output did not match test vector.\n");
  60. printf("Expected: ");
  61. for (i = 0; i < 28; i++) {
  62. printf("%02X", expected_224[i]);
  63. }
  64. printf("\n");
  65. printf("Received: ");
  66. for (i = 0; i < 28; i++) {
  67. printf("%02X", output[i]);
  68. }
  69. printf("\n");
  70. return 1;
  71. }
  72. return 0;
  73. }
  74. static int test_sha256(void) {
  75. unsigned char output[32];
  76. int i = 0;
  77. sha256(output, plaintext, 112);
  78. if (memcmp(expected_256, output, 32)) {
  79. printf("ERROR sha256 output did not match test vector.\n");
  80. printf("Expected: ");
  81. for (i = 0; i < 32; i++) {
  82. printf("%02X", expected_256[i]);
  83. }
  84. printf("\n");
  85. printf("Received: ");
  86. for (i = 0; i < 32; i++) {
  87. printf("%02X", output[i]);
  88. }
  89. printf("\n");
  90. return 1;
  91. }
  92. return 0;
  93. }
  94. static int test_sha384(void) {
  95. unsigned char output[48];
  96. int i = 0;
  97. sha384(output, plaintext, 112);
  98. if (memcmp(expected_384, output, 48)) {
  99. printf("ERROR sha384 output did not match test vector.\n");
  100. printf("Expected: ");
  101. for (i = 0; i < 48; i++) {
  102. printf("%02X", expected_384[i]);
  103. }
  104. printf("\n");
  105. printf("Received: ");
  106. for (i = 0; i < 48; i++) {
  107. printf("%02X", output[i]);
  108. }
  109. printf("\n");
  110. return 1;
  111. }
  112. return 0;
  113. }
  114. static int test_sha512(void) {
  115. unsigned char output[64];
  116. int i = 0;
  117. sha512(output, plaintext, 112);
  118. if (memcmp(expected_512, output, 64)) {
  119. printf("ERROR sha512 output did not match test vector.\n");
  120. printf("Expected: ");
  121. for (i = 0; i < 64; i++) {
  122. printf("%02X", expected_512[i]);
  123. }
  124. printf("\n");
  125. printf("Received: ");
  126. for (i = 0; i < 64; i++) {
  127. printf("%02X", output[i]);
  128. }
  129. printf("\n");
  130. return 1;
  131. }
  132. return 0;
  133. }
  134. int main(void) {
  135. int result = 0;
  136. result += test_sha224();
  137. result += test_sha256();
  138. result += test_sha256_incremental();
  139. result += test_sha384();
  140. result += test_sha512();
  141. if (result != 0) {
  142. puts("Errors occurred");
  143. }
  144. return result;
  145. }