static mrb_value mrb_des_decrypt(mrb_state *mrb, mrb_value self) { mrb_value mode, key, source, dest, iv; unsigned char output[100]; des_context ctx; mrb_int len=8; memset(output, 0, sizeof(output)); mrb_get_args(mrb, "SSSS", &mode, &key, &source, &iv); des_init(&ctx); des_setkey_dec(&ctx, RSTRING_PTR(key)); if (mrb_str_cmp(mrb, mode, mrb_str_new(mrb, "CBC", 3)) == 0) { des_crypt_cbc(&ctx, DES_DECRYPT, RSTRING_LEN(source), RSTRING_PTR(iv), RSTRING_PTR(source), output); len = RSTRING_LEN(source); } else if (mrb_str_cmp(mrb, mode, mrb_str_new(mrb, "ECB", 3)) == 0) { des_crypt_ecb(&ctx, RSTRING_PTR(source), output); } else { des_free(&ctx); return mrb_nil_value(); } des_free(&ctx); return mrb_str_new(mrb, output, len); }
int decrypt_doc(unsigned char* data, int size) { data += 0x10; // Skip dummy PGD header. size -= 0x10; // Adjust size. unsigned char *out = new unsigned char[size]; // Perform DES CBC decryption. des_context ctx; des_setkey_dec(&ctx, des_key); des_crypt_cbc(&ctx, DES_DECRYPT, size, des_iv, data, out); // Check for "DOC" header in the decrypted data. if ((out[0] == 0x44) && (out[1] == 0x4F) && (out[2] == 0x43) && (out[3] == 0x20)) { // Copy back the decrypted data. memcpy(data - 0x10, out, size); delete[] out; return 0; } else { delete[] out; return -1; } }
static int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length, unsigned char *iv, const unsigned char *input, unsigned char *output ) { #if defined(POLARSSL_CIPHER_MODE_CBC) return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input, output ); #else ((void) ctx); ((void) operation); ((void) length); ((void) iv); ((void) input); ((void) output); return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE; #endif /* POLARSSL_CIPHER_MODE_CBC */ }
int main( void ) { int keysize; unsigned long i, j, tsc; unsigned char tmp[64]; t_cpu_time timer; /* Keep compiler happy */ UNUSED(keysize); UNUSED(i); UNUSED(j); UNUSED(tsc); UNUSED(tmp[0]); UNUSED(timer); // USART options. static usart_serial_options_t USART_SERIAL_OPTIONS = { .baudrate = USART_SERIAL_EXAMPLE_BAUDRATE, .charlength = USART_SERIAL_CHAR_LENGTH, .paritytype = USART_SERIAL_PARITY, .stopbits = USART_SERIAL_STOP_BIT }; sysclk_init(); // Initialize the board. // The board-specific conf_board.h file contains the configuration of the board // initialization. board_init(); // Initialize Serial Interface using Stdio Library stdio_serial_init(USART_SERIAL_EXAMPLE,&USART_SERIAL_OPTIONS); printf( "Start Benchmark\n"); #if defined(POLARSSL_ARC4_C) arc4_context arc4; #endif #if defined(POLARSSL_DES_C) des3_context des3; des_context des; #endif #if defined(POLARSSL_AES_C) aes_context aes; #endif #if defined(POLARSSL_CAMELLIA_C) camellia_context camellia; #endif #if defined(POLARSSL_RSA_C) rsa_context rsa; #endif memset( buf, 0xAA, sizeof( buf ) ); printf( "\n" ); #if defined(POLARSSL_MD4_C) printf( " MD4 : " ); fflush( stdout ); cpu_set_timeout(cpu_ms_2_cy(1000, CPU_HZ),&timer); for( i = 1; !cpu_is_timeout(&timer); i++ ) md4( buf, BUFSIZE, tmp ); tsc = hardclock(); for( j = 0; j < 1024; j++ ) md4( buf, BUFSIZE, tmp ); printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); #endif #if defined(POLARSSL_MD5_C) printf( " MD5 : " ); fflush( stdout ); cpu_set_timeout(cpu_ms_2_cy(1000, CPU_HZ),&timer); for( i = 1; !cpu_is_timeout(&timer); i++ ) md5( buf, BUFSIZE, tmp ); tsc = hardclock(); for( j = 0; j < 1024; j++ ) md5( buf, BUFSIZE, tmp ); printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); #endif #if defined(POLARSSL_SHA1_C) printf( " SHA-1 : " ); fflush( stdout ); cpu_set_timeout(cpu_ms_2_cy(1000, CPU_HZ),&timer); for( i = 1; !cpu_is_timeout(&timer); i++ ) sha1( buf, BUFSIZE, tmp ); tsc = hardclock(); for( j = 0; j < 1024; j++ ) sha1( buf, BUFSIZE, tmp ); printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); #endif #if defined(POLARSSL_SHA2_C) printf( " SHA-256 : " ); fflush( stdout ); cpu_set_timeout(cpu_ms_2_cy(1000, CPU_HZ),&timer); for( i = 1; !cpu_is_timeout(&timer); i++ ) sha2( buf, BUFSIZE, tmp, 0 ); tsc = hardclock(); for( j = 0; j < 1024; j++ ) sha2( buf, BUFSIZE, tmp, 0 ); printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); #endif #if defined(POLARSSL_SHA4_C) printf( " SHA-512 : " ); fflush( stdout ); cpu_set_timeout(cpu_ms_2_cy(1000, CPU_HZ),&timer); for( i = 1; !cpu_is_timeout(&timer); i++ ) sha4( buf, BUFSIZE, tmp, 0 ); tsc = hardclock(); for( j = 0; j < 1024; j++ ) sha4( buf, BUFSIZE, tmp, 0 ); printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); #endif #if defined(POLARSSL_ARC4_C) printf( " ARC4 : " ); fflush( stdout ); arc4_setup( &arc4, tmp, 32 ); cpu_set_timeout(cpu_ms_2_cy(1000, CPU_HZ),&timer); for( i = 1; !cpu_is_timeout(&timer); i++ ) arc4_crypt( &arc4, BUFSIZE, buf, buf ); tsc = hardclock(); for( j = 0; j < 1024; j++ ) arc4_crypt( &arc4, BUFSIZE, buf, buf ); printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); #endif #if defined(POLARSSL_DES_C) printf( " 3DES : " ); fflush( stdout ); des3_set3key_enc( &des3, tmp ); cpu_set_timeout(cpu_ms_2_cy(1000, CPU_HZ),&timer); for( i = 1; !cpu_is_timeout(&timer); i++ ) des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ); tsc = hardclock(); for( j = 0; j < 1024; j++ ) des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ); printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); printf( " DES : " ); fflush( stdout ); des_setkey_enc( &des, tmp ); cpu_set_timeout(cpu_ms_2_cy(1000, CPU_HZ),&timer); for( i = 1; !cpu_is_timeout(&timer); i++ ) des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ); tsc = hardclock(); for( j = 0; j < 1024; j++ ) des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ); printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); #endif #if defined(POLARSSL_AES_C) for( keysize = 128; keysize <= 256; keysize += 64 ) { printf( " AES-%d : ", keysize ); fflush( stdout ); memset( buf, 0, sizeof( buf ) ); memset( tmp, 0, sizeof( tmp ) ); aes_setkey_enc( &aes, tmp, keysize ); cpu_set_timeout(cpu_ms_2_cy(1000, CPU_HZ),&timer); for( i = 1; !cpu_is_timeout(&timer); i++ ) aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ); tsc = hardclock(); for( j = 0; j < 4096; j++ ) aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ); printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); } #endif #if defined(POLARSSL_CAMELLIA_C) for( keysize = 128; keysize <= 256; keysize += 64 ) { printf( " CAMELLIA-%d : ", keysize ); fflush( stdout ); memset( buf, 0, sizeof( buf ) ); memset( tmp, 0, sizeof( tmp ) ); camellia_setkey_enc( &camellia, tmp, keysize ); cpu_set_timeout(cpu_ms_2_cy(1000, CPU_HZ),&timer); for( i = 1; !cpu_is_timeout(&timer); i++ ) camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf ); tsc = hardclock(); for( j = 0; j < 4096; j++ ) camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf ); printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); } #endif #if defined(POLARSSL_RSA_C) rsa_init( &rsa, RSA_PKCS_V15, 0 ); rsa_gen_key( &rsa, myrand, NULL, 1024, 65537 ); printf( " RSA-1024 : " ); fflush( stdout ); cpu_set_timeout(cpu_ms_2_cy(3000, CPU_HZ),&timer); for( i = 1; !cpu_is_timeout(&timer); i++ ) { buf[0] = 0; rsa_public( &rsa, buf, buf ); } printf( "%9lu public/s\n", i / 3 ); printf( " RSA-1024 : " ); fflush( stdout ); cpu_set_timeout(cpu_ms_2_cy(3000, CPU_HZ),&timer); for( i = 1; !cpu_is_timeout(&timer); i++ ) { buf[0] = 0; rsa_private( &rsa, buf, buf ); } printf( "%9lu private/s\n", i / 3 ); rsa_free( &rsa ); rsa_init( &rsa, RSA_PKCS_V15, 0 ); rsa_gen_key( &rsa, myrand, NULL, 2048, 65537 ); printf( " RSA-2048 : " ); fflush( stdout ); cpu_set_timeout(cpu_ms_2_cy(3000, CPU_HZ),&timer); for( i = 1; !cpu_is_timeout(&timer); i++ ) { buf[0] = 0; rsa_public( &rsa, buf, buf ); } printf( "%9lu public/s\n", i / 3 ); printf( " RSA-2048 : " ); fflush( stdout ); cpu_set_timeout(cpu_ms_2_cy(3000, CPU_HZ),&timer); for( i = 1; ! cpu_is_timeout(&timer); i++ ) { buf[0] = 0; rsa_private( &rsa, buf, buf ); } printf( "%9lu private/s\n", i / 3 ); rsa_free( &rsa ); rsa_init( &rsa, RSA_PKCS_V15, 0 ); rsa_gen_key( &rsa, myrand, NULL, 4096, 65537 ); printf( " RSA-4096 : " ); fflush( stdout ); cpu_set_timeout(cpu_ms_2_cy(3000, CPU_HZ),&timer); for( i = 1; !cpu_is_timeout(&timer); i++ ) { buf[0] = 0; rsa_public( &rsa, buf, buf ); } printf( "%9lu public/s\n", i / 3 ); printf( " RSA-4096 : " ); fflush( stdout ); cpu_set_timeout(cpu_ms_2_cy(3000, CPU_HZ),&timer); for( i = 1; ! cpu_is_timeout(&timer); i++ ) { buf[0] = 0; rsa_private( &rsa, buf, buf ); } printf( "%9lu private/s\n", i / 3 ); rsa_free( &rsa ); #endif printf( "\n" ); #ifdef WIN32 printf( " Press Enter to exit this program.\n" ); fflush( stdout ); getchar(); #endif return( 0 ); }
int main( int argc, char *argv[] ) { int keysize; unsigned long i, j, tsc; unsigned char tmp[64]; #if defined(POLARSSL_ARC4_C) arc4_context arc4; #endif #if defined(POLARSSL_DES_C) des3_context des3; des_context des; #endif #if defined(POLARSSL_AES_C) aes_context aes; #endif #if defined(POLARSSL_CAMELLIA_C) camellia_context camellia; #endif #if defined(POLARSSL_RSA_C) && defined(POLARSSL_BIGNUM_C) && \ defined(POLARSSL_GENPRIME) rsa_context rsa; #endif #if defined(POLARSSL_HAVEGE_C) havege_state hs; #endif #if defined(POLARSSL_CTR_DRBG_C) ctr_drbg_context ctr_drbg; #endif ((void) argc); ((void) argv); memset( buf, 0xAA, sizeof( buf ) ); printf( "\n" ); #if defined(POLARSSL_MD4_C) printf( HEADER_FORMAT, "MD4" ); fflush( stdout ); set_alarm( 1 ); for( i = 1; ! alarmed; i++ ) md4( buf, BUFSIZE, tmp ); tsc = hardclock(); for( j = 0; j < 1024; j++ ) md4( buf, BUFSIZE, tmp ); printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); #endif #if defined(POLARSSL_MD5_C) printf( HEADER_FORMAT, "MD5" ); fflush( stdout ); set_alarm( 1 ); for( i = 1; ! alarmed; i++ ) md5( buf, BUFSIZE, tmp ); tsc = hardclock(); for( j = 0; j < 1024; j++ ) md5( buf, BUFSIZE, tmp ); printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); #endif #if defined(POLARSSL_SHA1_C) printf( HEADER_FORMAT, "SHA-1" ); fflush( stdout ); set_alarm( 1 ); for( i = 1; ! alarmed; i++ ) sha1( buf, BUFSIZE, tmp ); tsc = hardclock(); for( j = 0; j < 1024; j++ ) sha1( buf, BUFSIZE, tmp ); printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); #endif #if defined(POLARSSL_SHA2_C) printf( HEADER_FORMAT, "SHA-256" ); fflush( stdout ); set_alarm( 1 ); for( i = 1; ! alarmed; i++ ) sha2( buf, BUFSIZE, tmp, 0 ); tsc = hardclock(); for( j = 0; j < 1024; j++ ) sha2( buf, BUFSIZE, tmp, 0 ); printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); #endif #if defined(POLARSSL_SHA4_C) printf( HEADER_FORMAT, "SHA-512" ); fflush( stdout ); set_alarm( 1 ); for( i = 1; ! alarmed; i++ ) sha4( buf, BUFSIZE, tmp, 0 ); tsc = hardclock(); for( j = 0; j < 1024; j++ ) sha4( buf, BUFSIZE, tmp, 0 ); printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); #endif #if defined(POLARSSL_ARC4_C) printf( HEADER_FORMAT, "ARC4" ); fflush( stdout ); arc4_setup( &arc4, tmp, 32 ); set_alarm( 1 ); for( i = 1; ! alarmed; i++ ) arc4_crypt( &arc4, BUFSIZE, buf, buf ); tsc = hardclock(); for( j = 0; j < 1024; j++ ) arc4_crypt( &arc4, BUFSIZE, buf, buf ); printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); #endif #if defined(POLARSSL_DES_C) printf( HEADER_FORMAT, "3DES" ); fflush( stdout ); des3_set3key_enc( &des3, tmp ); set_alarm( 1 ); for( i = 1; ! alarmed; i++ ) des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ); tsc = hardclock(); for( j = 0; j < 1024; j++ ) des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ); printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); printf( HEADER_FORMAT, "DES" ); fflush( stdout ); des_setkey_enc( &des, tmp ); set_alarm( 1 ); for( i = 1; ! alarmed; i++ ) des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ); tsc = hardclock(); for( j = 0; j < 1024; j++ ) des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ); printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); #endif #if defined(POLARSSL_AES_C) for( keysize = 128; keysize <= 256; keysize += 64 ) { printf( " AES-%d : ", keysize ); fflush( stdout ); memset( buf, 0, sizeof( buf ) ); memset( tmp, 0, sizeof( tmp ) ); aes_setkey_enc( &aes, tmp, keysize ); set_alarm( 1 ); for( i = 1; ! alarmed; i++ ) aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ); tsc = hardclock(); for( j = 0; j < 4096; j++ ) aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ); printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); } #endif #if defined(POLARSSL_CAMELLIA_C) for( keysize = 128; keysize <= 256; keysize += 64 ) { printf( " CAMELLIA-%d : ", keysize ); fflush( stdout ); memset( buf, 0, sizeof( buf ) ); memset( tmp, 0, sizeof( tmp ) ); camellia_setkey_enc( &camellia, tmp, keysize ); set_alarm( 1 ); for( i = 1; ! alarmed; i++ ) camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf ); tsc = hardclock(); for( j = 0; j < 4096; j++ ) camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf ); printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); } #endif #if defined(POLARSSL_HAVEGE_C) printf( HEADER_FORMAT, "HAVEGE" ); fflush( stdout ); havege_init( &hs ); set_alarm( 1 ); for( i = 1; ! alarmed; i++ ) havege_random( &hs, buf, BUFSIZE ); tsc = hardclock(); for( j = 1; j < 1024; j++ ) havege_random( &hs, buf, BUFSIZE ); printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); #endif #if defined(POLARSSL_CTR_DRBG_C) printf( HEADER_FORMAT, "CTR_DRBG (NOPR)" ); fflush( stdout ); if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 ) exit(1); set_alarm( 1 ); for( i = 1; ! alarmed; i++ ) if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) exit(1); tsc = hardclock(); for( j = 1; j < 1024; j++ ) if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) exit(1); printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); printf( HEADER_FORMAT, "CTR_DRBG (PR)" ); fflush( stdout ); if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 ) exit(1); ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_ON ); set_alarm( 1 ); for( i = 1; ! alarmed; i++ ) if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) exit(1); tsc = hardclock(); for( j = 1; j < 1024; j++ ) if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) exit(1); printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); #endif #if defined(POLARSSL_RSA_C) && defined(POLARSSL_BIGNUM_C) && \ defined(POLARSSL_GENPRIME) rsa_init( &rsa, RSA_PKCS_V15, 0 ); rsa_gen_key( &rsa, myrand, NULL, 1024, 65537 ); printf( HEADER_FORMAT, "RSA-1024" ); fflush( stdout ); set_alarm( 3 ); for( i = 1; ! alarmed; i++ ) { buf[0] = 0; rsa_public( &rsa, buf, buf ); } printf( "%9lu public/s\n", i / 3 ); printf( HEADER_FORMAT, "RSA-1024" ); fflush( stdout ); set_alarm( 3 ); for( i = 1; ! alarmed; i++ ) { buf[0] = 0; rsa_private( &rsa, buf, buf ); } printf( "%9lu private/s\n", i / 3 ); rsa_free( &rsa ); rsa_init( &rsa, RSA_PKCS_V15, 0 ); rsa_gen_key( &rsa, myrand, NULL, 2048, 65537 ); printf( HEADER_FORMAT, "RSA-2048" ); fflush( stdout ); set_alarm( 3 ); for( i = 1; ! alarmed; i++ ) { buf[0] = 0; rsa_public( &rsa, buf, buf ); } printf( "%9lu public/s\n", i / 3 ); printf( HEADER_FORMAT, "RSA-2048" ); fflush( stdout ); set_alarm( 3 ); for( i = 1; ! alarmed; i++ ) { buf[0] = 0; rsa_private( &rsa, buf, buf ); } printf( "%9lu private/s\n", i / 3 ); rsa_free( &rsa ); rsa_init( &rsa, RSA_PKCS_V15, 0 ); rsa_gen_key( &rsa, myrand, NULL, 4096, 65537 ); printf( HEADER_FORMAT, "RSA-4096" ); fflush( stdout ); set_alarm( 3 ); for( i = 1; ! alarmed; i++ ) { buf[0] = 0; rsa_public( &rsa, buf, buf ); } printf( "%9lu public/s\n", i / 3 ); printf( HEADER_FORMAT, "RSA-4096" ); fflush( stdout ); set_alarm( 3 ); for( i = 1; ! alarmed; i++ ) { buf[0] = 0; rsa_private( &rsa, buf, buf ); } printf( "%9lu private/s\n", i / 3 ); rsa_free( &rsa ); #endif printf( "\n" ); #if defined(_WIN32) printf( " Press Enter to exit this program.\n" ); fflush( stdout ); getchar(); #endif return( 0 ); }
int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length, unsigned char *iv, const unsigned char *input, unsigned char *output ) { return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input, output ); }
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], "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 { printf( "Unrecognized option: %s\n", argv[i] ); printf( "Available options:" OPTIONS ); } } } printf( "\n" ); memset( buf, 0xAA, sizeof( buf ) ); #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_setup( &arc4, tmp, 32 ); TIME_AND_TSC( "ARC4", arc4_crypt( &arc4, BUFSIZE, buf, buf ) ); } #endif #if defined(POLARSSL_DES_C) && defined(POLARSSL_CIPHER_MODE_CBC) if( todo.des3 ) { des3_context des3; des3_set3key_enc( &des3, tmp ); TIME_AND_TSC( "3DES", des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) ); } if( todo.des ) { des_context des; des_setkey_enc( &des, tmp ); TIME_AND_TSC( "DES", des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) ); } #endif #if defined(POLARSSL_AES_C) #if defined(POLARSSL_CIPHER_MODE_CBC) if( todo.aes_cbc ) { aes_context 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 ) ); } } #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 #endif #if defined(POLARSSL_CAMELLIA_C) && defined(POLARSSL_CIPHER_MODE_CBC) if( todo.camellia ) { camellia_context 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 ) ); } } #endif #if defined(POLARSSL_BLOWFISH_C) && defined(POLARSSL_CIPHER_MODE_CBC) if( todo.blowfish ) { blowfish_context 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 ) ); } } #endif #if defined(POLARSSL_HAVEGE_C) if( todo.havege ) { havege_state hs; havege_init( &hs ); TIME_AND_TSC( "HAVEGE", havege_random( &hs, buf, BUFSIZE ) ); } #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) ); }
int main(void) { int keysize; unsigned long i, j, tsc; unsigned char tmp[32]; #if defined(TROPICSSL_ARC4_C) arc4_context arc4; #endif #if defined(TROPICSSL_DES_C) des3_context des3; des_context des; #endif #if defined(TROPICSSL_AES_C) aes_context aes; #endif #if defined(TROPICSSL_CAMELLIA_C) camellia_context camellia; #endif #if defined(TROPICSSL_RSA_C) rsa_context rsa; #endif memset(buf, 0xAA, sizeof(buf)); printf("\n"); #if defined(TROPICSSL_MD4_C) printf(" MD4 : "); fflush(stdout); set_alarm(1); for (i = 1; !alarmed; i++) md4(buf, BUFSIZE, tmp); tsc = hardclock(); for (j = 0; j < 1024; j++) md4(buf, BUFSIZE, tmp); printf("%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, (hardclock() - tsc) / (j * BUFSIZE)); #endif #if defined(TROPICSSL_MD5_C) printf(" MD5 : "); fflush(stdout); set_alarm(1); for (i = 1; !alarmed; i++) md5(buf, BUFSIZE, tmp); tsc = hardclock(); for (j = 0; j < 1024; j++) md5(buf, BUFSIZE, tmp); printf("%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, (hardclock() - tsc) / (j * BUFSIZE)); #endif #if defined(TROPICSSL_SHA1_C) printf(" SHA-1 : "); fflush(stdout); set_alarm(1); for (i = 1; !alarmed; i++) sha1(buf, BUFSIZE, tmp); tsc = hardclock(); for (j = 0; j < 1024; j++) sha1(buf, BUFSIZE, tmp); printf("%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, (hardclock() - tsc) / (j * BUFSIZE)); #endif #if defined(TROPICSSL_SHA2_C) printf(" SHA-256 : "); fflush(stdout); set_alarm(1); for (i = 1; !alarmed; i++) sha2(buf, BUFSIZE, tmp, 0); tsc = hardclock(); for (j = 0; j < 1024; j++) sha2(buf, BUFSIZE, tmp, 0); printf("%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, (hardclock() - tsc) / (j * BUFSIZE)); #endif #if defined(TROPICSSL_ARC4_C) printf(" ARC4 : "); fflush(stdout); arc4_setup(&arc4, tmp, 32); set_alarm(1); for (i = 1; !alarmed; i++) arc4_crypt(&arc4, buf, BUFSIZE); tsc = hardclock(); for (j = 0; j < 1024; j++) arc4_crypt(&arc4, buf, BUFSIZE); printf("%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, (hardclock() - tsc) / (j * BUFSIZE)); #endif #if defined(TROPICSSL_DES_C) printf(" 3DES : "); fflush(stdout); des3_set3key_enc(&des3, tmp); set_alarm(1); for (i = 1; !alarmed; i++) des3_crypt_cbc(&des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf); tsc = hardclock(); for (j = 0; j < 1024; j++) des3_crypt_cbc(&des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf); printf("%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, (hardclock() - tsc) / (j * BUFSIZE)); printf(" DES : "); fflush(stdout); des_setkey_enc(&des, tmp); set_alarm(1); for (i = 1; !alarmed; i++) des_crypt_cbc(&des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf); tsc = hardclock(); for (j = 0; j < 1024; j++) des_crypt_cbc(&des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf); printf("%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, (hardclock() - tsc) / (j * BUFSIZE)); #endif #if defined(TROPICSSL_AES_C) for (keysize = 128; keysize <= 256; keysize += 64) { printf(" AES-%d : ", keysize); fflush(stdout); memset(buf, 0, sizeof(buf)); memset(tmp, 0, sizeof(tmp)); aes_setkey_enc(&aes, tmp, keysize); set_alarm(1); for (i = 1; !alarmed; i++) aes_crypt_cbc(&aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf); tsc = hardclock(); for (j = 0; j < 4096; j++) aes_crypt_cbc(&aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf); printf("%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, (hardclock() - tsc) / (j * BUFSIZE)); } #endif #if defined(TROPICSSL_CAMELLIA_C) for (keysize = 128; keysize <= 256; keysize += 64) { printf(" CAMELLIA-%d : ", keysize); fflush(stdout); memset(buf, 0, sizeof(buf)); memset(tmp, 0, sizeof(tmp)); camellia_setkey_enc(&camellia, tmp, keysize); set_alarm(1); for (i = 1; !alarmed; i++) camellia_crypt_cbc(&camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf); tsc = hardclock(); for (j = 0; j < 4096; j++) camellia_crypt_cbc(&camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf); printf("%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, (hardclock() - tsc) / (j * BUFSIZE)); } #endif #if defined(TROPICSSL_RSA_C) rsa_init(&rsa, RSA_PKCS_V15, 0, myrand, NULL); rsa_gen_key(&rsa, 1024, 65537); printf(" RSA-1024 : "); fflush(stdout); set_alarm(3); for (i = 1; !alarmed; i++) { buf[0] = 0; rsa_public(&rsa, buf, buf); } printf("%9lu public/s\n", i / 3); printf(" RSA-1024 : "); fflush(stdout); set_alarm(3); for (i = 1; !alarmed; i++) { buf[0] = 0; rsa_private(&rsa, buf, buf); } printf("%9lu private/s\n\n", i / 3); rsa_free(&rsa); #endif #ifdef WIN32 printf(" Press Enter to exit this program.\n"); fflush(stdout); getchar(); #endif return (0); }