X-Git-Url: http://git.code-monkey.de/?a=blobdiff_plain;f=src%2Fcrypt.c;h=ad93dd8bf9c5393f81e748f5c4ad526e63da3f88;hb=5ee3db88fdd2cbe29b490a53e7e55f2a56e9cf65;hp=7d370d0ee11c8f74d24a982ff9fdf9099f1eeba3;hpb=5191e1cb38d24ebf5c180ac7911893ca8bc4031f;p=umurmur.git diff --git a/src/crypt.c b/src/crypt.c index 7d370d0..ad93dd8 100644 --- a/src/crypt.c +++ b/src/crypt.c @@ -1,5 +1,5 @@ -/* Copyright (C) 2010, Martin Johansson - Copyright (C) 2005-2010, Thorvald Natvig +/* Copyright (C) 2009-2014, Martin Johansson + Copyright (C) 2005-2014, Thorvald Natvig All rights reserved. @@ -41,7 +41,12 @@ #include #include #include "crypt.h" -#include "log.h" +#include "ssl.h" + +#ifdef USE_POLARSSL_HAVEGE +extern havege_state hs; +#endif + static void CryptState_ocb_encrypt(cryptState_t *cs, const unsigned char *plain, unsigned char *encrypted, unsigned int len, const unsigned char *nonce, unsigned char *tag); static void CryptState_ocb_decrypt(cryptState_t *cs, const unsigned char *encrypted, unsigned char *plain, unsigned int len, const unsigned char *nonce, unsigned char *tag); @@ -67,8 +72,13 @@ void CryptState_genKey(cryptState_t *cs) { RAND_bytes(cs->raw_key, AES_BLOCK_SIZE); RAND_bytes(cs->encrypt_iv, AES_BLOCK_SIZE); RAND_bytes(cs->decrypt_iv, AES_BLOCK_SIZE); +#ifndef USE_POLARSSL AES_set_encrypt_key(cs->raw_key, 128, &cs->encrypt_key); AES_set_decrypt_key(cs->raw_key, 128, &cs->decrypt_key); +#else + aes_setkey_enc(&cs->aes_enc, cs->raw_key, 128); + aes_setkey_dec(&cs->aes_dec, cs->raw_key, 128); +#endif cs->bInit = true; } @@ -77,8 +87,13 @@ void CryptState_setKey(cryptState_t *cs, const unsigned char *rkey, const unsign memcpy(cs->raw_key, rkey, AES_BLOCK_SIZE); memcpy(cs->encrypt_iv, eiv, AES_BLOCK_SIZE); memcpy(cs->decrypt_iv, div, AES_BLOCK_SIZE); - AES_set_encrypt_key(cs->raw_key, 128, &cs->encrypt_key); +#ifndef USE_POLARSSL + AES_set_encrypt_key(cs->decrypt_iv, 128, &cs->encrypt_key); AES_set_decrypt_key(cs->raw_key, 128, &cs->decrypt_key); +#else + aes_setkey_enc(&cs->aes_enc, cs->decrypt_iv, 128); + aes_setkey_dec(&cs->aes_dec, cs->raw_key, 128); +#endif cs->bInit = true; } @@ -200,34 +215,6 @@ bool_t CryptState_decrypt(cryptState_t *cs, const unsigned char *source, unsigne return true; } -#if defined(__LP64__) -#define BLOCKSIZE 2 -#define SHIFTBITS 63 -typedef uint64_t subblock; - -#if __BYTE_ORDER == __BIG_ENDIAN -#define SWAPPED(x) (x) -#else -#ifdef __x86_64__ -#define SWAPPED(x) ({register uint64_t __out, __in = (x); __asm__("bswap %q0" : "=r"(__out) : "0"(__in)); __out;}) -#else -#include -#define SWAPPED(x) bswap_64(x) -#endif -#endif - -#else - -#define BLOCKSIZE 4 -#define SHIFTBITS 31 -typedef uint32_t subblock; -#define SWAPPED(x) htonl(x) - -#endif - -#define HIGHBIT (1<aes_enc, AES_ENCRYPT, (unsigned char *)(src), (unsigned char *)(dst)); +#define AESdecrypt(src, dst, cryptstate) aes_crypt_ecb(&(cryptstate)->aes_dec, AES_DECRYPT, (unsigned char *)(src), (unsigned char *)(dst)); +#else +#define AESencrypt(src, dst, cryptstate) AES_encrypt((unsigned char *)(src), (unsigned char *)(dst), &(cryptstate)->encrypt_key); +#define AESdecrypt(src, dst, cryptstate) AES_decrypt((unsigned char *)(src), (unsigned char *)(dst), &(cryptstate)->decrypt_key); +#endif void CryptState_ocb_encrypt(cryptState_t *cs, const unsigned char *plain, unsigned char *encrypted, unsigned int len, const unsigned char *nonce, unsigned char *tag) { subblock checksum[BLOCKSIZE], delta[BLOCKSIZE], tmp[BLOCKSIZE], pad[BLOCKSIZE]; // Initialize - AESencrypt(nonce, delta, &cs->encrypt_key); + AESencrypt(nonce, delta, cs); ZERO(checksum); while (len > AES_BLOCK_SIZE) { S2(delta); XOR(tmp, delta, (const subblock *)(plain)); - AESencrypt(tmp, tmp, &cs->encrypt_key); + AESencrypt(tmp, tmp, cs); XOR((subblock *)(encrypted), delta, tmp); XOR(checksum, checksum, (subblock *)(plain)); len -= AES_BLOCK_SIZE; @@ -282,7 +274,7 @@ void CryptState_ocb_encrypt(cryptState_t *cs, const unsigned char *plain, unsign ZERO(tmp); tmp[BLOCKSIZE - 1] = SWAPPED(len * 8); XOR(tmp, tmp, delta); - AESencrypt(tmp, pad, &cs->encrypt_key); + AESencrypt(tmp, pad, cs); memcpy(tmp, plain, len); memcpy((unsigned char *)tmp + len, (unsigned char *)pad + len, AES_BLOCK_SIZE - len); XOR(checksum, checksum, tmp); @@ -291,20 +283,19 @@ void CryptState_ocb_encrypt(cryptState_t *cs, const unsigned char *plain, unsign S3(delta); XOR(tmp, delta, checksum); - AESencrypt(tmp, tag, &cs->encrypt_key); + AESencrypt(tmp, tag, cs); } void CryptState_ocb_decrypt(cryptState_t *cs, const unsigned char *encrypted, unsigned char *plain, unsigned int len, const unsigned char *nonce, unsigned char *tag) { subblock checksum[BLOCKSIZE], delta[BLOCKSIZE], tmp[BLOCKSIZE], pad[BLOCKSIZE]; - // Initialize - AESencrypt(nonce, delta, &cs->encrypt_key); + AESencrypt(nonce, delta, cs); ZERO(checksum); while (len > AES_BLOCK_SIZE) { S2(delta); XOR(tmp, delta, (const subblock *)(encrypted)); - AESdecrypt(tmp, tmp, &cs->decrypt_key); + AESdecrypt(tmp, tmp, cs); XOR((subblock *)(plain), delta, tmp); XOR(checksum, checksum, (const subblock *)(plain)); len -= AES_BLOCK_SIZE; @@ -316,7 +307,7 @@ void CryptState_ocb_decrypt(cryptState_t *cs, const unsigned char *encrypted, un ZERO(tmp); tmp[BLOCKSIZE - 1] = SWAPPED(len * 8); XOR(tmp, tmp, delta); - AESencrypt(tmp, pad, &cs->encrypt_key); + AESencrypt(tmp, pad, cs); memset(tmp, 0, AES_BLOCK_SIZE); memcpy(tmp, encrypted, len); XOR(tmp, tmp, pad); @@ -325,5 +316,5 @@ void CryptState_ocb_decrypt(cryptState_t *cs, const unsigned char *encrypted, un S3(delta); XOR(tmp, delta, checksum); - AESencrypt(tmp, tag, &cs->encrypt_key); + AESencrypt(tmp, tag, cs); }