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.
 
 
 
 
 

135 lines
2.6 KiB

  1. #include "test.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. int failed_tests, n_tests, failed_this_test, running_a_test;
  5. void end_test() {
  6. if (!failed_this_test) {
  7. printf("[PASS]\n");
  8. }
  9. n_tests ++;
  10. running_a_test = 0;
  11. }
  12. void begin_test(const char *name) {
  13. if (running_a_test) end_test();
  14. printf("%s...%*s",name,(int)(30-strlen(name)),"");
  15. fflush(stdout);
  16. failed_this_test = 0;
  17. running_a_test = 1;
  18. }
  19. void youfail() {
  20. if (failed_this_test) return;
  21. failed_this_test = 1;
  22. failed_tests ++;
  23. printf("[FAIL]\n");
  24. }
  25. static int
  26. hexchar (char c) {
  27. if (c >= '0' && c <= '9') {
  28. return c - '0';
  29. } else if (c >= 'a' && c <= 'f') {
  30. return 10 + c - 'a';
  31. } else if (c >= 'A' && c <= 'F') {
  32. return 10 + c - 'A';
  33. } else {
  34. return -1;
  35. }
  36. }
  37. int
  38. hexdecode (
  39. unsigned char *bytes,
  40. const char *hex,
  41. unsigned int nbytes
  42. ) {
  43. if (strlen(hex) != nbytes*2) {
  44. return -1;
  45. }
  46. unsigned int i;
  47. for (i=0; i<nbytes; i++) {
  48. int hi = hexchar(hex[2*i]),
  49. lo = hexchar(hex[2*i+1]);
  50. if (hi<0 || lo<0) return -1;
  51. bytes[i] = hi*16 + lo;
  52. }
  53. return 0;
  54. }
  55. void
  56. hexprint (
  57. const char *descr,
  58. const unsigned char *bytes,
  59. unsigned int nbytes
  60. ) {
  61. if (descr) printf("%s = ", descr);
  62. unsigned int i;
  63. for (i=0; i<nbytes; i++) {
  64. printf("%02x", bytes[i]);
  65. }
  66. printf("\n");
  67. }
  68. void p448_print (
  69. const char *descr,
  70. const struct p448_t *a
  71. ) {
  72. p448_t b;
  73. p448_copy(&b, a);
  74. p448_strong_reduce(&b);
  75. int j;
  76. printf("%s = 0x", descr);
  77. for (j=sizeof(*a)/sizeof(word_t)-1; j>=0; j--) {
  78. printf(PRIxWORD58, b.limb[j]);
  79. }
  80. printf("\n");
  81. }
  82. void scalar_print (
  83. const char *descr,
  84. const word_t *scalar,
  85. int nwords
  86. ) {
  87. int j;
  88. printf("%s = 0x", descr);
  89. for (j=nwords-1; j>=0; j--) {
  90. printf(PRIxWORDfull, scalar[j]);
  91. }
  92. printf("\n");
  93. }
  94. int main(int argc, char **argv) {
  95. (void) argc;
  96. (void) argv;
  97. n_tests = running_a_test = failed_tests = 0;
  98. begin_test("SHA-512 NIST Monte Carlo");
  99. test_sha512_monte_carlo();
  100. begin_test("EC point operations");
  101. test_pointops();
  102. begin_test("Scalarmul compatibility");
  103. test_scalarmul_compatibility();
  104. begin_test("Scalarmul commutativity");
  105. test_scalarmul_commutativity();
  106. if (running_a_test) end_test();
  107. printf("\n");
  108. if (failed_tests) {
  109. printf("Failed %d / %d tests.\n", failed_tests, n_tests);
  110. } else {
  111. printf("Passed all %d tests.\n", n_tests);
  112. }
  113. return failed_tests ? 1 : 0;
  114. }