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.
 
 
 
 
 

196 lines
5.3 KiB

  1. #include "test.h"
  2. #include "goldilocks.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. int test_goldilocks () {
  7. const char *message1 = "hello world";
  8. const char *message2 = "Jello world";
  9. unsigned char signature[GOLDI_SIGNATURE_BYTES];
  10. unsigned char
  11. ss12[GOLDI_SHARED_SECRET_BYTES],
  12. ss21[GOLDI_SHARED_SECRET_BYTES],
  13. ss21p[GOLDI_SHARED_SECRET_BYTES],
  14. proto[GOLDI_SYMKEY_BYTES];
  15. struct goldilocks_public_key_t pub, pub2;
  16. struct goldilocks_private_key_t priv, priv2;
  17. struct goldilocks_precomputed_public_key_t *pre = NULL;
  18. int i, ret, good = 1;
  19. ret = goldilocks_init();
  20. if (ret) {
  21. youfail();
  22. printf(" Failed init.\n");
  23. }
  24. for (i=0; i<1000 && good; i++) {
  25. ret = goldilocks_keygen(&priv, &pub);
  26. if (ret) {
  27. youfail();
  28. printf(" Failed keygen trial %d.\n", i);
  29. good = 0;
  30. }
  31. goldilocks_destroy_precomputed_public_key( pre );
  32. pre = goldilocks_precompute_public_key ( &pub );
  33. if (!pre) {
  34. youfail();
  35. printf(" Failed precomp-public trial %d.\n", i);
  36. return -1;
  37. }
  38. ret = goldilocks_sign(
  39. signature,
  40. (const unsigned char *)message1,
  41. strlen(message1),
  42. &priv
  43. );
  44. if (ret) {
  45. youfail();
  46. printf(" Failed sign trial %d.\n", i);
  47. good = 0;
  48. }
  49. ret = goldilocks_verify(
  50. signature,
  51. (const unsigned char *)message1,
  52. strlen(message1),
  53. &pub
  54. );
  55. if (ret) {
  56. youfail();
  57. printf(" Failed verify trial %d.\n", i);
  58. good = 0;
  59. }
  60. ret = goldilocks_verify_precomputed (
  61. signature,
  62. (const unsigned char *)message1,
  63. strlen(message1),
  64. pre
  65. );
  66. if (ret) {
  67. youfail();
  68. printf(" Failed verify-pre trial %d.\n", i);
  69. good = 0;
  70. }
  71. /* terrible negative test */
  72. ret = goldilocks_verify(
  73. signature,
  74. (const unsigned char *)message2,
  75. strlen(message1),
  76. &pub
  77. );
  78. if (ret != GOLDI_EINVAL) {
  79. youfail();
  80. printf(" Failed nega-verify trial %d.\n", i);
  81. good = 0;
  82. }
  83. ret = goldilocks_verify_precomputed(
  84. signature,
  85. (const unsigned char *)message2,
  86. strlen(message1),
  87. pre
  88. );
  89. if (ret != GOLDI_EINVAL) {
  90. youfail();
  91. printf(" Failed nega-verify-pre trial %d.\n", i);
  92. good = 0;
  93. }
  94. /* honestly a slightly better negative test */
  95. memset(signature,0,sizeof(signature));
  96. ret = goldilocks_verify(
  97. signature,
  98. (const unsigned char *)message1,
  99. strlen(message1),
  100. &pub
  101. );
  102. if (ret != GOLDI_EINVAL) {
  103. youfail();
  104. printf(" Failed nega-verify-0 trial %d.\n", i);
  105. good = 0;
  106. }
  107. ret = goldilocks_verify_precomputed(
  108. signature,
  109. (const unsigned char *)message1,
  110. strlen(message1),
  111. pre
  112. );
  113. if (ret != GOLDI_EINVAL) {
  114. youfail();
  115. printf(" Failed nega-verify-pre-0 trial %d.\n", i);
  116. good = 0;
  117. }
  118. /* ecdh */
  119. ret = goldilocks_keygen(&priv2, &pub2);
  120. if (ret) {
  121. youfail();
  122. printf(" Failed keygen2 trial %d.\n", i);
  123. good = 0;
  124. }
  125. ret = goldilocks_shared_secret ( ss12, &priv, &pub2 );
  126. if (ret) {
  127. youfail();
  128. printf(" Failed ss12 trial %d.\n", i);
  129. good = 0;
  130. }
  131. ret = goldilocks_shared_secret ( ss21, &priv2, &pub );
  132. if (ret) {
  133. youfail();
  134. printf(" Failed ss21 trial %d.\n", i);
  135. good = 0;
  136. }
  137. ret = goldilocks_shared_secret_precomputed ( ss21p, &priv2, pre );
  138. if (ret) {
  139. youfail();
  140. printf(" Failed ss21p trial %d.\n", i);
  141. good = 0;
  142. }
  143. if (memcmp(ss12,ss21,sizeof(ss12))) {
  144. youfail();
  145. printf(" Failed shared-secret trial %d.\n", i);
  146. good = 0;
  147. }
  148. if (memcmp(ss21,ss21p,sizeof(ss21))) {
  149. youfail();
  150. printf(" Failed shared-secret precomp trial %d.\n", i);
  151. good = 0;
  152. }
  153. /* test derive / underive / priv to pub */
  154. goldilocks_underive_private_key ( proto, &priv );
  155. ret = goldilocks_derive_private_key ( &priv2, proto );
  156. if (ret || memcmp(&priv,&priv2,sizeof(priv))) {
  157. youfail();
  158. printf(" Failed derive round-trip trial %d.\n", i);
  159. good = 0;
  160. }
  161. ret = goldilocks_private_to_public ( &pub2, &priv );
  162. if (ret || memcmp(&pub,&pub2,sizeof(pub))) {
  163. youfail();
  164. printf(" Failed private-to-public trial %d.\n", i);
  165. good = 0;
  166. }
  167. }
  168. goldilocks_destroy_precomputed_public_key( pre );
  169. return good ? 0 : -1;
  170. }