void test_main (void) { struct arc4 arc4; size_t i; /* Initialize to 0x5a. */ msg ("initialize"); memset (buf, 0x5a, sizeof buf); /* Check that it's all 0x5a. */ msg ("read pass"); for (i = 0; i < SIZE; i++) if (buf[i] != 0x5a) fail ("byte %zu != 0x5a", i); /* Encrypt zeros. */ msg ("read/modify/write pass one"); arc4_init (&arc4, "foobar", 6); arc4_crypt (&arc4, buf, SIZE); /* Decrypt back to zeros. */ msg ("read/modify/write pass two"); arc4_init (&arc4, "foobar", 6); arc4_crypt (&arc4, buf, SIZE); /* Check that it's all 0x5a. */ msg ("read pass"); for (i = 0; i < SIZE; i++) if (buf[i] != 0x5a) fail ("byte %zu != 0x5a\nval = %x", i, buf[i]); //fail ("byte %zu != 0x5a", i); }
int main (int argc, char *argv[]) { const char *key = argv[argc - 1]; struct arc4 arc4; size_t i; /* Encrypt zeros. */ arc4_init (&arc4, key, strlen (key)); arc4_crypt (&arc4, buf, SIZE); printf("**** [child-linear.c] break1\n"); /* Decrypt back to zeros. */ arc4_init (&arc4, key, strlen (key)); printf("**** [child-linear.c] break2\n"); arc4_crypt (&arc4, buf, SIZE); printf("**** [child-linear.c] break3\n"); /* Check that it's all zeros. */ for (i = 0; i < SIZE; i++) if (buf[i] != '\0') fail ("byte %zu != 0", i); return 0x42; }
/* * Checkup routine */ int arc4_self_test( int verbose ) { int i; unsigned char ibuf[8]; unsigned char obuf[8]; arc4_context ctx; for( i = 0; i < 3; i++ ) { if( verbose != 0 ) printf( " ARC4 test #%d: ", i + 1 ); memcpy( ibuf, arc4_test_pt[i], 8 ); arc4_setup( &ctx, (unsigned char *) arc4_test_key[i], 8 ); arc4_crypt( &ctx, 8, ibuf, obuf ); if( memcmp( obuf, arc4_test_ct[i], 8 ) != 0 ) { if( verbose != 0 ) printf( "failed\n" ); return( 1 ); } if( verbose != 0 ) printf( "passed\n" ); } if( verbose != 0 ) printf( "\n" ); return( 0 ); }
/* * Checkup routine */ int arc4_self_test(int verbose) { int i; uint8_t buf[8]; arc4_context ctx; for (i = 0; i < 3; i++) { if (verbose != 0) printf(" ARC4 test #%d: ", i + 1); memcpy(buf, arc4_test_pt[i], 8); arc4_setup(&ctx, (const uint8_t *)arc4_test_key[i], 8); arc4_crypt(&ctx, buf, 8); if (memcmp(buf, arc4_test_ct[i], 8) != 0) { if (verbose != 0) printf("failed\n"); return (1); } if (verbose != 0) printf("passed\n"); } if (verbose != 0) printf("\n"); return (0); }
BOOL CNetSock::SendSock(char *pBuf, int Size) { int iRes; if(m_hSocket == INVALID_SOCKET) return FALSE; if (m_bEncryption) { unsigned char* tmp = (unsigned char*)malloc(Size); memcpy(tmp, pBuf, Size); arc4_crypt(&m_EncryptSendCtx, tmp, Size); iRes = send(m_hSocket, (const char*)tmp, Size, 0); free(tmp); } else { iRes = send(m_hSocket, pBuf, Size, 0); } if(iRes == SOCKET_ERROR) { TRACE(_T("Error send!\n")); m_Error = WSAGetLastError(); TRACE(_T("Error %d (size = %d)!\n"), m_Error, Size); return FALSE; } return TRUE; }
/* * Perform the MPPE rekey algorithm, from RFC 3078, sec. 7.3. * Well, not what's written there, but rather what they meant. */ static void mppe_rekey(ppp_mppe_state * state, int initial_key) { sha1_context sha1_ctx; u8_t sha1_digest[SHA1_SIGNATURE_SIZE]; /* * Key Derivation, from RFC 3078, RFC 3079. * Equivalent to Get_Key() for MS-CHAP as described in RFC 3079. */ sha1_starts(&sha1_ctx); sha1_update(&sha1_ctx, state->master_key, state->keylen); sha1_update(&sha1_ctx, mppe_sha1_pad1, SHA1_PAD_SIZE); sha1_update(&sha1_ctx, state->session_key, state->keylen); sha1_update(&sha1_ctx, mppe_sha1_pad2, SHA1_PAD_SIZE); sha1_finish(&sha1_ctx, sha1_digest); MEMCPY(state->session_key, sha1_digest, state->keylen); if (!initial_key) { arc4_setup(&state->arc4, sha1_digest, state->keylen); arc4_crypt(&state->arc4, state->session_key, state->keylen); } if (state->keylen == 8) { /* See RFC 3078 */ state->session_key[0] = 0xd1; state->session_key[1] = 0x26; state->session_key[2] = 0x9e; } arc4_setup(&state->arc4, state->session_key, state->keylen); }
void CTcpEncryption::EncryptPack(const char* sPacket, char* sResult, int nPackLen) { if(m_nSkipLen) memcpy(sResult, sPacket, m_nSkipLen); arc4_crypt( &m_encontext, (unsigned char *)sPacket+m_nSkipLen, nPackLen-m_nSkipLen, (unsigned char *)sResult+m_nSkipLen); }
void arc4_crypt_message(RC4_CTX *ctx, const void *msg, size_t msg_len, void *dst) { for (register size_t i=0; i<msg_len; i++) { register unsigned char b = ((unsigned char*)msg)[i]; arc4_crypt(ctx,&b); ((unsigned char*)dst)[i] = b; } }
void CTcpEncryption::InitEncrypt(int nSkipLen) { unsigned char vInitCrypto[8] = {0}; arc4_setup( &m_encontext, (unsigned char *) vInitCrypto, 8 ); arc4_crypt( &m_encontext, vInitCrypto, 8); arc4_setup( &m_encontext, (unsigned char *) vInitCrypto, 8 ); memcpy(&m_decontext, &m_encontext, sizeof(m_encontext)); m_nSkipLen = nSkipLen; }
void test_main (void) { char stack_obj[4096]; struct arc4 arc4; arc4_init (&arc4, "foobar", 6); memset (stack_obj, 0, sizeof stack_obj); arc4_crypt (&arc4, stack_obj, sizeof stack_obj); msg ("cksum: %lu", cksum (stack_obj, sizeof stack_obj)); }
int main (int argc, char *argv[]) { const char *key = argv[argc - 1]; struct arc4 arc4; size_t i; /* Encrypt zeros. */ arc4_init (&arc4, key, strlen (key)); arc4_crypt (&arc4, buf, SIZE); /* Decrypt back to zeros. */ arc4_init (&arc4, key, strlen (key)); arc4_crypt (&arc4, buf, SIZE); /* Check that it's all zeros. */ for (i = 0; i < SIZE; i++) if (buf[i] != '\0') fail ("byte %x at %p", buf[i], &buf[i]); return 0x42; }
/* Initialize buf1 with random data, then count the number of instances of each value within it. */ static void init (void) { struct arc4 arc4; size_t i; msg ("init"); arc4_init (&arc4, "foobar", 6); arc4_crypt (&arc4, buf1, sizeof buf1); for (i = 0; i < sizeof buf1; i++) histogram[buf1[i]]++; }
QByteArray crypt(const QByteArray b, const QString &key) { QString k; k = key; QByteArray bk = k.toUtf8(); char *out = (char *)malloc(b.length()); arc4_ctx ctx; arc4_init(&ctx, bk.data(), bk.length()); arc4_crypt(&ctx, (char *)b.data(), out, b.length()); QByteArray ret = QByteArray(out, b.length()); free(out); return ret; }
QString saje::decodePassword(const QByteArray &hash, const QString &key) { QString k = key; QByteArray bk = k.toUtf8(); char *out = (char *)malloc(hash.length()); arc4_ctx ctx; arc4_init(&ctx, bk.data(), bk.length()); arc4_crypt(&ctx, (char *)hash.data(), out, hash.length()); QByteArray ret = QByteArray(out, hash.length()); free(out); return ret; }
/* * Checkup routine */ int arc4_self_test( int verbose ) { int i, ret = 0; unsigned char ibuf[8]; unsigned char obuf[8]; arc4_context ctx; arc4_init( &ctx ); for( i = 0; i < 3; i++ ) { if( verbose != 0 ) polarssl_printf( " ARC4 test #%d: ", i + 1 ); memcpy( ibuf, arc4_test_pt[i], 8 ); arc4_setup( &ctx, arc4_test_key[i], 8 ); arc4_crypt( &ctx, 8, ibuf, obuf ); if( memcmp( obuf, arc4_test_ct[i], 8 ) != 0 ) { if( verbose != 0 ) polarssl_printf( "failed\n" ); ret = 1; goto exit; } if( verbose != 0 ) polarssl_printf( "passed\n" ); } if( verbose != 0 ) polarssl_printf( "\n" ); exit: arc4_free( &ctx ); return( ret ); }
static int ecb_arc4_crypt(struct blkcipher_desc *desc, struct scatterlist *dst, struct scatterlist *src, unsigned int nbytes) { struct arc4_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); struct blkcipher_walk walk={}; int err; blkcipher_walk_init(&walk, dst, src, nbytes); err = blkcipher_walk_virt(desc, &walk); while (walk.nbytes > 0) { u8 *wsrc = walk.src.virt.addr; u8 *wdst = walk.dst.virt.addr; arc4_crypt(ctx, wdst, wsrc, walk.nbytes); err = blkcipher_walk_done(desc, &walk, 0); } return err; }
/* * Encryption/decryption functions */ static int ssl_encrypt_buf(ssl_context * ssl) { size_t i, padlen; SSL_DEBUG_MSG(2, ("=> encrypt buf")); /* * Add MAC then encrypt */ if (ssl->minor_ver == SSL_MINOR_VERSION_0) { if (ssl->maclen == 16) ssl_mac_md5(ssl->mac_enc, ssl->out_msg, ssl->out_msglen, ssl->out_ctr, ssl->out_msgtype); if (ssl->maclen == 20) ssl_mac_sha1(ssl->mac_enc, ssl->out_msg, ssl->out_msglen, ssl->out_ctr, ssl->out_msgtype); } else { if (ssl->maclen == 16) md5_hmac(ssl->mac_enc, 16, ssl->out_ctr, ssl->out_msglen + 13, ssl->out_msg + ssl->out_msglen); if (ssl->maclen == 20) sha1_hmac(ssl->mac_enc, 20, ssl->out_ctr, ssl->out_msglen + 13, ssl->out_msg + ssl->out_msglen); } SSL_DEBUG_BUF(4, "computed mac", ssl->out_msg + ssl->out_msglen, ssl->maclen); ssl->out_msglen += ssl->maclen; for (i = 7; i >= 0; i--) if (++ssl->out_ctr[i] != 0) break; if (ssl->ivlen == 0) { #if defined(TROPICSSL_ARC4) padlen = 0; SSL_DEBUG_MSG(3, ("before encrypt: msglen = %d, " "including %d bytes of padding", ssl->out_msglen, 0)); SSL_DEBUG_BUF(4, "before encrypt: output payload", ssl->out_msg, ssl->out_msglen); arc4_crypt((arc4_context *) ssl->ctx_enc, ssl->out_msg, ssl->out_msglen); #else return (TROPICSSL_ERR_SSL_FEATURE_UNAVAILABLE); #endif } else { padlen = ssl->ivlen - (ssl->out_msglen + 1) % ssl->ivlen; if (padlen == ssl->ivlen) padlen = 0; for (i = 0; i <= padlen; i++) ssl->out_msg[ssl->out_msglen + i] = (uint8_t)padlen; ssl->out_msglen += padlen + 1; SSL_DEBUG_MSG(3, ("before encrypt: msglen = %d, " "including %d bytes of padding", ssl->out_msglen, padlen + 1)); SSL_DEBUG_BUF(4, "before encrypt: output payload", ssl->out_msg, ssl->out_msglen); switch (ssl->ivlen) { case 8: #if defined(TROPICSSL_DES) des3_crypt_cbc((des3_context *) ssl->ctx_enc, DES_ENCRYPT, ssl->out_msglen, ssl->iv_enc, ssl->out_msg, ssl->out_msg); break; #endif case 16: #if defined(TROPICSSL_AES) if (ssl->session->cipher == TLS_RSA_WITH_AES_128_CBC_SHA || ssl->session->cipher == TLS_RSA_WITH_AES_256_CBC_SHA || ssl->session->cipher == TLS_DHE_RSA_WITH_AES_256_CBC_SHA) { aes_crypt_cbc((aes_context *) ssl->ctx_enc, AES_ENCRYPT, ssl->out_msglen, ssl->iv_enc, ssl->out_msg, ssl->out_msg); break; } #endif #if defined(TROPICSSL_CAMELLIA) if (ssl->session->cipher == TLS_RSA_WITH_CAMELLIA_128_CBC_SHA || ssl->session->cipher == TLS_RSA_WITH_CAMELLIA_256_CBC_SHA || ssl->session->cipher == TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA) { camellia_crypt_cbc((camellia_context *) ssl->ctx_enc, CAMELLIA_ENCRYPT, ssl->out_msglen, ssl->iv_enc, ssl->out_msg, ssl->out_msg); break; } #endif default: return (TROPICSSL_ERR_SSL_FEATURE_UNAVAILABLE); } } SSL_DEBUG_MSG(2, ("<= encrypt buf")); return (0); }
void CTcpEncryption::DecryptPack(char* sPacket, int nPackLen) { arc4_crypt( &m_decontext, (unsigned char *)sPacket+m_nSkipLen, nPackLen-m_nSkipLen); }
void CNetSock::OnThreadSocket() { fd_set readfds; fd_set writefds; timeval TimeOut={0,100000}; int nCount; while(!m_Thread.IsStopped()) { if(!m_bAttached) { memset(&m_Addr, 0, sizeof(m_Addr)); m_Addr.sin_family = AF_INET; m_Addr.sin_port = htons(m_ServerPort); USES_CONVERSION; LPSTR server_name = T2A(m_ServerName); ULONG a = inet_addr(server_name); if(a == INADDR_NONE) { LPHOSTENT lphost; lphost = gethostbyname(server_name); if(lphost == NULL) { m_Thread.WaitEvent(500); continue; } m_Addr.sin_addr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr; } else m_Addr.sin_addr.s_addr = a; if(m_Thread.IsStopped()) break; int res = connect(m_hSocket, (sockaddr*)&m_Addr, sizeof(m_Addr)); if(res != 0) { m_Thread.WaitEvent(500); continue; } } OnSockConnect(); while(!m_Thread.IsStopped()) { FD_ZERO(&readfds); FD_ZERO(&writefds); FD_SET(m_hSocket, &readfds); if(m_bSelectWrite) FD_SET(m_hSocket, &writefds); nCount = select(FD_SETSIZE, &readfds, &writefds, NULL, &TimeOut); if(nCount == SOCKET_ERROR) // on error - stop thread { m_Thread.SetStop(); break; } if(nCount == 0) continue; // timeout if(FD_ISSET(m_hSocket, &readfds)) { nCount = recv(m_hSocket, m_pTempBuf, RECV_BUF_SIZE, 0); if(nCount <= 0) { // socket is closed closesocket(m_hSocket); m_hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(m_hSocket == INVALID_SOCKET) m_Thread.SetStop(); break; } else { if (m_bEncryption) { arc4_crypt(&m_EncryptRecvCtx, (unsigned char*)m_pTempBuf, nCount); } OnSockRecv(m_pTempBuf, nCount); } } if(FD_ISSET(m_hSocket, &writefds)) { m_bSelectWrite = FALSE; OnSockSend(); } } OnSockClose(); if(m_bAttached) break; } if(m_hSocket != INVALID_SOCKET) { closesocket(m_hSocket); m_hSocket = INVALID_SOCKET; } }
/* * Decompress (decrypt) an MPPE packet. */ err_t mppe_decompress(ppp_pcb *pcb, ppp_mppe_state *state, struct pbuf **pb) { struct pbuf *n0 = *pb, *n; u8_t *pl; u16_t ccount; u8_t flushed; /* MPPE Header */ if (n0->len < MPPE_OVHD) { PPPDEBUG(LOG_DEBUG, ("mppe_decompress[%d]: short pkt (%d)\n", pcb->netif->num, n0->len)); state->sanity_errors += 100; goto sanity_error; } pl = (u8_t*)n0->payload; flushed = MPPE_BITS(pl) & MPPE_BIT_FLUSHED; ccount = MPPE_CCOUNT(pl); PPPDEBUG(LOG_DEBUG, ("mppe_decompress[%d]: ccount %d\n", pcb->netif->num, ccount)); /* sanity checks -- terminate with extreme prejudice */ if (!(MPPE_BITS(pl) & MPPE_BIT_ENCRYPTED)) { PPPDEBUG(LOG_DEBUG, ("mppe_decompress[%d]: ENCRYPTED bit not set!\n", pcb->netif->num)); state->sanity_errors += 100; goto sanity_error; } if (!state->stateful && !flushed) { PPPDEBUG(LOG_DEBUG, ("mppe_decompress[%d]: FLUSHED bit not set in " "stateless mode!\n", pcb->netif->num)); state->sanity_errors += 100; goto sanity_error; } if (state->stateful && ((ccount & 0xff) == 0xff) && !flushed) { PPPDEBUG(LOG_DEBUG, ("mppe_decompress[%d]: FLUSHED bit not set on " "flag packet!\n", pcb->netif->num)); state->sanity_errors += 100; goto sanity_error; } /* * Check the coherency count. */ if (!state->stateful) { /* Discard late packet */ if ((ccount - state->ccount) % MPPE_CCOUNT_SPACE > MPPE_CCOUNT_SPACE / 2) { state->sanity_errors++; goto sanity_error; } /* RFC 3078, sec 8.1. Rekey for every packet. */ while (state->ccount != ccount) { mppe_rekey(state, 0); state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE; } } else { /* RFC 3078, sec 8.2. */ if (!state->discard) { /* normal state */ state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE; if (ccount != state->ccount) { /* * (ccount > state->ccount) * Packet loss detected, enter the discard state. * Signal the peer to rekey (by sending a CCP Reset-Request). */ state->discard = 1; ccp_resetrequest(pcb); return ERR_BUF; } } else { /* discard state */ if (!flushed) { /* ccp.c will be silent (no additional CCP Reset-Requests). */ return ERR_BUF; } else { /* Rekey for every missed "flag" packet. */ while ((ccount & ~0xff) != (state->ccount & ~0xff)) { mppe_rekey(state, 0); state->ccount = (state->ccount + 256) % MPPE_CCOUNT_SPACE; } /* reset */ state->discard = 0; state->ccount = ccount; /* * Another problem with RFC 3078 here. It implies that the * peer need not send a Reset-Ack packet. But RFC 1962 * requires it. Hopefully, M$ does send a Reset-Ack; even * though it isn't required for MPPE synchronization, it is * required to reset CCP state. */ } } if (flushed) mppe_rekey(state, 0); } /* Hide MPPE header */ pbuf_header(n0, -(s16_t)(MPPE_OVHD)); /* Decrypt the packet. */ for (n = n0; n != NULL; n = n->next) { arc4_crypt(&state->arc4, (u8_t*)n->payload, n->len); if (n->tot_len == n->len) { break; } } /* good packet credit */ state->sanity_errors >>= 1; return ERR_OK; sanity_error: if (state->sanity_errors >= SANITY_MAX) { /* * Take LCP down if the peer is sending too many bogons. * We don't want to do this for a single or just a few * instances since it could just be due to packet corruption. */ lcp_close(pcb, "Too many MPPE errors"); } return ERR_BUF; }
/* * Compress (encrypt) a packet. * It's strange to call this a compressor, since the output is always * MPPE_OVHD + 2 bytes larger than the input. */ err_t mppe_compress(ppp_pcb *pcb, ppp_mppe_state *state, struct pbuf **pb, u16_t protocol) { struct pbuf *n, *np; u8_t *pl; err_t err; LWIP_UNUSED_ARG(pcb); /* TCP stack requires that we don't change the packet payload, therefore we copy * the whole packet before encryption. */ np = pbuf_alloc(PBUF_RAW, MPPE_OVHD + sizeof(protocol) + (*pb)->tot_len, PBUF_POOL); if (!np) { return ERR_MEM; } /* Hide MPPE header + protocol */ pbuf_header(np, -(s16_t)(MPPE_OVHD + sizeof(protocol))); if ((err = pbuf_copy(np, *pb)) != ERR_OK) { pbuf_free(np); return err; } /* Reveal MPPE header + protocol */ pbuf_header(np, (s16_t)(MPPE_OVHD + sizeof(protocol))); *pb = np; pl = (u8_t*)np->payload; state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE; PPPDEBUG(LOG_DEBUG, ("mppe_compress[%d]: ccount %d\n", pcb->netif->num, state->ccount)); /* FIXME: use PUT* macros */ pl[0] = state->ccount>>8; pl[1] = state->ccount; if (!state->stateful || /* stateless mode */ ((state->ccount & 0xff) == 0xff) || /* "flag" packet */ (state->bits & MPPE_BIT_FLUSHED)) { /* CCP Reset-Request */ /* We must rekey */ if (state->stateful) { PPPDEBUG(LOG_DEBUG, ("mppe_compress[%d]: rekeying\n", pcb->netif->num)); } mppe_rekey(state, 0); state->bits |= MPPE_BIT_FLUSHED; } pl[0] |= state->bits; state->bits &= ~MPPE_BIT_FLUSHED; /* reset for next xmit */ pl += MPPE_OVHD; /* Add protocol */ /* FIXME: add PFC support */ pl[0] = protocol >> 8; pl[1] = protocol; /* Hide MPPE header */ pbuf_header(np, -(s16_t)MPPE_OVHD); /* Encrypt packet */ for (n = np; n != NULL; n = n->next) { arc4_crypt(&state->arc4, (u8_t*)n->payload, n->len); if (n->tot_len == n->len) { break; } } /* Reveal MPPE header */ pbuf_header(np, (s16_t)MPPE_OVHD); return ERR_OK; }
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 ); }
static void arc4_crypt_one(struct crypto_tfm *tfm, u8 *out, const u8 *in) { arc4_crypt(crypto_tfm_ctx(tfm), out, in, 1); }
int main( void ) { int keysize; unsigned long i, j, tsc; unsigned char buf[BUFSIZE]; unsigned char tmp[32]; arc4_context arc4; des3_context des3; des_context des; aes_context aes; rsa_context rsa; memset( buf, 0xAA, sizeof( buf ) ); printf( "\n" ); /* * MD2 timing */ printf( " MD2 : " ); fflush( stdout ); set_alarm( 1 ); for( i = 1; ! alarmed; i++ ) md2_csum( buf, BUFSIZE, tmp ); tsc = hardclock(); for( j = 0; j < 32; j++ ) md2_csum( buf, BUFSIZE, tmp ); printf( "%9ld Kb/s, %9ld cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); /* * MD4 timing */ printf( " MD4 : " ); fflush( stdout ); set_alarm( 1 ); for( i = 1; ! alarmed; i++ ) md4_csum( buf, BUFSIZE, tmp ); tsc = hardclock(); for( j = 0; j < 1024; j++ ) md4_csum( buf, BUFSIZE, tmp ); printf( "%9ld Kb/s, %9ld cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); /* * MD5 timing */ printf( " MD5 : " ); fflush( stdout ); set_alarm( 1 ); for( i = 1; ! alarmed; i++ ) md5_csum( buf, BUFSIZE, tmp ); tsc = hardclock(); for( j = 0; j < 1024; j++ ) md5_csum( buf, BUFSIZE, tmp ); printf( "%9ld Kb/s, %9ld cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); /* * SHA-1 timing */ printf( " SHA-1 : " ); fflush( stdout ); set_alarm( 1 ); for( i = 1; ! alarmed; i++ ) sha1_csum( buf, BUFSIZE, tmp ); tsc = hardclock(); for( j = 0; j < 1024; j++ ) sha1_csum( buf, BUFSIZE, tmp ); printf( "%9ld Kb/s, %9ld cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); /* * SHA-256 timing */ printf( " SHA-256 : " ); fflush( stdout ); set_alarm( 1 ); for( i = 1; ! alarmed; i++ ) sha2_csum( buf, BUFSIZE, tmp ); tsc = hardclock(); for( j = 0; j < 1024; j++ ) sha2_csum( buf, BUFSIZE, tmp ); printf( "%9ld Kb/s, %9ld cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); /* * ARC4 timing */ 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( "%9ld Kb/s, %9ld cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); /* * Triple-DES timing */ printf( " 3DES : " ); fflush( stdout ); des3_set_3keys( &des3, tmp ); set_alarm( 1 ); for( i = 1; ! alarmed; i++ ) des3_cbc_encrypt( &des3, tmp, buf, buf, BUFSIZE ); tsc = hardclock(); for( j = 0; j < 1024; j++ ) des3_cbc_encrypt( &des3, tmp, buf, buf, BUFSIZE ); printf( "%9ld Kb/s, %9ld cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); /* * DES timing */ printf( " DES : " ); fflush( stdout ); des_set_key( &des, tmp ); set_alarm( 1 ); for( i = 1; ! alarmed; i++ ) des_cbc_encrypt( &des, tmp, buf, buf, BUFSIZE ); tsc = hardclock(); for( j = 0; j < 1024; j++ ) des_cbc_encrypt( &des, tmp, buf, buf, BUFSIZE ); printf( "%9ld Kb/s, %9ld cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); /* * AES timings */ for( keysize = 128; keysize <= 256; keysize += 64 ) { printf( " AES-%d : ", keysize ); fflush( stdout ); aes_set_key( &aes, tmp, keysize ); set_alarm( 1 ); for( i = 1; ! alarmed; i++ ) aes_cbc_encrypt( &aes, tmp, buf, buf, BUFSIZE ); tsc = hardclock(); for( j = 0; j < 1024; j++ ) aes_cbc_encrypt( &aes, tmp, buf, buf, BUFSIZE ); printf( "%9ld Kb/s, %9ld cycles/byte\n", i * BUFSIZE / 1024, ( hardclock() - tsc ) / ( j * BUFSIZE ) ); } /* * RSA-1024 timing */ printf( " RSA-1024 : " ); fflush( stdout ); rsa_gen_key( &rsa, 1024, 65537, myrand, NULL ); set_alarm( 4 ); for( i = 1; ! alarmed; i++ ) { buf[0] = 0; rsa_public( &rsa, buf, 128, buf, 128 ); } printf( "%9ld public/s\n", i / 4 ); printf( " RSA-1024 : " ); fflush( stdout ); set_alarm( 4 ); for( i = 1; ! alarmed; i++ ) { buf[0] = 0; rsa_private( &rsa, buf, 128, buf, 128 ); } printf( "%9ld private/s\n", i / 4 ); rsa_free( &rsa ); /* * RSA-2048 timing */ printf( " RSA-2048 : " ); fflush( stdout ); rsa_gen_key( &rsa, 2048, 65537, myrand, NULL ); set_alarm( 4 ); for( i = 1; ! alarmed; i++ ) { buf[0] = 0; rsa_public( &rsa, buf, 256, buf, 256 ); } printf( "%9ld public/s\n", i / 4 ); printf( " RSA-2048 : " ); fflush( stdout ); set_alarm( 4 ); for( i = 1; ! alarmed; i++ ) { buf[0] = 0; rsa_private( &rsa, buf, 256, buf, 256 ); } printf( "%9ld private/s\n\n", i / 4 ); rsa_free( &rsa ); #ifdef WIN32 printf( " Press Enter to exit this program.\n" ); fflush( stdout ); getchar(); #endif return( 0 ); }
static int arc4_crypt_stream_wrap( void *ctx, size_t length, const unsigned char *input, unsigned char *output ) { return( arc4_crypt( (arc4_context *) ctx, length, input, output ) ); }
void crypto_rc4(CryptoRc4 rc4, uint32 len, uint8 * in_data, uint8 * out_data) { arc4_crypt(&rc4->ctx, len, in_data, out_data); }
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); }
static int ssl_decrypt_buf(ssl_context * ssl) { size_t i, padlen; uint8_t tmp[20]; SSL_DEBUG_MSG(2, ("=> decrypt buf")); if (ssl->in_msglen < ssl->minlen) { SSL_DEBUG_MSG(1, ("in_msglen (%d) < minlen (%d)", ssl->in_msglen, ssl->minlen)); return (TROPICSSL_ERR_SSL_INVALID_MAC); } if (ssl->ivlen == 0) { #if defined(TROPICSSL_ARC4) padlen = 0; arc4_crypt((arc4_context *) ssl->ctx_dec, ssl->in_msg, ssl->in_msglen); #else return (TROPICSSL_ERR_SSL_FEATURE_UNAVAILABLE); #endif } else { /* * Decrypt and check the padding */ if (ssl->in_msglen % ssl->ivlen != 0) { SSL_DEBUG_MSG(1, ("msglen (%d) %% ivlen (%d) != 0", ssl->in_msglen, ssl->ivlen)); return (TROPICSSL_ERR_SSL_INVALID_MAC); } switch (ssl->ivlen) { #if defined(TROPICSSL_DES) case 8: des3_crypt_cbc((des3_context *) ssl->ctx_dec, DES_DECRYPT, ssl->in_msglen, ssl->iv_dec, ssl->in_msg, ssl->in_msg); break; #endif case 16: #if defined(TROPICSSL_AES) if (ssl->session->cipher == TLS_RSA_WITH_AES_128_CBC_SHA || ssl->session->cipher == TLS_RSA_WITH_AES_256_CBC_SHA || ssl->session->cipher == TLS_DHE_RSA_WITH_AES_256_CBC_SHA) { aes_crypt_cbc((aes_context *) ssl->ctx_dec, AES_DECRYPT, ssl->in_msglen, ssl->iv_dec, ssl->in_msg, ssl->in_msg); break; } #endif #if defined(TROPICSSL_CAMELLIA) if (ssl->session->cipher == TLS_RSA_WITH_CAMELLIA_128_CBC_SHA || ssl->session->cipher == TLS_RSA_WITH_CAMELLIA_256_CBC_SHA || ssl->session->cipher == TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA) { camellia_crypt_cbc((camellia_context *) ssl->ctx_dec, CAMELLIA_DECRYPT, ssl->in_msglen, ssl->iv_dec, ssl->in_msg, ssl->in_msg); break; } #endif default: return (TROPICSSL_ERR_SSL_FEATURE_UNAVAILABLE); } padlen = 1 + ssl->in_msg[ssl->in_msglen - 1]; if (ssl->minor_ver == SSL_MINOR_VERSION_0) { if (padlen > ssl->ivlen) { SSL_DEBUG_MSG(1, ("bad padding length: is %d, " "should be no more than %d", padlen, ssl->ivlen)); padlen = 0; } } else { /* * TLSv1: always check the padding */ for (i = 1; i <= padlen; i++) { if (ssl->in_msg[ssl->in_msglen - i] != padlen - 1) { SSL_DEBUG_MSG(1, ("bad padding byte: should be " "%02x, but is %02x", padlen - 1, ssl-> in_msg[ssl->in_msglen - i])); padlen = 0; } } } } SSL_DEBUG_BUF(4, "raw buffer after decryption", ssl->in_msg, ssl->in_msglen); /* * Always compute the MAC (RFC4346, CBCTIME). */ ssl->in_msglen -= (ssl->maclen + padlen); ssl->in_hdr[3] = (uint8_t)(ssl->in_msglen >> 8); ssl->in_hdr[4] = (uint8_t)(ssl->in_msglen); memcpy(tmp, ssl->in_msg + ssl->in_msglen, 20); if (ssl->minor_ver == SSL_MINOR_VERSION_0) { if (ssl->maclen == 16) ssl_mac_md5(ssl->mac_dec, ssl->in_msg, ssl->in_msglen, ssl->in_ctr, ssl->in_msgtype); else ssl_mac_sha1(ssl->mac_dec, ssl->in_msg, ssl->in_msglen, ssl->in_ctr, ssl->in_msgtype); } else { if (ssl->maclen == 16) md5_hmac(ssl->mac_dec, 16, ssl->in_ctr, ssl->in_msglen + 13, ssl->in_msg + ssl->in_msglen); else sha1_hmac(ssl->mac_dec, 20, ssl->in_ctr, ssl->in_msglen + 13, ssl->in_msg + ssl->in_msglen); } SSL_DEBUG_BUF(4, "message mac", tmp, ssl->maclen); SSL_DEBUG_BUF(4, "computed mac", ssl->in_msg + ssl->in_msglen, ssl->maclen); if (memcmp(tmp, ssl->in_msg + ssl->in_msglen, ssl->maclen) != 0) { SSL_DEBUG_MSG(1, ("message mac does not match")); return (TROPICSSL_ERR_SSL_INVALID_MAC); } /* * Finally check the padding length; bad padding * will produce the same error as an invalid MAC. */ if (ssl->ivlen != 0 && padlen == 0) return (TROPICSSL_ERR_SSL_INVALID_MAC); if (ssl->in_msglen == 0) { ssl->nb_zero++; /* * Three or more empty messages may be a DoS attack * (excessive CPU consumption). */ if (ssl->nb_zero > 3) { SSL_DEBUG_MSG(1, ("received four consecutive empty " "messages, possible DoS attack")); return (TROPICSSL_ERR_SSL_INVALID_MAC); } } else ssl->nb_zero = 0; for (i = 7; i >= 0; i--) if (++ssl->in_ctr[i] != 0) break; SSL_DEBUG_MSG(2, ("<= decrypt buf")); return (0); }
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, 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) ); }