void entropy_free( entropy_context *ctx ) { #if defined(POLARSSL_HAVEGE_C) havege_free( &ctx->havege_data ); #endif polarssl_zeroize( ctx, sizeof( entropy_context ) ); #if defined(POLARSSL_THREADING_C) polarssl_mutex_free( &ctx->mutex ); #endif }
void receiver(int sender, int receiver, char *s_pub_key, char *r_priv_key, char *s_nonce, char *r_nonce) /*@ requires [_]public_invar(nsl_pub) &*& [_]decryption_key_classifier(nsl_public_key) &*& principal(receiver, _) &*& [?f1]cryptogram(s_pub_key, 8 * KEY_SIZE, ?s_pub_key_ccs, ?s_pub_key_cg) &*& s_pub_key_cg == cg_public_key(sender, ?s_id) &*& [?f2]cryptogram(r_priv_key, 8 * KEY_SIZE, ?r_priv_key_ccs, ?r_priv_key_cg) &*& r_priv_key_cg == cg_private_key(receiver, ?r_id) &*& chars(s_nonce, NONCE_SIZE, _) &*& chars(r_nonce, NONCE_SIZE, _); @*/ /*@ ensures principal(receiver, _) &*& [f1]cryptogram(s_pub_key, 8 * KEY_SIZE, s_pub_key_ccs, s_pub_key_cg) &*& [f2]cryptogram(r_priv_key, 8 * KEY_SIZE, r_priv_key_ccs, r_priv_key_cg) &*& cryptogram(r_nonce, NONCE_SIZE, ?r_nonce_ccs, ?r_nonce_cg) &*& r_nonce_cg == cg_nonce(receiver, _) &*& ( col || bad(sender) || bad(receiver) ? chars(s_nonce, NONCE_SIZE, _) : cryptogram(s_nonce, NONCE_SIZE, ?s_nonce_ccs, ?s_nonce_cg) &*& s_nonce_cg == cg_nonce(sender, _) &*& cg_info(s_nonce_cg) == int_pair(1, int_pair(receiver, r_id)) &*& cg_info(r_nonce_cg) == int_pair(2, int_pair(sender, int_pair(sender, int_pair(receiver, r_id)))) ); @*/ { //@ open principal(receiver, _); int socket1; int socket2; pk_context s_context; pk_context r_context; havege_state havege_state; if(net_bind(&socket1, NULL, SERVER_PORT) != 0) abort(); if(net_accept(socket1, &socket2, NULL) != 0) abort(); if(net_set_block(socket2) != 0) abort(); //@ close pk_context(&s_context); pk_init(&s_context); if (pk_parse_public_key(&s_context, s_pub_key, (unsigned int) 8 * KEY_SIZE) != 0) abort(); //@ close pk_context(&r_context); pk_init(&r_context); if (pk_parse_key(&r_context, r_priv_key, (unsigned int) 8 * KEY_SIZE, NULL, 0) != 0) abort(); // Generate NB //@ close havege_state(&havege_state); havege_init(&havege_state); //@ close principal(receiver, _); receiver_msg1(&socket2, &havege_state, &r_context, sender, receiver, s_nonce); //@ open principal(receiver, _); //@ assert receiver_inter(?p_orig, ?c_orig, ?p_inst, ?s_nonce_ccs, ?s_nonce_cg); //@ int info = int_pair(sender, int_pair(p_inst, int_pair(p_orig, c_orig))); //@ close random_request(receiver, int_pair(2, info), false); if (havege_random(&havege_state, r_nonce, NONCE_SIZE) != 0) abort(); //@ close principal(receiver, _); receiver_msg2(&socket2, &havege_state, &s_context, receiver, s_nonce, r_nonce); //@ if (col || bad(p_inst) || bad(receiver)) chars_to_crypto_chars(s_nonce, NONCE_SIZE); //@ close receiver_inter(p_orig, c_orig, p_inst, s_nonce_ccs, s_nonce_cg); receiver_msg3(&socket2, &havege_state, &r_context, sender, receiver, s_nonce, r_nonce); /*@ if (col || bad(sender) || bad(receiver)) crypto_chars_to_chars(s_nonce, NONCE_SIZE); @*/ havege_free(&havege_state); //@ open havege_state(&havege_state); //@ pk_release_context_with_key(&s_context); pk_free(&s_context); //@ open pk_context(&s_context); //@ pk_release_context_with_key(&r_context); pk_free(&r_context); //@ open pk_context(&r_context); net_close(socket2); net_close(socket1); }
void sender(int sender, int receiver, char *s_priv_key, char *r_pub_key, char *s_nonce, char *r_nonce) /*@ requires [_]public_invar(nsl_pub) &*& [_]decryption_key_classifier(nsl_public_key) &*& principal(sender, _) &*& [?f1]cryptogram(s_priv_key, 8 * KEY_SIZE, ?s_priv_key_ccs, ?s_priv_key_cg) &*& s_priv_key_cg == cg_private_key(sender, ?s_id) &*& [?f2]cryptogram(r_pub_key, 8 * KEY_SIZE, ?r_pub_key_ccs, ?r_pub_key_cg) &*& r_pub_key_cg == cg_public_key(receiver, ?r_id) &*& chars(s_nonce, NONCE_SIZE, _) &*& chars(r_nonce, NONCE_SIZE, _); @*/ /*@ ensures principal(sender, _) &*& [f1]cryptogram(s_priv_key, 8 * KEY_SIZE, s_priv_key_ccs, s_priv_key_cg) &*& [f2]cryptogram(r_pub_key, 8 * KEY_SIZE, r_pub_key_ccs, r_pub_key_cg) &*& cryptogram(s_nonce, NONCE_SIZE, ?s_nonce_ccs, ?s_nonce_cg) &*& s_nonce_cg == cg_nonce(sender, _) &*& cg_info(s_nonce_cg) == int_pair(1, int_pair(receiver, r_id)) &*& col || bad(sender) || bad(receiver) ? chars(r_nonce, NONCE_SIZE, _) : cryptogram(r_nonce, NONCE_SIZE, _, ?r_nonce_cg) &*& r_nonce_cg == cg_nonce(receiver, _) &*& cg_info(r_nonce_cg) == int_pair(2, int_pair(sender, int_pair(sender, int_pair(receiver, r_id)))); @*/ { int socket; pk_context s_context; pk_context r_context; havege_state havege_state; net_usleep(20000); if(net_connect(&socket, NULL, SERVER_PORT) != 0) abort(); if(net_set_block(socket) != 0) abort(); //@ close pk_context(&s_context); pk_init(&s_context); if (pk_parse_key(&s_context, s_priv_key, (unsigned int) 8 * KEY_SIZE, NULL, 0) != 0) abort(); //@ close pk_context(&r_context); pk_init(&r_context); if (pk_parse_public_key(&r_context, r_pub_key, (unsigned int) 8 * KEY_SIZE) != 0) abort(); // Generate NA //@ open principal(sender, _); //@ close havege_state(&havege_state); havege_init(&havege_state); //@ close random_request(sender, int_pair(1, int_pair(receiver, r_id)), false); if (havege_random(&havege_state, s_nonce, NONCE_SIZE) != 0) abort(); //@ assert cryptogram(s_nonce, NONCE_SIZE, ?cs_s_nonce, ?cg_s_nonce); //@ close principal(sender, _); sender_msg1(&socket, &havege_state, &r_context, sender, s_nonce); sender_msg2(&socket, &havege_state, &s_context, sender, receiver, s_nonce, r_nonce); sender_msg3(&socket, &havege_state, &r_context, sender, r_nonce); havege_free(&havege_state); //@ open havege_state(&havege_state); //@ pk_release_context_with_key(&s_context); pk_free(&s_context); //@ open pk_context(&s_context); //@ pk_release_context_with_key(&r_context); pk_free(&r_context); //@ open pk_context(&r_context); net_close(socket); }
void app_send(char *key, char *message, int message_len) /*@ requires polarssl_generated_values(?creator, ?count1) &*& [?f0]polarssl_world(sc_auth_polarssl_pub) &*& [?f1]polarssl_cryptogram(key, KEY_BYTE_SIZE, ?key_cs, ?key_cg) &*& key_cg == polarssl_symmetric_key(creator, ?key_id) &*& [?f2]chars(message, message_len, ?m_cs) &*& message_len >= POLARSSL_MIN_ENCRYPTED_BYTE_SIZE &*& message_len < POLARSSL_MAX_MESSAGE_BYTE_SIZE - 84 &*& bad(creator) ? [_]polarssl_public_generated_chars(sc_auth_polarssl_pub)(m_cs) : true == app_send_event(creator, m_cs); @*/ /*@ ensures polarssl_generated_values(creator, ?count2) &*& count2 > count1 &*& [f0]polarssl_world(sc_auth_polarssl_pub) &*& [f1]polarssl_cryptogram(key, KEY_BYTE_SIZE, key_cs, key_cg) &*& [f2]chars(message, message_len, m_cs); @*/ { int socket; havege_state havege_state; char iv[16]; // init { net_usleep(20000); if(net_connect(&socket, NULL, APP_RECEIVE_PORT) != 0) abort(); if(net_set_block(socket) != 0) abort(); //@ close havege_state(&havege_state); havege_init(&havege_state); } // iv stuff { //@ close random_request(creator, 0, false); if (havege_random(&havege_state, iv, 16) != 0) abort(); } //@ open polarssl_cryptogram(iv, 16, ?iv_cs, _); char* m = malloc(16 + message_len + 16); if (m == 0) abort(); memcpy(m, iv, 16); //@ assert chars(m, 16, iv_cs); //@ assert chars(m + 16, message_len + 16, ?cs1); //@ polarssl_public_generated_chars_assume(sc_auth_polarssl_pub, iv_cs); // encrypt message { unsigned int temp; gcm_context gcm_context; //@ close gcm_context(&gcm_context); //@ open [f1]polarssl_cryptogram(key, KEY_BYTE_SIZE, key_cs, key_cg); //@ close polarssl_key_id(creator, key_id); if (gcm_init(&gcm_context, POLARSSL_AES_CIPHER_ID, key, (unsigned int) KEY_BYTE_SIZE * 8) != 0) abort(); //@ close [f1]polarssl_cryptogram(key, KEY_BYTE_SIZE, key_cs, key_cg); //@ chars_split(m + 16, message_len); if (gcm_crypt_and_tag(&gcm_context, POLARSSL_GCM_ENCRYPT, (unsigned int) message_len, iv, 16, NULL, 0, message, m + 16, 16, (char*) ((m + 16) + message_len)) != 0) abort(); gcm_free(&gcm_context); //@ open gcm_context(&gcm_context); } //@ assert polarssl_cryptogram(m + 16, message_len, ?e_cs, ?e_cg); /*@ assert e_cg == polarssl_auth_encrypted( creator, key_id, ?t_cs, m_cs, iv_cs); @*/ //@ close sc_auth_polarssl_pub(e_cg); //@ leak sc_auth_polarssl_pub(e_cg); /*@ polarssl_public_message_from_cryptogram( sc_auth_polarssl_pub, m + 16, message_len, e_cs, e_cg); @*/ /*@ open polarssl_public_message(sc_auth_polarssl_pub) (m + 16, message_len, e_cs); @*/ //@ assert chars(m + 16 + message_len, 16, t_cs); //@ chars_join(m); //@ chars_join(m); //@ assert chars(m, 16 + message_len + 16, ?cs); //@ append_assoc(iv_cs, e_cs, t_cs); //@ assert cs == append(iv_cs, append(e_cs, t_cs)); //@ polarssl_public_generated_chars_assume(sc_auth_polarssl_pub, t_cs); /*@ polarssl_public_generated_chars_join( sc_auth_polarssl_pub, iv_cs, e_cs); @*/ /*@ polarssl_public_generated_chars_join( sc_auth_polarssl_pub, append(iv_cs, e_cs), t_cs); @*/ /*@ close polarssl_public_message(sc_auth_polarssl_pub) (m, 16 + message_len + 16, append(iv_cs, append(e_cs, t_cs))); @*/ net_send(&socket, m, (unsigned int) 16 + (unsigned int) message_len + 16); //@ open polarssl_public_message(sc_auth_polarssl_pub)(m, _, _); { free(m); havege_free(&havege_state); //@ open havege_state(&havege_state); net_close(socket); } }
void sender(char *enc_key, char *hmac_key, char *msg, unsigned int msg_len) /*@ requires [_]public_invar(enc_then_hmac_pub) &*& principal(?sender, _) &*& [?f1]cryptogram(enc_key, KEY_SIZE, ?enc_key_ccs, ?enc_key_cg) &*& [?f2]cryptogram(hmac_key, KEY_SIZE, ?hmac_key_ccs, ?hmac_key_cg) &*& enc_key_cg == cg_symmetric_key(sender, ?enc_id) &*& hmac_key_cg == cg_symmetric_key(sender, ?hmac_id) &*& cg_info(hmac_key_cg) == enc_id &*& shared_with(sender, enc_id) == shared_with(sender, hmac_id) &*& [?f3]crypto_chars(secret, msg, msg_len, ?msg_ccs) &*& MAX_SIZE >= msg_len &*& msg_len >= MINIMAL_STRING_SIZE &*& bad(sender) || bad(shared_with(sender, enc_id)) ? [_]public_ccs(msg_ccs) : true == send(sender, shared_with(sender, enc_id), msg_ccs); @*/ /*@ ensures principal(sender, _) &*& [f1]cryptogram(enc_key, KEY_SIZE, enc_key_ccs, enc_key_cg) &*& [f2]cryptogram(hmac_key, KEY_SIZE, hmac_key_ccs, hmac_key_cg) &*& [f3]crypto_chars(secret, msg, msg_len, msg_ccs); @*/ { //@ open principal(sender, _); int socket; havege_state havege_state; char iv[16]; unsigned int iv_off = 0; char hmac[64]; aes_context aes_context; net_usleep(20000); if(net_connect(&socket, NULL, SERVER_PORT) != 0) abort(); if(net_set_block(socket) != 0) abort(); { int message_len = 16 + (int) msg_len + 64; char* message = malloc(message_len); if (message == 0) abort(); // IV stuff //@ close havege_state(&havege_state); havege_init(&havege_state); //@ close random_request(sender, 0, false); if (havege_random(&havege_state, iv, 16) != 0) abort(); //@ open cryptogram(iv, 16, ?iv_ccs, ?iv_cg); //@ close enc_then_hmac_pub(iv_cg); //@ leak enc_then_hmac_pub(iv_cg); memcpy(message, iv, 16); //@ close cryptogram(message, 16, iv_ccs, iv_cg); //@ public_cryptogram(message, iv_cg); //@ assert chars(message, 16, ?iv_cs); //@ public_chars(message, 16); havege_free(&havege_state); //@ open havege_state(&havege_state); // encrypt //@ close aes_context(&aes_context); if (aes_setkey_enc(&aes_context, enc_key, (unsigned int) (KEY_SIZE * 8)) != 0) abort(); if (aes_crypt_cfb128(&aes_context, AES_ENCRYPT, msg_len, &iv_off, iv, msg, message + 16) != 0) abort(); //@ open cryptogram(message + 16, msg_len, ?enc_ccs, ?enc_cg); //@ close cryptogram(message + 16, msg_len, enc_ccs, enc_cg); //@ close enc_then_hmac_pub(enc_cg); //@ leak enc_then_hmac_pub(enc_cg); //@ public_cryptogram(message + 16, enc_cg); //@ assert chars(message + 16, msg_len, ?enc_cs); //@ public_chars(message + 16, msg_len); //@ assert chars(message, 16 + msg_len, append(iv_cs, enc_cs)); //@ assert enc_cg == cg_encrypted(sender, enc_id, msg_ccs, iv_ccs); zeroize(iv, 16); aes_free(&aes_context); //@ open aes_context(&aes_context); // hmac //@ chars_to_crypto_chars(message, 16 + msg_len); //@ HASH_PUB_PAYLOAD(append(iv_cs, enc_cs)) sha512_hmac(hmac_key, KEY_SIZE, message, (unsigned int) (16 + (int) msg_len), message + 16 + (int) msg_len, 0); //@ assert cryptogram(message + 16 + msg_len, 64, ?hmac_ccs, ?hmac_cg); //@ cs_to_ccs_append(iv_cs, enc_cs); //@ assert hmac_cg == cg_hmac(sender, hmac_id, append(iv_ccs, enc_ccs)); /*@ if (!col && !enc_then_hmac_public_key(sender, enc_id, true)) close enc_then_hmac_pub_1(enc_id, msg_ccs, iv_ccs); @*/ /*@ if (col || enc_then_hmac_public_key(sender, enc_id, true)) { assert [_]public_ccs(iv_ccs); assert [_]public_ccs(enc_ccs); public_ccs_join(iv_ccs, enc_ccs); } @*/ //@ close enc_then_hmac_pub(hmac_cg); //@ leak enc_then_hmac_pub(hmac_cg); //@ public_cryptogram(message + 16 + msg_len, hmac_cg); //@ assert chars(message + 16 + msg_len, 64, ?hmac_cs); //@ append_assoc(iv_ccs, enc_ccs, hmac_ccs); //@ append_assoc(iv_cs, enc_cs, hmac_cs); //@ cs_to_ccs_crypto_chars(message, append(iv_cs, enc_cs)); /*@ assert chars(message, message_len, append(iv_cs, append(enc_cs, hmac_cs))); @*/ net_send(&socket, message, (unsigned int) message_len); free(message); } net_close(socket); //@ close principal(sender, _); }
int main( int argc, char *argv[] ) { int keysize, i; unsigned char tmp[200]; char title[TITLE_LEN]; todo_list todo; if( argc == 1 ) memset( &todo, 1, sizeof( todo ) ); else { memset( &todo, 0, sizeof( todo ) ); for( i = 1; i < argc; i++ ) { if( strcmp( argv[i], "md4" ) == 0 ) todo.md4 = 1; else if( strcmp( argv[i], "md5" ) == 0 ) todo.md5 = 1; else if( strcmp( argv[i], "ripemd160" ) == 0 ) todo.ripemd160 = 1; else if( strcmp( argv[i], "sha1" ) == 0 ) todo.sha1 = 1; else if( strcmp( argv[i], "sha256" ) == 0 ) todo.sha256 = 1; else if( strcmp( argv[i], "sha512" ) == 0 ) todo.sha512 = 1; else if( strcmp( argv[i], "arc4" ) == 0 ) todo.arc4 = 1; else if( strcmp( argv[i], "des3" ) == 0 ) todo.des3 = 1; else if( strcmp( argv[i], "des" ) == 0 ) todo.des = 1; else if( strcmp( argv[i], "aes_cbc" ) == 0 ) todo.aes_cbc = 1; else if( strcmp( argv[i], "aes_gcm" ) == 0 ) todo.aes_gcm = 1; else if( strcmp( argv[i], "aes_ccm" ) == 0 ) todo.aes_ccm = 1; else if( strcmp( argv[i], "camellia" ) == 0 ) todo.camellia = 1; else if( strcmp( argv[i], "blowfish" ) == 0 ) todo.blowfish = 1; else if( strcmp( argv[i], "havege" ) == 0 ) todo.havege = 1; else if( strcmp( argv[i], "ctr_drbg" ) == 0 ) todo.ctr_drbg = 1; else if( strcmp( argv[i], "hmac_drbg" ) == 0 ) todo.hmac_drbg = 1; else if( strcmp( argv[i], "rsa" ) == 0 ) todo.rsa = 1; else if( strcmp( argv[i], "dhm" ) == 0 ) todo.dhm = 1; else if( strcmp( argv[i], "ecdsa" ) == 0 ) todo.ecdsa = 1; else if( strcmp( argv[i], "ecdh" ) == 0 ) todo.ecdh = 1; else { polarssl_printf( "Unrecognized option: %s\n", argv[i] ); polarssl_printf( "Available options: " OPTIONS ); } } } polarssl_printf( "\n" ); memset( buf, 0xAA, sizeof( buf ) ); memset( tmp, 0xBB, sizeof( tmp ) ); #if defined(POLARSSL_MD4_C) if( todo.md4 ) TIME_AND_TSC( "MD4", md4( buf, BUFSIZE, tmp ) ); #endif #if defined(POLARSSL_MD5_C) if( todo.md5 ) TIME_AND_TSC( "MD5", md5( buf, BUFSIZE, tmp ) ); #endif #if defined(POLARSSL_RIPEMD160_C) if( todo.ripemd160 ) TIME_AND_TSC( "RIPEMD160", ripemd160( buf, BUFSIZE, tmp ) ); #endif #if defined(POLARSSL_SHA1_C) if( todo.sha1 ) TIME_AND_TSC( "SHA-1", sha1( buf, BUFSIZE, tmp ) ); #endif #if defined(POLARSSL_SHA256_C) if( todo.sha256 ) TIME_AND_TSC( "SHA-256", sha256( buf, BUFSIZE, tmp, 0 ) ); #endif #if defined(POLARSSL_SHA512_C) if( todo.sha512 ) TIME_AND_TSC( "SHA-512", sha512( buf, BUFSIZE, tmp, 0 ) ); #endif #if defined(POLARSSL_ARC4_C) if( todo.arc4 ) { arc4_context arc4; arc4_init( &arc4 ); arc4_setup( &arc4, tmp, 32 ); TIME_AND_TSC( "ARC4", arc4_crypt( &arc4, BUFSIZE, buf, buf ) ); arc4_free( &arc4 ); } #endif #if defined(POLARSSL_DES_C) && defined(POLARSSL_CIPHER_MODE_CBC) if( todo.des3 ) { des3_context des3; des3_init( &des3 ); des3_set3key_enc( &des3, tmp ); TIME_AND_TSC( "3DES", des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) ); des3_free( &des3 ); } if( todo.des ) { des_context des; des_init( &des ); des_setkey_enc( &des, tmp ); TIME_AND_TSC( "DES", des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) ); des_free( &des ); } #endif #if defined(POLARSSL_AES_C) #if defined(POLARSSL_CIPHER_MODE_CBC) if( todo.aes_cbc ) { aes_context aes; aes_init( &aes ); for( keysize = 128; keysize <= 256; keysize += 64 ) { snprintf( title, sizeof( title ), "AES-CBC-%d", keysize ); memset( buf, 0, sizeof( buf ) ); memset( tmp, 0, sizeof( tmp ) ); aes_setkey_enc( &aes, tmp, keysize ); TIME_AND_TSC( title, aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) ); } aes_free( &aes ); } #endif #if defined(POLARSSL_GCM_C) if( todo.aes_gcm ) { gcm_context gcm; for( keysize = 128; keysize <= 256; keysize += 64 ) { snprintf( title, sizeof( title ), "AES-GCM-%d", keysize ); memset( buf, 0, sizeof( buf ) ); memset( tmp, 0, sizeof( tmp ) ); gcm_init( &gcm, POLARSSL_CIPHER_ID_AES, tmp, keysize ); TIME_AND_TSC( title, gcm_crypt_and_tag( &gcm, GCM_ENCRYPT, BUFSIZE, tmp, 12, NULL, 0, buf, buf, 16, tmp ) ); gcm_free( &gcm ); } } #endif #if defined(POLARSSL_CCM_C) if( todo.aes_ccm ) { ccm_context ccm; for( keysize = 128; keysize <= 256; keysize += 64 ) { snprintf( title, sizeof( title ), "AES-CCM-%d", keysize ); memset( buf, 0, sizeof( buf ) ); memset( tmp, 0, sizeof( tmp ) ); ccm_init( &ccm, POLARSSL_CIPHER_ID_AES, tmp, keysize ); TIME_AND_TSC( title, ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp, 12, NULL, 0, buf, buf, tmp, 16 ) ); ccm_free( &ccm ); } } #endif #endif #if defined(POLARSSL_CAMELLIA_C) && defined(POLARSSL_CIPHER_MODE_CBC) if( todo.camellia ) { camellia_context camellia; camellia_init( &camellia ); for( keysize = 128; keysize <= 256; keysize += 64 ) { snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize ); memset( buf, 0, sizeof( buf ) ); memset( tmp, 0, sizeof( tmp ) ); camellia_setkey_enc( &camellia, tmp, keysize ); TIME_AND_TSC( title, camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf ) ); } camellia_free( &camellia ); } #endif #if defined(POLARSSL_BLOWFISH_C) && defined(POLARSSL_CIPHER_MODE_CBC) if( todo.blowfish ) { blowfish_context blowfish; blowfish_init( &blowfish ); for( keysize = 128; keysize <= 256; keysize += 64 ) { snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize ); memset( buf, 0, sizeof( buf ) ); memset( tmp, 0, sizeof( tmp ) ); blowfish_setkey( &blowfish, tmp, keysize ); TIME_AND_TSC( title, blowfish_crypt_cbc( &blowfish, BLOWFISH_ENCRYPT, BUFSIZE, tmp, buf, buf ) ); } blowfish_free( &blowfish ); } #endif #if defined(POLARSSL_HAVEGE_C) if( todo.havege ) { havege_state hs; havege_init( &hs ); TIME_AND_TSC( "HAVEGE", havege_random( &hs, buf, BUFSIZE ) ); havege_free( &hs ); } #endif #if defined(POLARSSL_CTR_DRBG_C) if( todo.ctr_drbg ) { ctr_drbg_context ctr_drbg; if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 ) exit(1); TIME_AND_TSC( "CTR_DRBG (NOPR)", if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) exit(1) ); if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 ) exit(1); ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_ON ); TIME_AND_TSC( "CTR_DRBG (PR)", if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) exit(1) ); ctr_drbg_free( &ctr_drbg ); }
void attacker() /*@ requires [_]public_invar(?pub) &*& [_]decryption_key_classifier(?classifier) &*& public_invariant_constraints(pub, ?proof_pred) &*& proof_pred() &*& principal(?bad_one, _) &*& true == bad(bad_one) &*& is_public_key_classifier(_, pub, classifier, proof_pred); @*/ /*@ ensures public_invariant_constraints(pub, proof_pred) &*& proof_pred() &*& principal(bad_one, _) &*& is_public_key_classifier(_, pub, classifier, proof_pred); @*/ { bool havege_failure = false; int server_or_client; int port; int* socket; int socket1; int socket2; //@ open principal(bad_one, _); havege_state havege_state; //@ close havege_state(&havege_state); havege_init(&havege_state); //@ open public_invariant_constraints(pub, proof_pred); /*@ { lemma void principal_with_public_nonces(cryptogram nonce) requires is_bad_nonce_is_public(?proof, pub, proof_pred) &*& proof_pred() &*& nonce == cg_nonce(bad_one, _); ensures is_bad_nonce_is_public(proof, pub, proof_pred) &*& proof_pred() &*& [_]pub(nonce); { proof(nonce); } produce_lemma_function_pointer_chunk(principal_with_public_nonces) : principal_with_public_nonces (pub, attacker_nonce_pred(pub, proof_pred), bad_one)(nonce__) { open attacker_nonce_pred(pub, proof_pred)(); call(); close attacker_nonce_pred(pub, proof_pred)(); } {duplicate_lemma_function_pointer_chunk(principal_with_public_nonces);}; } @*/ //@ close_havege_util(pub, proof_pred, bad_one); r_int(&havege_state, &server_or_client); r_int(&havege_state, &port); //@ open_havege_util(pub, proof_pred, bad_one); bool network_failure = false; if (server_or_client % 2 == 0) { if(net_connect(&socket1, NULL, port) != 0) network_failure = true; else if(net_set_block(socket1) != 0) network_failure = true; socket = &socket1; } else { if(net_bind(&socket1, NULL, port) != 0) {network_failure = true;} else if(net_accept(socket1, &socket2, NULL) != 0) {net_close(socket1); network_failure = true;} else if(net_set_block(socket2) != 0) {net_close(socket1); network_failure = true;} socket = &socket2; } if (!network_failure) { //@ close attacker_invariant(pub, proof_pred, ?kc, &havege_state, socket, bad_one); int j = 0; while(j < POLARSSL_ATTACKER_ITERATIONS) /*@ invariant attacker_invariant(pub, proof_pred, kc, &havege_state, socket, bad_one); @*/ { attacker_core(&havege_state, socket); j++; } //@ open attacker_invariant(pub, proof_pred, kc, &havege_state, socket, bad_one); if (server_or_client % 2 == 0) net_close(socket1); else { net_close(socket1); net_close(socket2); } } //@ close public_invariant_constraints(pub, proof_pred); //@ close principal(bad_one, _); havege_free(&havege_state); //@ open havege_state(&havege_state); /*@ leak is_principal_with_public_nonces(_, pub, attacker_nonce_pred(pub, proof_pred), bad_one); @*/ }