int32 CTCPRequestPacket::SendToSocket(uint8* data, uint32 length) { int32 iResult; WBUFW(data,(0x00)) = length; // packet size WBUFL(data,(0x04)) = 0x46465849; // "XIFF" md5((uint8*)(key), blowfish.hash, 24); blowfish_init((int8*)blowfish.hash,16, blowfish.P, blowfish.S[0]); md5(data+8, data+length-0x18+0x04, length-0x18-0x04); uint8 tmp = (length-12)/4; tmp -= tmp%2; for(uint8 i = 0; i < tmp; i += 2) { blowfish_encipher((uint32*)data+i+2, (uint32*)data+i+3, blowfish.P, blowfish.S[0]); } memcpy(&data[length]-0x04, key+16, 4); iResult = send(*m_socket, (const int8*)data, length, 0); if (iResult == SOCKET_ERROR) { #ifdef WIN32 ShowError("send failed with error: %d\n", WSAGetLastError()); #else ShowError("send failed with error: %d\n", errno); #endif return 0; } return ReceiveFromSocket(); }
static void * blowfish_ctx_alloc( void ) { blowfish_context *ctx; ctx = (blowfish_context *) polarssl_malloc( sizeof( blowfish_context ) ); if( ctx == NULL ) return( NULL ); blowfish_init( ctx ); return( ctx ); }
IoObject *IoBlowfish_beginProcessing(IoObject *self, IoObject *locals, IoMessage *m) { /*doc Blowfish beginProcessing Sets the key from the key slot and initializes the cipher. */ UArray *key = IoObject_rawGetUArraySlot(self, locals, m, IOSYMBOL("key")); blowfish_ctx *context = &(DATA(self)->context); blowfish_init(context, (uint8_t *)UArray_bytes(key), UArray_sizeInBytes(key)); return self; }
int blowfish_testRun(void) { // NOTE: we use a static variable here to avoid stack overflows due to the huge // context structure of blowfish. static BlowfishContext ctx; blowfish_init(&ctx); BlockCipher *c = &ctx.c; for (int i=0; i<NUM_VARIABLE_KEY_TESTS; ++i) { uint32_t data[2] = { cpu_to_be32(plaintext_l[i]), cpu_to_be32(plaintext_r[i]) }; cipher_set_vkey(c, variable_key[i], 8); cipher_ecb_encrypt(c, data); ASSERT(data[0] == cpu_to_be32(ciphertext_l[i])); ASSERT(data[1] == cpu_to_be32(ciphertext_r[i])); cipher_ecb_decrypt(c, data); ASSERT(data[0] == cpu_to_be32(plaintext_l[i])); ASSERT(data[1] == cpu_to_be32(plaintext_r[i])); } for (int j=0; j<NUM_SET_KEY_TESTS; ++j) { int i = j + NUM_VARIABLE_KEY_TESTS; uint32_t data[2] = { cpu_to_be32(plaintext_l[i]), cpu_to_be32(plaintext_r[i]) }; cipher_set_vkey(c, &set_key[0], j+1); cipher_ecb_encrypt(c, data); ASSERT(data[0] == cpu_to_be32(ciphertext_l[i])); ASSERT(data[1] == cpu_to_be32(ciphertext_r[i])); cipher_ecb_decrypt(c, data); ASSERT(data[0] == cpu_to_be32(plaintext_l[i])); ASSERT(data[1] == cpu_to_be32(plaintext_r[i])); } uint8_t data[8]; memset(data, 0, 8); cipher_set_vkey(c, "0123456789", 10); for (int i=0;i<1000;++i) cipher_ecb_encrypt(c, data); for (int i=0;i<1000;++i) cipher_ecb_decrypt(c, data); ASSERT(memcmp(data, "\x00\x00\x00\x00\x00\x00\x00\x00", 8) == 0); return 0; }
int crypto_stream_xor(unsigned char *out, const unsigned char *in, unsigned long long inlen, const unsigned char *n, const unsigned char *k) { #define PTR_ALIGN(ptr, mask) ((void *)((((long)(ptr)) + (mask)) & ~((long)(mask)))) const unsigned long align = 4096; char ctxbuf[sizeof(struct blowfish_ctx) + align]; struct blowfish_ctx *ctx = PTR_ALIGN(ctxbuf, align - 1); uint64_t iv; uint64_t block; blowfish_init(ctx, k, CRYPTO_KEYBYTES); iv = __builtin_bswap64(*(uint64_t *)n); /* be => le */ while (likely(inlen >= BLOCKSIZE)) { block = __builtin_bswap64(iv++); /* le => be */ blowfish_enc_blk(ctx, out, &block); if (unlikely(in)) { *(uint64_t *)out ^= *(uint64_t *)in; in += BLOCKSIZE; } out += BLOCKSIZE; inlen -= BLOCKSIZE; } if (unlikely(inlen > 0)) { /* handle remaining bytes */ unsigned int i; block = __builtin_bswap64(iv); /* le => be */ blowfish_enc_blk(ctx, &block, &block); if (in) { for (i = 0; i < inlen; i++) out[i] = in[i] ^ ((uint8_t*)&block)[i]; } else { for (i = 0; i < inlen; i++) out[i] = ((uint8_t*)&block)[i]; } } return 0; }
int crypto_stream_xor(unsigned char *out, const unsigned char *in, unsigned long long inlen, const unsigned char *n, const unsigned char *k) { #define PTR_ALIGN(ptr, mask) ((void *)((((long)(ptr)) + (mask)) & ~((long)(mask)))) const unsigned long align = 16; char ctxbuf[sizeof(struct blowfish_ctx) + align]; struct blowfish_ctx *ctx = PTR_ALIGN(ctxbuf, align - 1); uint64_t iv; uint64_t ivs[4]; blowfish_init(ctx, k, CRYPTO_KEYBYTES); iv = __builtin_bswap64(*(uint64_t *)n); /* be => le */ while (likely(inlen >= BLOCKSIZE * 4)) { ivs[0] = __builtin_bswap64(iv + 0); /* le => be */ ivs[1] = __builtin_bswap64(iv + 1); /* le => be */ ivs[2] = __builtin_bswap64(iv + 2); /* le => be */ ivs[3] = __builtin_bswap64(iv + 3); /* le => be */ iv += 4; blowfish_enc_blk4(ctx, out, (uint8_t *)ivs); if (unlikely(in)) { ((uint64_t *)out)[0] ^= ((uint64_t *)in)[0]; ((uint64_t *)out)[1] ^= ((uint64_t *)in)[1]; ((uint64_t *)out)[2] ^= ((uint64_t *)in)[2]; ((uint64_t *)out)[3] ^= ((uint64_t *)in)[3]; in += BLOCKSIZE * 4; } out += BLOCKSIZE * 4; inlen -= BLOCKSIZE * 4; } if (unlikely(inlen > 0)) { unsigned int nblock = inlen / BLOCKSIZE; unsigned int lastlen = inlen % BLOCKSIZE; unsigned int i, j; for (i = 0; i < nblock + !!lastlen; i++) ivs[i] = __builtin_bswap64(iv++); /* le => be */ for (; i < 4; i++) ivs[i] = 0; blowfish_enc_blk4(ctx, (uint8_t *)ivs, (uint8_t *)ivs); if (in) { for (i = 0; inlen >= BLOCKSIZE; i++) { *(uint64_t *)out = *(uint64_t *)in ^ ivs[i]; inlen -= BLOCKSIZE; in += BLOCKSIZE; out += BLOCKSIZE; } for (j = 0; j < inlen; j++) out[j] = in[j] ^ ((uint8_t*)&ivs[i])[j]; } else { for (i = 0; inlen >= BLOCKSIZE; i++) { *(uint64_t *)out = ivs[i]; inlen -= BLOCKSIZE; out += BLOCKSIZE; } for (j = 0; j < inlen; j++) out[j] = ((uint8_t*)&ivs[i])[j]; } } return 0; }
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 ); }
int crypto_stream_xor(unsigned char *out, const unsigned char *in, unsigned long long inlen, const unsigned char *n, const unsigned char *k) { #define PTR_ALIGN(ptr, mask) ((void *)((((long)(ptr)) + (mask)) & ~((long)(mask)))) const unsigned long align = 16; char ctxbuf[sizeof(struct blowfish_ctx) + align]; struct blowfish_ctx *ctx = PTR_ALIGN(ctxbuf, align - 1); uint64_t iv; uint64_t ivs[16]; unsigned int i; blowfish_init(ctx, k, CRYPTO_KEYBYTES); bswap64(&iv, (const uint64_t *)n); /* be => le */ while (likely(inlen >= BLOCKSIZE * 16)) { bswap64(&ivs[0], &iv); /* le => be */ for (i = 1; i < 16; i++) { add64(&ivs[i], &iv, i); bswap64(&ivs[i], &ivs[i]); /* le => be */ } add64(&iv, &iv, 16); __blowfish_enc_blk_16way(ctx, out, (uint8_t *)ivs, 0); if (unlikely(in)) { for (i = 0; i < 16; i+=2) xor128(&((uint64_t *)out)[i], &((uint64_t *)out)[i], &((uint64_t *)in)[i]); in += BLOCKSIZE * 16; } out += BLOCKSIZE * 16; inlen -= BLOCKSIZE * 16; } if (unlikely(inlen > 0)) { unsigned int nblock = inlen / BLOCKSIZE; unsigned int lastlen = inlen % BLOCKSIZE; unsigned int j; for (i = 0; i < nblock + !!lastlen; i++) { bswap64(&ivs[i], &iv); /* le => be */ inc64(&iv); } for (; i < 16; i++) { ivs[i] = 0; } __blowfish_enc_blk_16way(ctx, (uint8_t *)ivs, (uint8_t *)ivs, 0); if (in) { for (i = 0; inlen >= 2*BLOCKSIZE; i+=2) { xor128((uint64_t *)out, (uint64_t *)in, (uint64_t *)&ivs[i]); inlen -= 2*BLOCKSIZE; in += 2*BLOCKSIZE; out += 2*BLOCKSIZE; } for (j = 0; j < inlen; j++) out[j] = in[j] ^ ((uint8_t*)&ivs[i])[j]; } else { for (i = 0; inlen >= 2*BLOCKSIZE; i+=2) { mov128((uint64_t *)out, (uint64_t *)&ivs[i]); inlen -= 2*BLOCKSIZE; out += 2*BLOCKSIZE; } for (j = 0; j < inlen; j++) out[j] = ((uint8_t*)&ivs[i])[j]; } } return 0; }