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