jpaketest.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #include <openssl/opensslconf.h>
  2. #ifdef OPENSSL_NO_JPAKE
  3. # include <stdio.h>
  4. int main(int argc, char *argv[])
  5. {
  6. printf("No J-PAKE support\n");
  7. return (0);
  8. }
  9. #else
  10. # include <openssl/jpake.h>
  11. # include <openssl/err.h>
  12. static void showbn(const char *name, const BIGNUM *bn)
  13. {
  14. fputs(name, stdout);
  15. fputs(" = ", stdout);
  16. BN_print_fp(stdout, bn);
  17. putc('\n', stdout);
  18. }
  19. static int run_jpake(JPAKE_CTX *alice, JPAKE_CTX *bob)
  20. {
  21. JPAKE_STEP1 alice_s1;
  22. JPAKE_STEP1 bob_s1;
  23. JPAKE_STEP2 alice_s2;
  24. JPAKE_STEP2 bob_s2;
  25. JPAKE_STEP3A alice_s3a;
  26. JPAKE_STEP3B bob_s3b;
  27. /* Alice -> Bob: step 1 */
  28. puts("A->B s1");
  29. JPAKE_STEP1_init(&alice_s1);
  30. JPAKE_STEP1_generate(&alice_s1, alice);
  31. if (!JPAKE_STEP1_process(bob, &alice_s1)) {
  32. printf("Bob fails to process Alice's step 1\n");
  33. ERR_print_errors_fp(stdout);
  34. return 1;
  35. }
  36. JPAKE_STEP1_release(&alice_s1);
  37. /* Bob -> Alice: step 1 */
  38. puts("B->A s1");
  39. JPAKE_STEP1_init(&bob_s1);
  40. JPAKE_STEP1_generate(&bob_s1, bob);
  41. if (!JPAKE_STEP1_process(alice, &bob_s1)) {
  42. printf("Alice fails to process Bob's step 1\n");
  43. ERR_print_errors_fp(stdout);
  44. return 2;
  45. }
  46. JPAKE_STEP1_release(&bob_s1);
  47. /* Alice -> Bob: step 2 */
  48. puts("A->B s2");
  49. JPAKE_STEP2_init(&alice_s2);
  50. JPAKE_STEP2_generate(&alice_s2, alice);
  51. if (!JPAKE_STEP2_process(bob, &alice_s2)) {
  52. printf("Bob fails to process Alice's step 2\n");
  53. ERR_print_errors_fp(stdout);
  54. return 3;
  55. }
  56. JPAKE_STEP2_release(&alice_s2);
  57. /* Bob -> Alice: step 2 */
  58. puts("B->A s2");
  59. JPAKE_STEP2_init(&bob_s2);
  60. JPAKE_STEP2_generate(&bob_s2, bob);
  61. if (!JPAKE_STEP2_process(alice, &bob_s2)) {
  62. printf("Alice fails to process Bob's step 2\n");
  63. ERR_print_errors_fp(stdout);
  64. return 4;
  65. }
  66. JPAKE_STEP2_release(&bob_s2);
  67. showbn("Alice's key", JPAKE_get_shared_key(alice));
  68. showbn("Bob's key ", JPAKE_get_shared_key(bob));
  69. /* Alice -> Bob: step 3a */
  70. puts("A->B s3a");
  71. JPAKE_STEP3A_init(&alice_s3a);
  72. JPAKE_STEP3A_generate(&alice_s3a, alice);
  73. if (!JPAKE_STEP3A_process(bob, &alice_s3a)) {
  74. printf("Bob fails to process Alice's step 3a\n");
  75. ERR_print_errors_fp(stdout);
  76. return 5;
  77. }
  78. JPAKE_STEP3A_release(&alice_s3a);
  79. /* Bob -> Alice: step 3b */
  80. puts("B->A s3b");
  81. JPAKE_STEP3B_init(&bob_s3b);
  82. JPAKE_STEP3B_generate(&bob_s3b, bob);
  83. if (!JPAKE_STEP3B_process(alice, &bob_s3b)) {
  84. printf("Alice fails to process Bob's step 3b\n");
  85. ERR_print_errors_fp(stdout);
  86. return 6;
  87. }
  88. JPAKE_STEP3B_release(&bob_s3b);
  89. return 0;
  90. }
  91. int main(int argc, char **argv)
  92. {
  93. JPAKE_CTX *alice;
  94. JPAKE_CTX *bob;
  95. BIGNUM *p = NULL;
  96. BIGNUM *g = NULL;
  97. BIGNUM *q = NULL;
  98. BIGNUM *secret = BN_new();
  99. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  100. BIO *bio_err;
  101. bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  102. CRYPTO_set_mem_debug(1);
  103. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  104. #endif
  105. ERR_load_crypto_strings();
  106. /*-
  107. BN_hex2bn(&p, "fd7f53811d75122952df4a9c2eece4e7f611b7523cef4400c31e3f80b6512669455d402251fb593d8d58fabfc5f5ba30f6cb9b556cd7813b801d346ff26660b76b9950a5a49f9fe8047b1022c24fbba9d7feb7c61bf83b57e7c6a8a6150f04fb83f6d3c51ec3023554135a169132f675f3ae2b61d72aeff22203199dd14801c7");
  108. BN_hex2bn(&g, "f7e1a085d69b3ddecbbcab5c36b857b97994afbbfa3aea82f9574c0b3d0782675159578ebad4594fe67107108180b449167123e84c281613b7cf09328cc8a6e13c167a8b547c8d28e0a3ae1e2bb3a675916ea37f0bfa213562f1fb627a01243bcca4f1bea8519089a883dfe15ae59f06928b665e807b552564014c3bfecf492a");
  109. BN_hex2bn(&q, "9760508f15230bccb292b982a2eb840bf0581cf5");
  110. */
  111. /*-
  112. p = BN_new();
  113. BN_generate_prime(p, 1024, 1, NULL, NULL, NULL, NULL);
  114. */
  115. /* Use a safe prime for p (that we found earlier) */
  116. BN_hex2bn(&p,
  117. "F9E5B365665EA7A05A9C534502780FEE6F1AB5BD4F49947FD036DBD7E905269AF46EF28B0FC07487EE4F5D20FB3C0AF8E700F3A2FA3414970CBED44FEDFF80CE78D800F184BB82435D137AADA2C6C16523247930A63B85661D1FC817A51ACD96168E95898A1F83A79FFB529368AA7833ABD1B0C3AEDDB14D2E1A2F71D99F763F");
  118. showbn("p", p);
  119. g = BN_new();
  120. BN_set_word(g, 2);
  121. showbn("g", g);
  122. q = BN_new();
  123. BN_rshift1(q, p);
  124. showbn("q", q);
  125. BN_rand(secret, 32, -1, 0);
  126. /* A normal run, expect this to work... */
  127. alice = JPAKE_CTX_new("Alice", "Bob", p, g, q, secret);
  128. bob = JPAKE_CTX_new("Bob", "Alice", p, g, q, secret);
  129. if (run_jpake(alice, bob) != 0) {
  130. fprintf(stderr, "Plain JPAKE run failed\n");
  131. return 1;
  132. }
  133. JPAKE_CTX_free(bob);
  134. JPAKE_CTX_free(alice);
  135. /* Now give Alice and Bob different secrets */
  136. alice = JPAKE_CTX_new("Alice", "Bob", p, g, q, secret);
  137. BN_add_word(secret, 1);
  138. bob = JPAKE_CTX_new("Bob", "Alice", p, g, q, secret);
  139. if (run_jpake(alice, bob) != 5) {
  140. fprintf(stderr, "Mismatched secret JPAKE run failed\n");
  141. return 1;
  142. }
  143. JPAKE_CTX_free(bob);
  144. JPAKE_CTX_free(alice);
  145. BN_free(secret);
  146. BN_free(q);
  147. BN_free(g);
  148. BN_free(p);
  149. CRYPTO_cleanup_all_ex_data();
  150. ERR_remove_thread_state(NULL);
  151. ERR_free_strings();
  152. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  153. CRYPTO_mem_leaks(bio_err);
  154. #endif
  155. return 0;
  156. }
  157. #endif