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.
 
 
 
 

147 lines
4.3 KiB

  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "sp800-185.h"
  5. const unsigned char name[] = "";
  6. const unsigned char customization[] = "Email Signature";
  7. const unsigned char output1[32] = {
  8. 0xC1, 0xC3, 0x69, 0x25, 0xB6, 0x40, 0x9A, 0x04, 0xF1, 0xB5, 0x04, 0xFC, 0xBC, 0xA9, 0xD8, 0x2B,
  9. 0x40, 0x17, 0x27, 0x7C, 0xB5, 0xED, 0x2B, 0x20, 0x65, 0xFC, 0x1D, 0x38, 0x14, 0xD5, 0xAA, 0xF5
  10. };
  11. const unsigned char output2[32] = {
  12. 0xC5, 0x22, 0x1D, 0x50, 0xE4, 0xF8, 0x22, 0xD9, 0x6A, 0x2E, 0x88, 0x81, 0xA9, 0x61, 0x42, 0x0F,
  13. 0x29, 0x4B, 0x7B, 0x24, 0xFE, 0x3D, 0x20, 0x94, 0xBA, 0xED, 0x2C, 0x65, 0x24, 0xCC, 0x16, 0x6B
  14. };
  15. const unsigned char output3[64] = {
  16. 0xD0, 0x08, 0x82, 0x8E, 0x2B, 0x80, 0xAC, 0x9D, 0x22, 0x18, 0xFF, 0xEE, 0x1D, 0x07, 0x0C, 0x48,
  17. 0xB8, 0xE4, 0xC8, 0x7B, 0xFF, 0x32, 0xC9, 0x69, 0x9D, 0x5B, 0x68, 0x96, 0xEE, 0xE0, 0xED, 0xD1,
  18. 0x64, 0x02, 0x0E, 0x2B, 0xE0, 0x56, 0x08, 0x58, 0xD9, 0xC0, 0x0C, 0x03, 0x7E, 0x34, 0xA9, 0x69,
  19. 0x37, 0xC5, 0x61, 0xA7, 0x4C, 0x41, 0x2B, 0xB4, 0xC7, 0x46, 0x46, 0x95, 0x27, 0x28, 0x1C, 0x8C
  20. };
  21. const unsigned char output4[64] = {
  22. 0x07, 0xDC, 0x27, 0xB1, 0x1E, 0x51, 0xFB, 0xAC, 0x75, 0xBC, 0x7B, 0x3C, 0x1D, 0x98, 0x3E, 0x8B,
  23. 0x4B, 0x85, 0xFB, 0x1D, 0xEF, 0xAF, 0x21, 0x89, 0x12, 0xAC, 0x86, 0x43, 0x02, 0x73, 0x09, 0x17,
  24. 0x27, 0xF4, 0x2B, 0x17, 0xED, 0x1D, 0xF6, 0x3E, 0x8E, 0xC1, 0x18, 0xF0, 0x4B, 0x23, 0x63, 0x3C,
  25. 0x1D, 0xFB, 0x15, 0x74, 0xC8, 0xFB, 0x55, 0xCB, 0x45, 0xDA, 0x8E, 0x25, 0xAF, 0xB0, 0x92, 0xBB
  26. };
  27. static int test_cshake128(void) {
  28. unsigned char input[200];
  29. unsigned char output[32];
  30. int i;
  31. int returncode = 0;
  32. size_t namelen = strlen((const char *)name);
  33. size_t cstmlen = strlen((const char *)customization);
  34. for (i = 0; i < 4; i++) {
  35. input[i] = i;
  36. }
  37. cshake128(output, 32, name, namelen, customization, cstmlen, input, 4);
  38. if (memcmp(output1, output, 32) != 0) {
  39. puts("ERROR shake128 did not match test vector 1.");
  40. printf(" Expected: ");
  41. for (i = 0; i < 32; i++) {
  42. printf("%02X", output1[i]);
  43. }
  44. puts("");
  45. printf(" Received: ");
  46. for (i = 0; i < 32; i++) {
  47. printf("%02X", output[i]);
  48. }
  49. puts("");
  50. returncode = 1;
  51. }
  52. for (i = 0; i < 200; i++) {
  53. input[i] = i;
  54. }
  55. cshake128(output, 32, name, namelen, customization, cstmlen, input, 200);
  56. if (memcmp(output2, output, 32) != 0) {
  57. puts("ERROR shake128 did not match test vector 2.");
  58. printf(" Expected: ");
  59. for (i = 0; i < 32; i++) {
  60. printf("%02X", output2[i]);
  61. }
  62. puts("");
  63. printf(" Received: ");
  64. for (i = 0; i < 32; i++) {
  65. printf("%02X", output[i]);
  66. }
  67. puts("");
  68. returncode = 1;
  69. }
  70. return returncode;
  71. }
  72. static int test_cshake256(void) {
  73. unsigned char input[200];
  74. unsigned char output[64];
  75. int i;
  76. int returncode = 0;
  77. size_t namelen = strlen((const char *)name);
  78. size_t cstmlen = strlen((const char *)customization);
  79. for (i = 0; i < 4; i++) {
  80. input[i] = i;
  81. }
  82. cshake256(output, 64, name, namelen, customization, cstmlen, input, 4);
  83. if (memcmp(output3, output, 64) != 0) {
  84. puts("ERROR shake256 did not match test vector 3.");
  85. printf(" Expected: ");
  86. for (i = 0; i < 64; i++) {
  87. printf("%02X", output3[i]);
  88. }
  89. puts("");
  90. printf(" Received: ");
  91. for (i = 0; i < 64; i++) {
  92. printf("%02X", output[i]);
  93. }
  94. puts("");
  95. returncode = 1;
  96. }
  97. for (i = 0; i < 200; i++) {
  98. input[i] = i;
  99. }
  100. cshake256(output, 64, name, namelen, customization, cstmlen, input, 200);
  101. if (memcmp(output4, output, 64) != 0) {
  102. puts("ERROR shake256 did not match test vector 4.");
  103. printf(" Expected: ");
  104. for (i = 0; i < 64; i++) {
  105. printf("%02X", output4[i]);
  106. }
  107. puts("");
  108. printf(" Received: ");
  109. for (i = 0; i < 64; i++) {
  110. printf("%02X", output[i]);
  111. }
  112. puts("");
  113. returncode = 1;
  114. }
  115. return returncode;
  116. }
  117. int main(void) {
  118. int result = 0;
  119. result += test_cshake128();
  120. result += test_cshake256();
  121. if (result != 0) {
  122. puts("Errors occurred");
  123. }
  124. return result;
  125. }