From ba3920edd58b5f94a41d2e22c5acea7b48987e9f Mon Sep 17 00:00:00 2001 From: jsing Date: Thu, 8 May 2014 15:13:06 +0000 Subject: [PATCH] KNF. --- lib/libcrypto/evp/e_aes.c | 1020 ++++++++--------- lib/libcrypto/evp/e_aes_cbc_hmac_sha1.c | 521 +++++---- lib/libcrypto/evp/e_bf.c | 37 +- lib/libcrypto/evp/e_camellia.c | 74 +- lib/libcrypto/evp/e_cast.c | 41 +- lib/libcrypto/evp/e_chacha.c | 2 +- lib/libcrypto/evp/e_des.c | 221 ++-- lib/libcrypto/evp/e_des3.c | 300 ++--- lib/libcrypto/evp/e_idea.c | 63 +- lib/libcrypto/evp/e_null.c | 53 +- lib/libcrypto/evp/e_old.c | 57 +- lib/libcrypto/evp/e_rc2.c | 193 ++-- lib/libcrypto/evp/e_rc4.c | 79 +- lib/libcrypto/evp/e_rc4_hmac_md5.c | 226 ++-- lib/libcrypto/evp/e_rc5.c | 60 +- lib/libcrypto/evp/e_xcbc_d.c | 96 +- lib/libssl/src/crypto/evp/e_aes.c | 1020 ++++++++--------- .../src/crypto/evp/e_aes_cbc_hmac_sha1.c | 521 +++++---- lib/libssl/src/crypto/evp/e_bf.c | 37 +- lib/libssl/src/crypto/evp/e_camellia.c | 74 +- lib/libssl/src/crypto/evp/e_cast.c | 41 +- lib/libssl/src/crypto/evp/e_chacha.c | 2 +- lib/libssl/src/crypto/evp/e_des.c | 221 ++-- lib/libssl/src/crypto/evp/e_des3.c | 300 ++--- lib/libssl/src/crypto/evp/e_idea.c | 63 +- lib/libssl/src/crypto/evp/e_null.c | 53 +- lib/libssl/src/crypto/evp/e_old.c | 57 +- lib/libssl/src/crypto/evp/e_rc2.c | 193 ++-- lib/libssl/src/crypto/evp/e_rc4.c | 79 +- lib/libssl/src/crypto/evp/e_rc4_hmac_md5.c | 226 ++-- lib/libssl/src/crypto/evp/e_rc5.c | 60 +- lib/libssl/src/crypto/evp/e_xcbc_d.c | 96 +- 32 files changed, 3088 insertions(+), 2998 deletions(-) diff --git a/lib/libcrypto/evp/e_aes.c b/lib/libcrypto/evp/e_aes.c index d6f0124a945..db0fdf85c86 100644 --- a/lib/libcrypto/evp/e_aes.c +++ b/lib/libcrypto/evp/e_aes.c @@ -6,7 +6,7 @@ * are met: * * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. + * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in @@ -60,18 +60,16 @@ #include "modes_lcl.h" #include -typedef struct - { +typedef struct { AES_KEY ks; block128_f block; union { cbc128_f cbc; ctr128_f ctr; } stream; - } EVP_AES_KEY; +} EVP_AES_KEY; -typedef struct - { +typedef struct { AES_KEY ks; /* AES key schedule to use */ int key_set; /* Set if key initialised */ int iv_set; /* Set if an iv is set */ @@ -82,20 +80,17 @@ typedef struct int iv_gen; /* It is OK to generate IVs */ int tls_aad_len; /* TLS AAD length */ ctr128_f ctr; - } EVP_AES_GCM_CTX; +} EVP_AES_GCM_CTX; -typedef struct - { +typedef struct { AES_KEY ks1, ks2; /* AES key schedules to use */ XTS128_CONTEXT xts; - void (*stream)(const unsigned char *in, - unsigned char *out, size_t length, - const AES_KEY *key1, const AES_KEY *key2, - const unsigned char iv[16]); - } EVP_AES_XTS_CTX; - -typedef struct - { + void (*stream)(const unsigned char *in, unsigned char *out, + size_t length, const AES_KEY *key1, const AES_KEY *key2, + const unsigned char iv[16]); +} EVP_AES_XTS_CTX; + +typedef struct { AES_KEY ks; /* AES key schedule to use */ int key_set; /* Set if key initialised */ int iv_set; /* Set if an iv is set */ @@ -104,53 +99,46 @@ typedef struct int L, M; /* L and M parameters from RFC3610 */ CCM128_CONTEXT ccm; ccm128_f str; - } EVP_AES_CCM_CTX; +} EVP_AES_CCM_CTX; #define MAXBITCHUNK ((size_t)1<<(sizeof(size_t)*8-4)) #ifdef VPAES_ASM int vpaes_set_encrypt_key(const unsigned char *userKey, int bits, - AES_KEY *key); + AES_KEY *key); int vpaes_set_decrypt_key(const unsigned char *userKey, int bits, - AES_KEY *key); + AES_KEY *key); void vpaes_encrypt(const unsigned char *in, unsigned char *out, - const AES_KEY *key); + const AES_KEY *key); void vpaes_decrypt(const unsigned char *in, unsigned char *out, - const AES_KEY *key); + const AES_KEY *key); -void vpaes_cbc_encrypt(const unsigned char *in, - unsigned char *out, - size_t length, - const AES_KEY *key, - unsigned char *ivec, int enc); +void vpaes_cbc_encrypt(const unsigned char *in, unsigned char *out, + size_t length, const AES_KEY *key, unsigned char *ivec, int enc); #endif #ifdef BSAES_ASM void bsaes_cbc_encrypt(const unsigned char *in, unsigned char *out, - size_t length, const AES_KEY *key, - unsigned char ivec[16], int enc); + size_t length, const AES_KEY *key, unsigned char ivec[16], int enc); void bsaes_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out, - size_t len, const AES_KEY *key, - const unsigned char ivec[16]); + size_t len, const AES_KEY *key, const unsigned char ivec[16]); void bsaes_xts_encrypt(const unsigned char *inp, unsigned char *out, - size_t len, const AES_KEY *key1, - const AES_KEY *key2, const unsigned char iv[16]); + size_t len, const AES_KEY *key1, const AES_KEY *key2, + const unsigned char iv[16]); void bsaes_xts_decrypt(const unsigned char *inp, unsigned char *out, - size_t len, const AES_KEY *key1, - const AES_KEY *key2, const unsigned char iv[16]); + size_t len, const AES_KEY *key1, const AES_KEY *key2, + const unsigned char iv[16]); #endif #ifdef AES_CTR_ASM void AES_ctr32_encrypt(const unsigned char *in, unsigned char *out, - size_t blocks, const AES_KEY *key, - const unsigned char ivec[AES_BLOCK_SIZE]); + size_t blocks, const AES_KEY *key, + const unsigned char ivec[AES_BLOCK_SIZE]); #endif #ifdef AES_XTS_ASM -void AES_xts_encrypt(const char *inp,char *out,size_t len, - const AES_KEY *key1, const AES_KEY *key2, - const unsigned char iv[16]); -void AES_xts_decrypt(const char *inp,char *out,size_t len, - const AES_KEY *key1, const AES_KEY *key2, - const unsigned char iv[16]); +void AES_xts_encrypt(const char *inp, char *out, size_t len, + const AES_KEY *key1, const AES_KEY *key2, const unsigned char iv[16]); +void AES_xts_decrypt(const char *inp, char *out, size_t len, + const AES_KEY *key1, const AES_KEY *key2, const unsigned char iv[16]); #endif #if defined(AES_ASM) && !defined(I386_ONLY) && ( \ @@ -174,160 +162,142 @@ extern unsigned int OPENSSL_ia32cap_P[2]; #define AESNI_CAPABLE (OPENSSL_ia32cap_P[1]&(1<<(57-32))) int aesni_set_encrypt_key(const unsigned char *userKey, int bits, - AES_KEY *key); + AES_KEY *key); int aesni_set_decrypt_key(const unsigned char *userKey, int bits, - AES_KEY *key); + AES_KEY *key); void aesni_encrypt(const unsigned char *in, unsigned char *out, - const AES_KEY *key); + const AES_KEY *key); void aesni_decrypt(const unsigned char *in, unsigned char *out, - const AES_KEY *key); - -void aesni_ecb_encrypt(const unsigned char *in, - unsigned char *out, - size_t length, - const AES_KEY *key, - int enc); -void aesni_cbc_encrypt(const unsigned char *in, - unsigned char *out, - size_t length, - const AES_KEY *key, - unsigned char *ivec, int enc); - -void aesni_ctr32_encrypt_blocks(const unsigned char *in, - unsigned char *out, - size_t blocks, - const void *key, - const unsigned char *ivec); - -void aesni_xts_encrypt(const unsigned char *in, - unsigned char *out, - size_t length, - const AES_KEY *key1, const AES_KEY *key2, - const unsigned char iv[16]); - -void aesni_xts_decrypt(const unsigned char *in, - unsigned char *out, - size_t length, - const AES_KEY *key1, const AES_KEY *key2, - const unsigned char iv[16]); - -void aesni_ccm64_encrypt_blocks (const unsigned char *in, - unsigned char *out, - size_t blocks, - const void *key, - const unsigned char ivec[16], - unsigned char cmac[16]); - -void aesni_ccm64_decrypt_blocks (const unsigned char *in, - unsigned char *out, - size_t blocks, - const void *key, - const unsigned char ivec[16], - unsigned char cmac[16]); - -static int aesni_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { + const AES_KEY *key); + +void aesni_ecb_encrypt(const unsigned char *in, unsigned char *out, + size_t length, const AES_KEY *key, int enc); +void aesni_cbc_encrypt(const unsigned char *in, unsigned char *out, + size_t length, const AES_KEY *key, unsigned char *ivec, int enc); + +void aesni_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out, + size_t blocks, const void *key, const unsigned char *ivec); + +void aesni_xts_encrypt(const unsigned char *in, unsigned char *out, + size_t length, const AES_KEY *key1, const AES_KEY *key2, + const unsigned char iv[16]); + +void aesni_xts_decrypt(const unsigned char *in, unsigned char *out, + size_t length, const AES_KEY *key1, const AES_KEY *key2, + const unsigned char iv[16]); + +void aesni_ccm64_encrypt_blocks (const unsigned char *in, unsigned char *out, + size_t blocks, const void *key, const unsigned char ivec[16], + unsigned char cmac[16]); + +void aesni_ccm64_decrypt_blocks (const unsigned char *in, unsigned char *out, + size_t blocks, const void *key, const unsigned char ivec[16], + unsigned char cmac[16]); + +static int +aesni_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ int ret, mode; EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; mode = ctx->cipher->flags & EVP_CIPH_MODE; - if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) - && !enc) - { - ret = aesni_set_decrypt_key(key, ctx->key_len*8, ctx->cipher_data); - dat->block = (block128_f)aesni_decrypt; - dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ? - (cbc128_f)aesni_cbc_encrypt : - NULL; - } - else { - ret = aesni_set_encrypt_key(key, ctx->key_len*8, ctx->cipher_data); - dat->block = (block128_f)aesni_encrypt; - if (mode==EVP_CIPH_CBC_MODE) - dat->stream.cbc = (cbc128_f)aesni_cbc_encrypt; - else if (mode==EVP_CIPH_CTR_MODE) + if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) && + !enc) { + ret = aesni_set_decrypt_key(key, ctx->key_len * 8, + ctx->cipher_data); + dat->block = (block128_f)aesni_decrypt; + dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? + (cbc128_f)aesni_cbc_encrypt : NULL; + } else { + ret = aesni_set_encrypt_key(key, ctx->key_len * 8, + ctx->cipher_data); + dat->block = (block128_f)aesni_encrypt; + if (mode == EVP_CIPH_CBC_MODE) + dat->stream.cbc = (cbc128_f)aesni_cbc_encrypt; + else if (mode == EVP_CIPH_CTR_MODE) dat->stream.ctr = (ctr128_f)aesni_ctr32_encrypt_blocks; else dat->stream.cbc = NULL; - } + } - if(ret < 0) - { - EVPerr(EVP_F_AESNI_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED); + if (ret < 0) { + EVPerr(EVP_F_AESNI_INIT_KEY, EVP_R_AES_KEY_SETUP_FAILED); return 0; - } + } return 1; - } +} -static int aesni_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, - const unsigned char *in, size_t len) +static int +aesni_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len) { - aesni_cbc_encrypt(in,out,len,ctx->cipher_data,ctx->iv,ctx->encrypt); + aesni_cbc_encrypt(in, out, len, ctx->cipher_data, ctx->iv, + ctx->encrypt); return 1; } -static int aesni_ecb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, - const unsigned char *in, size_t len) +static int +aesni_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len) { size_t bl = ctx->cipher->block_size; - if (lencipher_data,ctx->encrypt); + aesni_ecb_encrypt(in, out, len, ctx->cipher_data, ctx->encrypt); return 1; } #define aesni_ofb_cipher aes_ofb_cipher -static int aesni_ofb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, - const unsigned char *in,size_t len); +static int aesni_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len); #define aesni_cfb_cipher aes_cfb_cipher -static int aesni_cfb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, - const unsigned char *in,size_t len); +static int aesni_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len); #define aesni_cfb8_cipher aes_cfb8_cipher -static int aesni_cfb8_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, - const unsigned char *in,size_t len); +static int aesni_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len); #define aesni_cfb1_cipher aes_cfb1_cipher -static int aesni_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, - const unsigned char *in,size_t len); +static int aesni_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len); #define aesni_ctr_cipher aes_ctr_cipher static int aesni_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t len); + const unsigned char *in, size_t len); -static int aesni_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { +static int +aesni_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ EVP_AES_GCM_CTX *gctx = ctx->cipher_data; + if (!iv && !key) return 1; - if (key) - { + if (key) { aesni_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks); CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, - (block128_f)aesni_encrypt); + (block128_f)aesni_encrypt); gctx->ctr = (ctr128_f)aesni_ctr32_encrypt_blocks; /* If we have an iv can set it directly, otherwise use * saved IV. */ if (iv == NULL && gctx->iv_set) iv = gctx->iv; - if (iv) - { + if (iv) { CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); gctx->iv_set = 1; - } - gctx->key_set = 1; } - else - { + gctx->key_set = 1; + } else { /* If key set use IV, otherwise copy */ if (gctx->key_set) CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); @@ -335,83 +305,82 @@ static int aesni_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, memcpy(gctx->iv, iv, gctx->ivlen); gctx->iv_set = 1; gctx->iv_gen = 0; - } - return 1; } + return 1; +} #define aesni_gcm_cipher aes_gcm_cipher static int aesni_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t len); + const unsigned char *in, size_t len); -static int aesni_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { +static int +aesni_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ EVP_AES_XTS_CTX *xctx = ctx->cipher_data; + if (!iv && !key) return 1; - if (key) - { + if (key) { /* key_len is two AES keys */ - if (enc) - { - aesni_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1); + if (enc) { + aesni_set_encrypt_key(key, ctx->key_len * 4, + &xctx->ks1); xctx->xts.block1 = (block128_f)aesni_encrypt; xctx->stream = aesni_xts_encrypt; - } - else - { - aesni_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1); + } else { + aesni_set_decrypt_key(key, ctx->key_len * 4, + &xctx->ks1); xctx->xts.block1 = (block128_f)aesni_decrypt; xctx->stream = aesni_xts_decrypt; - } + } - aesni_set_encrypt_key(key + ctx->key_len/2, - ctx->key_len * 4, &xctx->ks2); + aesni_set_encrypt_key(key + ctx->key_len / 2, + ctx->key_len * 4, &xctx->ks2); xctx->xts.block2 = (block128_f)aesni_encrypt; xctx->xts.key1 = &xctx->ks1; - } + } - if (iv) - { + if (iv) { xctx->xts.key2 = &xctx->ks2; memcpy(ctx->iv, iv, 16); - } + } return 1; - } +} #define aesni_xts_cipher aes_xts_cipher static int aesni_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t len); + const unsigned char *in, size_t len); -static int aesni_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { +static int +aesni_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ EVP_AES_CCM_CTX *cctx = ctx->cipher_data; + if (!iv && !key) return 1; - if (key) - { + if (key) { aesni_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks); CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L, - &cctx->ks, (block128_f)aesni_encrypt); - cctx->str = enc?(ccm128_f)aesni_ccm64_encrypt_blocks : - (ccm128_f)aesni_ccm64_decrypt_blocks; + &cctx->ks, (block128_f)aesni_encrypt); + cctx->str = enc ? (ccm128_f)aesni_ccm64_encrypt_blocks : + (ccm128_f)aesni_ccm64_decrypt_blocks; cctx->key_set = 1; - } - if (iv) - { + } + if (iv) { memcpy(ctx->iv, iv, 15 - cctx->L); cctx->iv_set = 1; - } - return 1; } + return 1; +} #define aesni_ccm_cipher aes_ccm_cipher static int aesni_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t len); + const unsigned char *in, size_t len); #define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \ static const EVP_CIPHER aesni_##keylen##_##mode = { \ @@ -493,199 +462,205 @@ const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \ BLOCK_CIPHER_generic(nid,keylen,1,16,cfb8,cfb8,CFB,flags) \ BLOCK_CIPHER_generic(nid,keylen,1,16,ctr,ctr,CTR,flags) -static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { +static int +aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ int ret, mode; EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; mode = ctx->cipher->flags & EVP_CIPH_MODE; - if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) - && !enc) + if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) && + !enc) #ifdef BSAES_CAPABLE - if (BSAES_CAPABLE && mode==EVP_CIPH_CBC_MODE) - { - ret = AES_set_decrypt_key(key,ctx->key_len*8,&dat->ks); - dat->block = (block128_f)AES_decrypt; - dat->stream.cbc = (cbc128_f)bsaes_cbc_encrypt; - } - else + if (BSAES_CAPABLE && mode == EVP_CIPH_CBC_MODE) { + ret = AES_set_decrypt_key(key, ctx->key_len * 8, + &dat->ks); + dat->block = (block128_f)AES_decrypt; + dat->stream.cbc = (cbc128_f)bsaes_cbc_encrypt; + } else #endif #ifdef VPAES_CAPABLE - if (VPAES_CAPABLE) - { - ret = vpaes_set_decrypt_key(key,ctx->key_len*8,&dat->ks); - dat->block = (block128_f)vpaes_decrypt; - dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ? - (cbc128_f)vpaes_cbc_encrypt : - NULL; - } - else + if (VPAES_CAPABLE) { + ret = vpaes_set_decrypt_key(key, ctx->key_len * 8, + &dat->ks); + dat->block = (block128_f)vpaes_decrypt; + dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? + (cbc128_f)vpaes_cbc_encrypt : NULL; + } else #endif { - ret = AES_set_decrypt_key(key,ctx->key_len*8,&dat->ks); - dat->block = (block128_f)AES_decrypt; - dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ? - (cbc128_f)AES_cbc_encrypt : - NULL; - } - else + ret = AES_set_decrypt_key(key, ctx->key_len * 8, + &dat->ks); + dat->block = (block128_f)AES_decrypt; + dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? + (cbc128_f)AES_cbc_encrypt : NULL; + } else #ifdef BSAES_CAPABLE - if (BSAES_CAPABLE && mode==EVP_CIPH_CTR_MODE) - { - ret = AES_set_encrypt_key(key,ctx->key_len*8,&dat->ks); - dat->block = (block128_f)AES_encrypt; - dat->stream.ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks; - } - else + if (BSAES_CAPABLE && mode == EVP_CIPH_CTR_MODE) { + ret = AES_set_encrypt_key(key, ctx->key_len * 8, + &dat->ks); + dat->block = (block128_f)AES_encrypt; + dat->stream.ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks; + } else #endif #ifdef VPAES_CAPABLE - if (VPAES_CAPABLE) - { - ret = vpaes_set_encrypt_key(key,ctx->key_len*8,&dat->ks); - dat->block = (block128_f)vpaes_encrypt; - dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ? - (cbc128_f)vpaes_cbc_encrypt : - NULL; - } - else + if (VPAES_CAPABLE) { + ret = vpaes_set_encrypt_key(key, ctx->key_len * 8, + &dat->ks); + dat->block = (block128_f)vpaes_encrypt; + dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? + (cbc128_f)vpaes_cbc_encrypt : NULL; + } else #endif { - ret = AES_set_encrypt_key(key,ctx->key_len*8,&dat->ks); - dat->block = (block128_f)AES_encrypt; - dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ? - (cbc128_f)AES_cbc_encrypt : - NULL; + ret = AES_set_encrypt_key(key, ctx->key_len * 8, + &dat->ks); + dat->block = (block128_f)AES_encrypt; + dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? + (cbc128_f)AES_cbc_encrypt : NULL; #ifdef AES_CTR_ASM - if (mode==EVP_CIPH_CTR_MODE) - dat->stream.ctr = (ctr128_f)AES_ctr32_encrypt; + if (mode == EVP_CIPH_CTR_MODE) + dat->stream.ctr = (ctr128_f)AES_ctr32_encrypt; #endif } - if(ret < 0) - { - EVPerr(EVP_F_AES_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED); + if (ret < 0) { + EVPerr(EVP_F_AES_INIT_KEY, EVP_R_AES_KEY_SETUP_FAILED); return 0; - } + } return 1; - } +} -static int aes_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, - const unsigned char *in, size_t len) +static int +aes_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len) { EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; if (dat->stream.cbc) - (*dat->stream.cbc)(in,out,len,&dat->ks,ctx->iv,ctx->encrypt); + (*dat->stream.cbc)(in, out, len, &dat->ks, ctx->iv, + ctx->encrypt); else if (ctx->encrypt) - CRYPTO_cbc128_encrypt(in,out,len,&dat->ks,ctx->iv,dat->block); + CRYPTO_cbc128_encrypt(in, out, len, &dat->ks, ctx->iv, + dat->block); else - CRYPTO_cbc128_decrypt(in,out,len,&dat->ks,ctx->iv,dat->block); + CRYPTO_cbc128_decrypt(in, out, len, &dat->ks, ctx->iv, + dat->block); return 1; } -static int aes_ecb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, - const unsigned char *in, size_t len) +static int +aes_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len) { size_t bl = ctx->cipher->block_size; size_t i; EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; - if (lenblock)(in+i,out+i,&dat->ks); + for (i = 0, len -= bl; i <= len; i += bl) + (*dat->block)(in + i, out + i, &dat->ks); return 1; } -static int aes_ofb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, - const unsigned char *in,size_t len) +static int +aes_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len) { EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; - CRYPTO_ofb128_encrypt(in,out,len,&dat->ks, - ctx->iv,&ctx->num,dat->block); + CRYPTO_ofb128_encrypt(in, out, len, &dat->ks, ctx->iv, &ctx->num, + dat->block); return 1; } -static int aes_cfb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, - const unsigned char *in,size_t len) +static int +aes_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len) { EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; - CRYPTO_cfb128_encrypt(in,out,len,&dat->ks, - ctx->iv,&ctx->num,ctx->encrypt,dat->block); + CRYPTO_cfb128_encrypt(in, out, len, &dat->ks, ctx->iv, &ctx->num, + ctx->encrypt, dat->block); return 1; } -static int aes_cfb8_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, - const unsigned char *in,size_t len) +static int +aes_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len) { EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; - CRYPTO_cfb128_8_encrypt(in,out,len,&dat->ks, - ctx->iv,&ctx->num,ctx->encrypt,dat->block); + CRYPTO_cfb128_8_encrypt(in, out, len, &dat->ks, ctx->iv, &ctx->num, + ctx->encrypt, dat->block); return 1; } -static int aes_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, - const unsigned char *in,size_t len) +static int +aes_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len) { EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; if (ctx->flags&EVP_CIPH_FLAG_LENGTH_BITS) { - CRYPTO_cfb128_1_encrypt(in,out,len,&dat->ks, - ctx->iv,&ctx->num,ctx->encrypt,dat->block); + CRYPTO_cfb128_1_encrypt(in, out, len, &dat->ks, ctx->iv, + &ctx->num, ctx->encrypt, dat->block); return 1; } - while (len>=MAXBITCHUNK) { - CRYPTO_cfb128_1_encrypt(in,out,MAXBITCHUNK*8,&dat->ks, - ctx->iv,&ctx->num,ctx->encrypt,dat->block); - len-=MAXBITCHUNK; + while (len >= MAXBITCHUNK) { + CRYPTO_cfb128_1_encrypt(in, out, MAXBITCHUNK*8, &dat->ks, + ctx->iv, &ctx->num, ctx->encrypt, dat->block); + len -= MAXBITCHUNK; } if (len) - CRYPTO_cfb128_1_encrypt(in,out,len*8,&dat->ks, - ctx->iv,&ctx->num,ctx->encrypt,dat->block); - + CRYPTO_cfb128_1_encrypt(in, out, len*8, &dat->ks, + ctx->iv, &ctx->num, ctx->encrypt, dat->block); + return 1; } static int aes_ctr_cipher (EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t len) + const unsigned char *in, size_t len) { unsigned int num = ctx->num; EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; if (dat->stream.ctr) - CRYPTO_ctr128_encrypt_ctr32(in,out,len,&dat->ks, - ctx->iv,ctx->buf,&num,dat->stream.ctr); + CRYPTO_ctr128_encrypt_ctr32(in, out, len, &dat->ks, + ctx->iv, ctx->buf, &num, dat->stream.ctr); else - CRYPTO_ctr128_encrypt(in,out,len,&dat->ks, - ctx->iv,ctx->buf,&num,dat->block); + CRYPTO_ctr128_encrypt(in, out, len, &dat->ks, + ctx->iv, ctx->buf, &num, dat->block); ctx->num = (size_t)num; return 1; } -BLOCK_CIPHER_generic_pack(NID_aes,128,EVP_CIPH_FLAG_FIPS) -BLOCK_CIPHER_generic_pack(NID_aes,192,EVP_CIPH_FLAG_FIPS) -BLOCK_CIPHER_generic_pack(NID_aes,256,EVP_CIPH_FLAG_FIPS) +BLOCK_CIPHER_generic_pack(NID_aes, 128, EVP_CIPH_FLAG_FIPS) +BLOCK_CIPHER_generic_pack(NID_aes, 192, EVP_CIPH_FLAG_FIPS) +BLOCK_CIPHER_generic_pack(NID_aes, 256, EVP_CIPH_FLAG_FIPS) -static int aes_gcm_cleanup(EVP_CIPHER_CTX *c) - { +static int +aes_gcm_cleanup(EVP_CIPHER_CTX *c) +{ EVP_AES_GCM_CTX *gctx = c->cipher_data; + OPENSSL_cleanse(&gctx->gcm, sizeof(gctx->gcm)); if (gctx->iv != c->iv) free(gctx->iv); return 1; - } +} /* increment counter (64-bit int) by 1 */ -static void ctr64_inc(unsigned char *counter) { - int n=8; +static void +ctr64_inc(unsigned char *counter) { + int n = 8; unsigned char c; do { @@ -693,15 +668,17 @@ static void ctr64_inc(unsigned char *counter) { c = counter[n]; ++c; counter[n] = c; - if (c) return; + if (c) + return; } while (n); } -static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) - { +static int +aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) +{ EVP_AES_GCM_CTX *gctx = c->cipher_data; - switch (type) - { + + switch (type) { case EVP_CTRL_INIT: gctx->key_set = 0; gctx->iv_set = 0; @@ -716,19 +693,18 @@ static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) if (arg <= 0) return 0; #ifdef OPENSSL_FIPS - if (FIPS_module_mode() && !(c->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW) - && arg < 12) + if (FIPS_module_mode() && + !(c->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW) && arg < 12) return 0; #endif /* Allocate memory for IV if needed */ - if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen)) - { + if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen)) { if (gctx->iv != c->iv) free(gctx->iv); gctx->iv = malloc(arg); if (!gctx->iv) return 0; - } + } gctx->ivlen = arg; return 1; @@ -747,12 +723,11 @@ static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) case EVP_CTRL_GCM_SET_IV_FIXED: /* Special case: -1 length restores whole IV */ - if (arg == -1) - { + if (arg == -1) { memcpy(gctx->iv, ptr, gctx->ivlen); gctx->iv_gen = 1; return 1; - } + } /* Fixed field must be at least 4 bytes and invocation field * at least 8. */ @@ -761,7 +736,7 @@ static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) if (arg) memcpy(gctx->iv, ptr, arg); if (c->encrypt && - RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0) + RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0) return 0; gctx->iv_gen = 1; return 1; @@ -795,63 +770,68 @@ static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) return 0; memcpy(c->buf, ptr, arg); gctx->tls_aad_len = arg; - { - unsigned int len=c->buf[arg-2]<<8|c->buf[arg-1]; + { + unsigned int len = c->buf[arg - 2] << 8 | + c->buf[arg - 1]; + /* Correct length for explicit IV */ len -= EVP_GCM_TLS_EXPLICIT_IV_LEN; + /* If decrypting correct for tag too */ if (!c->encrypt) len -= EVP_GCM_TLS_TAG_LEN; - c->buf[arg-2] = len>>8; - c->buf[arg-1] = len & 0xff; - } + c->buf[arg - 2] = len >> 8; + c->buf[arg - 1] = len & 0xff; + } /* Extra padding: tag appended to record */ return EVP_GCM_TLS_TAG_LEN; default: return -1; - } } +} -static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { +static int +aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ EVP_AES_GCM_CTX *gctx = ctx->cipher_data; + if (!iv && !key) return 1; - if (key) - { do { + if (key) { + do { #ifdef BSAES_CAPABLE - if (BSAES_CAPABLE) - { - AES_set_encrypt_key(key,ctx->key_len*8,&gctx->ks); - CRYPTO_gcm128_init(&gctx->gcm,&gctx->ks, - (block128_f)AES_encrypt); - gctx->ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks; - break; - } - else + if (BSAES_CAPABLE) { + AES_set_encrypt_key(key, ctx->key_len * 8, + &gctx->ks); + CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, + (block128_f)AES_encrypt); + gctx->ctr = + (ctr128_f)bsaes_ctr32_encrypt_blocks; + break; + } else #endif #ifdef VPAES_CAPABLE - if (VPAES_CAPABLE) - { - vpaes_set_encrypt_key(key,ctx->key_len*8,&gctx->ks); - CRYPTO_gcm128_init(&gctx->gcm,&gctx->ks, - (block128_f)vpaes_encrypt); - gctx->ctr = NULL; - break; - } - else + if (VPAES_CAPABLE) { + vpaes_set_encrypt_key(key, ctx->key_len * 8, + &gctx->ks); + CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, + (block128_f)vpaes_encrypt); + gctx->ctr = NULL; + break; + } else #endif - (void)0; /* terminate potentially open 'else' */ + (void)0; /* terminate potentially open 'else' */ - AES_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks); - CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, (block128_f)AES_encrypt); + AES_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks); + CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, + (block128_f)AES_encrypt); #ifdef AES_CTR_ASM - gctx->ctr = (ctr128_f)AES_ctr32_encrypt; + gctx->ctr = (ctr128_f)AES_ctr32_encrypt; #else - gctx->ctr = NULL; + gctx->ctr = NULL; #endif } while (0); @@ -860,15 +840,12 @@ static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, */ if (iv == NULL && gctx->iv_set) iv = gctx->iv; - if (iv) - { + if (iv) { CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); gctx->iv_set = 1; - } - gctx->key_set = 1; } - else - { + gctx->key_set = 1; + } else { /* If key set use IV, otherwise copy */ if (gctx->key_set) CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); @@ -876,9 +853,9 @@ static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, memcpy(gctx->iv, iv, gctx->ivlen); gctx->iv_set = 1; gctx->iv_gen = 0; - } - return 1; } + return 1; +} /* Handle TLS GCM packet format. This consists of the last portion of the IV * followed by the payload and finally the tag. On encrypt generate IV, @@ -886,83 +863,82 @@ static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, * and verify tag. */ -static int aes_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t len) - { +static int +aes_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len) +{ EVP_AES_GCM_CTX *gctx = ctx->cipher_data; int rv = -1; + /* Encrypt/decrypt must be performed in place */ - if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN+EVP_GCM_TLS_TAG_LEN)) + if (out != in || + len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN)) return -1; + /* Set IV from start of buffer or generate IV and write to start * of buffer. */ if (EVP_CIPHER_CTX_ctrl(ctx, ctx->encrypt ? - EVP_CTRL_GCM_IV_GEN : EVP_CTRL_GCM_SET_IV_INV, - EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0) + EVP_CTRL_GCM_IV_GEN : EVP_CTRL_GCM_SET_IV_INV, + EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0) goto err; + /* Use saved AAD */ if (CRYPTO_gcm128_aad(&gctx->gcm, ctx->buf, gctx->tls_aad_len)) goto err; + /* Fix buffer and length to point to payload */ in += EVP_GCM_TLS_EXPLICIT_IV_LEN; out += EVP_GCM_TLS_EXPLICIT_IV_LEN; len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN; - if (ctx->encrypt) - { + if (ctx->encrypt) { /* Encrypt payload */ - if (gctx->ctr) - { - if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm, - in, out, len, - gctx->ctr)) + if (gctx->ctr) { + if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm, in, out, + len, gctx->ctr)) goto err; - } - else { + } else { if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len)) goto err; - } + } out += len; + /* Finally write tag */ CRYPTO_gcm128_tag(&gctx->gcm, out, EVP_GCM_TLS_TAG_LEN); rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN; - } - else - { + } else { /* Decrypt */ - if (gctx->ctr) - { - if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm, - in, out, len, - gctx->ctr)) + if (gctx->ctr) { + if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm, in, out, + len, gctx->ctr)) goto err; - } - else { + } else { if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len)) goto err; - } + } /* Retrieve tag */ - CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, - EVP_GCM_TLS_TAG_LEN); + CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, EVP_GCM_TLS_TAG_LEN); + /* If tag mismatch wipe buffer */ - if (memcmp(ctx->buf, in + len, EVP_GCM_TLS_TAG_LEN)) - { + if (memcmp(ctx->buf, in + len, EVP_GCM_TLS_TAG_LEN)) { OPENSSL_cleanse(out, len); goto err; - } - rv = len; } + rv = len; + } - err: +err: gctx->iv_set = 0; gctx->tls_aad_len = -1; return rv; - } +} -static int aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t len) - { +static int +aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len) +{ EVP_AES_GCM_CTX *gctx = ctx->cipher_data; + /* If not set up, return error */ if (!gctx->key_set) return -1; @@ -972,95 +948,88 @@ static int aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, if (!gctx->iv_set) return -1; - if (in) - { - if (out == NULL) - { + + if (in) { + if (out == NULL) { if (CRYPTO_gcm128_aad(&gctx->gcm, in, len)) return -1; - } - else if (ctx->encrypt) - { - if (gctx->ctr) - { + } else if (ctx->encrypt) { + if (gctx->ctr) { if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm, - in, out, len, - gctx->ctr)) + in, out, len, gctx->ctr)) return -1; - } - else { - if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len)) + } else { + if (CRYPTO_gcm128_encrypt(&gctx->gcm, + in, out, len)) return -1; - } } - else - { - if (gctx->ctr) - { + } else { + if (gctx->ctr) { if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm, - in, out, len, - gctx->ctr)) + in, out, len, gctx->ctr)) return -1; - } - else { - if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len)) + } else { + if (CRYPTO_gcm128_decrypt(&gctx->gcm, + in, out, len)) return -1; - } } - return len; } - else - { - if (!ctx->encrypt) - { + return len; + } else { + if (!ctx->encrypt) { if (gctx->taglen < 0) return -1; - if (CRYPTO_gcm128_finish(&gctx->gcm, - ctx->buf, gctx->taglen) != 0) + if (CRYPTO_gcm128_finish(&gctx->gcm, ctx->buf, + gctx->taglen) != 0) return -1; gctx->iv_set = 0; return 0; - } + } CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, 16); gctx->taglen = 16; + /* Don't reuse the IV */ gctx->iv_set = 0; return 0; - } - } +} + #define CUSTOM_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 \ | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \ | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT) -BLOCK_CIPHER_custom(NID_aes,128,1,12,gcm,GCM, - EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS) -BLOCK_CIPHER_custom(NID_aes,192,1,12,gcm,GCM, - EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS) -BLOCK_CIPHER_custom(NID_aes,256,1,12,gcm,GCM, - EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS) +BLOCK_CIPHER_custom(NID_aes, 128, 1,12, gcm, GCM, + EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS) +BLOCK_CIPHER_custom(NID_aes, 192, 1,12, gcm, GCM, + EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS) +BLOCK_CIPHER_custom(NID_aes, 256, 1,12, gcm, GCM, + EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS) -static int aes_xts_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) - { +static int +aes_xts_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) +{ EVP_AES_XTS_CTX *xctx = c->cipher_data; + if (type != EVP_CTRL_INIT) return -1; + /* key1 and key2 are used as an indicator both key and IV are set */ xctx->xts.key1 = NULL; xctx->xts.key2 = NULL; return 1; - } +} -static int aes_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { +static int +aes_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ EVP_AES_XTS_CTX *xctx = ctx->cipher_data; + if (!iv && !key) return 1; - if (key) do - { + if (key) do { #ifdef AES_XTS_ASM xctx->stream = enc ? AES_xts_encrypt : AES_xts_decrypt; #else @@ -1069,100 +1038,98 @@ static int aes_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, /* key_len is two AES keys */ #ifdef BSAES_CAPABLE if (BSAES_CAPABLE) - xctx->stream = enc ? bsaes_xts_encrypt : bsaes_xts_decrypt; + xctx->stream = enc ? bsaes_xts_encrypt : + bsaes_xts_decrypt; else #endif #ifdef VPAES_CAPABLE - if (VPAES_CAPABLE) - { - if (enc) - { - vpaes_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1); - xctx->xts.block1 = (block128_f)vpaes_encrypt; - } - else - { - vpaes_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1); - xctx->xts.block1 = (block128_f)vpaes_decrypt; + if (VPAES_CAPABLE) { + if (enc) { + vpaes_set_encrypt_key(key, ctx->key_len * 4, + &xctx->ks1); + xctx->xts.block1 = (block128_f)vpaes_encrypt; + } else { + vpaes_set_decrypt_key(key, ctx->key_len * 4, + &xctx->ks1); + xctx->xts.block1 = (block128_f)vpaes_decrypt; } - vpaes_set_encrypt_key(key + ctx->key_len/2, - ctx->key_len * 4, &xctx->ks2); - xctx->xts.block2 = (block128_f)vpaes_encrypt; + vpaes_set_encrypt_key(key + ctx->key_len / 2, + ctx->key_len * 4, &xctx->ks2); + xctx->xts.block2 = (block128_f)vpaes_encrypt; - xctx->xts.key1 = &xctx->ks1; - break; - } - else + xctx->xts.key1 = &xctx->ks1; + break; + } else #endif - (void)0; /* terminate potentially open 'else' */ + (void)0; /* terminate potentially open 'else' */ - if (enc) - { + if (enc) { AES_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1); xctx->xts.block1 = (block128_f)AES_encrypt; - } - else - { + } else { AES_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1); xctx->xts.block1 = (block128_f)AES_decrypt; - } + } - AES_set_encrypt_key(key + ctx->key_len/2, - ctx->key_len * 4, &xctx->ks2); + AES_set_encrypt_key(key + ctx->key_len / 2, + ctx->key_len * 4, &xctx->ks2); xctx->xts.block2 = (block128_f)AES_encrypt; xctx->xts.key1 = &xctx->ks1; - } while (0); + } while (0); - if (iv) - { + if (iv) { xctx->xts.key2 = &xctx->ks2; memcpy(ctx->iv, iv, 16); - } + } return 1; - } +} -static int aes_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t len) - { +static int +aes_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len) +{ EVP_AES_XTS_CTX *xctx = ctx->cipher_data; + if (!xctx->xts.key1 || !xctx->xts.key2) return 0; - if (!out || !in || lenflags & EVP_CIPH_FLAG_NON_FIPS_ALLOW) && - (len > (1UL<<20)*16)) - { + if (FIPS_module_mode() && + !(ctx->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW) && + (len > (1UL << 20) * 16)) { EVPerr(EVP_F_AES_XTS_CIPHER, EVP_R_TOO_LARGE); return 0; - } + } #endif if (xctx->stream) - (*xctx->stream)(in, out, len, - xctx->xts.key1, xctx->xts.key2, ctx->iv); + (*xctx->stream)(in, out, len, xctx->xts.key1, xctx->xts.key2, + ctx->iv); else if (CRYPTO_xts128_encrypt(&xctx->xts, ctx->iv, in, out, len, - ctx->encrypt)) + ctx->encrypt)) return 0; return 1; - } +} #define aes_xts_cleanup NULL #define XTS_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CUSTOM_IV \ | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT) -BLOCK_CIPHER_custom(NID_aes,128,1,16,xts,XTS,EVP_CIPH_FLAG_FIPS|XTS_FLAGS) -BLOCK_CIPHER_custom(NID_aes,256,1,16,xts,XTS,EVP_CIPH_FLAG_FIPS|XTS_FLAGS) +BLOCK_CIPHER_custom(NID_aes, 128, 1,16, xts, XTS, EVP_CIPH_FLAG_FIPS|XTS_FLAGS) +BLOCK_CIPHER_custom(NID_aes, 256, 1,16, xts, XTS, EVP_CIPH_FLAG_FIPS|XTS_FLAGS) -static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) - { +static int +aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) +{ EVP_AES_CCM_CTX *cctx = c->cipher_data; - switch (type) - { + + switch (type) { case EVP_CTRL_INIT: cctx->key_set = 0; cctx->iv_set = 0; @@ -1174,6 +1141,7 @@ static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) case EVP_CTRL_CCM_SET_IVLEN: arg = 15 - arg; + case EVP_CTRL_CCM_SET_L: if (arg < 2 || arg > 8) return 0; @@ -1185,18 +1153,17 @@ static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) return 0; if ((c->encrypt && ptr) || (!c->encrypt && !ptr)) return 0; - if (ptr) - { + if (ptr) { cctx->tag_set = 1; memcpy(c->buf, ptr, arg); - } + } cctx->M = arg; return 1; case EVP_CTRL_CCM_GET_TAG: if (!c->encrypt || !cctx->tag_set) return 0; - if(!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg)) + if (!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg)) return 0; cctx->tag_set = 0; cctx->iv_set = 0; @@ -1205,116 +1172,111 @@ static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) default: return -1; - - } } +} -static int aes_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { +static int +aes_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ EVP_AES_CCM_CTX *cctx = ctx->cipher_data; + if (!iv && !key) return 1; - if (key) do - { + if (key) do { #ifdef VPAES_CAPABLE - if (VPAES_CAPABLE) - { + if (VPAES_CAPABLE) { vpaes_set_encrypt_key(key, ctx->key_len*8, &cctx->ks); CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L, - &cctx->ks, (block128_f)vpaes_encrypt); + &cctx->ks, (block128_f)vpaes_encrypt); cctx->str = NULL; cctx->key_set = 1; break; - } + } #endif AES_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks); CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L, - &cctx->ks, (block128_f)AES_encrypt); + &cctx->ks, (block128_f)AES_encrypt); cctx->str = NULL; cctx->key_set = 1; - } while (0); - if (iv) - { + } while (0); + if (iv) { memcpy(ctx->iv, iv, 15 - cctx->L); cctx->iv_set = 1; - } - return 1; } + return 1; +} -static int aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t len) - { +static int +aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len) +{ EVP_AES_CCM_CTX *cctx = ctx->cipher_data; CCM128_CONTEXT *ccm = &cctx->ccm; + /* If not set up, return error */ if (!cctx->iv_set && !cctx->key_set) return -1; if (!ctx->encrypt && !cctx->tag_set) return -1; - if (!out) - { - if (!in) - { - if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L,len)) + + if (!out) { + if (!in) { + if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, + len)) return -1; cctx->len_set = 1; return len; - } + } /* If have AAD need message length */ if (!cctx->len_set && len) return -1; CRYPTO_ccm128_aad(ccm, in, len); return len; - } + } /* EVP_*Final() doesn't return any data */ if (!in) return 0; /* If not set length yet do it */ - if (!cctx->len_set) - { + if (!cctx->len_set) { if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len)) return -1; cctx->len_set = 1; - } - if (ctx->encrypt) - { + } + if (ctx->encrypt) { if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len, - cctx->str) : - CRYPTO_ccm128_encrypt(ccm, in, out, len)) + cctx->str) : CRYPTO_ccm128_encrypt(ccm, in, out, len)) return -1; cctx->tag_set = 1; return len; - } - else - { + } else { int rv = -1; if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len, - cctx->str) : - !CRYPTO_ccm128_decrypt(ccm, in, out, len)) - { + cctx->str) : !CRYPTO_ccm128_decrypt(ccm, in, out, len)) { unsigned char tag[16]; - if (CRYPTO_ccm128_tag(ccm, tag, cctx->M)) - { + if (CRYPTO_ccm128_tag(ccm, tag, cctx->M)) { if (!memcmp(tag, ctx->buf, cctx->M)) rv = len; - } } + } if (rv == -1) OPENSSL_cleanse(out, len); cctx->iv_set = 0; cctx->tag_set = 0; cctx->len_set = 0; return rv; - } - } +} + #define aes_ccm_cleanup NULL -BLOCK_CIPHER_custom(NID_aes,128,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS) -BLOCK_CIPHER_custom(NID_aes,192,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS) -BLOCK_CIPHER_custom(NID_aes,256,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS) +BLOCK_CIPHER_custom(NID_aes, 128, 1,12, ccm, CCM, + EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS) +BLOCK_CIPHER_custom(NID_aes, 192, 1,12, ccm, CCM, + EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS) +BLOCK_CIPHER_custom(NID_aes, 256, 1,12, ccm, CCM, + EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS) #endif #endif diff --git a/lib/libcrypto/evp/e_aes_cbc_hmac_sha1.c b/lib/libcrypto/evp/e_aes_cbc_hmac_sha1.c index 4d76ec74d2c..af0edb3dcf9 100644 --- a/lib/libcrypto/evp/e_aes_cbc_hmac_sha1.c +++ b/lib/libcrypto/evp/e_aes_cbc_hmac_sha1.c @@ -72,16 +72,15 @@ #define TLS1_1_VERSION 0x0302 -typedef struct - { - AES_KEY ks; - SHA_CTX head,tail,md; - size_t payload_length; /* AAD length in decrypt case */ - union { - unsigned int tls_ver; - unsigned char tls_aad[16]; /* 13 used */ - } aux; - } EVP_AES_HMAC_SHA1; +typedef struct { + AES_KEY ks; + SHA_CTX head, tail, md; + size_t payload_length; /* AAD length in decrypt case */ + union { + unsigned int tls_ver; + unsigned char tls_aad[16]; /* 13 used */ + } aux; +} EVP_AES_HMAC_SHA1; #define NO_PAYLOAD_LENGTH ((size_t)-1) @@ -97,43 +96,37 @@ typedef struct extern unsigned int OPENSSL_ia32cap_P[2]; #define AESNI_CAPABLE (1<<(57-32)) -int aesni_set_encrypt_key(const unsigned char *userKey, int bits, - AES_KEY *key); -int aesni_set_decrypt_key(const unsigned char *userKey, int bits, - AES_KEY *key); +int aesni_set_encrypt_key(const unsigned char *userKey, int bits, AES_KEY *key); +int aesni_set_decrypt_key(const unsigned char *userKey, int bits, AES_KEY *key); -void aesni_cbc_encrypt(const unsigned char *in, - unsigned char *out, - size_t length, - const AES_KEY *key, - unsigned char *ivec, int enc); +void aesni_cbc_encrypt(const unsigned char *in, unsigned char *out, + size_t length, const AES_KEY *key, unsigned char *ivec, int enc); void aesni_cbc_sha1_enc (const void *inp, void *out, size_t blocks, - const AES_KEY *key, unsigned char iv[16], - SHA_CTX *ctx,const void *in0); + const AES_KEY *key, unsigned char iv[16], SHA_CTX *ctx, const void *in0); #define data(ctx) ((EVP_AES_HMAC_SHA1 *)(ctx)->cipher_data) -static int aesni_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx, - const unsigned char *inkey, - const unsigned char *iv, int enc) - { +static int +aesni_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *inkey, + const unsigned char *iv, int enc) +{ EVP_AES_HMAC_SHA1 *key = data(ctx); int ret; if (enc) - ret=aesni_set_encrypt_key(inkey,ctx->key_len*8,&key->ks); + ret = aesni_set_encrypt_key(inkey, ctx->key_len * 8, &key->ks); else - ret=aesni_set_decrypt_key(inkey,ctx->key_len*8,&key->ks); + ret = aesni_set_decrypt_key(inkey, ctx->key_len * 8, &key->ks); SHA1_Init(&key->head); /* handy when benchmarking */ key->tail = key->head; - key->md = key->head; + key->md = key->head; key->payload_length = NO_PAYLOAD_LENGTH; - return ret<0?0:1; - } + return ret < 0 ? 0 : 1; +} #define STITCHED_CALL @@ -141,16 +134,19 @@ static int aesni_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx, #define aes_off 0 #endif -void sha1_block_data_order (void *c,const void *p,size_t len); +void sha1_block_data_order (void *c, const void *p, size_t len); -static void sha1_update(SHA_CTX *c,const void *data,size_t len) -{ const unsigned char *ptr = data; +static void +sha1_update(SHA_CTX *c, const void *data, size_t len) +{ + const unsigned char *ptr = data; size_t res; if ((res = c->num)) { - res = SHA_CBLOCK-res; - if (lenNh += len>>29; - c->Nl += len<<=3; - if (c->Nl<(unsigned int)len) c->Nh++; + c->Nh += len >> 29; + c->Nl += len <<= 3; + if (c->Nl < (unsigned int)len) + c->Nh++; } if (res) - SHA1_Update(c,ptr,res); + SHA1_Update(c, ptr, res); } #ifdef SHA1_Update @@ -176,96 +173,106 @@ static void sha1_update(SHA_CTX *c,const void *data,size_t len) #endif #define SHA1_Update sha1_update -static int aesni_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t len) - { +static int +aesni_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len) +{ EVP_AES_HMAC_SHA1 *key = data(ctx); unsigned int l; - size_t plen = key->payload_length, - iv = 0, /* explicit IV in TLS 1.1 and later */ - sha_off = 0; + size_t plen = key->payload_length, + iv = 0, /* explicit IV in TLS 1.1 and later */ + sha_off = 0; #if defined(STITCHED_CALL) - size_t aes_off = 0, - blocks; + size_t aes_off = 0, blocks; - sha_off = SHA_CBLOCK-key->md.num; + sha_off = SHA_CBLOCK - key->md.num; #endif key->payload_length = NO_PAYLOAD_LENGTH; - if (len%AES_BLOCK_SIZE) return 0; + if (len % AES_BLOCK_SIZE) + return 0; if (ctx->encrypt) { - if (plen==NO_PAYLOAD_LENGTH) + if (plen == NO_PAYLOAD_LENGTH) plen = len; - else if (len!=((plen+SHA_DIGEST_LENGTH+AES_BLOCK_SIZE)&-AES_BLOCK_SIZE)) + else if (len != ((plen + SHA_DIGEST_LENGTH + AES_BLOCK_SIZE) & + -AES_BLOCK_SIZE)) return 0; else if (key->aux.tls_ver >= TLS1_1_VERSION) iv = AES_BLOCK_SIZE; #if defined(STITCHED_CALL) - if (plen>(sha_off+iv) && (blocks=(plen-(sha_off+iv))/SHA_CBLOCK)) { - SHA1_Update(&key->md,in+iv,sha_off); + if (plen > (sha_off + iv) && + (blocks = (plen - (sha_off + iv)) / SHA_CBLOCK)) { + SHA1_Update(&key->md, in + iv, sha_off); - aesni_cbc_sha1_enc(in,out,blocks,&key->ks, - ctx->iv,&key->md,in+iv+sha_off); + aesni_cbc_sha1_enc(in, out, blocks, &key->ks, + ctx->iv, &key->md, in + iv + sha_off); blocks *= SHA_CBLOCK; aes_off += blocks; sha_off += blocks; - key->md.Nh += blocks>>29; - key->md.Nl += blocks<<=3; - if (key->md.Nl<(unsigned int)blocks) key->md.Nh++; + key->md.Nh += blocks >> 29; + key->md.Nl += blocks <<= 3; + if (key->md.Nl < (unsigned int)blocks) + key->md.Nh++; } else { sha_off = 0; } #endif sha_off += iv; - SHA1_Update(&key->md,in+sha_off,plen-sha_off); + SHA1_Update(&key->md, in + sha_off, plen - sha_off); - if (plen!=len) { /* "TLS" mode of operation */ - if (in!=out) - memcpy(out+aes_off,in+aes_off,plen-aes_off); + if (plen != len) { /* "TLS" mode of operation */ + if (in != out) + memcpy(out + aes_off, in + aes_off, + plen - aes_off); /* calculate HMAC and append it to payload */ - SHA1_Final(out+plen,&key->md); + SHA1_Final(out + plen, &key->md); key->md = key->tail; - SHA1_Update(&key->md,out+plen,SHA_DIGEST_LENGTH); - SHA1_Final(out+plen,&key->md); + SHA1_Update(&key->md, out + plen, SHA_DIGEST_LENGTH); + SHA1_Final(out + plen, &key->md); /* pad the payload|hmac */ plen += SHA_DIGEST_LENGTH; - for (l=len-plen-1;plenks,ctx->iv,1); + aesni_cbc_encrypt(out + aes_off, out + aes_off, + len - aes_off, &key->ks, ctx->iv, 1); } else { - aesni_cbc_encrypt(in+aes_off,out+aes_off,len-aes_off, - &key->ks,ctx->iv,1); + aesni_cbc_encrypt(in + aes_off, out + aes_off, + len - aes_off, &key->ks, ctx->iv, 1); } } else { - union { unsigned int u[SHA_DIGEST_LENGTH/sizeof(unsigned int)]; - unsigned char c[32+SHA_DIGEST_LENGTH]; } mac, *pmac; + union { + unsigned int u[SHA_DIGEST_LENGTH/sizeof(unsigned int)]; + unsigned char c[32 + SHA_DIGEST_LENGTH]; + } mac, *pmac; /* arrange cache line alignment */ - pmac = (void *)(((size_t)mac.c+31)&((size_t)0-32)); + pmac = (void *)(((size_t)mac.c + 31) & ((size_t)0 - 32)); /* decrypt HMAC|padding at once */ - aesni_cbc_encrypt(in,out,len, - &key->ks,ctx->iv,0); + aesni_cbc_encrypt(in, out, len, &key->ks, ctx->iv, 0); if (plen) { /* "TLS" mode of operation */ size_t inp_len, mask, j, i; unsigned int res, maxpad, pad, bitlen; int ret = 1; - union { unsigned int u[SHA_LBLOCK]; - unsigned char c[SHA_CBLOCK]; } - *data = (void *)key->md.data; + union { + unsigned int u[SHA_LBLOCK]; + unsigned char c[SHA_CBLOCK]; + } + *data = (void *)key->md.data; - if ((key->aux.tls_aad[plen-4]<<8|key->aux.tls_aad[plen-3]) - >= TLS1_1_VERSION) + if ((key->aux.tls_aad[plen - 4] << 8 | + key->aux.tls_aad[plen - 3]) >= TLS1_1_VERSION) iv = AES_BLOCK_SIZE; - if (len<(iv+SHA_DIGEST_LENGTH+1)) + if (len < (iv + SHA_DIGEST_LENGTH + 1)) return 0; /* omit explicit iv */ @@ -273,93 +280,102 @@ static int aesni_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, len -= iv; /* figure out payload length */ - pad = out[len-1]; - maxpad = len-(SHA_DIGEST_LENGTH+1); - maxpad |= (255-maxpad)>>(sizeof(maxpad)*8-8); + pad = out[len - 1]; + maxpad = len - (SHA_DIGEST_LENGTH + 1); + maxpad |= (255 - maxpad) >> (sizeof(maxpad) * 8 - 8); maxpad &= 255; - inp_len = len - (SHA_DIGEST_LENGTH+pad+1); - mask = (0-((inp_len-len)>>(sizeof(inp_len)*8-1))); + inp_len = len - (SHA_DIGEST_LENGTH + pad + 1); + mask = (0 - ((inp_len - len) >> + (sizeof(inp_len) * 8 - 1))); inp_len &= mask; ret &= (int)mask; - key->aux.tls_aad[plen-2] = inp_len>>8; - key->aux.tls_aad[plen-1] = inp_len; + key->aux.tls_aad[plen - 2] = inp_len >> 8; + key->aux.tls_aad[plen - 1] = inp_len; /* calculate HMAC */ key->md = key->head; - SHA1_Update(&key->md,key->aux.tls_aad,plen); + SHA1_Update(&key->md, key->aux.tls_aad, plen); #if 1 len -= SHA_DIGEST_LENGTH; /* amend mac */ - if (len>=(256+SHA_CBLOCK)) { - j = (len-(256+SHA_CBLOCK))&(0-SHA_CBLOCK); - j += SHA_CBLOCK-key->md.num; - SHA1_Update(&key->md,out,j); + if (len >= (256 + SHA_CBLOCK)) { + j = (len - (256 + SHA_CBLOCK)) & + (0 - SHA_CBLOCK); + j += SHA_CBLOCK - key->md.num; + SHA1_Update(&key->md, out, j); out += j; len -= j; inp_len -= j; } /* but pretend as if we hashed padded payload */ - bitlen = key->md.Nl+(inp_len<<3); /* at most 18 bits */ + bitlen = key->md.Nl + (inp_len << 3); /* at most 18 bits */ #ifdef BSWAP bitlen = BSWAP(bitlen); #else mac.c[0] = 0; - mac.c[1] = (unsigned char)(bitlen>>16); - mac.c[2] = (unsigned char)(bitlen>>8); + mac.c[1] = (unsigned char)(bitlen >> 16); + mac.c[2] = (unsigned char)(bitlen >> 8); mac.c[3] = (unsigned char)bitlen; bitlen = mac.u[0]; #endif - pmac->u[0]=0; - pmac->u[1]=0; - pmac->u[2]=0; - pmac->u[3]=0; - pmac->u[4]=0; + pmac->u[0] = 0; + pmac->u[1] = 0; + pmac->u[2] = 0; + pmac->u[3] = 0; + pmac->u[4] = 0; - for (res=key->md.num, j=0;jmd.num, j = 0; j < len; j++) { size_t c = out[j]; - mask = (j-inp_len)>>(sizeof(j)*8-8); + mask = (j - inp_len) >> (sizeof(j) * 8 - 8); c &= mask; - c |= 0x80&~mask&~((inp_len-j)>>(sizeof(j)*8-8)); - data->c[res++]=(unsigned char)c; + c |= 0x80 & ~mask & + ~((inp_len - j) >> (sizeof(j) * 8 - 8)); + data->c[res++] = (unsigned char)c; - if (res!=SHA_CBLOCK) continue; + if (res != SHA_CBLOCK) + continue; /* j is not incremented yet */ - mask = 0-((inp_len+7-j)>>(sizeof(j)*8-1)); - data->u[SHA_LBLOCK-1] |= bitlen&mask; - sha1_block_data_order(&key->md,data,1); - mask &= 0-((j-inp_len-72)>>(sizeof(j)*8-1)); + mask = 0 - ((inp_len + 7 - j) >> + (sizeof(j) * 8 - 1)); + data->u[SHA_LBLOCK - 1] |= bitlen&mask; + sha1_block_data_order(&key->md, data, 1); + mask &= 0 - ((j - inp_len - 72) >> + (sizeof(j) * 8 - 1)); pmac->u[0] |= key->md.h0 & mask; pmac->u[1] |= key->md.h1 & mask; pmac->u[2] |= key->md.h2 & mask; pmac->u[3] |= key->md.h3 & mask; pmac->u[4] |= key->md.h4 & mask; - res=0; + res = 0; } - for(i=res;ic[i]=0; + for (i = res; i < SHA_CBLOCK; i++, j++) + data->c[i] = 0; - if (res>SHA_CBLOCK-8) { - mask = 0-((inp_len+8-j)>>(sizeof(j)*8-1)); - data->u[SHA_LBLOCK-1] |= bitlen&mask; - sha1_block_data_order(&key->md,data,1); - mask &= 0-((j-inp_len-73)>>(sizeof(j)*8-1)); + if (res > SHA_CBLOCK - 8) { + mask = 0 - ((inp_len + 8 - j) >> + (sizeof(j) * 8 - 1)); + data->u[SHA_LBLOCK - 1] |= bitlen & mask; + sha1_block_data_order(&key->md, data, 1); + mask &= 0 - ((j - inp_len - 73) >> + (sizeof(j) * 8 - 1)); pmac->u[0] |= key->md.h0 & mask; pmac->u[1] |= key->md.h1 & mask; pmac->u[2] |= key->md.h2 & mask; pmac->u[3] |= key->md.h3 & mask; pmac->u[4] |= key->md.h4 & mask; - memset(data,0,SHA_CBLOCK); - j+=64; + memset(data, 0, SHA_CBLOCK); + j += 64; } - data->u[SHA_LBLOCK-1] = bitlen; - sha1_block_data_order(&key->md,data,1); - mask = 0-((j-inp_len-73)>>(sizeof(j)*8-1)); + data->u[SHA_LBLOCK - 1] = bitlen; + sha1_block_data_order(&key->md, data, 1); + mask = 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1)); pmac->u[0] |= key->md.h0 & mask; pmac->u[1] |= key->md.h1 & mask; pmac->u[2] |= key->md.h2 & mask; @@ -373,209 +389,218 @@ static int aesni_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, pmac->u[3] = BSWAP(pmac->u[3]); pmac->u[4] = BSWAP(pmac->u[4]); #else - for (i=0;i<5;i++) { + for (i = 0; i < 5; i++) { res = pmac->u[i]; - pmac->c[4*i+0]=(unsigned char)(res>>24); - pmac->c[4*i+1]=(unsigned char)(res>>16); - pmac->c[4*i+2]=(unsigned char)(res>>8); - pmac->c[4*i+3]=(unsigned char)res; + pmac->c[4 * i + 0] = (unsigned char)(res >> 24); + pmac->c[4 * i + 1] = (unsigned char)(res >> 16); + pmac->c[4 * i + 2] = (unsigned char)(res >> 8); + pmac->c[4 * i + 3] = (unsigned char)res; } #endif len += SHA_DIGEST_LENGTH; #else - SHA1_Update(&key->md,out,inp_len); + SHA1_Update(&key->md, out, inp_len); res = key->md.num; - SHA1_Final(pmac->c,&key->md); + SHA1_Final(pmac->c, &key->md); { - unsigned int inp_blocks, pad_blocks; - - /* but pretend as if we hashed padded payload */ - inp_blocks = 1+((SHA_CBLOCK-9-res)>>(sizeof(res)*8-1)); - res += (unsigned int)(len-inp_len); - pad_blocks = res / SHA_CBLOCK; - res %= SHA_CBLOCK; - pad_blocks += 1+((SHA_CBLOCK-9-res)>>(sizeof(res)*8-1)); - for (;inp_blocksmd,data,1); + unsigned int inp_blocks, pad_blocks; + + /* but pretend as if we hashed padded payload */ + inp_blocks = 1 + ((SHA_CBLOCK - 9 - res) >> + (sizeof(res) * 8 - 1)); + res += (unsigned int)(len - inp_len); + pad_blocks = res / SHA_CBLOCK; + res %= SHA_CBLOCK; + pad_blocks += 1 + ((SHA_CBLOCK - 9 - res) >> + (sizeof(res) * 8 - 1)); + for (; inp_blocks < pad_blocks; inp_blocks++) + sha1_block_data_order(&key->md, + data, 1); } #endif key->md = key->tail; - SHA1_Update(&key->md,pmac->c,SHA_DIGEST_LENGTH); - SHA1_Final(pmac->c,&key->md); + SHA1_Update(&key->md, pmac->c, SHA_DIGEST_LENGTH); + SHA1_Final(pmac->c, &key->md); /* verify HMAC */ out += inp_len; len -= inp_len; #if 1 { - unsigned char *p = out+len-1-maxpad-SHA_DIGEST_LENGTH; - size_t off = out-p; - unsigned int c, cmask; - - maxpad += SHA_DIGEST_LENGTH; - for (res=0,i=0,j=0;j>(sizeof(int)*8-1); - res |= (c^pad)&~cmask; /* ... and padding */ - cmask &= ((int)(off-1-j))>>(sizeof(int)*8-1); - res |= (c^pmac->c[i])&cmask; - i += 1&cmask; - } - maxpad -= SHA_DIGEST_LENGTH; - - res = 0-((0-res)>>(sizeof(res)*8-1)); - ret &= (int)~res; + unsigned char *p = + out + len - 1 - maxpad - SHA_DIGEST_LENGTH; + size_t off = out - p; + unsigned int c, cmask; + + maxpad += SHA_DIGEST_LENGTH; + for (res = 0, i = 0, j = 0; j < maxpad; j++) { + c = p[j]; + cmask = ((int)(j - off - + SHA_DIGEST_LENGTH)) >> + (sizeof(int) * 8 - 1); + res |= (c ^ pad) & ~cmask; /* ... and padding */ + cmask &= ((int)(off - 1 - j)) >> + (sizeof(int) * 8 - 1); + res |= (c ^ pmac->c[i]) & cmask; + i += 1 & cmask; + } + maxpad -= SHA_DIGEST_LENGTH; + + res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1)); + ret &= (int)~res; } #else - for (res=0,i=0;ic[i]; - res = 0-((0-res)>>(sizeof(res)*8-1)); + for (res = 0, i = 0; i < SHA_DIGEST_LENGTH; i++) + res |= out[i] ^ pmac->c[i]; + res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1)); ret &= (int)~res; /* verify padding */ - pad = (pad&~res) | (maxpad&res); - out = out+len-1-pad; - for (res=0,i=0;i>(sizeof(res)*8-1); + res = (0 - res) >> (sizeof(res) * 8 - 1); ret &= (int)~res; #endif return ret; } else { - SHA1_Update(&key->md,out,len); + SHA1_Update(&key->md, out, len); } } return 1; - } +} -static int aesni_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) - { +static int +aesni_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) +{ EVP_AES_HMAC_SHA1 *key = data(ctx); - switch (type) - { + switch (type) { case EVP_CTRL_AEAD_SET_MAC_KEY: { - unsigned int i; - unsigned char hmac_key[64]; + unsigned int i; + unsigned char hmac_key[64]; - memset (hmac_key,0,sizeof(hmac_key)); + memset (hmac_key, 0, sizeof(hmac_key)); - if (arg > (int)sizeof(hmac_key)) { - SHA1_Init(&key->head); - SHA1_Update(&key->head,ptr,arg); - SHA1_Final(hmac_key,&key->head); - } else { - memcpy(hmac_key,ptr,arg); - } + if (arg > (int)sizeof(hmac_key)) { + SHA1_Init(&key->head); + SHA1_Update(&key->head, ptr, arg); + SHA1_Final(hmac_key, &key->head); + } else { + memcpy(hmac_key, ptr, arg); + } - for (i=0;ihead); - SHA1_Update(&key->head,hmac_key,sizeof(hmac_key)); + for (i = 0; i < sizeof(hmac_key); i++) + hmac_key[i] ^= 0x36; /* ipad */ + SHA1_Init(&key->head); + SHA1_Update(&key->head, hmac_key, sizeof(hmac_key)); - for (i=0;itail); - SHA1_Update(&key->tail,hmac_key,sizeof(hmac_key)); + for (i = 0; i < sizeof(hmac_key); i++) + hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */ + SHA1_Init(&key->tail); + SHA1_Update(&key->tail, hmac_key, sizeof(hmac_key)); - OPENSSL_cleanse(hmac_key,sizeof(hmac_key)); + OPENSSL_cleanse(hmac_key, sizeof(hmac_key)); - return 1; + return 1; } case EVP_CTRL_AEAD_TLS1_AAD: { - unsigned char *p=ptr; - unsigned int len=p[arg-2]<<8|p[arg-1]; - - if (ctx->encrypt) - { - key->payload_length = len; - if ((key->aux.tls_ver=p[arg-4]<<8|p[arg-3]) >= TLS1_1_VERSION) { - len -= AES_BLOCK_SIZE; - p[arg-2] = len>>8; - p[arg-1] = len; - } - key->md = key->head; - SHA1_Update(&key->md,p,arg); - - return (int)(((len+SHA_DIGEST_LENGTH+AES_BLOCK_SIZE)&-AES_BLOCK_SIZE) - - len); - } - else - { - if (arg>13) arg = 13; - memcpy(key->aux.tls_aad,ptr,arg); - key->payload_length = arg; - - return SHA_DIGEST_LENGTH; + unsigned char *p = ptr; + unsigned int len = p[arg - 2] << 8 | p[arg - 1]; + + if (ctx->encrypt) { + key->payload_length = len; + if ((key->aux.tls_ver = p[arg - 4] << 8 | + p[arg - 3]) >= TLS1_1_VERSION) { + len -= AES_BLOCK_SIZE; + p[arg - 2] = len >> 8; + p[arg - 1] = len; + } + key->md = key->head; + SHA1_Update(&key->md, p, arg); + + return (int)(((len + SHA_DIGEST_LENGTH + + AES_BLOCK_SIZE) & -AES_BLOCK_SIZE) - len); + } else { + if (arg > 13) + arg = 13; + memcpy(key->aux.tls_aad, ptr, arg); + key->payload_length = arg; + + return SHA_DIGEST_LENGTH; } } default: return -1; - } } +} -static EVP_CIPHER aesni_128_cbc_hmac_sha1_cipher = - { +static EVP_CIPHER aesni_128_cbc_hmac_sha1_cipher = { #ifdef NID_aes_128_cbc_hmac_sha1 NID_aes_128_cbc_hmac_sha1, #else NID_undef, #endif - 16,16,16, + 16, 16, 16, EVP_CIPH_CBC_MODE|EVP_CIPH_FLAG_DEFAULT_ASN1|EVP_CIPH_FLAG_AEAD_CIPHER, aesni_cbc_hmac_sha1_init_key, aesni_cbc_hmac_sha1_cipher, NULL, sizeof(EVP_AES_HMAC_SHA1), - EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_set_asn1_iv, - EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_get_asn1_iv, + EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv, + EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv, aesni_cbc_hmac_sha1_ctrl, NULL - }; +}; -static EVP_CIPHER aesni_256_cbc_hmac_sha1_cipher = - { +static EVP_CIPHER aesni_256_cbc_hmac_sha1_cipher = { #ifdef NID_aes_256_cbc_hmac_sha1 NID_aes_256_cbc_hmac_sha1, #else NID_undef, #endif - 16,32,16, + 16, 32, 16, EVP_CIPH_CBC_MODE|EVP_CIPH_FLAG_DEFAULT_ASN1|EVP_CIPH_FLAG_AEAD_CIPHER, aesni_cbc_hmac_sha1_init_key, aesni_cbc_hmac_sha1_cipher, NULL, sizeof(EVP_AES_HMAC_SHA1), - EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_set_asn1_iv, - EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_get_asn1_iv, + EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv, + EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv, aesni_cbc_hmac_sha1_ctrl, NULL - }; +}; -const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha1(void) - { - return(OPENSSL_ia32cap_P[1]&AESNI_CAPABLE? - &aesni_128_cbc_hmac_sha1_cipher:NULL); - } +const EVP_CIPHER * +EVP_aes_128_cbc_hmac_sha1(void) +{ + return(OPENSSL_ia32cap_P[1] & AESNI_CAPABLE? + &aesni_128_cbc_hmac_sha1_cipher : NULL); +} -const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha1(void) - { - return(OPENSSL_ia32cap_P[1]&AESNI_CAPABLE? - &aesni_256_cbc_hmac_sha1_cipher:NULL); - } +const EVP_CIPHER * +EVP_aes_256_cbc_hmac_sha1(void) +{ + return(OPENSSL_ia32cap_P[1] & AESNI_CAPABLE? + &aesni_256_cbc_hmac_sha1_cipher : NULL); +} #else -const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha1(void) - { - return NULL; - } -const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha1(void) - { +const EVP_CIPHER * +EVP_aes_128_cbc_hmac_sha1(void) +{ return NULL; - } +} + +const EVP_CIPHER * +EVP_aes_256_cbc_hmac_sha1(void) +{ + return NULL; +} #endif #endif diff --git a/lib/libcrypto/evp/e_bf.c b/lib/libcrypto/evp/e_bf.c index cc224e53639..62194767c87 100644 --- a/lib/libcrypto/evp/e_bf.c +++ b/lib/libcrypto/evp/e_bf.c @@ -5,21 +5,21 @@ * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. - * + * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). - * + * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -34,10 +34,10 @@ * Eric Young (eay@cryptsoft.com)" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). - * 4. If you include any Windows specific code (or a derivative thereof) from + * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" - * + * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -49,7 +49,7 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * + * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence @@ -65,24 +65,23 @@ #include static int bf_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc); + const unsigned char *iv, int enc); -typedef struct - { +typedef struct { BF_KEY ks; - } EVP_BF_KEY; +} EVP_BF_KEY; #define data(ctx) EVP_C_DATA(EVP_BF_KEY,ctx) IMPLEMENT_BLOCK_CIPHER(bf, ks, BF, EVP_BF_KEY, NID_bf, 8, 16, 8, 64, - EVP_CIPH_VARIABLE_LENGTH, bf_init_key, NULL, - EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv, NULL) - -static int bf_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { - BF_set_key(&data(ctx)->ks,EVP_CIPHER_CTX_key_length(ctx),key); - return 1; - } + EVP_CIPH_VARIABLE_LENGTH, bf_init_key, NULL, + EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv, NULL) +static int +bf_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ + BF_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx), key); + return 1; +} #endif diff --git a/lib/libcrypto/evp/e_camellia.c b/lib/libcrypto/evp/e_camellia.c index 8bb7c320d39..377d121b891 100644 --- a/lib/libcrypto/evp/e_camellia.c +++ b/lib/libcrypto/evp/e_camellia.c @@ -7,7 +7,7 @@ * are met: * * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. + * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in @@ -63,63 +63,61 @@ #include "evp_locl.h" static int camellia_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc); + const unsigned char *iv, int enc); /* Camellia subkey Structure */ -typedef struct - { +typedef struct { CAMELLIA_KEY ks; - } EVP_CAMELLIA_KEY; +} EVP_CAMELLIA_KEY; /* Attribute operation for Camellia */ #define data(ctx) EVP_C_DATA(EVP_CAMELLIA_KEY,ctx) IMPLEMENT_BLOCK_CIPHER(camellia_128, ks, Camellia, EVP_CAMELLIA_KEY, - NID_camellia_128, 16, 16, 16, 128, - 0, camellia_init_key, NULL, - EVP_CIPHER_set_asn1_iv, - EVP_CIPHER_get_asn1_iv, - NULL) + NID_camellia_128, 16, 16, 16, 128, + 0, camellia_init_key, NULL, + EVP_CIPHER_set_asn1_iv, + EVP_CIPHER_get_asn1_iv, + NULL) IMPLEMENT_BLOCK_CIPHER(camellia_192, ks, Camellia, EVP_CAMELLIA_KEY, - NID_camellia_192, 16, 24, 16, 128, - 0, camellia_init_key, NULL, - EVP_CIPHER_set_asn1_iv, - EVP_CIPHER_get_asn1_iv, - NULL) + NID_camellia_192, 16, 24, 16, 128, + 0, camellia_init_key, NULL, + EVP_CIPHER_set_asn1_iv, + EVP_CIPHER_get_asn1_iv, + NULL) IMPLEMENT_BLOCK_CIPHER(camellia_256, ks, Camellia, EVP_CAMELLIA_KEY, - NID_camellia_256, 16, 32, 16, 128, - 0, camellia_init_key, NULL, - EVP_CIPHER_set_asn1_iv, - EVP_CIPHER_get_asn1_iv, - NULL) + NID_camellia_256, 16, 32, 16, 128, + 0, camellia_init_key, NULL, + EVP_CIPHER_set_asn1_iv, + EVP_CIPHER_get_asn1_iv, + NULL) #define IMPLEMENT_CAMELLIA_CFBR(ksize,cbits) IMPLEMENT_CFBR(camellia,Camellia,EVP_CAMELLIA_KEY,ks,ksize,cbits,16) -IMPLEMENT_CAMELLIA_CFBR(128,1) -IMPLEMENT_CAMELLIA_CFBR(192,1) -IMPLEMENT_CAMELLIA_CFBR(256,1) +IMPLEMENT_CAMELLIA_CFBR(128, 1) +IMPLEMENT_CAMELLIA_CFBR(192, 1) +IMPLEMENT_CAMELLIA_CFBR(256, 1) -IMPLEMENT_CAMELLIA_CFBR(128,8) -IMPLEMENT_CAMELLIA_CFBR(192,8) -IMPLEMENT_CAMELLIA_CFBR(256,8) +IMPLEMENT_CAMELLIA_CFBR(128, 8) +IMPLEMENT_CAMELLIA_CFBR(192, 8) +IMPLEMENT_CAMELLIA_CFBR(256, 8) - -/* The subkey for Camellia is generated. */ -static int camellia_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { +/* The subkey for Camellia is generated. */ +static int +camellia_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ int ret; - ret=Camellia_set_key(key, ctx->key_len * 8, ctx->cipher_data); + ret = Camellia_set_key(key, ctx->key_len * 8, ctx->cipher_data); - if(ret < 0) - { - EVPerr(EVP_F_CAMELLIA_INIT_KEY,EVP_R_CAMELLIA_KEY_SETUP_FAILED); + if (ret < 0) { + EVPerr(EVP_F_CAMELLIA_INIT_KEY, + EVP_R_CAMELLIA_KEY_SETUP_FAILED); return 0; - } - - return 1; } + return 1; +} #endif diff --git a/lib/libcrypto/evp/e_cast.c b/lib/libcrypto/evp/e_cast.c index d77bcd9298f..199c5bf48e6 100644 --- a/lib/libcrypto/evp/e_cast.c +++ b/lib/libcrypto/evp/e_cast.c @@ -5,21 +5,21 @@ * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. - * + * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). - * + * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -34,10 +34,10 @@ * Eric Young (eay@cryptsoft.com)" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). - * 4. If you include any Windows specific code (or a derivative thereof) from + * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" - * + * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -49,7 +49,7 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * + * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence @@ -66,25 +66,24 @@ #include static int cast_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv,int enc); + const unsigned char *iv, int enc); -typedef struct - { +typedef struct { CAST_KEY ks; - } EVP_CAST_KEY; +} EVP_CAST_KEY; #define data(ctx) EVP_C_DATA(EVP_CAST_KEY,ctx) -IMPLEMENT_BLOCK_CIPHER(cast5, ks, CAST, EVP_CAST_KEY, - NID_cast5, 8, CAST_KEY_LENGTH, 8, 64, - EVP_CIPH_VARIABLE_LENGTH, cast_init_key, NULL, - EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv, NULL) - -static int cast_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { - CAST_set_key(&data(ctx)->ks,EVP_CIPHER_CTX_key_length(ctx),key); - return 1; - } +IMPLEMENT_BLOCK_CIPHER(cast5, ks, CAST, EVP_CAST_KEY, + NID_cast5, 8, CAST_KEY_LENGTH, 8, 64, + EVP_CIPH_VARIABLE_LENGTH, cast_init_key, NULL, + EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv, NULL) +static int +cast_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ + CAST_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx), key); + return 1; +} #endif diff --git a/lib/libcrypto/evp/e_chacha.c b/lib/libcrypto/evp/e_chacha.c index 4a20186006a..0c32b99df4e 100644 --- a/lib/libcrypto/evp/e_chacha.c +++ b/lib/libcrypto/evp/e_chacha.c @@ -41,7 +41,7 @@ static const EVP_CIPHER chacha20_cipher = { const EVP_CIPHER * EVP_chacha20(void) { - return(&chacha20_cipher); + return (&chacha20_cipher); } static int diff --git a/lib/libcrypto/evp/e_des.c b/lib/libcrypto/evp/e_des.c index ca009f2c525..ac46ba6a966 100644 --- a/lib/libcrypto/evp/e_des.c +++ b/lib/libcrypto/evp/e_des.c @@ -5,21 +5,21 @@ * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. - * + * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). - * + * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -34,10 +34,10 @@ * Eric Young (eay@cryptsoft.com)" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). - * 4. If you include any Windows specific code (or a derivative thereof) from + * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" - * + * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -49,7 +49,7 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * + * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence @@ -66,150 +66,155 @@ #include static int des_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc); + const unsigned char *iv, int enc); static int des_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr); /* Because of various casts and different names can't use IMPLEMENT_BLOCK_CIPHER */ -static int des_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) +static int +des_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) { BLOCK_CIPHER_ecb_loop() - DES_ecb_encrypt((DES_cblock *)(in + i), (DES_cblock *)(out + i), ctx->cipher_data, ctx->encrypt); + DES_ecb_encrypt((DES_cblock *)(in + i), (DES_cblock *)(out + i), + ctx->cipher_data, ctx->encrypt); return 1; } -static int des_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) +static int +des_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) { - while(inl>=EVP_MAXCHUNK) - { + while (inl >= EVP_MAXCHUNK) { DES_ofb64_encrypt(in, out, (long)EVP_MAXCHUNK, ctx->cipher_data, - (DES_cblock *)ctx->iv, &ctx->num); - inl-=EVP_MAXCHUNK; - in +=EVP_MAXCHUNK; - out+=EVP_MAXCHUNK; - } + (DES_cblock *)ctx->iv, &ctx->num); + inl -= EVP_MAXCHUNK; + in += EVP_MAXCHUNK; + out += EVP_MAXCHUNK; + } if (inl) DES_ofb64_encrypt(in, out, (long)inl, ctx->cipher_data, - (DES_cblock *)ctx->iv, &ctx->num); + (DES_cblock *)ctx->iv, &ctx->num); return 1; } -static int des_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) +static int +des_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) { - while(inl>=EVP_MAXCHUNK) - { + while (inl >= EVP_MAXCHUNK) { DES_ncbc_encrypt(in, out, (long)EVP_MAXCHUNK, ctx->cipher_data, - (DES_cblock *)ctx->iv, ctx->encrypt); - inl-=EVP_MAXCHUNK; - in +=EVP_MAXCHUNK; - out+=EVP_MAXCHUNK; - } + (DES_cblock *)ctx->iv, ctx->encrypt); + inl -= EVP_MAXCHUNK; + in += EVP_MAXCHUNK; + out += EVP_MAXCHUNK; + } if (inl) DES_ncbc_encrypt(in, out, (long)inl, ctx->cipher_data, - (DES_cblock *)ctx->iv, ctx->encrypt); + (DES_cblock *)ctx->iv, ctx->encrypt); return 1; } -static int des_cfb64_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) +static int +des_cfb64_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) { - while(inl>=EVP_MAXCHUNK) - { - DES_cfb64_encrypt(in,out, (long)EVP_MAXCHUNK, ctx->cipher_data, - (DES_cblock *)ctx->iv, &ctx->num, ctx->encrypt); - inl-=EVP_MAXCHUNK; - in +=EVP_MAXCHUNK; - out+=EVP_MAXCHUNK; - } + while (inl >= EVP_MAXCHUNK) { + DES_cfb64_encrypt(in, out, (long)EVP_MAXCHUNK, ctx->cipher_data, + (DES_cblock *)ctx->iv, &ctx->num, ctx->encrypt); + inl -= EVP_MAXCHUNK; + in += EVP_MAXCHUNK; + out += EVP_MAXCHUNK; + } if (inl) DES_cfb64_encrypt(in, out, (long)inl, ctx->cipher_data, - (DES_cblock *)ctx->iv, &ctx->num, ctx->encrypt); + (DES_cblock *)ctx->iv, &ctx->num, ctx->encrypt); return 1; } /* Although we have a CFB-r implementation for DES, it doesn't pack the right way, so wrap it here */ -static int des_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) - { - size_t n,chunk=EVP_MAXCHUNK/8; - unsigned char c[1],d[1]; - - if (inl=chunk) - { - for(n=0 ; n < chunk*8; ++n) - { - c[0]=(in[n/8]&(1 << (7-n%8))) ? 0x80 : 0; - DES_cfb_encrypt(c,d,1,1,ctx->cipher_data,(DES_cblock *)ctx->iv, - ctx->encrypt); - out[n/8]=(out[n/8]&~(0x80 >> (unsigned int)(n%8))) | - ((d[0]&0x80) >> (unsigned int)(n%8)); - } - inl-=chunk; - in +=chunk; - out+=chunk; - if (inl= chunk) { + for (n = 0; n < chunk*8; ++n) { + c[0] = (in[n / 8] & (1 << (7 - n % 8))) ? 0x80 : 0; + DES_cfb_encrypt(c, d, 1, 1, ctx->cipher_data, + (DES_cblock *)ctx->iv, ctx->encrypt); + out[n / 8] = (out[n / 8] & + ~(0x80 >> (unsigned int)(n % 8))) | + ((d[0] & 0x80) >> (unsigned int)(n % 8)); + } + inl -= chunk; + in += chunk; + out += chunk; + if (inl < chunk) + chunk = inl; } - return 1; - } - -static int des_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) - { - while (inl>=EVP_MAXCHUNK) - { - DES_cfb_encrypt(in,out,8,(long)EVP_MAXCHUNK,ctx->cipher_data, - (DES_cblock *)ctx->iv,ctx->encrypt); - inl-=EVP_MAXCHUNK; - in +=EVP_MAXCHUNK; - out+=EVP_MAXCHUNK; + return 1; +} + +static int +des_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) +{ + while (inl >= EVP_MAXCHUNK) { + DES_cfb_encrypt(in, out, 8, (long)EVP_MAXCHUNK, + ctx->cipher_data, (DES_cblock *)ctx->iv, ctx->encrypt); + inl -= EVP_MAXCHUNK; + in += EVP_MAXCHUNK; + out += EVP_MAXCHUNK; } - if (inl) - DES_cfb_encrypt(in,out,8,(long)inl,ctx->cipher_data, - (DES_cblock *)ctx->iv,ctx->encrypt); - return 1; - } + if (inl) + DES_cfb_encrypt(in, out, 8,(long)inl, ctx->cipher_data, + (DES_cblock *)ctx->iv, ctx->encrypt); + return 1; +} BLOCK_CIPHER_defs(des, DES_key_schedule, NID_des, 8, 8, 8, 64, - EVP_CIPH_RAND_KEY, des_init_key, NULL, - EVP_CIPHER_set_asn1_iv, - EVP_CIPHER_get_asn1_iv, - des_ctrl) - -BLOCK_CIPHER_def_cfb(des,DES_key_schedule,NID_des,8,8,1, - EVP_CIPH_RAND_KEY, des_init_key,NULL, - EVP_CIPHER_set_asn1_iv, - EVP_CIPHER_get_asn1_iv,des_ctrl) - -BLOCK_CIPHER_def_cfb(des,DES_key_schedule,NID_des,8,8,8, - EVP_CIPH_RAND_KEY,des_init_key,NULL, - EVP_CIPHER_set_asn1_iv, - EVP_CIPHER_get_asn1_iv,des_ctrl) - -static int des_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { + EVP_CIPH_RAND_KEY, des_init_key, NULL, + EVP_CIPHER_set_asn1_iv, + EVP_CIPHER_get_asn1_iv, + des_ctrl) + +BLOCK_CIPHER_def_cfb(des, DES_key_schedule, NID_des, 8,8, 1, + EVP_CIPH_RAND_KEY, des_init_key, NULL, + EVP_CIPHER_set_asn1_iv, + EVP_CIPHER_get_asn1_iv, des_ctrl) + +BLOCK_CIPHER_def_cfb(des, DES_key_schedule, NID_des, 8,8, 8, + EVP_CIPH_RAND_KEY, des_init_key, NULL, + EVP_CIPHER_set_asn1_iv, + EVP_CIPHER_get_asn1_iv, des_ctrl) + +static int +des_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ DES_cblock *deskey = (DES_cblock *)key; + #ifdef EVP_CHECK_DES_KEY - if(DES_set_key_checked(deskey,ctx->cipher_data) != 0) + if (DES_set_key_checked(deskey, ctx->cipher_data) != 0) return 0; #else - DES_set_key_unchecked(deskey,ctx->cipher_data); + DES_set_key_unchecked(deskey, ctx->cipher_data); #endif return 1; - } +} -static int des_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) - { - - switch(type) - { +static int +des_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) +{ + switch (type) { case EVP_CTRL_RAND_KEY: if (RAND_bytes(ptr, 8) <= 0) return 0; @@ -218,7 +223,7 @@ static int des_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) default: return -1; - } } +} #endif diff --git a/lib/libcrypto/evp/e_des3.c b/lib/libcrypto/evp/e_des3.c index 8d7b7de292b..ddb069dda57 100644 --- a/lib/libcrypto/evp/e_des3.c +++ b/lib/libcrypto/evp/e_des3.c @@ -5,21 +5,21 @@ * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. - * + * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). - * + * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -34,10 +34,10 @@ * Eric Young (eay@cryptsoft.com)" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). - * 4. If you include any Windows specific code (or a derivative thereof) from + * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" - * + * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -49,7 +49,7 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * + * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence @@ -68,150 +68,147 @@ #ifndef OPENSSL_FIPS static int des_ede_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv,int enc); + const unsigned char *iv, int enc); static int des_ede3_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv,int enc); + const unsigned char *iv, int enc); static int des3_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr); -typedef struct - { +typedef struct { DES_key_schedule ks1;/* key schedule */ DES_key_schedule ks2;/* key schedule (for ede) */ DES_key_schedule ks3;/* key schedule (for ede3) */ - } DES_EDE_KEY; +} DES_EDE_KEY; #define data(ctx) ((DES_EDE_KEY *)(ctx)->cipher_data) /* Because of various casts and different args can't use IMPLEMENT_BLOCK_CIPHER */ -static int des_ede_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) +static int +des_ede_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) { BLOCK_CIPHER_ecb_loop() - DES_ecb3_encrypt((const_DES_cblock *)(in + i), - (DES_cblock *)(out + i), - &data(ctx)->ks1, &data(ctx)->ks2, - &data(ctx)->ks3, - ctx->encrypt); + DES_ecb3_encrypt((const_DES_cblock *)(in + i), (DES_cblock *)(out + i), + &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, ctx->encrypt); return 1; } -static int des_ede_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) +static int +des_ede_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) { - while (inl>=EVP_MAXCHUNK) - { + while (inl >= EVP_MAXCHUNK) { DES_ede3_ofb64_encrypt(in, out, (long)EVP_MAXCHUNK, - &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, - (DES_cblock *)ctx->iv, &ctx->num); - inl-=EVP_MAXCHUNK; - in +=EVP_MAXCHUNK; - out+=EVP_MAXCHUNK; - } + &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, + (DES_cblock *)ctx->iv, &ctx->num); + inl -= EVP_MAXCHUNK; + in += EVP_MAXCHUNK; + out += EVP_MAXCHUNK; + } if (inl) DES_ede3_ofb64_encrypt(in, out, (long)inl, - &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, - (DES_cblock *)ctx->iv, &ctx->num); + &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, + (DES_cblock *)ctx->iv, &ctx->num); return 1; } -static int des_ede_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) +static int +des_ede_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) { #ifdef KSSL_DEBUG { - int i; - char *cp; - printf("des_ede_cbc_cipher(ctx=%lx, buflen=%d)\n", ctx, ctx->buf_len); - printf("\t iv= "); - for(i=0;i<8;i++) - printf("%02X",ctx->iv[i]); - printf("\n"); + int i; + char *cp; + printf("des_ede_cbc_cipher(ctx=%lx, buflen=%d)\n", ctx, ctx->buf_len); + printf("\t iv= "); + for (i = 0; i < 8; i++) + printf("%02X",ctx->iv[i]); + printf("\n"); } #endif /* KSSL_DEBUG */ - while (inl>=EVP_MAXCHUNK) - { + while (inl >= EVP_MAXCHUNK) { DES_ede3_cbc_encrypt(in, out, (long)EVP_MAXCHUNK, - &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, - (DES_cblock *)ctx->iv, ctx->encrypt); - inl-=EVP_MAXCHUNK; - in +=EVP_MAXCHUNK; - out+=EVP_MAXCHUNK; - } + &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, + (DES_cblock *)ctx->iv, ctx->encrypt); + inl -= EVP_MAXCHUNK; + in += EVP_MAXCHUNK; + out += EVP_MAXCHUNK; + } if (inl) DES_ede3_cbc_encrypt(in, out, (long)inl, - &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, - (DES_cblock *)ctx->iv, ctx->encrypt); + &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, + (DES_cblock *)ctx->iv, ctx->encrypt); return 1; } -static int des_ede_cfb64_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) +static int +des_ede_cfb64_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) { - while (inl>=EVP_MAXCHUNK) - { - DES_ede3_cfb64_encrypt(in, out, (long)EVP_MAXCHUNK, - &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, - (DES_cblock *)ctx->iv, &ctx->num, ctx->encrypt); - inl-=EVP_MAXCHUNK; - in +=EVP_MAXCHUNK; - out+=EVP_MAXCHUNK; - } + while (inl >= EVP_MAXCHUNK) { + DES_ede3_cfb64_encrypt(in, out, (long)EVP_MAXCHUNK, + &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, + (DES_cblock *)ctx->iv, &ctx->num, ctx->encrypt); + inl -= EVP_MAXCHUNK; + in += EVP_MAXCHUNK; + out += EVP_MAXCHUNK; + } if (inl) DES_ede3_cfb64_encrypt(in, out, (long)inl, - &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, - (DES_cblock *)ctx->iv, &ctx->num, ctx->encrypt); + &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, + (DES_cblock *)ctx->iv, &ctx->num, ctx->encrypt); return 1; } /* Although we have a CFB-r implementation for 3-DES, it doesn't pack the right way, so wrap it here */ -static int des_ede3_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) - { - size_t n; - unsigned char c[1],d[1]; +static int +des_ede3_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) +{ + size_t n; + unsigned char c[1], d[1]; - for(n=0 ; n < inl ; ++n) - { - c[0]=(in[n/8]&(1 << (7-n%8))) ? 0x80 : 0; - DES_ede3_cfb_encrypt(c,d,1,1, - &data(ctx)->ks1,&data(ctx)->ks2,&data(ctx)->ks3, - (DES_cblock *)ctx->iv,ctx->encrypt); - out[n/8]=(out[n/8]&~(0x80 >> (unsigned int)(n%8))) | - ((d[0]&0x80) >> (unsigned int)(n%8)); + for (n = 0; n < inl; ++n) { + c[0] = (in[n/8]&(1 << (7 - n % 8))) ? 0x80 : 0; + DES_ede3_cfb_encrypt(c, d, 1, 1, + &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, + (DES_cblock *)ctx->iv, ctx->encrypt); + out[n / 8] = (out[n / 8] & ~(0x80 >> (unsigned int)(n % 8))) | + ((d[0] & 0x80) >> (unsigned int)(n % 8)); } - return 1; - } + return 1; +} -static int des_ede3_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) - { - while (inl>=EVP_MAXCHUNK) - { - DES_ede3_cfb_encrypt(in,out,8,(long)EVP_MAXCHUNK, - &data(ctx)->ks1,&data(ctx)->ks2,&data(ctx)->ks3, - (DES_cblock *)ctx->iv,ctx->encrypt); - inl-=EVP_MAXCHUNK; - in +=EVP_MAXCHUNK; - out+=EVP_MAXCHUNK; +static int +des_ede3_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) +{ + while (inl >= EVP_MAXCHUNK) { + DES_ede3_cfb_encrypt(in, out, 8, (long)EVP_MAXCHUNK, + &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, + (DES_cblock *)ctx->iv, ctx->encrypt); + inl -= EVP_MAXCHUNK; + in += EVP_MAXCHUNK; + out += EVP_MAXCHUNK; } - if (inl) - DES_ede3_cfb_encrypt(in,out,8,(long)inl, - &data(ctx)->ks1,&data(ctx)->ks2,&data(ctx)->ks3, - (DES_cblock *)ctx->iv,ctx->encrypt); - return 1; - } + if (inl) + DES_ede3_cfb_encrypt(in, out, 8, (long)inl, + &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, + (DES_cblock *)ctx->iv, ctx->encrypt); + return 1; +} BLOCK_CIPHER_defs(des_ede, DES_EDE_KEY, NID_des_ede, 8, 16, 8, 64, - EVP_CIPH_RAND_KEY, des_ede_init_key, NULL, - EVP_CIPHER_set_asn1_iv, - EVP_CIPHER_get_asn1_iv, - des3_ctrl) + EVP_CIPH_RAND_KEY, des_ede_init_key, NULL, + EVP_CIPHER_set_asn1_iv, + EVP_CIPHER_get_asn1_iv, + des3_ctrl) #define des_ede3_cfb64_cipher des_ede_cfb64_cipher #define des_ede3_ofb_cipher des_ede_ofb_cipher @@ -219,75 +216,78 @@ BLOCK_CIPHER_defs(des_ede, DES_EDE_KEY, NID_des_ede, 8, 16, 8, 64, #define des_ede3_ecb_cipher des_ede_ecb_cipher BLOCK_CIPHER_defs(des_ede3, DES_EDE_KEY, NID_des_ede3, 8, 24, 8, 64, - EVP_CIPH_RAND_KEY, des_ede3_init_key, NULL, - EVP_CIPHER_set_asn1_iv, - EVP_CIPHER_get_asn1_iv, - des3_ctrl) - -BLOCK_CIPHER_def_cfb(des_ede3,DES_EDE_KEY,NID_des_ede3,24,8,1, - EVP_CIPH_RAND_KEY, des_ede3_init_key,NULL, - EVP_CIPHER_set_asn1_iv, - EVP_CIPHER_get_asn1_iv, - des3_ctrl) - -BLOCK_CIPHER_def_cfb(des_ede3,DES_EDE_KEY,NID_des_ede3,24,8,8, - EVP_CIPH_RAND_KEY, des_ede3_init_key,NULL, - EVP_CIPHER_set_asn1_iv, - EVP_CIPHER_get_asn1_iv, - des3_ctrl) + EVP_CIPH_RAND_KEY, des_ede3_init_key, NULL, + EVP_CIPHER_set_asn1_iv, + EVP_CIPHER_get_asn1_iv, + des3_ctrl) -static int des_ede_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { +BLOCK_CIPHER_def_cfb(des_ede3, DES_EDE_KEY, NID_des_ede3, 24, 8, 1, + EVP_CIPH_RAND_KEY, des_ede3_init_key, NULL, + EVP_CIPHER_set_asn1_iv, + EVP_CIPHER_get_asn1_iv, + des3_ctrl) + +BLOCK_CIPHER_def_cfb(des_ede3, DES_EDE_KEY, NID_des_ede3, 24, 8, 8, + EVP_CIPH_RAND_KEY, des_ede3_init_key, NULL, + EVP_CIPHER_set_asn1_iv, + EVP_CIPHER_get_asn1_iv, + des3_ctrl) + +static int +des_ede_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ DES_cblock *deskey = (DES_cblock *)key; + #ifdef EVP_CHECK_DES_KEY - if (DES_set_key_checked(&deskey[0],&data(ctx)->ks1) - !! DES_set_key_checked(&deskey[1],&data(ctx)->ks2)) + if (DES_set_key_checked(&deskey[0], &data(ctx)->ks1) + !! DES_set_key_checked(&deskey[1], &data(ctx)->ks2)) return 0; #else - DES_set_key_unchecked(&deskey[0],&data(ctx)->ks1); - DES_set_key_unchecked(&deskey[1],&data(ctx)->ks2); + DES_set_key_unchecked(&deskey[0], &data(ctx)->ks1); + DES_set_key_unchecked(&deskey[1], &data(ctx)->ks2); #endif - memcpy(&data(ctx)->ks3,&data(ctx)->ks1, - sizeof(data(ctx)->ks1)); + memcpy(&data(ctx)->ks3, &data(ctx)->ks1, + sizeof(data(ctx)->ks1)); return 1; - } +} -static int des_ede3_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { +static int +des_ede3_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ DES_cblock *deskey = (DES_cblock *)key; + #ifdef KSSL_DEBUG { - int i; - printf("des_ede3_init_key(ctx=%lx)\n", ctx); - printf("\tKEY= "); - for(i=0;i<24;i++) printf("%02X",key[i]); printf("\n"); - printf("\t IV= "); - for(i=0;i<8;i++) printf("%02X",iv[i]); printf("\n"); + int i; + printf("des_ede3_init_key(ctx=%lx)\n", ctx); + printf("\tKEY= "); + for(i=0;i<24;i++) printf("%02X",key[i]); printf("\n"); + printf("\t IV= "); + for(i=0;i<8;i++) printf("%02X",iv[i]); printf("\n"); } #endif /* KSSL_DEBUG */ #ifdef EVP_CHECK_DES_KEY - if (DES_set_key_checked(&deskey[0],&data(ctx)->ks1) - || DES_set_key_checked(&deskey[1],&data(ctx)->ks2) - || DES_set_key_checked(&deskey[2],&data(ctx)->ks3)) - return 0; + if (DES_set_key_checked(&deskey[0], &data(ctx)->ks1) || + DES_set_key_checked(&deskey[1], &data(ctx)->ks2) || + DES_set_key_checked(&deskey[2], &data(ctx)->ks3)) + return 0; #else - DES_set_key_unchecked(&deskey[0],&data(ctx)->ks1); - DES_set_key_unchecked(&deskey[1],&data(ctx)->ks2); - DES_set_key_unchecked(&deskey[2],&data(ctx)->ks3); + DES_set_key_unchecked(&deskey[0], &data(ctx)->ks1); + DES_set_key_unchecked(&deskey[1], &data(ctx)->ks2); + DES_set_key_unchecked(&deskey[2], &data(ctx)->ks3); #endif return 1; - } - -static int des3_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) - { +} +static int +des3_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) +{ DES_cblock *deskey = ptr; - switch(type) - { + switch (type) { case EVP_CTRL_RAND_KEY: if (RAND_bytes(ptr, c->key_len) <= 0) return 0; @@ -300,15 +300,17 @@ static int des3_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) default: return -1; - } } +} -const EVP_CIPHER *EVP_des_ede(void) +const EVP_CIPHER * +EVP_des_ede(void) { return &des_ede_ecb; } -const EVP_CIPHER *EVP_des_ede3(void) +const EVP_CIPHER * +EVP_des_ede3(void) { return &des_ede3_ecb; } diff --git a/lib/libcrypto/evp/e_idea.c b/lib/libcrypto/evp/e_idea.c index 806b0803600..8b8a647a8cc 100644 --- a/lib/libcrypto/evp/e_idea.c +++ b/lib/libcrypto/evp/e_idea.c @@ -5,21 +5,21 @@ * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. - * + * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). - * + * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -34,10 +34,10 @@ * Eric Young (eay@cryptsoft.com)" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). - * 4. If you include any Windows specific code (or a derivative thereof) from + * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" - * + * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -49,7 +49,7 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * + * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence @@ -66,53 +66,56 @@ #include static int idea_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv,int enc); + const unsigned char *iv, int enc); /* NB idea_ecb_encrypt doesn't take an 'encrypt' argument so we treat it as a special - * case + * case */ -static int idea_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) +static int +idea_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) { BLOCK_CIPHER_ecb_loop() - idea_ecb_encrypt(in + i, out + i, ctx->cipher_data); + idea_ecb_encrypt(in + i, out + i, ctx->cipher_data); return 1; } /* Can't use IMPLEMENT_BLOCK_CIPHER because idea_ecb_encrypt is different */ -typedef struct - { +typedef struct { IDEA_KEY_SCHEDULE ks; - } EVP_IDEA_KEY; +} EVP_IDEA_KEY; BLOCK_CIPHER_func_cbc(idea, idea, EVP_IDEA_KEY, ks) BLOCK_CIPHER_func_ofb(idea, idea, 64, EVP_IDEA_KEY, ks) BLOCK_CIPHER_func_cfb(idea, idea, 64, EVP_IDEA_KEY, ks) BLOCK_CIPHER_defs(idea, IDEA_KEY_SCHEDULE, NID_idea, 8, 16, 8, 64, - 0, idea_init_key, NULL, - EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv, NULL) + 0, idea_init_key, NULL, + EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv, NULL) -static int idea_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { - if(!enc) { - if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_OFB_MODE) enc = 1; - else if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_CFB_MODE) enc = 1; +static int +idea_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ + if (!enc) { + if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_OFB_MODE) + enc = 1; + else if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_CFB_MODE) + enc = 1; } - if (enc) idea_set_encrypt_key(key,ctx->cipher_data); - else - { + if (enc) + idea_set_encrypt_key(key, ctx->cipher_data); + else { IDEA_KEY_SCHEDULE tmp; - idea_set_encrypt_key(key,&tmp); - idea_set_decrypt_key(&tmp,ctx->cipher_data); + idea_set_encrypt_key(key, &tmp); + idea_set_decrypt_key(&tmp, ctx->cipher_data); OPENSSL_cleanse((unsigned char *)&tmp, - sizeof(IDEA_KEY_SCHEDULE)); - } - return 1; + sizeof(IDEA_KEY_SCHEDULE)); } + return 1; +} #endif diff --git a/lib/libcrypto/evp/e_null.c b/lib/libcrypto/evp/e_null.c index 98a78499f96..d94751a07a5 100644 --- a/lib/libcrypto/evp/e_null.c +++ b/lib/libcrypto/evp/e_null.c @@ -5,21 +5,21 @@ * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. - * + * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). - * + * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -34,10 +34,10 @@ * Eric Young (eay@cryptsoft.com)" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). - * 4. If you include any Windows specific code (or a derivative thereof) from + * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" - * + * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -49,7 +49,7 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * + * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence @@ -62,13 +62,13 @@ #include static int null_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv,int enc); + const unsigned char *iv, int enc); static int null_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl); -static const EVP_CIPHER n_cipher= - { + const unsigned char *in, size_t inl); + +static const EVP_CIPHER n_cipher = { NID_undef, - 1,0,0, + 1, 0, 0, 0, null_init_key, null_cipher, @@ -78,24 +78,27 @@ static const EVP_CIPHER n_cipher= NULL, NULL, NULL - }; +}; -const EVP_CIPHER *EVP_enc_null(void) - { - return(&n_cipher); - } +const EVP_CIPHER * +EVP_enc_null(void) +{ + return (&n_cipher); +} -static int null_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { +static int +null_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ /* memset(&(ctx->c),0,sizeof(ctx->c));*/ return 1; - } +} -static int null_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) - { +static int +null_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) +{ if (in != out) - memcpy((char *)out,(const char *)in,inl); + memcpy((char *)out, (const char *)in, inl); return 1; - } +} diff --git a/lib/libcrypto/evp/e_old.c b/lib/libcrypto/evp/e_old.c index 1642af4869d..c27b61a4bfc 100644 --- a/lib/libcrypto/evp/e_old.c +++ b/lib/libcrypto/evp/e_old.c @@ -10,7 +10,7 @@ * are met: * * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. + * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in @@ -71,55 +71,88 @@ static void *dummy = &dummy; #ifndef OPENSSL_NO_BF #undef EVP_bf_cfb const EVP_CIPHER *EVP_bf_cfb(void); -const EVP_CIPHER *EVP_bf_cfb(void) { return EVP_bf_cfb64(); } +const EVP_CIPHER *EVP_bf_cfb(void) +{ + return EVP_bf_cfb64(); +} #endif #ifndef OPENSSL_NO_DES #undef EVP_des_cfb const EVP_CIPHER *EVP_des_cfb(void); -const EVP_CIPHER *EVP_des_cfb(void) { return EVP_des_cfb64(); } +const EVP_CIPHER *EVP_des_cfb(void) +{ + return EVP_des_cfb64(); +} #undef EVP_des_ede3_cfb const EVP_CIPHER *EVP_des_ede3_cfb(void); -const EVP_CIPHER *EVP_des_ede3_cfb(void) { return EVP_des_ede3_cfb64(); } +const EVP_CIPHER *EVP_des_ede3_cfb(void) +{ + return EVP_des_ede3_cfb64(); +} #undef EVP_des_ede_cfb const EVP_CIPHER *EVP_des_ede_cfb(void); -const EVP_CIPHER *EVP_des_ede_cfb(void) { return EVP_des_ede_cfb64(); } +const EVP_CIPHER *EVP_des_ede_cfb(void) +{ + return EVP_des_ede_cfb64(); +} #endif #ifndef OPENSSL_NO_IDEA #undef EVP_idea_cfb const EVP_CIPHER *EVP_idea_cfb(void); -const EVP_CIPHER *EVP_idea_cfb(void) { return EVP_idea_cfb64(); } +const EVP_CIPHER *EVP_idea_cfb(void) +{ + return EVP_idea_cfb64(); +} #endif #ifndef OPENSSL_NO_RC2 #undef EVP_rc2_cfb const EVP_CIPHER *EVP_rc2_cfb(void); -const EVP_CIPHER *EVP_rc2_cfb(void) { return EVP_rc2_cfb64(); } +const EVP_CIPHER *EVP_rc2_cfb(void) +{ + return EVP_rc2_cfb64(); +} #endif #ifndef OPENSSL_NO_CAST #undef EVP_cast5_cfb const EVP_CIPHER *EVP_cast5_cfb(void); -const EVP_CIPHER *EVP_cast5_cfb(void) { return EVP_cast5_cfb64(); } +const EVP_CIPHER *EVP_cast5_cfb(void) +{ + return EVP_cast5_cfb64(); +} #endif #ifndef OPENSSL_NO_RC5 #undef EVP_rc5_32_12_16_cfb const EVP_CIPHER *EVP_rc5_32_12_16_cfb(void); -const EVP_CIPHER *EVP_rc5_32_12_16_cfb(void) { return EVP_rc5_32_12_16_cfb64(); } +const EVP_CIPHER *EVP_rc5_32_12_16_cfb(void) +{ + return EVP_rc5_32_12_16_cfb64(); +} #endif #ifndef OPENSSL_NO_AES #undef EVP_aes_128_cfb const EVP_CIPHER *EVP_aes_128_cfb(void); -const EVP_CIPHER *EVP_aes_128_cfb(void) { return EVP_aes_128_cfb128(); } +const EVP_CIPHER *EVP_aes_128_cfb(void) +{ + return EVP_aes_128_cfb128(); +} #undef EVP_aes_192_cfb const EVP_CIPHER *EVP_aes_192_cfb(void); -const EVP_CIPHER *EVP_aes_192_cfb(void) { return EVP_aes_192_cfb128(); } +const EVP_CIPHER *EVP_aes_192_cfb(void) +{ + return EVP_aes_192_cfb128(); +} #undef EVP_aes_256_cfb const EVP_CIPHER *EVP_aes_256_cfb(void); -const EVP_CIPHER *EVP_aes_256_cfb(void) { return EVP_aes_256_cfb128(); } +const EVP_CIPHER *EVP_aes_256_cfb(void) +{ + return EVP_aes_256_cfb128(); +} #endif #endif diff --git a/lib/libcrypto/evp/e_rc2.c b/lib/libcrypto/evp/e_rc2.c index d4c33b58d4d..f6f45048903 100644 --- a/lib/libcrypto/evp/e_rc2.c +++ b/lib/libcrypto/evp/e_rc2.c @@ -5,21 +5,21 @@ * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. - * + * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). - * + * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -34,10 +34,10 @@ * Eric Young (eay@cryptsoft.com)" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). - * 4. If you include any Windows specific code (or a derivative thereof) from + * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" - * + * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -49,7 +49,7 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * + * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence @@ -67,37 +67,35 @@ #include static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv,int enc); + const unsigned char *iv, int enc); static int rc2_meth_to_magic(EVP_CIPHER_CTX *ctx); static int rc2_magic_to_meth(int i); static int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type); static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type); static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr); -typedef struct - { +typedef struct { int key_bits; /* effective key bits */ RC2_KEY ks; /* key schedule */ - } EVP_RC2_KEY; +} EVP_RC2_KEY; #define data(ctx) ((EVP_RC2_KEY *)(ctx)->cipher_data) IMPLEMENT_BLOCK_CIPHER(rc2, ks, RC2, EVP_RC2_KEY, NID_rc2, - 8, - RC2_KEY_LENGTH, 8, 64, - EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT, - rc2_init_key, NULL, - rc2_set_asn1_type_and_iv, rc2_get_asn1_type_and_iv, - rc2_ctrl) + 8, + RC2_KEY_LENGTH, 8, 64, + EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT, + rc2_init_key, NULL, + rc2_set_asn1_type_and_iv, rc2_get_asn1_type_and_iv, + rc2_ctrl) #define RC2_40_MAGIC 0xa0 #define RC2_64_MAGIC 0x78 #define RC2_128_MAGIC 0x3a -static const EVP_CIPHER r2_64_cbc_cipher= - { +static const EVP_CIPHER r2_64_cbc_cipher = { NID_rc2_64_cbc, - 8,8 /* 64 bit */,8, + 8, 8 /* 64 bit */, 8, EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT, rc2_init_key, rc2_cbc_cipher, @@ -107,12 +105,11 @@ static const EVP_CIPHER r2_64_cbc_cipher= rc2_get_asn1_type_and_iv, rc2_ctrl, NULL - }; +}; -static const EVP_CIPHER r2_40_cbc_cipher= - { +static const EVP_CIPHER r2_40_cbc_cipher = { NID_rc2_40_cbc, - 8,5 /* 40 bit */,8, + 8, 5 /* 40 bit */, 8, EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT, rc2_init_key, rc2_cbc_cipher, @@ -122,93 +119,105 @@ static const EVP_CIPHER r2_40_cbc_cipher= rc2_get_asn1_type_and_iv, rc2_ctrl, NULL - }; - -const EVP_CIPHER *EVP_rc2_64_cbc(void) - { - return(&r2_64_cbc_cipher); - } - -const EVP_CIPHER *EVP_rc2_40_cbc(void) - { - return(&r2_40_cbc_cipher); - } - -static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { - RC2_set_key(&data(ctx)->ks,EVP_CIPHER_CTX_key_length(ctx), - key,data(ctx)->key_bits); +}; + +const EVP_CIPHER * +EVP_rc2_64_cbc(void) +{ + return (&r2_64_cbc_cipher); +} + +const EVP_CIPHER * +EVP_rc2_40_cbc(void) +{ + return (&r2_40_cbc_cipher); +} + +static int +rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ + RC2_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx), + key, data(ctx)->key_bits); return 1; - } +} -static int rc2_meth_to_magic(EVP_CIPHER_CTX *e) - { +static int +rc2_meth_to_magic(EVP_CIPHER_CTX *e) +{ int i; EVP_CIPHER_CTX_ctrl(e, EVP_CTRL_GET_RC2_KEY_BITS, 0, &i); - if (i == 128) return(RC2_128_MAGIC); - else if (i == 64) return(RC2_64_MAGIC); - else if (i == 40) return(RC2_40_MAGIC); - else return(0); - } - -static int rc2_magic_to_meth(int i) - { - if (i == RC2_128_MAGIC) return 128; - else if (i == RC2_64_MAGIC) return 64; - else if (i == RC2_40_MAGIC) return 40; + if (i == 128) + return (RC2_128_MAGIC); + else if (i == 64) + return (RC2_64_MAGIC); + else if (i == 40) + return (RC2_40_MAGIC); else - { - EVPerr(EVP_F_RC2_MAGIC_TO_METH,EVP_R_UNSUPPORTED_KEY_SIZE); - return(0); - } + return (0); +} + +static int +rc2_magic_to_meth(int i) +{ + if (i == RC2_128_MAGIC) + return 128; + else if (i == RC2_64_MAGIC) + return 64; + else if (i == RC2_40_MAGIC) + return 40; + else { + EVPerr(EVP_F_RC2_MAGIC_TO_METH, EVP_R_UNSUPPORTED_KEY_SIZE); + return (0); } +} -static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type) - { - long num=0; - int i=0; +static int +rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type) +{ + long num = 0; + int i = 0; int key_bits; unsigned int l; unsigned char iv[EVP_MAX_IV_LENGTH]; - if (type != NULL) - { - l=EVP_CIPHER_CTX_iv_length(c); + if (type != NULL) { + l = EVP_CIPHER_CTX_iv_length(c); OPENSSL_assert(l <= sizeof(iv)); - i=ASN1_TYPE_get_int_octetstring(type,&num,iv,l); + i = ASN1_TYPE_get_int_octetstring(type, &num, iv, l); if (i != (int)l) - return(-1); - key_bits =rc2_magic_to_meth((int)num); + return (-1); + key_bits = rc2_magic_to_meth((int)num); if (!key_bits) - return(-1); - if(i > 0 && !EVP_CipherInit_ex(c, NULL, NULL, NULL, iv, -1)) + return (-1); + if (i > 0 && !EVP_CipherInit_ex(c, NULL, NULL, NULL, iv, -1)) return -1; - EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_RC2_KEY_BITS, key_bits, NULL); + EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_RC2_KEY_BITS, + key_bits, NULL); EVP_CIPHER_CTX_set_key_length(c, key_bits / 8); - } - return(i); } + return (i); +} -static int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type) - { +static int +rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type) +{ long num; - int i=0,j; + int i = 0, j; - if (type != NULL) - { - num=rc2_meth_to_magic(c); - j=EVP_CIPHER_CTX_iv_length(c); - i=ASN1_TYPE_set_int_octetstring(type,num,c->oiv,j); - } - return(i); + if (type != NULL) { + num = rc2_meth_to_magic(c); + j = EVP_CIPHER_CTX_iv_length(c); + i = ASN1_TYPE_set_int_octetstring(type, num, c->oiv, j); } + return (i); +} -static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) - { - switch(type) - { +static int +rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) +{ + switch (type) { case EVP_CTRL_INIT: data(c)->key_bits = EVP_CIPHER_CTX_key_length(c) * 8; return 1; @@ -216,14 +225,14 @@ static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) case EVP_CTRL_GET_RC2_KEY_BITS: *(int *)ptr = data(c)->key_bits; return 1; - + case EVP_CTRL_SET_RC2_KEY_BITS: - if(arg > 0) - { + if (arg > 0) { data(c)->key_bits = arg; return 1; - } + } return 0; + #ifdef PBE_PRF_TEST case EVP_CTRL_PBE_PRF_NID: *(int *)ptr = NID_hmacWithMD5; @@ -232,7 +241,7 @@ static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) default: return -1; - } } +} #endif diff --git a/lib/libcrypto/evp/e_rc4.c b/lib/libcrypto/evp/e_rc4.c index b4f6bda82d4..f66885f70d3 100644 --- a/lib/libcrypto/evp/e_rc4.c +++ b/lib/libcrypto/evp/e_rc4.c @@ -5,21 +5,21 @@ * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. - * + * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). - * + * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -34,10 +34,10 @@ * Eric Young (eay@cryptsoft.com)" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). - * 4. If you include any Windows specific code (or a derivative thereof) from + * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" - * + * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -49,7 +49,7 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * + * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence @@ -69,21 +69,20 @@ /* FIXME: surely this is available elsewhere? */ #define EVP_RC4_KEY_SIZE 16 -typedef struct - { +typedef struct { RC4_KEY ks; /* working key */ - } EVP_RC4_KEY; +} EVP_RC4_KEY; #define data(ctx) ((EVP_RC4_KEY *)(ctx)->cipher_data) static int rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv,int enc); + const unsigned char *iv, int enc); static int rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl); -static const EVP_CIPHER r4_cipher= - { + const unsigned char *in, size_t inl); + +static const EVP_CIPHER r4_cipher = { NID_rc4, - 1,EVP_RC4_KEY_SIZE,0, + 1, EVP_RC4_KEY_SIZE, 0, EVP_CIPH_VARIABLE_LENGTH, rc4_init_key, rc4_cipher, @@ -93,45 +92,47 @@ static const EVP_CIPHER r4_cipher= NULL, NULL, NULL - }; +}; -static const EVP_CIPHER r4_40_cipher= - { +static const EVP_CIPHER r4_40_cipher = { NID_rc4_40, - 1,5 /* 40 bit */,0, + 1, 5 /* 40 bit */, 0, EVP_CIPH_VARIABLE_LENGTH, rc4_init_key, rc4_cipher, NULL, sizeof(EVP_RC4_KEY), - NULL, + NULL, NULL, NULL, NULL - }; +}; -const EVP_CIPHER *EVP_rc4(void) - { - return(&r4_cipher); - } +const EVP_CIPHER * +EVP_rc4(void) +{ + return (&r4_cipher); +} -const EVP_CIPHER *EVP_rc4_40(void) - { - return(&r4_40_cipher); - } +const EVP_CIPHER * +EVP_rc4_40(void) +{ + return (&r4_40_cipher); +} -static int rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { - RC4_set_key(&data(ctx)->ks,EVP_CIPHER_CTX_key_length(ctx), - key); +static int +rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ + RC4_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx), key); return 1; - } +} -static int rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) - { - RC4(&data(ctx)->ks,inl,in,out); +static int +rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) +{ + RC4(&data(ctx)->ks, inl, in, out); return 1; - } +} #endif diff --git a/lib/libcrypto/evp/e_rc4_hmac_md5.c b/lib/libcrypto/evp/e_rc4_hmac_md5.c index 56563191ba1..d4655c56d94 100644 --- a/lib/libcrypto/evp/e_rc4_hmac_md5.c +++ b/lib/libcrypto/evp/e_rc4_hmac_md5.c @@ -68,37 +68,35 @@ /* FIXME: surely this is available elsewhere? */ #define EVP_RC4_KEY_SIZE 16 -typedef struct - { - RC4_KEY ks; - MD5_CTX head,tail,md; - size_t payload_length; - } EVP_RC4_HMAC_MD5; +typedef struct { + RC4_KEY ks; + MD5_CTX head, tail, md; + size_t payload_length; +} EVP_RC4_HMAC_MD5; #define NO_PAYLOAD_LENGTH ((size_t)-1) void rc4_md5_enc (RC4_KEY *key, const void *in0, void *out, - MD5_CTX *ctx,const void *inp,size_t blocks); + MD5_CTX *ctx, const void *inp, size_t blocks); #define data(ctx) ((EVP_RC4_HMAC_MD5 *)(ctx)->cipher_data) -static int rc4_hmac_md5_init_key(EVP_CIPHER_CTX *ctx, - const unsigned char *inkey, - const unsigned char *iv, int enc) - { +static int +rc4_hmac_md5_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *inkey, + const unsigned char *iv, int enc) +{ EVP_RC4_HMAC_MD5 *key = data(ctx); - RC4_set_key(&key->ks,EVP_CIPHER_CTX_key_length(ctx), - inkey); + RC4_set_key(&key->ks, EVP_CIPHER_CTX_key_length(ctx), inkey); MD5_Init(&key->head); /* handy when benchmarking */ key->tail = key->head; - key->md = key->head; + key->md = key->head; key->payload_length = NO_PAYLOAD_LENGTH; return 1; - } +} #if !defined(OPENSSL_NO_ASM) && ( \ defined(__x86_64) || defined(__x86_64__) || \ @@ -113,173 +111,184 @@ static int rc4_hmac_md5_init_key(EVP_CIPHER_CTX *ctx, #define md5_off 0 #endif -static int rc4_hmac_md5_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t len) - { +static int +rc4_hmac_md5_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len) +{ EVP_RC4_HMAC_MD5 *key = data(ctx); #if defined(STITCHED_CALL) size_t rc4_off = 32-1-(key->ks.x&(32-1)), /* 32 is $MOD from rc4_md5-x86_64.pl */ - md5_off = MD5_CBLOCK-key->md.num, - blocks; + md5_off = MD5_CBLOCK - key->md.num, + blocks; unsigned int l; extern unsigned int OPENSSL_ia32cap_P[]; #endif size_t plen = key->payload_length; - if (plen!=NO_PAYLOAD_LENGTH && len!=(plen+MD5_DIGEST_LENGTH)) return 0; + if (plen != NO_PAYLOAD_LENGTH && len != (plen + MD5_DIGEST_LENGTH)) + return 0; if (ctx->encrypt) { - if (plen==NO_PAYLOAD_LENGTH) plen = len; + if (plen == NO_PAYLOAD_LENGTH) + plen = len; #if defined(STITCHED_CALL) /* cipher has to "fall behind" */ - if (rc4_off>md5_off) md5_off+=MD5_CBLOCK; + if (rc4_off > md5_off) + md5_off += MD5_CBLOCK; - if (plen>md5_off && (blocks=(plen-md5_off)/MD5_CBLOCK) && - (OPENSSL_ia32cap_P[0]&(1<<20))==0) { - MD5_Update(&key->md,in,md5_off); - RC4(&key->ks,rc4_off,in,out); + if (plen > md5_off && + (blocks = (plen - md5_off) / MD5_CBLOCK) && + (OPENSSL_ia32cap_P[0]&(1 << 20)) == 0) { + MD5_Update(&key->md, in, md5_off); + RC4(&key->ks, rc4_off, in, out); - rc4_md5_enc(&key->ks,in+rc4_off,out+rc4_off, - &key->md,in+md5_off,blocks); + rc4_md5_enc(&key->ks, in + rc4_off, out + rc4_off, + &key->md, in + md5_off, blocks); blocks *= MD5_CBLOCK; rc4_off += blocks; md5_off += blocks; - key->md.Nh += blocks>>29; - key->md.Nl += blocks<<=3; - if (key->md.Nl<(unsigned int)blocks) key->md.Nh++; + key->md.Nh += blocks >> 29; + key->md.Nl += blocks <<= 3; + if (key->md.Nl < (unsigned int)blocks) + key->md.Nh++; } else { rc4_off = 0; md5_off = 0; } #endif - MD5_Update(&key->md,in+md5_off,plen-md5_off); + MD5_Update(&key->md, in + md5_off, plen - md5_off); if (plen!=len) { /* "TLS" mode of operation */ - if (in!=out) - memcpy(out+rc4_off,in+rc4_off,plen-rc4_off); + if (in != out) + memcpy(out + rc4_off, in + rc4_off, + plen - rc4_off); /* calculate HMAC and append it to payload */ - MD5_Final(out+plen,&key->md); + MD5_Final(out + plen, &key->md); key->md = key->tail; - MD5_Update(&key->md,out+plen,MD5_DIGEST_LENGTH); - MD5_Final(out+plen,&key->md); + MD5_Update(&key->md, out + plen, MD5_DIGEST_LENGTH); + MD5_Final(out + plen, &key->md); + /* encrypt HMAC at once */ - RC4(&key->ks,len-rc4_off,out+rc4_off,out+rc4_off); + RC4(&key->ks, len - rc4_off, out + rc4_off, + out + rc4_off); } else { - RC4(&key->ks,len-rc4_off,in+rc4_off,out+rc4_off); + RC4(&key->ks, len - rc4_off, in + rc4_off, + out + rc4_off); } } else { unsigned char mac[MD5_DIGEST_LENGTH]; #if defined(STITCHED_CALL) /* digest has to "fall behind" */ - if (md5_off>rc4_off) rc4_off += 2*MD5_CBLOCK; - else rc4_off += MD5_CBLOCK; - - if (len>rc4_off && (blocks=(len-rc4_off)/MD5_CBLOCK) && - (OPENSSL_ia32cap_P[0]&(1<<20))==0) { - RC4(&key->ks,rc4_off,in,out); - MD5_Update(&key->md,out,md5_off); - - rc4_md5_enc(&key->ks,in+rc4_off,out+rc4_off, - &key->md,out+md5_off,blocks); + if (md5_off > rc4_off) + rc4_off += 2*MD5_CBLOCK; + else + rc4_off += MD5_CBLOCK; + + if (len > rc4_off && (blocks = (len - rc4_off) / MD5_CBLOCK) && + (OPENSSL_ia32cap_P[0] & (1 << 20)) == 0) { + RC4(&key->ks, rc4_off, in, out); + MD5_Update(&key->md, out, md5_off); + + rc4_md5_enc(&key->ks, in + rc4_off, out + rc4_off, + &key->md, out + md5_off, blocks); blocks *= MD5_CBLOCK; rc4_off += blocks; md5_off += blocks; - l = (key->md.Nl+(blocks<<3))&0xffffffffU; - if (lmd.Nl) key->md.Nh++; - key->md.Nl = l; - key->md.Nh += blocks>>29; + l = (key->md.Nl + (blocks << 3)) & 0xffffffffU; + if (l < key->md.Nl) + key->md.Nh++; + key->md.Nl = l; + key->md.Nh += blocks >> 29; } else { - md5_off=0; - rc4_off=0; + md5_off = 0; + rc4_off = 0; } #endif /* decrypt HMAC at once */ - RC4(&key->ks,len-rc4_off,in+rc4_off,out+rc4_off); + RC4(&key->ks, len - rc4_off, in + rc4_off, out + rc4_off); if (plen!=NO_PAYLOAD_LENGTH) { /* "TLS" mode of operation */ - MD5_Update(&key->md,out+md5_off,plen-md5_off); + MD5_Update(&key->md, out + md5_off, plen - md5_off); /* calculate HMAC and verify it */ - MD5_Final(mac,&key->md); + MD5_Final(mac, &key->md); key->md = key->tail; - MD5_Update(&key->md,mac,MD5_DIGEST_LENGTH); - MD5_Final(mac,&key->md); + MD5_Update(&key->md, mac, MD5_DIGEST_LENGTH); + MD5_Final(mac, &key->md); - if (memcmp(out+plen,mac,MD5_DIGEST_LENGTH)) + if (memcmp(out + plen, mac, MD5_DIGEST_LENGTH)) return 0; } else { - MD5_Update(&key->md,out+md5_off,len-md5_off); + MD5_Update(&key->md, out + md5_off, len - md5_off); } } key->payload_length = NO_PAYLOAD_LENGTH; return 1; - } +} -static int rc4_hmac_md5_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) - { +static int +rc4_hmac_md5_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) +{ EVP_RC4_HMAC_MD5 *key = data(ctx); - switch (type) - { + switch (type) { case EVP_CTRL_AEAD_SET_MAC_KEY: { - unsigned int i; - unsigned char hmac_key[64]; + unsigned int i; + unsigned char hmac_key[64]; - memset (hmac_key,0,sizeof(hmac_key)); + memset (hmac_key, 0, sizeof(hmac_key)); - if (arg > (int)sizeof(hmac_key)) { - MD5_Init(&key->head); - MD5_Update(&key->head,ptr,arg); - MD5_Final(hmac_key,&key->head); - } else { - memcpy(hmac_key,ptr,arg); - } + if (arg > (int)sizeof(hmac_key)) { + MD5_Init(&key->head); + MD5_Update(&key->head, ptr, arg); + MD5_Final(hmac_key, &key->head); + } else { + memcpy(hmac_key, ptr, arg); + } - for (i=0;ihead); - MD5_Update(&key->head,hmac_key,sizeof(hmac_key)); + for (i = 0; i < sizeof(hmac_key); i++) + hmac_key[i] ^= 0x36; /* ipad */ + MD5_Init(&key->head); + MD5_Update(&key->head, hmac_key, sizeof(hmac_key)); - for (i=0;itail); - MD5_Update(&key->tail,hmac_key,sizeof(hmac_key)); + for (i = 0; i < sizeof(hmac_key); i++) + hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */ + MD5_Init(&key->tail); + MD5_Update(&key->tail, hmac_key, sizeof(hmac_key)); - return 1; + return 1; } case EVP_CTRL_AEAD_TLS1_AAD: { - unsigned char *p=ptr; - unsigned int len=p[arg-2]<<8|p[arg-1]; - - if (!ctx->encrypt) - { - len -= MD5_DIGEST_LENGTH; - p[arg-2] = len>>8; - p[arg-1] = len; + unsigned char *p = ptr; + unsigned int len = p[arg - 2] << 8 | p[arg - 1]; + + if (!ctx->encrypt) { + len -= MD5_DIGEST_LENGTH; + p[arg - 2] = len >> 8; + p[arg - 1] = len; } - key->payload_length=len; - key->md = key->head; - MD5_Update(&key->md,p,arg); + key->payload_length = len; + key->md = key->head; + MD5_Update(&key->md, p, arg); - return MD5_DIGEST_LENGTH; + return MD5_DIGEST_LENGTH; } default: return -1; - } } +} -static EVP_CIPHER r4_hmac_md5_cipher= - { +static EVP_CIPHER r4_hmac_md5_cipher = { #ifdef NID_rc4_hmac_md5 NID_rc4_hmac_md5, #else NID_undef, #endif - 1,EVP_RC4_KEY_SIZE,0, + 1, EVP_RC4_KEY_SIZE, 0, EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH|EVP_CIPH_FLAG_AEAD_CIPHER, rc4_hmac_md5_init_key, rc4_hmac_md5_cipher, @@ -289,10 +298,11 @@ static EVP_CIPHER r4_hmac_md5_cipher= NULL, rc4_hmac_md5_ctrl, NULL - }; +}; -const EVP_CIPHER *EVP_rc4_hmac_md5(void) - { - return(&r4_hmac_md5_cipher); - } +const EVP_CIPHER * +EVP_rc4_hmac_md5(void) +{ + return (&r4_hmac_md5_cipher); +} #endif diff --git a/lib/libcrypto/evp/e_rc5.c b/lib/libcrypto/evp/e_rc5.c index 19a10c64024..efbd03735e6 100644 --- a/lib/libcrypto/evp/e_rc5.c +++ b/lib/libcrypto/evp/e_rc5.c @@ -5,21 +5,21 @@ * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. - * + * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). - * + * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -34,10 +34,10 @@ * Eric Young (eay@cryptsoft.com)" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). - * 4. If you include any Windows specific code (or a derivative thereof) from + * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" - * + * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -49,7 +49,7 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * + * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence @@ -67,27 +67,26 @@ #include static int r_32_12_16_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv,int enc); + const unsigned char *iv, int enc); static int rc5_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr); -typedef struct - { +typedef struct { int rounds; /* number of rounds */ RC5_32_KEY ks; /* key schedule */ - } EVP_RC5_KEY; +} EVP_RC5_KEY; #define data(ctx) EVP_C_DATA(EVP_RC5_KEY,ctx) IMPLEMENT_BLOCK_CIPHER(rc5_32_12_16, ks, RC5_32, EVP_RC5_KEY, NID_rc5, - 8, RC5_32_KEY_LENGTH, 8, 64, - EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT, - r_32_12_16_init_key, NULL, - NULL, NULL, rc5_ctrl) + 8, RC5_32_KEY_LENGTH, 8, 64, + EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT, + r_32_12_16_init_key, NULL, + NULL, NULL, rc5_ctrl) -static int rc5_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) - { - switch(type) - { +static int +rc5_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) +{ + switch (type) { case EVP_CTRL_INIT: data(c)->rounds = RC5_12_ROUNDS; return 1; @@ -95,10 +94,9 @@ static int rc5_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) case EVP_CTRL_GET_RC5_ROUNDS: *(int *)ptr = data(c)->rounds; return 1; - + case EVP_CTRL_SET_RC5_ROUNDS: - switch(arg) - { + switch (arg) { case RC5_8_ROUNDS: case RC5_12_ROUNDS: case RC5_16_ROUNDS: @@ -106,21 +104,23 @@ static int rc5_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) return 1; default: - EVPerr(EVP_F_RC5_CTRL, EVP_R_UNSUPORTED_NUMBER_OF_ROUNDS); + EVPerr(EVP_F_RC5_CTRL, + EVP_R_UNSUPORTED_NUMBER_OF_ROUNDS); return 0; - } + } default: return -1; - } } +} -static int r_32_12_16_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { - RC5_32_set_key(&data(ctx)->ks,EVP_CIPHER_CTX_key_length(ctx), - key,data(ctx)->rounds); +static int +r_32_12_16_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ + RC5_32_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx), key, + data(ctx)->rounds); return 1; - } +} #endif diff --git a/lib/libcrypto/evp/e_xcbc_d.c b/lib/libcrypto/evp/e_xcbc_d.c index 250e88c8c5d..7313e4d225c 100644 --- a/lib/libcrypto/evp/e_xcbc_d.c +++ b/lib/libcrypto/evp/e_xcbc_d.c @@ -5,21 +5,21 @@ * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. - * + * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). - * + * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -34,10 +34,10 @@ * Eric Young (eay@cryptsoft.com)" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). - * 4. If you include any Windows specific code (or a derivative thereof) from + * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" - * + * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -49,7 +49,7 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * + * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence @@ -67,24 +67,22 @@ #include static int desx_cbc_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv,int enc); + const unsigned char *iv, int enc); static int desx_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl); + const unsigned char *in, size_t inl); -typedef struct - { - DES_key_schedule ks;/* key schedule */ - DES_cblock inw; - DES_cblock outw; - } DESX_CBC_KEY; +typedef struct { + DES_key_schedule ks;/* key schedule */ + DES_cblock inw; + DES_cblock outw; +} DESX_CBC_KEY; #define data(ctx) ((DESX_CBC_KEY *)(ctx)->cipher_data) -static const EVP_CIPHER d_xcbc_cipher= - { +static const EVP_CIPHER d_xcbc_cipher = { NID_desx_cbc, - 8,24,8, + 8, 24, 8, EVP_CIPH_CBC_MODE, desx_cbc_init_key, desx_cbc_cipher, @@ -94,45 +92,43 @@ static const EVP_CIPHER d_xcbc_cipher= EVP_CIPHER_get_asn1_iv, NULL, NULL - }; +}; -const EVP_CIPHER *EVP_desx_cbc(void) - { - return(&d_xcbc_cipher); - } - -static int desx_cbc_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { +const EVP_CIPHER * +EVP_desx_cbc(void) +{ + return (&d_xcbc_cipher); +} + +static int +desx_cbc_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ DES_cblock *deskey = (DES_cblock *)key; - DES_set_key_unchecked(deskey,&data(ctx)->ks); - memcpy(&data(ctx)->inw[0],&key[8],8); - memcpy(&data(ctx)->outw[0],&key[16],8); + DES_set_key_unchecked(deskey, &data(ctx)->ks); + memcpy(&data(ctx)->inw[0], &key[8], 8); + memcpy(&data(ctx)->outw[0], &key[16], 8); return 1; - } +} -static int desx_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) - { - while (inl>=EVP_MAXCHUNK) - { - DES_xcbc_encrypt(in,out,(long)EVP_MAXCHUNK,&data(ctx)->ks, - (DES_cblock *)&(ctx->iv[0]), - &data(ctx)->inw, - &data(ctx)->outw, - ctx->encrypt); - inl-=EVP_MAXCHUNK; - in +=EVP_MAXCHUNK; - out+=EVP_MAXCHUNK; - } +static int +desx_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) +{ + while (inl >= EVP_MAXCHUNK) { + DES_xcbc_encrypt(in, out, (long)EVP_MAXCHUNK, &data(ctx)->ks, + (DES_cblock *)&(ctx->iv[0]), &data(ctx)->inw, + &data(ctx)->outw, ctx->encrypt); + inl -= EVP_MAXCHUNK; + in += EVP_MAXCHUNK; + out += EVP_MAXCHUNK; + } if (inl) - DES_xcbc_encrypt(in,out,(long)inl,&data(ctx)->ks, - (DES_cblock *)&(ctx->iv[0]), - &data(ctx)->inw, - &data(ctx)->outw, - ctx->encrypt); + DES_xcbc_encrypt(in, out, (long)inl, &data(ctx)->ks, + (DES_cblock *)&(ctx->iv[0]), &data(ctx)->inw, + &data(ctx)->outw, ctx->encrypt); return 1; - } +} #endif diff --git a/lib/libssl/src/crypto/evp/e_aes.c b/lib/libssl/src/crypto/evp/e_aes.c index d6f0124a945..db0fdf85c86 100644 --- a/lib/libssl/src/crypto/evp/e_aes.c +++ b/lib/libssl/src/crypto/evp/e_aes.c @@ -6,7 +6,7 @@ * are met: * * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. + * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in @@ -60,18 +60,16 @@ #include "modes_lcl.h" #include -typedef struct - { +typedef struct { AES_KEY ks; block128_f block; union { cbc128_f cbc; ctr128_f ctr; } stream; - } EVP_AES_KEY; +} EVP_AES_KEY; -typedef struct - { +typedef struct { AES_KEY ks; /* AES key schedule to use */ int key_set; /* Set if key initialised */ int iv_set; /* Set if an iv is set */ @@ -82,20 +80,17 @@ typedef struct int iv_gen; /* It is OK to generate IVs */ int tls_aad_len; /* TLS AAD length */ ctr128_f ctr; - } EVP_AES_GCM_CTX; +} EVP_AES_GCM_CTX; -typedef struct - { +typedef struct { AES_KEY ks1, ks2; /* AES key schedules to use */ XTS128_CONTEXT xts; - void (*stream)(const unsigned char *in, - unsigned char *out, size_t length, - const AES_KEY *key1, const AES_KEY *key2, - const unsigned char iv[16]); - } EVP_AES_XTS_CTX; - -typedef struct - { + void (*stream)(const unsigned char *in, unsigned char *out, + size_t length, const AES_KEY *key1, const AES_KEY *key2, + const unsigned char iv[16]); +} EVP_AES_XTS_CTX; + +typedef struct { AES_KEY ks; /* AES key schedule to use */ int key_set; /* Set if key initialised */ int iv_set; /* Set if an iv is set */ @@ -104,53 +99,46 @@ typedef struct int L, M; /* L and M parameters from RFC3610 */ CCM128_CONTEXT ccm; ccm128_f str; - } EVP_AES_CCM_CTX; +} EVP_AES_CCM_CTX; #define MAXBITCHUNK ((size_t)1<<(sizeof(size_t)*8-4)) #ifdef VPAES_ASM int vpaes_set_encrypt_key(const unsigned char *userKey, int bits, - AES_KEY *key); + AES_KEY *key); int vpaes_set_decrypt_key(const unsigned char *userKey, int bits, - AES_KEY *key); + AES_KEY *key); void vpaes_encrypt(const unsigned char *in, unsigned char *out, - const AES_KEY *key); + const AES_KEY *key); void vpaes_decrypt(const unsigned char *in, unsigned char *out, - const AES_KEY *key); + const AES_KEY *key); -void vpaes_cbc_encrypt(const unsigned char *in, - unsigned char *out, - size_t length, - const AES_KEY *key, - unsigned char *ivec, int enc); +void vpaes_cbc_encrypt(const unsigned char *in, unsigned char *out, + size_t length, const AES_KEY *key, unsigned char *ivec, int enc); #endif #ifdef BSAES_ASM void bsaes_cbc_encrypt(const unsigned char *in, unsigned char *out, - size_t length, const AES_KEY *key, - unsigned char ivec[16], int enc); + size_t length, const AES_KEY *key, unsigned char ivec[16], int enc); void bsaes_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out, - size_t len, const AES_KEY *key, - const unsigned char ivec[16]); + size_t len, const AES_KEY *key, const unsigned char ivec[16]); void bsaes_xts_encrypt(const unsigned char *inp, unsigned char *out, - size_t len, const AES_KEY *key1, - const AES_KEY *key2, const unsigned char iv[16]); + size_t len, const AES_KEY *key1, const AES_KEY *key2, + const unsigned char iv[16]); void bsaes_xts_decrypt(const unsigned char *inp, unsigned char *out, - size_t len, const AES_KEY *key1, - const AES_KEY *key2, const unsigned char iv[16]); + size_t len, const AES_KEY *key1, const AES_KEY *key2, + const unsigned char iv[16]); #endif #ifdef AES_CTR_ASM void AES_ctr32_encrypt(const unsigned char *in, unsigned char *out, - size_t blocks, const AES_KEY *key, - const unsigned char ivec[AES_BLOCK_SIZE]); + size_t blocks, const AES_KEY *key, + const unsigned char ivec[AES_BLOCK_SIZE]); #endif #ifdef AES_XTS_ASM -void AES_xts_encrypt(const char *inp,char *out,size_t len, - const AES_KEY *key1, const AES_KEY *key2, - const unsigned char iv[16]); -void AES_xts_decrypt(const char *inp,char *out,size_t len, - const AES_KEY *key1, const AES_KEY *key2, - const unsigned char iv[16]); +void AES_xts_encrypt(const char *inp, char *out, size_t len, + const AES_KEY *key1, const AES_KEY *key2, const unsigned char iv[16]); +void AES_xts_decrypt(const char *inp, char *out, size_t len, + const AES_KEY *key1, const AES_KEY *key2, const unsigned char iv[16]); #endif #if defined(AES_ASM) && !defined(I386_ONLY) && ( \ @@ -174,160 +162,142 @@ extern unsigned int OPENSSL_ia32cap_P[2]; #define AESNI_CAPABLE (OPENSSL_ia32cap_P[1]&(1<<(57-32))) int aesni_set_encrypt_key(const unsigned char *userKey, int bits, - AES_KEY *key); + AES_KEY *key); int aesni_set_decrypt_key(const unsigned char *userKey, int bits, - AES_KEY *key); + AES_KEY *key); void aesni_encrypt(const unsigned char *in, unsigned char *out, - const AES_KEY *key); + const AES_KEY *key); void aesni_decrypt(const unsigned char *in, unsigned char *out, - const AES_KEY *key); - -void aesni_ecb_encrypt(const unsigned char *in, - unsigned char *out, - size_t length, - const AES_KEY *key, - int enc); -void aesni_cbc_encrypt(const unsigned char *in, - unsigned char *out, - size_t length, - const AES_KEY *key, - unsigned char *ivec, int enc); - -void aesni_ctr32_encrypt_blocks(const unsigned char *in, - unsigned char *out, - size_t blocks, - const void *key, - const unsigned char *ivec); - -void aesni_xts_encrypt(const unsigned char *in, - unsigned char *out, - size_t length, - const AES_KEY *key1, const AES_KEY *key2, - const unsigned char iv[16]); - -void aesni_xts_decrypt(const unsigned char *in, - unsigned char *out, - size_t length, - const AES_KEY *key1, const AES_KEY *key2, - const unsigned char iv[16]); - -void aesni_ccm64_encrypt_blocks (const unsigned char *in, - unsigned char *out, - size_t blocks, - const void *key, - const unsigned char ivec[16], - unsigned char cmac[16]); - -void aesni_ccm64_decrypt_blocks (const unsigned char *in, - unsigned char *out, - size_t blocks, - const void *key, - const unsigned char ivec[16], - unsigned char cmac[16]); - -static int aesni_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { + const AES_KEY *key); + +void aesni_ecb_encrypt(const unsigned char *in, unsigned char *out, + size_t length, const AES_KEY *key, int enc); +void aesni_cbc_encrypt(const unsigned char *in, unsigned char *out, + size_t length, const AES_KEY *key, unsigned char *ivec, int enc); + +void aesni_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out, + size_t blocks, const void *key, const unsigned char *ivec); + +void aesni_xts_encrypt(const unsigned char *in, unsigned char *out, + size_t length, const AES_KEY *key1, const AES_KEY *key2, + const unsigned char iv[16]); + +void aesni_xts_decrypt(const unsigned char *in, unsigned char *out, + size_t length, const AES_KEY *key1, const AES_KEY *key2, + const unsigned char iv[16]); + +void aesni_ccm64_encrypt_blocks (const unsigned char *in, unsigned char *out, + size_t blocks, const void *key, const unsigned char ivec[16], + unsigned char cmac[16]); + +void aesni_ccm64_decrypt_blocks (const unsigned char *in, unsigned char *out, + size_t blocks, const void *key, const unsigned char ivec[16], + unsigned char cmac[16]); + +static int +aesni_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ int ret, mode; EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; mode = ctx->cipher->flags & EVP_CIPH_MODE; - if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) - && !enc) - { - ret = aesni_set_decrypt_key(key, ctx->key_len*8, ctx->cipher_data); - dat->block = (block128_f)aesni_decrypt; - dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ? - (cbc128_f)aesni_cbc_encrypt : - NULL; - } - else { - ret = aesni_set_encrypt_key(key, ctx->key_len*8, ctx->cipher_data); - dat->block = (block128_f)aesni_encrypt; - if (mode==EVP_CIPH_CBC_MODE) - dat->stream.cbc = (cbc128_f)aesni_cbc_encrypt; - else if (mode==EVP_CIPH_CTR_MODE) + if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) && + !enc) { + ret = aesni_set_decrypt_key(key, ctx->key_len * 8, + ctx->cipher_data); + dat->block = (block128_f)aesni_decrypt; + dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? + (cbc128_f)aesni_cbc_encrypt : NULL; + } else { + ret = aesni_set_encrypt_key(key, ctx->key_len * 8, + ctx->cipher_data); + dat->block = (block128_f)aesni_encrypt; + if (mode == EVP_CIPH_CBC_MODE) + dat->stream.cbc = (cbc128_f)aesni_cbc_encrypt; + else if (mode == EVP_CIPH_CTR_MODE) dat->stream.ctr = (ctr128_f)aesni_ctr32_encrypt_blocks; else dat->stream.cbc = NULL; - } + } - if(ret < 0) - { - EVPerr(EVP_F_AESNI_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED); + if (ret < 0) { + EVPerr(EVP_F_AESNI_INIT_KEY, EVP_R_AES_KEY_SETUP_FAILED); return 0; - } + } return 1; - } +} -static int aesni_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, - const unsigned char *in, size_t len) +static int +aesni_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len) { - aesni_cbc_encrypt(in,out,len,ctx->cipher_data,ctx->iv,ctx->encrypt); + aesni_cbc_encrypt(in, out, len, ctx->cipher_data, ctx->iv, + ctx->encrypt); return 1; } -static int aesni_ecb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, - const unsigned char *in, size_t len) +static int +aesni_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len) { size_t bl = ctx->cipher->block_size; - if (lencipher_data,ctx->encrypt); + aesni_ecb_encrypt(in, out, len, ctx->cipher_data, ctx->encrypt); return 1; } #define aesni_ofb_cipher aes_ofb_cipher -static int aesni_ofb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, - const unsigned char *in,size_t len); +static int aesni_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len); #define aesni_cfb_cipher aes_cfb_cipher -static int aesni_cfb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, - const unsigned char *in,size_t len); +static int aesni_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len); #define aesni_cfb8_cipher aes_cfb8_cipher -static int aesni_cfb8_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, - const unsigned char *in,size_t len); +static int aesni_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len); #define aesni_cfb1_cipher aes_cfb1_cipher -static int aesni_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, - const unsigned char *in,size_t len); +static int aesni_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len); #define aesni_ctr_cipher aes_ctr_cipher static int aesni_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t len); + const unsigned char *in, size_t len); -static int aesni_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { +static int +aesni_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ EVP_AES_GCM_CTX *gctx = ctx->cipher_data; + if (!iv && !key) return 1; - if (key) - { + if (key) { aesni_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks); CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, - (block128_f)aesni_encrypt); + (block128_f)aesni_encrypt); gctx->ctr = (ctr128_f)aesni_ctr32_encrypt_blocks; /* If we have an iv can set it directly, otherwise use * saved IV. */ if (iv == NULL && gctx->iv_set) iv = gctx->iv; - if (iv) - { + if (iv) { CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); gctx->iv_set = 1; - } - gctx->key_set = 1; } - else - { + gctx->key_set = 1; + } else { /* If key set use IV, otherwise copy */ if (gctx->key_set) CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); @@ -335,83 +305,82 @@ static int aesni_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, memcpy(gctx->iv, iv, gctx->ivlen); gctx->iv_set = 1; gctx->iv_gen = 0; - } - return 1; } + return 1; +} #define aesni_gcm_cipher aes_gcm_cipher static int aesni_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t len); + const unsigned char *in, size_t len); -static int aesni_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { +static int +aesni_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ EVP_AES_XTS_CTX *xctx = ctx->cipher_data; + if (!iv && !key) return 1; - if (key) - { + if (key) { /* key_len is two AES keys */ - if (enc) - { - aesni_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1); + if (enc) { + aesni_set_encrypt_key(key, ctx->key_len * 4, + &xctx->ks1); xctx->xts.block1 = (block128_f)aesni_encrypt; xctx->stream = aesni_xts_encrypt; - } - else - { - aesni_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1); + } else { + aesni_set_decrypt_key(key, ctx->key_len * 4, + &xctx->ks1); xctx->xts.block1 = (block128_f)aesni_decrypt; xctx->stream = aesni_xts_decrypt; - } + } - aesni_set_encrypt_key(key + ctx->key_len/2, - ctx->key_len * 4, &xctx->ks2); + aesni_set_encrypt_key(key + ctx->key_len / 2, + ctx->key_len * 4, &xctx->ks2); xctx->xts.block2 = (block128_f)aesni_encrypt; xctx->xts.key1 = &xctx->ks1; - } + } - if (iv) - { + if (iv) { xctx->xts.key2 = &xctx->ks2; memcpy(ctx->iv, iv, 16); - } + } return 1; - } +} #define aesni_xts_cipher aes_xts_cipher static int aesni_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t len); + const unsigned char *in, size_t len); -static int aesni_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { +static int +aesni_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ EVP_AES_CCM_CTX *cctx = ctx->cipher_data; + if (!iv && !key) return 1; - if (key) - { + if (key) { aesni_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks); CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L, - &cctx->ks, (block128_f)aesni_encrypt); - cctx->str = enc?(ccm128_f)aesni_ccm64_encrypt_blocks : - (ccm128_f)aesni_ccm64_decrypt_blocks; + &cctx->ks, (block128_f)aesni_encrypt); + cctx->str = enc ? (ccm128_f)aesni_ccm64_encrypt_blocks : + (ccm128_f)aesni_ccm64_decrypt_blocks; cctx->key_set = 1; - } - if (iv) - { + } + if (iv) { memcpy(ctx->iv, iv, 15 - cctx->L); cctx->iv_set = 1; - } - return 1; } + return 1; +} #define aesni_ccm_cipher aes_ccm_cipher static int aesni_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t len); + const unsigned char *in, size_t len); #define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \ static const EVP_CIPHER aesni_##keylen##_##mode = { \ @@ -493,199 +462,205 @@ const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \ BLOCK_CIPHER_generic(nid,keylen,1,16,cfb8,cfb8,CFB,flags) \ BLOCK_CIPHER_generic(nid,keylen,1,16,ctr,ctr,CTR,flags) -static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { +static int +aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ int ret, mode; EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; mode = ctx->cipher->flags & EVP_CIPH_MODE; - if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) - && !enc) + if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) && + !enc) #ifdef BSAES_CAPABLE - if (BSAES_CAPABLE && mode==EVP_CIPH_CBC_MODE) - { - ret = AES_set_decrypt_key(key,ctx->key_len*8,&dat->ks); - dat->block = (block128_f)AES_decrypt; - dat->stream.cbc = (cbc128_f)bsaes_cbc_encrypt; - } - else + if (BSAES_CAPABLE && mode == EVP_CIPH_CBC_MODE) { + ret = AES_set_decrypt_key(key, ctx->key_len * 8, + &dat->ks); + dat->block = (block128_f)AES_decrypt; + dat->stream.cbc = (cbc128_f)bsaes_cbc_encrypt; + } else #endif #ifdef VPAES_CAPABLE - if (VPAES_CAPABLE) - { - ret = vpaes_set_decrypt_key(key,ctx->key_len*8,&dat->ks); - dat->block = (block128_f)vpaes_decrypt; - dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ? - (cbc128_f)vpaes_cbc_encrypt : - NULL; - } - else + if (VPAES_CAPABLE) { + ret = vpaes_set_decrypt_key(key, ctx->key_len * 8, + &dat->ks); + dat->block = (block128_f)vpaes_decrypt; + dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? + (cbc128_f)vpaes_cbc_encrypt : NULL; + } else #endif { - ret = AES_set_decrypt_key(key,ctx->key_len*8,&dat->ks); - dat->block = (block128_f)AES_decrypt; - dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ? - (cbc128_f)AES_cbc_encrypt : - NULL; - } - else + ret = AES_set_decrypt_key(key, ctx->key_len * 8, + &dat->ks); + dat->block = (block128_f)AES_decrypt; + dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? + (cbc128_f)AES_cbc_encrypt : NULL; + } else #ifdef BSAES_CAPABLE - if (BSAES_CAPABLE && mode==EVP_CIPH_CTR_MODE) - { - ret = AES_set_encrypt_key(key,ctx->key_len*8,&dat->ks); - dat->block = (block128_f)AES_encrypt; - dat->stream.ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks; - } - else + if (BSAES_CAPABLE && mode == EVP_CIPH_CTR_MODE) { + ret = AES_set_encrypt_key(key, ctx->key_len * 8, + &dat->ks); + dat->block = (block128_f)AES_encrypt; + dat->stream.ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks; + } else #endif #ifdef VPAES_CAPABLE - if (VPAES_CAPABLE) - { - ret = vpaes_set_encrypt_key(key,ctx->key_len*8,&dat->ks); - dat->block = (block128_f)vpaes_encrypt; - dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ? - (cbc128_f)vpaes_cbc_encrypt : - NULL; - } - else + if (VPAES_CAPABLE) { + ret = vpaes_set_encrypt_key(key, ctx->key_len * 8, + &dat->ks); + dat->block = (block128_f)vpaes_encrypt; + dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? + (cbc128_f)vpaes_cbc_encrypt : NULL; + } else #endif { - ret = AES_set_encrypt_key(key,ctx->key_len*8,&dat->ks); - dat->block = (block128_f)AES_encrypt; - dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ? - (cbc128_f)AES_cbc_encrypt : - NULL; + ret = AES_set_encrypt_key(key, ctx->key_len * 8, + &dat->ks); + dat->block = (block128_f)AES_encrypt; + dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? + (cbc128_f)AES_cbc_encrypt : NULL; #ifdef AES_CTR_ASM - if (mode==EVP_CIPH_CTR_MODE) - dat->stream.ctr = (ctr128_f)AES_ctr32_encrypt; + if (mode == EVP_CIPH_CTR_MODE) + dat->stream.ctr = (ctr128_f)AES_ctr32_encrypt; #endif } - if(ret < 0) - { - EVPerr(EVP_F_AES_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED); + if (ret < 0) { + EVPerr(EVP_F_AES_INIT_KEY, EVP_R_AES_KEY_SETUP_FAILED); return 0; - } + } return 1; - } +} -static int aes_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, - const unsigned char *in, size_t len) +static int +aes_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len) { EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; if (dat->stream.cbc) - (*dat->stream.cbc)(in,out,len,&dat->ks,ctx->iv,ctx->encrypt); + (*dat->stream.cbc)(in, out, len, &dat->ks, ctx->iv, + ctx->encrypt); else if (ctx->encrypt) - CRYPTO_cbc128_encrypt(in,out,len,&dat->ks,ctx->iv,dat->block); + CRYPTO_cbc128_encrypt(in, out, len, &dat->ks, ctx->iv, + dat->block); else - CRYPTO_cbc128_decrypt(in,out,len,&dat->ks,ctx->iv,dat->block); + CRYPTO_cbc128_decrypt(in, out, len, &dat->ks, ctx->iv, + dat->block); return 1; } -static int aes_ecb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, - const unsigned char *in, size_t len) +static int +aes_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len) { size_t bl = ctx->cipher->block_size; size_t i; EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; - if (lenblock)(in+i,out+i,&dat->ks); + for (i = 0, len -= bl; i <= len; i += bl) + (*dat->block)(in + i, out + i, &dat->ks); return 1; } -static int aes_ofb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, - const unsigned char *in,size_t len) +static int +aes_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len) { EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; - CRYPTO_ofb128_encrypt(in,out,len,&dat->ks, - ctx->iv,&ctx->num,dat->block); + CRYPTO_ofb128_encrypt(in, out, len, &dat->ks, ctx->iv, &ctx->num, + dat->block); return 1; } -static int aes_cfb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, - const unsigned char *in,size_t len) +static int +aes_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len) { EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; - CRYPTO_cfb128_encrypt(in,out,len,&dat->ks, - ctx->iv,&ctx->num,ctx->encrypt,dat->block); + CRYPTO_cfb128_encrypt(in, out, len, &dat->ks, ctx->iv, &ctx->num, + ctx->encrypt, dat->block); return 1; } -static int aes_cfb8_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, - const unsigned char *in,size_t len) +static int +aes_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len) { EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; - CRYPTO_cfb128_8_encrypt(in,out,len,&dat->ks, - ctx->iv,&ctx->num,ctx->encrypt,dat->block); + CRYPTO_cfb128_8_encrypt(in, out, len, &dat->ks, ctx->iv, &ctx->num, + ctx->encrypt, dat->block); return 1; } -static int aes_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, - const unsigned char *in,size_t len) +static int +aes_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len) { EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; if (ctx->flags&EVP_CIPH_FLAG_LENGTH_BITS) { - CRYPTO_cfb128_1_encrypt(in,out,len,&dat->ks, - ctx->iv,&ctx->num,ctx->encrypt,dat->block); + CRYPTO_cfb128_1_encrypt(in, out, len, &dat->ks, ctx->iv, + &ctx->num, ctx->encrypt, dat->block); return 1; } - while (len>=MAXBITCHUNK) { - CRYPTO_cfb128_1_encrypt(in,out,MAXBITCHUNK*8,&dat->ks, - ctx->iv,&ctx->num,ctx->encrypt,dat->block); - len-=MAXBITCHUNK; + while (len >= MAXBITCHUNK) { + CRYPTO_cfb128_1_encrypt(in, out, MAXBITCHUNK*8, &dat->ks, + ctx->iv, &ctx->num, ctx->encrypt, dat->block); + len -= MAXBITCHUNK; } if (len) - CRYPTO_cfb128_1_encrypt(in,out,len*8,&dat->ks, - ctx->iv,&ctx->num,ctx->encrypt,dat->block); - + CRYPTO_cfb128_1_encrypt(in, out, len*8, &dat->ks, + ctx->iv, &ctx->num, ctx->encrypt, dat->block); + return 1; } static int aes_ctr_cipher (EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t len) + const unsigned char *in, size_t len) { unsigned int num = ctx->num; EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; if (dat->stream.ctr) - CRYPTO_ctr128_encrypt_ctr32(in,out,len,&dat->ks, - ctx->iv,ctx->buf,&num,dat->stream.ctr); + CRYPTO_ctr128_encrypt_ctr32(in, out, len, &dat->ks, + ctx->iv, ctx->buf, &num, dat->stream.ctr); else - CRYPTO_ctr128_encrypt(in,out,len,&dat->ks, - ctx->iv,ctx->buf,&num,dat->block); + CRYPTO_ctr128_encrypt(in, out, len, &dat->ks, + ctx->iv, ctx->buf, &num, dat->block); ctx->num = (size_t)num; return 1; } -BLOCK_CIPHER_generic_pack(NID_aes,128,EVP_CIPH_FLAG_FIPS) -BLOCK_CIPHER_generic_pack(NID_aes,192,EVP_CIPH_FLAG_FIPS) -BLOCK_CIPHER_generic_pack(NID_aes,256,EVP_CIPH_FLAG_FIPS) +BLOCK_CIPHER_generic_pack(NID_aes, 128, EVP_CIPH_FLAG_FIPS) +BLOCK_CIPHER_generic_pack(NID_aes, 192, EVP_CIPH_FLAG_FIPS) +BLOCK_CIPHER_generic_pack(NID_aes, 256, EVP_CIPH_FLAG_FIPS) -static int aes_gcm_cleanup(EVP_CIPHER_CTX *c) - { +static int +aes_gcm_cleanup(EVP_CIPHER_CTX *c) +{ EVP_AES_GCM_CTX *gctx = c->cipher_data; + OPENSSL_cleanse(&gctx->gcm, sizeof(gctx->gcm)); if (gctx->iv != c->iv) free(gctx->iv); return 1; - } +} /* increment counter (64-bit int) by 1 */ -static void ctr64_inc(unsigned char *counter) { - int n=8; +static void +ctr64_inc(unsigned char *counter) { + int n = 8; unsigned char c; do { @@ -693,15 +668,17 @@ static void ctr64_inc(unsigned char *counter) { c = counter[n]; ++c; counter[n] = c; - if (c) return; + if (c) + return; } while (n); } -static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) - { +static int +aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) +{ EVP_AES_GCM_CTX *gctx = c->cipher_data; - switch (type) - { + + switch (type) { case EVP_CTRL_INIT: gctx->key_set = 0; gctx->iv_set = 0; @@ -716,19 +693,18 @@ static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) if (arg <= 0) return 0; #ifdef OPENSSL_FIPS - if (FIPS_module_mode() && !(c->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW) - && arg < 12) + if (FIPS_module_mode() && + !(c->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW) && arg < 12) return 0; #endif /* Allocate memory for IV if needed */ - if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen)) - { + if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen)) { if (gctx->iv != c->iv) free(gctx->iv); gctx->iv = malloc(arg); if (!gctx->iv) return 0; - } + } gctx->ivlen = arg; return 1; @@ -747,12 +723,11 @@ static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) case EVP_CTRL_GCM_SET_IV_FIXED: /* Special case: -1 length restores whole IV */ - if (arg == -1) - { + if (arg == -1) { memcpy(gctx->iv, ptr, gctx->ivlen); gctx->iv_gen = 1; return 1; - } + } /* Fixed field must be at least 4 bytes and invocation field * at least 8. */ @@ -761,7 +736,7 @@ static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) if (arg) memcpy(gctx->iv, ptr, arg); if (c->encrypt && - RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0) + RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0) return 0; gctx->iv_gen = 1; return 1; @@ -795,63 +770,68 @@ static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) return 0; memcpy(c->buf, ptr, arg); gctx->tls_aad_len = arg; - { - unsigned int len=c->buf[arg-2]<<8|c->buf[arg-1]; + { + unsigned int len = c->buf[arg - 2] << 8 | + c->buf[arg - 1]; + /* Correct length for explicit IV */ len -= EVP_GCM_TLS_EXPLICIT_IV_LEN; + /* If decrypting correct for tag too */ if (!c->encrypt) len -= EVP_GCM_TLS_TAG_LEN; - c->buf[arg-2] = len>>8; - c->buf[arg-1] = len & 0xff; - } + c->buf[arg - 2] = len >> 8; + c->buf[arg - 1] = len & 0xff; + } /* Extra padding: tag appended to record */ return EVP_GCM_TLS_TAG_LEN; default: return -1; - } } +} -static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { +static int +aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ EVP_AES_GCM_CTX *gctx = ctx->cipher_data; + if (!iv && !key) return 1; - if (key) - { do { + if (key) { + do { #ifdef BSAES_CAPABLE - if (BSAES_CAPABLE) - { - AES_set_encrypt_key(key,ctx->key_len*8,&gctx->ks); - CRYPTO_gcm128_init(&gctx->gcm,&gctx->ks, - (block128_f)AES_encrypt); - gctx->ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks; - break; - } - else + if (BSAES_CAPABLE) { + AES_set_encrypt_key(key, ctx->key_len * 8, + &gctx->ks); + CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, + (block128_f)AES_encrypt); + gctx->ctr = + (ctr128_f)bsaes_ctr32_encrypt_blocks; + break; + } else #endif #ifdef VPAES_CAPABLE - if (VPAES_CAPABLE) - { - vpaes_set_encrypt_key(key,ctx->key_len*8,&gctx->ks); - CRYPTO_gcm128_init(&gctx->gcm,&gctx->ks, - (block128_f)vpaes_encrypt); - gctx->ctr = NULL; - break; - } - else + if (VPAES_CAPABLE) { + vpaes_set_encrypt_key(key, ctx->key_len * 8, + &gctx->ks); + CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, + (block128_f)vpaes_encrypt); + gctx->ctr = NULL; + break; + } else #endif - (void)0; /* terminate potentially open 'else' */ + (void)0; /* terminate potentially open 'else' */ - AES_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks); - CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, (block128_f)AES_encrypt); + AES_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks); + CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, + (block128_f)AES_encrypt); #ifdef AES_CTR_ASM - gctx->ctr = (ctr128_f)AES_ctr32_encrypt; + gctx->ctr = (ctr128_f)AES_ctr32_encrypt; #else - gctx->ctr = NULL; + gctx->ctr = NULL; #endif } while (0); @@ -860,15 +840,12 @@ static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, */ if (iv == NULL && gctx->iv_set) iv = gctx->iv; - if (iv) - { + if (iv) { CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); gctx->iv_set = 1; - } - gctx->key_set = 1; } - else - { + gctx->key_set = 1; + } else { /* If key set use IV, otherwise copy */ if (gctx->key_set) CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen); @@ -876,9 +853,9 @@ static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, memcpy(gctx->iv, iv, gctx->ivlen); gctx->iv_set = 1; gctx->iv_gen = 0; - } - return 1; } + return 1; +} /* Handle TLS GCM packet format. This consists of the last portion of the IV * followed by the payload and finally the tag. On encrypt generate IV, @@ -886,83 +863,82 @@ static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, * and verify tag. */ -static int aes_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t len) - { +static int +aes_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len) +{ EVP_AES_GCM_CTX *gctx = ctx->cipher_data; int rv = -1; + /* Encrypt/decrypt must be performed in place */ - if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN+EVP_GCM_TLS_TAG_LEN)) + if (out != in || + len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN)) return -1; + /* Set IV from start of buffer or generate IV and write to start * of buffer. */ if (EVP_CIPHER_CTX_ctrl(ctx, ctx->encrypt ? - EVP_CTRL_GCM_IV_GEN : EVP_CTRL_GCM_SET_IV_INV, - EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0) + EVP_CTRL_GCM_IV_GEN : EVP_CTRL_GCM_SET_IV_INV, + EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0) goto err; + /* Use saved AAD */ if (CRYPTO_gcm128_aad(&gctx->gcm, ctx->buf, gctx->tls_aad_len)) goto err; + /* Fix buffer and length to point to payload */ in += EVP_GCM_TLS_EXPLICIT_IV_LEN; out += EVP_GCM_TLS_EXPLICIT_IV_LEN; len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN; - if (ctx->encrypt) - { + if (ctx->encrypt) { /* Encrypt payload */ - if (gctx->ctr) - { - if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm, - in, out, len, - gctx->ctr)) + if (gctx->ctr) { + if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm, in, out, + len, gctx->ctr)) goto err; - } - else { + } else { if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len)) goto err; - } + } out += len; + /* Finally write tag */ CRYPTO_gcm128_tag(&gctx->gcm, out, EVP_GCM_TLS_TAG_LEN); rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN; - } - else - { + } else { /* Decrypt */ - if (gctx->ctr) - { - if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm, - in, out, len, - gctx->ctr)) + if (gctx->ctr) { + if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm, in, out, + len, gctx->ctr)) goto err; - } - else { + } else { if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len)) goto err; - } + } /* Retrieve tag */ - CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, - EVP_GCM_TLS_TAG_LEN); + CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, EVP_GCM_TLS_TAG_LEN); + /* If tag mismatch wipe buffer */ - if (memcmp(ctx->buf, in + len, EVP_GCM_TLS_TAG_LEN)) - { + if (memcmp(ctx->buf, in + len, EVP_GCM_TLS_TAG_LEN)) { OPENSSL_cleanse(out, len); goto err; - } - rv = len; } + rv = len; + } - err: +err: gctx->iv_set = 0; gctx->tls_aad_len = -1; return rv; - } +} -static int aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t len) - { +static int +aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len) +{ EVP_AES_GCM_CTX *gctx = ctx->cipher_data; + /* If not set up, return error */ if (!gctx->key_set) return -1; @@ -972,95 +948,88 @@ static int aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, if (!gctx->iv_set) return -1; - if (in) - { - if (out == NULL) - { + + if (in) { + if (out == NULL) { if (CRYPTO_gcm128_aad(&gctx->gcm, in, len)) return -1; - } - else if (ctx->encrypt) - { - if (gctx->ctr) - { + } else if (ctx->encrypt) { + if (gctx->ctr) { if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm, - in, out, len, - gctx->ctr)) + in, out, len, gctx->ctr)) return -1; - } - else { - if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len)) + } else { + if (CRYPTO_gcm128_encrypt(&gctx->gcm, + in, out, len)) return -1; - } } - else - { - if (gctx->ctr) - { + } else { + if (gctx->ctr) { if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm, - in, out, len, - gctx->ctr)) + in, out, len, gctx->ctr)) return -1; - } - else { - if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len)) + } else { + if (CRYPTO_gcm128_decrypt(&gctx->gcm, + in, out, len)) return -1; - } } - return len; } - else - { - if (!ctx->encrypt) - { + return len; + } else { + if (!ctx->encrypt) { if (gctx->taglen < 0) return -1; - if (CRYPTO_gcm128_finish(&gctx->gcm, - ctx->buf, gctx->taglen) != 0) + if (CRYPTO_gcm128_finish(&gctx->gcm, ctx->buf, + gctx->taglen) != 0) return -1; gctx->iv_set = 0; return 0; - } + } CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, 16); gctx->taglen = 16; + /* Don't reuse the IV */ gctx->iv_set = 0; return 0; - } - } +} + #define CUSTOM_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 \ | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \ | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT) -BLOCK_CIPHER_custom(NID_aes,128,1,12,gcm,GCM, - EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS) -BLOCK_CIPHER_custom(NID_aes,192,1,12,gcm,GCM, - EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS) -BLOCK_CIPHER_custom(NID_aes,256,1,12,gcm,GCM, - EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS) +BLOCK_CIPHER_custom(NID_aes, 128, 1,12, gcm, GCM, + EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS) +BLOCK_CIPHER_custom(NID_aes, 192, 1,12, gcm, GCM, + EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS) +BLOCK_CIPHER_custom(NID_aes, 256, 1,12, gcm, GCM, + EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS) -static int aes_xts_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) - { +static int +aes_xts_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) +{ EVP_AES_XTS_CTX *xctx = c->cipher_data; + if (type != EVP_CTRL_INIT) return -1; + /* key1 and key2 are used as an indicator both key and IV are set */ xctx->xts.key1 = NULL; xctx->xts.key2 = NULL; return 1; - } +} -static int aes_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { +static int +aes_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ EVP_AES_XTS_CTX *xctx = ctx->cipher_data; + if (!iv && !key) return 1; - if (key) do - { + if (key) do { #ifdef AES_XTS_ASM xctx->stream = enc ? AES_xts_encrypt : AES_xts_decrypt; #else @@ -1069,100 +1038,98 @@ static int aes_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, /* key_len is two AES keys */ #ifdef BSAES_CAPABLE if (BSAES_CAPABLE) - xctx->stream = enc ? bsaes_xts_encrypt : bsaes_xts_decrypt; + xctx->stream = enc ? bsaes_xts_encrypt : + bsaes_xts_decrypt; else #endif #ifdef VPAES_CAPABLE - if (VPAES_CAPABLE) - { - if (enc) - { - vpaes_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1); - xctx->xts.block1 = (block128_f)vpaes_encrypt; - } - else - { - vpaes_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1); - xctx->xts.block1 = (block128_f)vpaes_decrypt; + if (VPAES_CAPABLE) { + if (enc) { + vpaes_set_encrypt_key(key, ctx->key_len * 4, + &xctx->ks1); + xctx->xts.block1 = (block128_f)vpaes_encrypt; + } else { + vpaes_set_decrypt_key(key, ctx->key_len * 4, + &xctx->ks1); + xctx->xts.block1 = (block128_f)vpaes_decrypt; } - vpaes_set_encrypt_key(key + ctx->key_len/2, - ctx->key_len * 4, &xctx->ks2); - xctx->xts.block2 = (block128_f)vpaes_encrypt; + vpaes_set_encrypt_key(key + ctx->key_len / 2, + ctx->key_len * 4, &xctx->ks2); + xctx->xts.block2 = (block128_f)vpaes_encrypt; - xctx->xts.key1 = &xctx->ks1; - break; - } - else + xctx->xts.key1 = &xctx->ks1; + break; + } else #endif - (void)0; /* terminate potentially open 'else' */ + (void)0; /* terminate potentially open 'else' */ - if (enc) - { + if (enc) { AES_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1); xctx->xts.block1 = (block128_f)AES_encrypt; - } - else - { + } else { AES_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1); xctx->xts.block1 = (block128_f)AES_decrypt; - } + } - AES_set_encrypt_key(key + ctx->key_len/2, - ctx->key_len * 4, &xctx->ks2); + AES_set_encrypt_key(key + ctx->key_len / 2, + ctx->key_len * 4, &xctx->ks2); xctx->xts.block2 = (block128_f)AES_encrypt; xctx->xts.key1 = &xctx->ks1; - } while (0); + } while (0); - if (iv) - { + if (iv) { xctx->xts.key2 = &xctx->ks2; memcpy(ctx->iv, iv, 16); - } + } return 1; - } +} -static int aes_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t len) - { +static int +aes_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len) +{ EVP_AES_XTS_CTX *xctx = ctx->cipher_data; + if (!xctx->xts.key1 || !xctx->xts.key2) return 0; - if (!out || !in || lenflags & EVP_CIPH_FLAG_NON_FIPS_ALLOW) && - (len > (1UL<<20)*16)) - { + if (FIPS_module_mode() && + !(ctx->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW) && + (len > (1UL << 20) * 16)) { EVPerr(EVP_F_AES_XTS_CIPHER, EVP_R_TOO_LARGE); return 0; - } + } #endif if (xctx->stream) - (*xctx->stream)(in, out, len, - xctx->xts.key1, xctx->xts.key2, ctx->iv); + (*xctx->stream)(in, out, len, xctx->xts.key1, xctx->xts.key2, + ctx->iv); else if (CRYPTO_xts128_encrypt(&xctx->xts, ctx->iv, in, out, len, - ctx->encrypt)) + ctx->encrypt)) return 0; return 1; - } +} #define aes_xts_cleanup NULL #define XTS_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CUSTOM_IV \ | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT) -BLOCK_CIPHER_custom(NID_aes,128,1,16,xts,XTS,EVP_CIPH_FLAG_FIPS|XTS_FLAGS) -BLOCK_CIPHER_custom(NID_aes,256,1,16,xts,XTS,EVP_CIPH_FLAG_FIPS|XTS_FLAGS) +BLOCK_CIPHER_custom(NID_aes, 128, 1,16, xts, XTS, EVP_CIPH_FLAG_FIPS|XTS_FLAGS) +BLOCK_CIPHER_custom(NID_aes, 256, 1,16, xts, XTS, EVP_CIPH_FLAG_FIPS|XTS_FLAGS) -static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) - { +static int +aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) +{ EVP_AES_CCM_CTX *cctx = c->cipher_data; - switch (type) - { + + switch (type) { case EVP_CTRL_INIT: cctx->key_set = 0; cctx->iv_set = 0; @@ -1174,6 +1141,7 @@ static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) case EVP_CTRL_CCM_SET_IVLEN: arg = 15 - arg; + case EVP_CTRL_CCM_SET_L: if (arg < 2 || arg > 8) return 0; @@ -1185,18 +1153,17 @@ static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) return 0; if ((c->encrypt && ptr) || (!c->encrypt && !ptr)) return 0; - if (ptr) - { + if (ptr) { cctx->tag_set = 1; memcpy(c->buf, ptr, arg); - } + } cctx->M = arg; return 1; case EVP_CTRL_CCM_GET_TAG: if (!c->encrypt || !cctx->tag_set) return 0; - if(!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg)) + if (!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg)) return 0; cctx->tag_set = 0; cctx->iv_set = 0; @@ -1205,116 +1172,111 @@ static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) default: return -1; - - } } +} -static int aes_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { +static int +aes_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ EVP_AES_CCM_CTX *cctx = ctx->cipher_data; + if (!iv && !key) return 1; - if (key) do - { + if (key) do { #ifdef VPAES_CAPABLE - if (VPAES_CAPABLE) - { + if (VPAES_CAPABLE) { vpaes_set_encrypt_key(key, ctx->key_len*8, &cctx->ks); CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L, - &cctx->ks, (block128_f)vpaes_encrypt); + &cctx->ks, (block128_f)vpaes_encrypt); cctx->str = NULL; cctx->key_set = 1; break; - } + } #endif AES_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks); CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L, - &cctx->ks, (block128_f)AES_encrypt); + &cctx->ks, (block128_f)AES_encrypt); cctx->str = NULL; cctx->key_set = 1; - } while (0); - if (iv) - { + } while (0); + if (iv) { memcpy(ctx->iv, iv, 15 - cctx->L); cctx->iv_set = 1; - } - return 1; } + return 1; +} -static int aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t len) - { +static int +aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len) +{ EVP_AES_CCM_CTX *cctx = ctx->cipher_data; CCM128_CONTEXT *ccm = &cctx->ccm; + /* If not set up, return error */ if (!cctx->iv_set && !cctx->key_set) return -1; if (!ctx->encrypt && !cctx->tag_set) return -1; - if (!out) - { - if (!in) - { - if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L,len)) + + if (!out) { + if (!in) { + if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, + len)) return -1; cctx->len_set = 1; return len; - } + } /* If have AAD need message length */ if (!cctx->len_set && len) return -1; CRYPTO_ccm128_aad(ccm, in, len); return len; - } + } /* EVP_*Final() doesn't return any data */ if (!in) return 0; /* If not set length yet do it */ - if (!cctx->len_set) - { + if (!cctx->len_set) { if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len)) return -1; cctx->len_set = 1; - } - if (ctx->encrypt) - { + } + if (ctx->encrypt) { if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len, - cctx->str) : - CRYPTO_ccm128_encrypt(ccm, in, out, len)) + cctx->str) : CRYPTO_ccm128_encrypt(ccm, in, out, len)) return -1; cctx->tag_set = 1; return len; - } - else - { + } else { int rv = -1; if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len, - cctx->str) : - !CRYPTO_ccm128_decrypt(ccm, in, out, len)) - { + cctx->str) : !CRYPTO_ccm128_decrypt(ccm, in, out, len)) { unsigned char tag[16]; - if (CRYPTO_ccm128_tag(ccm, tag, cctx->M)) - { + if (CRYPTO_ccm128_tag(ccm, tag, cctx->M)) { if (!memcmp(tag, ctx->buf, cctx->M)) rv = len; - } } + } if (rv == -1) OPENSSL_cleanse(out, len); cctx->iv_set = 0; cctx->tag_set = 0; cctx->len_set = 0; return rv; - } - } +} + #define aes_ccm_cleanup NULL -BLOCK_CIPHER_custom(NID_aes,128,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS) -BLOCK_CIPHER_custom(NID_aes,192,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS) -BLOCK_CIPHER_custom(NID_aes,256,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS) +BLOCK_CIPHER_custom(NID_aes, 128, 1,12, ccm, CCM, + EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS) +BLOCK_CIPHER_custom(NID_aes, 192, 1,12, ccm, CCM, + EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS) +BLOCK_CIPHER_custom(NID_aes, 256, 1,12, ccm, CCM, + EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS) #endif #endif diff --git a/lib/libssl/src/crypto/evp/e_aes_cbc_hmac_sha1.c b/lib/libssl/src/crypto/evp/e_aes_cbc_hmac_sha1.c index 4d76ec74d2c..af0edb3dcf9 100644 --- a/lib/libssl/src/crypto/evp/e_aes_cbc_hmac_sha1.c +++ b/lib/libssl/src/crypto/evp/e_aes_cbc_hmac_sha1.c @@ -72,16 +72,15 @@ #define TLS1_1_VERSION 0x0302 -typedef struct - { - AES_KEY ks; - SHA_CTX head,tail,md; - size_t payload_length; /* AAD length in decrypt case */ - union { - unsigned int tls_ver; - unsigned char tls_aad[16]; /* 13 used */ - } aux; - } EVP_AES_HMAC_SHA1; +typedef struct { + AES_KEY ks; + SHA_CTX head, tail, md; + size_t payload_length; /* AAD length in decrypt case */ + union { + unsigned int tls_ver; + unsigned char tls_aad[16]; /* 13 used */ + } aux; +} EVP_AES_HMAC_SHA1; #define NO_PAYLOAD_LENGTH ((size_t)-1) @@ -97,43 +96,37 @@ typedef struct extern unsigned int OPENSSL_ia32cap_P[2]; #define AESNI_CAPABLE (1<<(57-32)) -int aesni_set_encrypt_key(const unsigned char *userKey, int bits, - AES_KEY *key); -int aesni_set_decrypt_key(const unsigned char *userKey, int bits, - AES_KEY *key); +int aesni_set_encrypt_key(const unsigned char *userKey, int bits, AES_KEY *key); +int aesni_set_decrypt_key(const unsigned char *userKey, int bits, AES_KEY *key); -void aesni_cbc_encrypt(const unsigned char *in, - unsigned char *out, - size_t length, - const AES_KEY *key, - unsigned char *ivec, int enc); +void aesni_cbc_encrypt(const unsigned char *in, unsigned char *out, + size_t length, const AES_KEY *key, unsigned char *ivec, int enc); void aesni_cbc_sha1_enc (const void *inp, void *out, size_t blocks, - const AES_KEY *key, unsigned char iv[16], - SHA_CTX *ctx,const void *in0); + const AES_KEY *key, unsigned char iv[16], SHA_CTX *ctx, const void *in0); #define data(ctx) ((EVP_AES_HMAC_SHA1 *)(ctx)->cipher_data) -static int aesni_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx, - const unsigned char *inkey, - const unsigned char *iv, int enc) - { +static int +aesni_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *inkey, + const unsigned char *iv, int enc) +{ EVP_AES_HMAC_SHA1 *key = data(ctx); int ret; if (enc) - ret=aesni_set_encrypt_key(inkey,ctx->key_len*8,&key->ks); + ret = aesni_set_encrypt_key(inkey, ctx->key_len * 8, &key->ks); else - ret=aesni_set_decrypt_key(inkey,ctx->key_len*8,&key->ks); + ret = aesni_set_decrypt_key(inkey, ctx->key_len * 8, &key->ks); SHA1_Init(&key->head); /* handy when benchmarking */ key->tail = key->head; - key->md = key->head; + key->md = key->head; key->payload_length = NO_PAYLOAD_LENGTH; - return ret<0?0:1; - } + return ret < 0 ? 0 : 1; +} #define STITCHED_CALL @@ -141,16 +134,19 @@ static int aesni_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx, #define aes_off 0 #endif -void sha1_block_data_order (void *c,const void *p,size_t len); +void sha1_block_data_order (void *c, const void *p, size_t len); -static void sha1_update(SHA_CTX *c,const void *data,size_t len) -{ const unsigned char *ptr = data; +static void +sha1_update(SHA_CTX *c, const void *data, size_t len) +{ + const unsigned char *ptr = data; size_t res; if ((res = c->num)) { - res = SHA_CBLOCK-res; - if (lenNh += len>>29; - c->Nl += len<<=3; - if (c->Nl<(unsigned int)len) c->Nh++; + c->Nh += len >> 29; + c->Nl += len <<= 3; + if (c->Nl < (unsigned int)len) + c->Nh++; } if (res) - SHA1_Update(c,ptr,res); + SHA1_Update(c, ptr, res); } #ifdef SHA1_Update @@ -176,96 +173,106 @@ static void sha1_update(SHA_CTX *c,const void *data,size_t len) #endif #define SHA1_Update sha1_update -static int aesni_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t len) - { +static int +aesni_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len) +{ EVP_AES_HMAC_SHA1 *key = data(ctx); unsigned int l; - size_t plen = key->payload_length, - iv = 0, /* explicit IV in TLS 1.1 and later */ - sha_off = 0; + size_t plen = key->payload_length, + iv = 0, /* explicit IV in TLS 1.1 and later */ + sha_off = 0; #if defined(STITCHED_CALL) - size_t aes_off = 0, - blocks; + size_t aes_off = 0, blocks; - sha_off = SHA_CBLOCK-key->md.num; + sha_off = SHA_CBLOCK - key->md.num; #endif key->payload_length = NO_PAYLOAD_LENGTH; - if (len%AES_BLOCK_SIZE) return 0; + if (len % AES_BLOCK_SIZE) + return 0; if (ctx->encrypt) { - if (plen==NO_PAYLOAD_LENGTH) + if (plen == NO_PAYLOAD_LENGTH) plen = len; - else if (len!=((plen+SHA_DIGEST_LENGTH+AES_BLOCK_SIZE)&-AES_BLOCK_SIZE)) + else if (len != ((plen + SHA_DIGEST_LENGTH + AES_BLOCK_SIZE) & + -AES_BLOCK_SIZE)) return 0; else if (key->aux.tls_ver >= TLS1_1_VERSION) iv = AES_BLOCK_SIZE; #if defined(STITCHED_CALL) - if (plen>(sha_off+iv) && (blocks=(plen-(sha_off+iv))/SHA_CBLOCK)) { - SHA1_Update(&key->md,in+iv,sha_off); + if (plen > (sha_off + iv) && + (blocks = (plen - (sha_off + iv)) / SHA_CBLOCK)) { + SHA1_Update(&key->md, in + iv, sha_off); - aesni_cbc_sha1_enc(in,out,blocks,&key->ks, - ctx->iv,&key->md,in+iv+sha_off); + aesni_cbc_sha1_enc(in, out, blocks, &key->ks, + ctx->iv, &key->md, in + iv + sha_off); blocks *= SHA_CBLOCK; aes_off += blocks; sha_off += blocks; - key->md.Nh += blocks>>29; - key->md.Nl += blocks<<=3; - if (key->md.Nl<(unsigned int)blocks) key->md.Nh++; + key->md.Nh += blocks >> 29; + key->md.Nl += blocks <<= 3; + if (key->md.Nl < (unsigned int)blocks) + key->md.Nh++; } else { sha_off = 0; } #endif sha_off += iv; - SHA1_Update(&key->md,in+sha_off,plen-sha_off); + SHA1_Update(&key->md, in + sha_off, plen - sha_off); - if (plen!=len) { /* "TLS" mode of operation */ - if (in!=out) - memcpy(out+aes_off,in+aes_off,plen-aes_off); + if (plen != len) { /* "TLS" mode of operation */ + if (in != out) + memcpy(out + aes_off, in + aes_off, + plen - aes_off); /* calculate HMAC and append it to payload */ - SHA1_Final(out+plen,&key->md); + SHA1_Final(out + plen, &key->md); key->md = key->tail; - SHA1_Update(&key->md,out+plen,SHA_DIGEST_LENGTH); - SHA1_Final(out+plen,&key->md); + SHA1_Update(&key->md, out + plen, SHA_DIGEST_LENGTH); + SHA1_Final(out + plen, &key->md); /* pad the payload|hmac */ plen += SHA_DIGEST_LENGTH; - for (l=len-plen-1;plenks,ctx->iv,1); + aesni_cbc_encrypt(out + aes_off, out + aes_off, + len - aes_off, &key->ks, ctx->iv, 1); } else { - aesni_cbc_encrypt(in+aes_off,out+aes_off,len-aes_off, - &key->ks,ctx->iv,1); + aesni_cbc_encrypt(in + aes_off, out + aes_off, + len - aes_off, &key->ks, ctx->iv, 1); } } else { - union { unsigned int u[SHA_DIGEST_LENGTH/sizeof(unsigned int)]; - unsigned char c[32+SHA_DIGEST_LENGTH]; } mac, *pmac; + union { + unsigned int u[SHA_DIGEST_LENGTH/sizeof(unsigned int)]; + unsigned char c[32 + SHA_DIGEST_LENGTH]; + } mac, *pmac; /* arrange cache line alignment */ - pmac = (void *)(((size_t)mac.c+31)&((size_t)0-32)); + pmac = (void *)(((size_t)mac.c + 31) & ((size_t)0 - 32)); /* decrypt HMAC|padding at once */ - aesni_cbc_encrypt(in,out,len, - &key->ks,ctx->iv,0); + aesni_cbc_encrypt(in, out, len, &key->ks, ctx->iv, 0); if (plen) { /* "TLS" mode of operation */ size_t inp_len, mask, j, i; unsigned int res, maxpad, pad, bitlen; int ret = 1; - union { unsigned int u[SHA_LBLOCK]; - unsigned char c[SHA_CBLOCK]; } - *data = (void *)key->md.data; + union { + unsigned int u[SHA_LBLOCK]; + unsigned char c[SHA_CBLOCK]; + } + *data = (void *)key->md.data; - if ((key->aux.tls_aad[plen-4]<<8|key->aux.tls_aad[plen-3]) - >= TLS1_1_VERSION) + if ((key->aux.tls_aad[plen - 4] << 8 | + key->aux.tls_aad[plen - 3]) >= TLS1_1_VERSION) iv = AES_BLOCK_SIZE; - if (len<(iv+SHA_DIGEST_LENGTH+1)) + if (len < (iv + SHA_DIGEST_LENGTH + 1)) return 0; /* omit explicit iv */ @@ -273,93 +280,102 @@ static int aesni_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, len -= iv; /* figure out payload length */ - pad = out[len-1]; - maxpad = len-(SHA_DIGEST_LENGTH+1); - maxpad |= (255-maxpad)>>(sizeof(maxpad)*8-8); + pad = out[len - 1]; + maxpad = len - (SHA_DIGEST_LENGTH + 1); + maxpad |= (255 - maxpad) >> (sizeof(maxpad) * 8 - 8); maxpad &= 255; - inp_len = len - (SHA_DIGEST_LENGTH+pad+1); - mask = (0-((inp_len-len)>>(sizeof(inp_len)*8-1))); + inp_len = len - (SHA_DIGEST_LENGTH + pad + 1); + mask = (0 - ((inp_len - len) >> + (sizeof(inp_len) * 8 - 1))); inp_len &= mask; ret &= (int)mask; - key->aux.tls_aad[plen-2] = inp_len>>8; - key->aux.tls_aad[plen-1] = inp_len; + key->aux.tls_aad[plen - 2] = inp_len >> 8; + key->aux.tls_aad[plen - 1] = inp_len; /* calculate HMAC */ key->md = key->head; - SHA1_Update(&key->md,key->aux.tls_aad,plen); + SHA1_Update(&key->md, key->aux.tls_aad, plen); #if 1 len -= SHA_DIGEST_LENGTH; /* amend mac */ - if (len>=(256+SHA_CBLOCK)) { - j = (len-(256+SHA_CBLOCK))&(0-SHA_CBLOCK); - j += SHA_CBLOCK-key->md.num; - SHA1_Update(&key->md,out,j); + if (len >= (256 + SHA_CBLOCK)) { + j = (len - (256 + SHA_CBLOCK)) & + (0 - SHA_CBLOCK); + j += SHA_CBLOCK - key->md.num; + SHA1_Update(&key->md, out, j); out += j; len -= j; inp_len -= j; } /* but pretend as if we hashed padded payload */ - bitlen = key->md.Nl+(inp_len<<3); /* at most 18 bits */ + bitlen = key->md.Nl + (inp_len << 3); /* at most 18 bits */ #ifdef BSWAP bitlen = BSWAP(bitlen); #else mac.c[0] = 0; - mac.c[1] = (unsigned char)(bitlen>>16); - mac.c[2] = (unsigned char)(bitlen>>8); + mac.c[1] = (unsigned char)(bitlen >> 16); + mac.c[2] = (unsigned char)(bitlen >> 8); mac.c[3] = (unsigned char)bitlen; bitlen = mac.u[0]; #endif - pmac->u[0]=0; - pmac->u[1]=0; - pmac->u[2]=0; - pmac->u[3]=0; - pmac->u[4]=0; + pmac->u[0] = 0; + pmac->u[1] = 0; + pmac->u[2] = 0; + pmac->u[3] = 0; + pmac->u[4] = 0; - for (res=key->md.num, j=0;jmd.num, j = 0; j < len; j++) { size_t c = out[j]; - mask = (j-inp_len)>>(sizeof(j)*8-8); + mask = (j - inp_len) >> (sizeof(j) * 8 - 8); c &= mask; - c |= 0x80&~mask&~((inp_len-j)>>(sizeof(j)*8-8)); - data->c[res++]=(unsigned char)c; + c |= 0x80 & ~mask & + ~((inp_len - j) >> (sizeof(j) * 8 - 8)); + data->c[res++] = (unsigned char)c; - if (res!=SHA_CBLOCK) continue; + if (res != SHA_CBLOCK) + continue; /* j is not incremented yet */ - mask = 0-((inp_len+7-j)>>(sizeof(j)*8-1)); - data->u[SHA_LBLOCK-1] |= bitlen&mask; - sha1_block_data_order(&key->md,data,1); - mask &= 0-((j-inp_len-72)>>(sizeof(j)*8-1)); + mask = 0 - ((inp_len + 7 - j) >> + (sizeof(j) * 8 - 1)); + data->u[SHA_LBLOCK - 1] |= bitlen&mask; + sha1_block_data_order(&key->md, data, 1); + mask &= 0 - ((j - inp_len - 72) >> + (sizeof(j) * 8 - 1)); pmac->u[0] |= key->md.h0 & mask; pmac->u[1] |= key->md.h1 & mask; pmac->u[2] |= key->md.h2 & mask; pmac->u[3] |= key->md.h3 & mask; pmac->u[4] |= key->md.h4 & mask; - res=0; + res = 0; } - for(i=res;ic[i]=0; + for (i = res; i < SHA_CBLOCK; i++, j++) + data->c[i] = 0; - if (res>SHA_CBLOCK-8) { - mask = 0-((inp_len+8-j)>>(sizeof(j)*8-1)); - data->u[SHA_LBLOCK-1] |= bitlen&mask; - sha1_block_data_order(&key->md,data,1); - mask &= 0-((j-inp_len-73)>>(sizeof(j)*8-1)); + if (res > SHA_CBLOCK - 8) { + mask = 0 - ((inp_len + 8 - j) >> + (sizeof(j) * 8 - 1)); + data->u[SHA_LBLOCK - 1] |= bitlen & mask; + sha1_block_data_order(&key->md, data, 1); + mask &= 0 - ((j - inp_len - 73) >> + (sizeof(j) * 8 - 1)); pmac->u[0] |= key->md.h0 & mask; pmac->u[1] |= key->md.h1 & mask; pmac->u[2] |= key->md.h2 & mask; pmac->u[3] |= key->md.h3 & mask; pmac->u[4] |= key->md.h4 & mask; - memset(data,0,SHA_CBLOCK); - j+=64; + memset(data, 0, SHA_CBLOCK); + j += 64; } - data->u[SHA_LBLOCK-1] = bitlen; - sha1_block_data_order(&key->md,data,1); - mask = 0-((j-inp_len-73)>>(sizeof(j)*8-1)); + data->u[SHA_LBLOCK - 1] = bitlen; + sha1_block_data_order(&key->md, data, 1); + mask = 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1)); pmac->u[0] |= key->md.h0 & mask; pmac->u[1] |= key->md.h1 & mask; pmac->u[2] |= key->md.h2 & mask; @@ -373,209 +389,218 @@ static int aesni_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, pmac->u[3] = BSWAP(pmac->u[3]); pmac->u[4] = BSWAP(pmac->u[4]); #else - for (i=0;i<5;i++) { + for (i = 0; i < 5; i++) { res = pmac->u[i]; - pmac->c[4*i+0]=(unsigned char)(res>>24); - pmac->c[4*i+1]=(unsigned char)(res>>16); - pmac->c[4*i+2]=(unsigned char)(res>>8); - pmac->c[4*i+3]=(unsigned char)res; + pmac->c[4 * i + 0] = (unsigned char)(res >> 24); + pmac->c[4 * i + 1] = (unsigned char)(res >> 16); + pmac->c[4 * i + 2] = (unsigned char)(res >> 8); + pmac->c[4 * i + 3] = (unsigned char)res; } #endif len += SHA_DIGEST_LENGTH; #else - SHA1_Update(&key->md,out,inp_len); + SHA1_Update(&key->md, out, inp_len); res = key->md.num; - SHA1_Final(pmac->c,&key->md); + SHA1_Final(pmac->c, &key->md); { - unsigned int inp_blocks, pad_blocks; - - /* but pretend as if we hashed padded payload */ - inp_blocks = 1+((SHA_CBLOCK-9-res)>>(sizeof(res)*8-1)); - res += (unsigned int)(len-inp_len); - pad_blocks = res / SHA_CBLOCK; - res %= SHA_CBLOCK; - pad_blocks += 1+((SHA_CBLOCK-9-res)>>(sizeof(res)*8-1)); - for (;inp_blocksmd,data,1); + unsigned int inp_blocks, pad_blocks; + + /* but pretend as if we hashed padded payload */ + inp_blocks = 1 + ((SHA_CBLOCK - 9 - res) >> + (sizeof(res) * 8 - 1)); + res += (unsigned int)(len - inp_len); + pad_blocks = res / SHA_CBLOCK; + res %= SHA_CBLOCK; + pad_blocks += 1 + ((SHA_CBLOCK - 9 - res) >> + (sizeof(res) * 8 - 1)); + for (; inp_blocks < pad_blocks; inp_blocks++) + sha1_block_data_order(&key->md, + data, 1); } #endif key->md = key->tail; - SHA1_Update(&key->md,pmac->c,SHA_DIGEST_LENGTH); - SHA1_Final(pmac->c,&key->md); + SHA1_Update(&key->md, pmac->c, SHA_DIGEST_LENGTH); + SHA1_Final(pmac->c, &key->md); /* verify HMAC */ out += inp_len; len -= inp_len; #if 1 { - unsigned char *p = out+len-1-maxpad-SHA_DIGEST_LENGTH; - size_t off = out-p; - unsigned int c, cmask; - - maxpad += SHA_DIGEST_LENGTH; - for (res=0,i=0,j=0;j>(sizeof(int)*8-1); - res |= (c^pad)&~cmask; /* ... and padding */ - cmask &= ((int)(off-1-j))>>(sizeof(int)*8-1); - res |= (c^pmac->c[i])&cmask; - i += 1&cmask; - } - maxpad -= SHA_DIGEST_LENGTH; - - res = 0-((0-res)>>(sizeof(res)*8-1)); - ret &= (int)~res; + unsigned char *p = + out + len - 1 - maxpad - SHA_DIGEST_LENGTH; + size_t off = out - p; + unsigned int c, cmask; + + maxpad += SHA_DIGEST_LENGTH; + for (res = 0, i = 0, j = 0; j < maxpad; j++) { + c = p[j]; + cmask = ((int)(j - off - + SHA_DIGEST_LENGTH)) >> + (sizeof(int) * 8 - 1); + res |= (c ^ pad) & ~cmask; /* ... and padding */ + cmask &= ((int)(off - 1 - j)) >> + (sizeof(int) * 8 - 1); + res |= (c ^ pmac->c[i]) & cmask; + i += 1 & cmask; + } + maxpad -= SHA_DIGEST_LENGTH; + + res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1)); + ret &= (int)~res; } #else - for (res=0,i=0;ic[i]; - res = 0-((0-res)>>(sizeof(res)*8-1)); + for (res = 0, i = 0; i < SHA_DIGEST_LENGTH; i++) + res |= out[i] ^ pmac->c[i]; + res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1)); ret &= (int)~res; /* verify padding */ - pad = (pad&~res) | (maxpad&res); - out = out+len-1-pad; - for (res=0,i=0;i>(sizeof(res)*8-1); + res = (0 - res) >> (sizeof(res) * 8 - 1); ret &= (int)~res; #endif return ret; } else { - SHA1_Update(&key->md,out,len); + SHA1_Update(&key->md, out, len); } } return 1; - } +} -static int aesni_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) - { +static int +aesni_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) +{ EVP_AES_HMAC_SHA1 *key = data(ctx); - switch (type) - { + switch (type) { case EVP_CTRL_AEAD_SET_MAC_KEY: { - unsigned int i; - unsigned char hmac_key[64]; + unsigned int i; + unsigned char hmac_key[64]; - memset (hmac_key,0,sizeof(hmac_key)); + memset (hmac_key, 0, sizeof(hmac_key)); - if (arg > (int)sizeof(hmac_key)) { - SHA1_Init(&key->head); - SHA1_Update(&key->head,ptr,arg); - SHA1_Final(hmac_key,&key->head); - } else { - memcpy(hmac_key,ptr,arg); - } + if (arg > (int)sizeof(hmac_key)) { + SHA1_Init(&key->head); + SHA1_Update(&key->head, ptr, arg); + SHA1_Final(hmac_key, &key->head); + } else { + memcpy(hmac_key, ptr, arg); + } - for (i=0;ihead); - SHA1_Update(&key->head,hmac_key,sizeof(hmac_key)); + for (i = 0; i < sizeof(hmac_key); i++) + hmac_key[i] ^= 0x36; /* ipad */ + SHA1_Init(&key->head); + SHA1_Update(&key->head, hmac_key, sizeof(hmac_key)); - for (i=0;itail); - SHA1_Update(&key->tail,hmac_key,sizeof(hmac_key)); + for (i = 0; i < sizeof(hmac_key); i++) + hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */ + SHA1_Init(&key->tail); + SHA1_Update(&key->tail, hmac_key, sizeof(hmac_key)); - OPENSSL_cleanse(hmac_key,sizeof(hmac_key)); + OPENSSL_cleanse(hmac_key, sizeof(hmac_key)); - return 1; + return 1; } case EVP_CTRL_AEAD_TLS1_AAD: { - unsigned char *p=ptr; - unsigned int len=p[arg-2]<<8|p[arg-1]; - - if (ctx->encrypt) - { - key->payload_length = len; - if ((key->aux.tls_ver=p[arg-4]<<8|p[arg-3]) >= TLS1_1_VERSION) { - len -= AES_BLOCK_SIZE; - p[arg-2] = len>>8; - p[arg-1] = len; - } - key->md = key->head; - SHA1_Update(&key->md,p,arg); - - return (int)(((len+SHA_DIGEST_LENGTH+AES_BLOCK_SIZE)&-AES_BLOCK_SIZE) - - len); - } - else - { - if (arg>13) arg = 13; - memcpy(key->aux.tls_aad,ptr,arg); - key->payload_length = arg; - - return SHA_DIGEST_LENGTH; + unsigned char *p = ptr; + unsigned int len = p[arg - 2] << 8 | p[arg - 1]; + + if (ctx->encrypt) { + key->payload_length = len; + if ((key->aux.tls_ver = p[arg - 4] << 8 | + p[arg - 3]) >= TLS1_1_VERSION) { + len -= AES_BLOCK_SIZE; + p[arg - 2] = len >> 8; + p[arg - 1] = len; + } + key->md = key->head; + SHA1_Update(&key->md, p, arg); + + return (int)(((len + SHA_DIGEST_LENGTH + + AES_BLOCK_SIZE) & -AES_BLOCK_SIZE) - len); + } else { + if (arg > 13) + arg = 13; + memcpy(key->aux.tls_aad, ptr, arg); + key->payload_length = arg; + + return SHA_DIGEST_LENGTH; } } default: return -1; - } } +} -static EVP_CIPHER aesni_128_cbc_hmac_sha1_cipher = - { +static EVP_CIPHER aesni_128_cbc_hmac_sha1_cipher = { #ifdef NID_aes_128_cbc_hmac_sha1 NID_aes_128_cbc_hmac_sha1, #else NID_undef, #endif - 16,16,16, + 16, 16, 16, EVP_CIPH_CBC_MODE|EVP_CIPH_FLAG_DEFAULT_ASN1|EVP_CIPH_FLAG_AEAD_CIPHER, aesni_cbc_hmac_sha1_init_key, aesni_cbc_hmac_sha1_cipher, NULL, sizeof(EVP_AES_HMAC_SHA1), - EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_set_asn1_iv, - EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_get_asn1_iv, + EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv, + EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv, aesni_cbc_hmac_sha1_ctrl, NULL - }; +}; -static EVP_CIPHER aesni_256_cbc_hmac_sha1_cipher = - { +static EVP_CIPHER aesni_256_cbc_hmac_sha1_cipher = { #ifdef NID_aes_256_cbc_hmac_sha1 NID_aes_256_cbc_hmac_sha1, #else NID_undef, #endif - 16,32,16, + 16, 32, 16, EVP_CIPH_CBC_MODE|EVP_CIPH_FLAG_DEFAULT_ASN1|EVP_CIPH_FLAG_AEAD_CIPHER, aesni_cbc_hmac_sha1_init_key, aesni_cbc_hmac_sha1_cipher, NULL, sizeof(EVP_AES_HMAC_SHA1), - EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_set_asn1_iv, - EVP_CIPH_FLAG_DEFAULT_ASN1?NULL:EVP_CIPHER_get_asn1_iv, + EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv, + EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv, aesni_cbc_hmac_sha1_ctrl, NULL - }; +}; -const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha1(void) - { - return(OPENSSL_ia32cap_P[1]&AESNI_CAPABLE? - &aesni_128_cbc_hmac_sha1_cipher:NULL); - } +const EVP_CIPHER * +EVP_aes_128_cbc_hmac_sha1(void) +{ + return(OPENSSL_ia32cap_P[1] & AESNI_CAPABLE? + &aesni_128_cbc_hmac_sha1_cipher : NULL); +} -const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha1(void) - { - return(OPENSSL_ia32cap_P[1]&AESNI_CAPABLE? - &aesni_256_cbc_hmac_sha1_cipher:NULL); - } +const EVP_CIPHER * +EVP_aes_256_cbc_hmac_sha1(void) +{ + return(OPENSSL_ia32cap_P[1] & AESNI_CAPABLE? + &aesni_256_cbc_hmac_sha1_cipher : NULL); +} #else -const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha1(void) - { - return NULL; - } -const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha1(void) - { +const EVP_CIPHER * +EVP_aes_128_cbc_hmac_sha1(void) +{ return NULL; - } +} + +const EVP_CIPHER * +EVP_aes_256_cbc_hmac_sha1(void) +{ + return NULL; +} #endif #endif diff --git a/lib/libssl/src/crypto/evp/e_bf.c b/lib/libssl/src/crypto/evp/e_bf.c index cc224e53639..62194767c87 100644 --- a/lib/libssl/src/crypto/evp/e_bf.c +++ b/lib/libssl/src/crypto/evp/e_bf.c @@ -5,21 +5,21 @@ * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. - * + * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). - * + * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -34,10 +34,10 @@ * Eric Young (eay@cryptsoft.com)" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). - * 4. If you include any Windows specific code (or a derivative thereof) from + * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" - * + * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -49,7 +49,7 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * + * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence @@ -65,24 +65,23 @@ #include static int bf_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc); + const unsigned char *iv, int enc); -typedef struct - { +typedef struct { BF_KEY ks; - } EVP_BF_KEY; +} EVP_BF_KEY; #define data(ctx) EVP_C_DATA(EVP_BF_KEY,ctx) IMPLEMENT_BLOCK_CIPHER(bf, ks, BF, EVP_BF_KEY, NID_bf, 8, 16, 8, 64, - EVP_CIPH_VARIABLE_LENGTH, bf_init_key, NULL, - EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv, NULL) - -static int bf_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { - BF_set_key(&data(ctx)->ks,EVP_CIPHER_CTX_key_length(ctx),key); - return 1; - } + EVP_CIPH_VARIABLE_LENGTH, bf_init_key, NULL, + EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv, NULL) +static int +bf_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ + BF_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx), key); + return 1; +} #endif diff --git a/lib/libssl/src/crypto/evp/e_camellia.c b/lib/libssl/src/crypto/evp/e_camellia.c index 8bb7c320d39..377d121b891 100644 --- a/lib/libssl/src/crypto/evp/e_camellia.c +++ b/lib/libssl/src/crypto/evp/e_camellia.c @@ -7,7 +7,7 @@ * are met: * * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. + * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in @@ -63,63 +63,61 @@ #include "evp_locl.h" static int camellia_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc); + const unsigned char *iv, int enc); /* Camellia subkey Structure */ -typedef struct - { +typedef struct { CAMELLIA_KEY ks; - } EVP_CAMELLIA_KEY; +} EVP_CAMELLIA_KEY; /* Attribute operation for Camellia */ #define data(ctx) EVP_C_DATA(EVP_CAMELLIA_KEY,ctx) IMPLEMENT_BLOCK_CIPHER(camellia_128, ks, Camellia, EVP_CAMELLIA_KEY, - NID_camellia_128, 16, 16, 16, 128, - 0, camellia_init_key, NULL, - EVP_CIPHER_set_asn1_iv, - EVP_CIPHER_get_asn1_iv, - NULL) + NID_camellia_128, 16, 16, 16, 128, + 0, camellia_init_key, NULL, + EVP_CIPHER_set_asn1_iv, + EVP_CIPHER_get_asn1_iv, + NULL) IMPLEMENT_BLOCK_CIPHER(camellia_192, ks, Camellia, EVP_CAMELLIA_KEY, - NID_camellia_192, 16, 24, 16, 128, - 0, camellia_init_key, NULL, - EVP_CIPHER_set_asn1_iv, - EVP_CIPHER_get_asn1_iv, - NULL) + NID_camellia_192, 16, 24, 16, 128, + 0, camellia_init_key, NULL, + EVP_CIPHER_set_asn1_iv, + EVP_CIPHER_get_asn1_iv, + NULL) IMPLEMENT_BLOCK_CIPHER(camellia_256, ks, Camellia, EVP_CAMELLIA_KEY, - NID_camellia_256, 16, 32, 16, 128, - 0, camellia_init_key, NULL, - EVP_CIPHER_set_asn1_iv, - EVP_CIPHER_get_asn1_iv, - NULL) + NID_camellia_256, 16, 32, 16, 128, + 0, camellia_init_key, NULL, + EVP_CIPHER_set_asn1_iv, + EVP_CIPHER_get_asn1_iv, + NULL) #define IMPLEMENT_CAMELLIA_CFBR(ksize,cbits) IMPLEMENT_CFBR(camellia,Camellia,EVP_CAMELLIA_KEY,ks,ksize,cbits,16) -IMPLEMENT_CAMELLIA_CFBR(128,1) -IMPLEMENT_CAMELLIA_CFBR(192,1) -IMPLEMENT_CAMELLIA_CFBR(256,1) +IMPLEMENT_CAMELLIA_CFBR(128, 1) +IMPLEMENT_CAMELLIA_CFBR(192, 1) +IMPLEMENT_CAMELLIA_CFBR(256, 1) -IMPLEMENT_CAMELLIA_CFBR(128,8) -IMPLEMENT_CAMELLIA_CFBR(192,8) -IMPLEMENT_CAMELLIA_CFBR(256,8) +IMPLEMENT_CAMELLIA_CFBR(128, 8) +IMPLEMENT_CAMELLIA_CFBR(192, 8) +IMPLEMENT_CAMELLIA_CFBR(256, 8) - -/* The subkey for Camellia is generated. */ -static int camellia_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { +/* The subkey for Camellia is generated. */ +static int +camellia_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ int ret; - ret=Camellia_set_key(key, ctx->key_len * 8, ctx->cipher_data); + ret = Camellia_set_key(key, ctx->key_len * 8, ctx->cipher_data); - if(ret < 0) - { - EVPerr(EVP_F_CAMELLIA_INIT_KEY,EVP_R_CAMELLIA_KEY_SETUP_FAILED); + if (ret < 0) { + EVPerr(EVP_F_CAMELLIA_INIT_KEY, + EVP_R_CAMELLIA_KEY_SETUP_FAILED); return 0; - } - - return 1; } + return 1; +} #endif diff --git a/lib/libssl/src/crypto/evp/e_cast.c b/lib/libssl/src/crypto/evp/e_cast.c index d77bcd9298f..199c5bf48e6 100644 --- a/lib/libssl/src/crypto/evp/e_cast.c +++ b/lib/libssl/src/crypto/evp/e_cast.c @@ -5,21 +5,21 @@ * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. - * + * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). - * + * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -34,10 +34,10 @@ * Eric Young (eay@cryptsoft.com)" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). - * 4. If you include any Windows specific code (or a derivative thereof) from + * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" - * + * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -49,7 +49,7 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * + * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence @@ -66,25 +66,24 @@ #include static int cast_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv,int enc); + const unsigned char *iv, int enc); -typedef struct - { +typedef struct { CAST_KEY ks; - } EVP_CAST_KEY; +} EVP_CAST_KEY; #define data(ctx) EVP_C_DATA(EVP_CAST_KEY,ctx) -IMPLEMENT_BLOCK_CIPHER(cast5, ks, CAST, EVP_CAST_KEY, - NID_cast5, 8, CAST_KEY_LENGTH, 8, 64, - EVP_CIPH_VARIABLE_LENGTH, cast_init_key, NULL, - EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv, NULL) - -static int cast_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { - CAST_set_key(&data(ctx)->ks,EVP_CIPHER_CTX_key_length(ctx),key); - return 1; - } +IMPLEMENT_BLOCK_CIPHER(cast5, ks, CAST, EVP_CAST_KEY, + NID_cast5, 8, CAST_KEY_LENGTH, 8, 64, + EVP_CIPH_VARIABLE_LENGTH, cast_init_key, NULL, + EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv, NULL) +static int +cast_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ + CAST_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx), key); + return 1; +} #endif diff --git a/lib/libssl/src/crypto/evp/e_chacha.c b/lib/libssl/src/crypto/evp/e_chacha.c index 4a20186006a..0c32b99df4e 100644 --- a/lib/libssl/src/crypto/evp/e_chacha.c +++ b/lib/libssl/src/crypto/evp/e_chacha.c @@ -41,7 +41,7 @@ static const EVP_CIPHER chacha20_cipher = { const EVP_CIPHER * EVP_chacha20(void) { - return(&chacha20_cipher); + return (&chacha20_cipher); } static int diff --git a/lib/libssl/src/crypto/evp/e_des.c b/lib/libssl/src/crypto/evp/e_des.c index ca009f2c525..ac46ba6a966 100644 --- a/lib/libssl/src/crypto/evp/e_des.c +++ b/lib/libssl/src/crypto/evp/e_des.c @@ -5,21 +5,21 @@ * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. - * + * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). - * + * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -34,10 +34,10 @@ * Eric Young (eay@cryptsoft.com)" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). - * 4. If you include any Windows specific code (or a derivative thereof) from + * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" - * + * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -49,7 +49,7 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * + * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence @@ -66,150 +66,155 @@ #include static int des_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc); + const unsigned char *iv, int enc); static int des_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr); /* Because of various casts and different names can't use IMPLEMENT_BLOCK_CIPHER */ -static int des_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) +static int +des_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) { BLOCK_CIPHER_ecb_loop() - DES_ecb_encrypt((DES_cblock *)(in + i), (DES_cblock *)(out + i), ctx->cipher_data, ctx->encrypt); + DES_ecb_encrypt((DES_cblock *)(in + i), (DES_cblock *)(out + i), + ctx->cipher_data, ctx->encrypt); return 1; } -static int des_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) +static int +des_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) { - while(inl>=EVP_MAXCHUNK) - { + while (inl >= EVP_MAXCHUNK) { DES_ofb64_encrypt(in, out, (long)EVP_MAXCHUNK, ctx->cipher_data, - (DES_cblock *)ctx->iv, &ctx->num); - inl-=EVP_MAXCHUNK; - in +=EVP_MAXCHUNK; - out+=EVP_MAXCHUNK; - } + (DES_cblock *)ctx->iv, &ctx->num); + inl -= EVP_MAXCHUNK; + in += EVP_MAXCHUNK; + out += EVP_MAXCHUNK; + } if (inl) DES_ofb64_encrypt(in, out, (long)inl, ctx->cipher_data, - (DES_cblock *)ctx->iv, &ctx->num); + (DES_cblock *)ctx->iv, &ctx->num); return 1; } -static int des_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) +static int +des_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) { - while(inl>=EVP_MAXCHUNK) - { + while (inl >= EVP_MAXCHUNK) { DES_ncbc_encrypt(in, out, (long)EVP_MAXCHUNK, ctx->cipher_data, - (DES_cblock *)ctx->iv, ctx->encrypt); - inl-=EVP_MAXCHUNK; - in +=EVP_MAXCHUNK; - out+=EVP_MAXCHUNK; - } + (DES_cblock *)ctx->iv, ctx->encrypt); + inl -= EVP_MAXCHUNK; + in += EVP_MAXCHUNK; + out += EVP_MAXCHUNK; + } if (inl) DES_ncbc_encrypt(in, out, (long)inl, ctx->cipher_data, - (DES_cblock *)ctx->iv, ctx->encrypt); + (DES_cblock *)ctx->iv, ctx->encrypt); return 1; } -static int des_cfb64_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) +static int +des_cfb64_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) { - while(inl>=EVP_MAXCHUNK) - { - DES_cfb64_encrypt(in,out, (long)EVP_MAXCHUNK, ctx->cipher_data, - (DES_cblock *)ctx->iv, &ctx->num, ctx->encrypt); - inl-=EVP_MAXCHUNK; - in +=EVP_MAXCHUNK; - out+=EVP_MAXCHUNK; - } + while (inl >= EVP_MAXCHUNK) { + DES_cfb64_encrypt(in, out, (long)EVP_MAXCHUNK, ctx->cipher_data, + (DES_cblock *)ctx->iv, &ctx->num, ctx->encrypt); + inl -= EVP_MAXCHUNK; + in += EVP_MAXCHUNK; + out += EVP_MAXCHUNK; + } if (inl) DES_cfb64_encrypt(in, out, (long)inl, ctx->cipher_data, - (DES_cblock *)ctx->iv, &ctx->num, ctx->encrypt); + (DES_cblock *)ctx->iv, &ctx->num, ctx->encrypt); return 1; } /* Although we have a CFB-r implementation for DES, it doesn't pack the right way, so wrap it here */ -static int des_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) - { - size_t n,chunk=EVP_MAXCHUNK/8; - unsigned char c[1],d[1]; - - if (inl=chunk) - { - for(n=0 ; n < chunk*8; ++n) - { - c[0]=(in[n/8]&(1 << (7-n%8))) ? 0x80 : 0; - DES_cfb_encrypt(c,d,1,1,ctx->cipher_data,(DES_cblock *)ctx->iv, - ctx->encrypt); - out[n/8]=(out[n/8]&~(0x80 >> (unsigned int)(n%8))) | - ((d[0]&0x80) >> (unsigned int)(n%8)); - } - inl-=chunk; - in +=chunk; - out+=chunk; - if (inl= chunk) { + for (n = 0; n < chunk*8; ++n) { + c[0] = (in[n / 8] & (1 << (7 - n % 8))) ? 0x80 : 0; + DES_cfb_encrypt(c, d, 1, 1, ctx->cipher_data, + (DES_cblock *)ctx->iv, ctx->encrypt); + out[n / 8] = (out[n / 8] & + ~(0x80 >> (unsigned int)(n % 8))) | + ((d[0] & 0x80) >> (unsigned int)(n % 8)); + } + inl -= chunk; + in += chunk; + out += chunk; + if (inl < chunk) + chunk = inl; } - return 1; - } - -static int des_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) - { - while (inl>=EVP_MAXCHUNK) - { - DES_cfb_encrypt(in,out,8,(long)EVP_MAXCHUNK,ctx->cipher_data, - (DES_cblock *)ctx->iv,ctx->encrypt); - inl-=EVP_MAXCHUNK; - in +=EVP_MAXCHUNK; - out+=EVP_MAXCHUNK; + return 1; +} + +static int +des_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) +{ + while (inl >= EVP_MAXCHUNK) { + DES_cfb_encrypt(in, out, 8, (long)EVP_MAXCHUNK, + ctx->cipher_data, (DES_cblock *)ctx->iv, ctx->encrypt); + inl -= EVP_MAXCHUNK; + in += EVP_MAXCHUNK; + out += EVP_MAXCHUNK; } - if (inl) - DES_cfb_encrypt(in,out,8,(long)inl,ctx->cipher_data, - (DES_cblock *)ctx->iv,ctx->encrypt); - return 1; - } + if (inl) + DES_cfb_encrypt(in, out, 8,(long)inl, ctx->cipher_data, + (DES_cblock *)ctx->iv, ctx->encrypt); + return 1; +} BLOCK_CIPHER_defs(des, DES_key_schedule, NID_des, 8, 8, 8, 64, - EVP_CIPH_RAND_KEY, des_init_key, NULL, - EVP_CIPHER_set_asn1_iv, - EVP_CIPHER_get_asn1_iv, - des_ctrl) - -BLOCK_CIPHER_def_cfb(des,DES_key_schedule,NID_des,8,8,1, - EVP_CIPH_RAND_KEY, des_init_key,NULL, - EVP_CIPHER_set_asn1_iv, - EVP_CIPHER_get_asn1_iv,des_ctrl) - -BLOCK_CIPHER_def_cfb(des,DES_key_schedule,NID_des,8,8,8, - EVP_CIPH_RAND_KEY,des_init_key,NULL, - EVP_CIPHER_set_asn1_iv, - EVP_CIPHER_get_asn1_iv,des_ctrl) - -static int des_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { + EVP_CIPH_RAND_KEY, des_init_key, NULL, + EVP_CIPHER_set_asn1_iv, + EVP_CIPHER_get_asn1_iv, + des_ctrl) + +BLOCK_CIPHER_def_cfb(des, DES_key_schedule, NID_des, 8,8, 1, + EVP_CIPH_RAND_KEY, des_init_key, NULL, + EVP_CIPHER_set_asn1_iv, + EVP_CIPHER_get_asn1_iv, des_ctrl) + +BLOCK_CIPHER_def_cfb(des, DES_key_schedule, NID_des, 8,8, 8, + EVP_CIPH_RAND_KEY, des_init_key, NULL, + EVP_CIPHER_set_asn1_iv, + EVP_CIPHER_get_asn1_iv, des_ctrl) + +static int +des_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ DES_cblock *deskey = (DES_cblock *)key; + #ifdef EVP_CHECK_DES_KEY - if(DES_set_key_checked(deskey,ctx->cipher_data) != 0) + if (DES_set_key_checked(deskey, ctx->cipher_data) != 0) return 0; #else - DES_set_key_unchecked(deskey,ctx->cipher_data); + DES_set_key_unchecked(deskey, ctx->cipher_data); #endif return 1; - } +} -static int des_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) - { - - switch(type) - { +static int +des_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) +{ + switch (type) { case EVP_CTRL_RAND_KEY: if (RAND_bytes(ptr, 8) <= 0) return 0; @@ -218,7 +223,7 @@ static int des_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) default: return -1; - } } +} #endif diff --git a/lib/libssl/src/crypto/evp/e_des3.c b/lib/libssl/src/crypto/evp/e_des3.c index 8d7b7de292b..ddb069dda57 100644 --- a/lib/libssl/src/crypto/evp/e_des3.c +++ b/lib/libssl/src/crypto/evp/e_des3.c @@ -5,21 +5,21 @@ * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. - * + * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). - * + * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -34,10 +34,10 @@ * Eric Young (eay@cryptsoft.com)" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). - * 4. If you include any Windows specific code (or a derivative thereof) from + * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" - * + * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -49,7 +49,7 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * + * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence @@ -68,150 +68,147 @@ #ifndef OPENSSL_FIPS static int des_ede_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv,int enc); + const unsigned char *iv, int enc); static int des_ede3_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv,int enc); + const unsigned char *iv, int enc); static int des3_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr); -typedef struct - { +typedef struct { DES_key_schedule ks1;/* key schedule */ DES_key_schedule ks2;/* key schedule (for ede) */ DES_key_schedule ks3;/* key schedule (for ede3) */ - } DES_EDE_KEY; +} DES_EDE_KEY; #define data(ctx) ((DES_EDE_KEY *)(ctx)->cipher_data) /* Because of various casts and different args can't use IMPLEMENT_BLOCK_CIPHER */ -static int des_ede_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) +static int +des_ede_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) { BLOCK_CIPHER_ecb_loop() - DES_ecb3_encrypt((const_DES_cblock *)(in + i), - (DES_cblock *)(out + i), - &data(ctx)->ks1, &data(ctx)->ks2, - &data(ctx)->ks3, - ctx->encrypt); + DES_ecb3_encrypt((const_DES_cblock *)(in + i), (DES_cblock *)(out + i), + &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, ctx->encrypt); return 1; } -static int des_ede_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) +static int +des_ede_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) { - while (inl>=EVP_MAXCHUNK) - { + while (inl >= EVP_MAXCHUNK) { DES_ede3_ofb64_encrypt(in, out, (long)EVP_MAXCHUNK, - &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, - (DES_cblock *)ctx->iv, &ctx->num); - inl-=EVP_MAXCHUNK; - in +=EVP_MAXCHUNK; - out+=EVP_MAXCHUNK; - } + &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, + (DES_cblock *)ctx->iv, &ctx->num); + inl -= EVP_MAXCHUNK; + in += EVP_MAXCHUNK; + out += EVP_MAXCHUNK; + } if (inl) DES_ede3_ofb64_encrypt(in, out, (long)inl, - &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, - (DES_cblock *)ctx->iv, &ctx->num); + &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, + (DES_cblock *)ctx->iv, &ctx->num); return 1; } -static int des_ede_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) +static int +des_ede_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) { #ifdef KSSL_DEBUG { - int i; - char *cp; - printf("des_ede_cbc_cipher(ctx=%lx, buflen=%d)\n", ctx, ctx->buf_len); - printf("\t iv= "); - for(i=0;i<8;i++) - printf("%02X",ctx->iv[i]); - printf("\n"); + int i; + char *cp; + printf("des_ede_cbc_cipher(ctx=%lx, buflen=%d)\n", ctx, ctx->buf_len); + printf("\t iv= "); + for (i = 0; i < 8; i++) + printf("%02X",ctx->iv[i]); + printf("\n"); } #endif /* KSSL_DEBUG */ - while (inl>=EVP_MAXCHUNK) - { + while (inl >= EVP_MAXCHUNK) { DES_ede3_cbc_encrypt(in, out, (long)EVP_MAXCHUNK, - &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, - (DES_cblock *)ctx->iv, ctx->encrypt); - inl-=EVP_MAXCHUNK; - in +=EVP_MAXCHUNK; - out+=EVP_MAXCHUNK; - } + &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, + (DES_cblock *)ctx->iv, ctx->encrypt); + inl -= EVP_MAXCHUNK; + in += EVP_MAXCHUNK; + out += EVP_MAXCHUNK; + } if (inl) DES_ede3_cbc_encrypt(in, out, (long)inl, - &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, - (DES_cblock *)ctx->iv, ctx->encrypt); + &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, + (DES_cblock *)ctx->iv, ctx->encrypt); return 1; } -static int des_ede_cfb64_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) +static int +des_ede_cfb64_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) { - while (inl>=EVP_MAXCHUNK) - { - DES_ede3_cfb64_encrypt(in, out, (long)EVP_MAXCHUNK, - &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, - (DES_cblock *)ctx->iv, &ctx->num, ctx->encrypt); - inl-=EVP_MAXCHUNK; - in +=EVP_MAXCHUNK; - out+=EVP_MAXCHUNK; - } + while (inl >= EVP_MAXCHUNK) { + DES_ede3_cfb64_encrypt(in, out, (long)EVP_MAXCHUNK, + &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, + (DES_cblock *)ctx->iv, &ctx->num, ctx->encrypt); + inl -= EVP_MAXCHUNK; + in += EVP_MAXCHUNK; + out += EVP_MAXCHUNK; + } if (inl) DES_ede3_cfb64_encrypt(in, out, (long)inl, - &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, - (DES_cblock *)ctx->iv, &ctx->num, ctx->encrypt); + &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, + (DES_cblock *)ctx->iv, &ctx->num, ctx->encrypt); return 1; } /* Although we have a CFB-r implementation for 3-DES, it doesn't pack the right way, so wrap it here */ -static int des_ede3_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) - { - size_t n; - unsigned char c[1],d[1]; +static int +des_ede3_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) +{ + size_t n; + unsigned char c[1], d[1]; - for(n=0 ; n < inl ; ++n) - { - c[0]=(in[n/8]&(1 << (7-n%8))) ? 0x80 : 0; - DES_ede3_cfb_encrypt(c,d,1,1, - &data(ctx)->ks1,&data(ctx)->ks2,&data(ctx)->ks3, - (DES_cblock *)ctx->iv,ctx->encrypt); - out[n/8]=(out[n/8]&~(0x80 >> (unsigned int)(n%8))) | - ((d[0]&0x80) >> (unsigned int)(n%8)); + for (n = 0; n < inl; ++n) { + c[0] = (in[n/8]&(1 << (7 - n % 8))) ? 0x80 : 0; + DES_ede3_cfb_encrypt(c, d, 1, 1, + &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, + (DES_cblock *)ctx->iv, ctx->encrypt); + out[n / 8] = (out[n / 8] & ~(0x80 >> (unsigned int)(n % 8))) | + ((d[0] & 0x80) >> (unsigned int)(n % 8)); } - return 1; - } + return 1; +} -static int des_ede3_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) - { - while (inl>=EVP_MAXCHUNK) - { - DES_ede3_cfb_encrypt(in,out,8,(long)EVP_MAXCHUNK, - &data(ctx)->ks1,&data(ctx)->ks2,&data(ctx)->ks3, - (DES_cblock *)ctx->iv,ctx->encrypt); - inl-=EVP_MAXCHUNK; - in +=EVP_MAXCHUNK; - out+=EVP_MAXCHUNK; +static int +des_ede3_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) +{ + while (inl >= EVP_MAXCHUNK) { + DES_ede3_cfb_encrypt(in, out, 8, (long)EVP_MAXCHUNK, + &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, + (DES_cblock *)ctx->iv, ctx->encrypt); + inl -= EVP_MAXCHUNK; + in += EVP_MAXCHUNK; + out += EVP_MAXCHUNK; } - if (inl) - DES_ede3_cfb_encrypt(in,out,8,(long)inl, - &data(ctx)->ks1,&data(ctx)->ks2,&data(ctx)->ks3, - (DES_cblock *)ctx->iv,ctx->encrypt); - return 1; - } + if (inl) + DES_ede3_cfb_encrypt(in, out, 8, (long)inl, + &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3, + (DES_cblock *)ctx->iv, ctx->encrypt); + return 1; +} BLOCK_CIPHER_defs(des_ede, DES_EDE_KEY, NID_des_ede, 8, 16, 8, 64, - EVP_CIPH_RAND_KEY, des_ede_init_key, NULL, - EVP_CIPHER_set_asn1_iv, - EVP_CIPHER_get_asn1_iv, - des3_ctrl) + EVP_CIPH_RAND_KEY, des_ede_init_key, NULL, + EVP_CIPHER_set_asn1_iv, + EVP_CIPHER_get_asn1_iv, + des3_ctrl) #define des_ede3_cfb64_cipher des_ede_cfb64_cipher #define des_ede3_ofb_cipher des_ede_ofb_cipher @@ -219,75 +216,78 @@ BLOCK_CIPHER_defs(des_ede, DES_EDE_KEY, NID_des_ede, 8, 16, 8, 64, #define des_ede3_ecb_cipher des_ede_ecb_cipher BLOCK_CIPHER_defs(des_ede3, DES_EDE_KEY, NID_des_ede3, 8, 24, 8, 64, - EVP_CIPH_RAND_KEY, des_ede3_init_key, NULL, - EVP_CIPHER_set_asn1_iv, - EVP_CIPHER_get_asn1_iv, - des3_ctrl) - -BLOCK_CIPHER_def_cfb(des_ede3,DES_EDE_KEY,NID_des_ede3,24,8,1, - EVP_CIPH_RAND_KEY, des_ede3_init_key,NULL, - EVP_CIPHER_set_asn1_iv, - EVP_CIPHER_get_asn1_iv, - des3_ctrl) - -BLOCK_CIPHER_def_cfb(des_ede3,DES_EDE_KEY,NID_des_ede3,24,8,8, - EVP_CIPH_RAND_KEY, des_ede3_init_key,NULL, - EVP_CIPHER_set_asn1_iv, - EVP_CIPHER_get_asn1_iv, - des3_ctrl) + EVP_CIPH_RAND_KEY, des_ede3_init_key, NULL, + EVP_CIPHER_set_asn1_iv, + EVP_CIPHER_get_asn1_iv, + des3_ctrl) -static int des_ede_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { +BLOCK_CIPHER_def_cfb(des_ede3, DES_EDE_KEY, NID_des_ede3, 24, 8, 1, + EVP_CIPH_RAND_KEY, des_ede3_init_key, NULL, + EVP_CIPHER_set_asn1_iv, + EVP_CIPHER_get_asn1_iv, + des3_ctrl) + +BLOCK_CIPHER_def_cfb(des_ede3, DES_EDE_KEY, NID_des_ede3, 24, 8, 8, + EVP_CIPH_RAND_KEY, des_ede3_init_key, NULL, + EVP_CIPHER_set_asn1_iv, + EVP_CIPHER_get_asn1_iv, + des3_ctrl) + +static int +des_ede_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ DES_cblock *deskey = (DES_cblock *)key; + #ifdef EVP_CHECK_DES_KEY - if (DES_set_key_checked(&deskey[0],&data(ctx)->ks1) - !! DES_set_key_checked(&deskey[1],&data(ctx)->ks2)) + if (DES_set_key_checked(&deskey[0], &data(ctx)->ks1) + !! DES_set_key_checked(&deskey[1], &data(ctx)->ks2)) return 0; #else - DES_set_key_unchecked(&deskey[0],&data(ctx)->ks1); - DES_set_key_unchecked(&deskey[1],&data(ctx)->ks2); + DES_set_key_unchecked(&deskey[0], &data(ctx)->ks1); + DES_set_key_unchecked(&deskey[1], &data(ctx)->ks2); #endif - memcpy(&data(ctx)->ks3,&data(ctx)->ks1, - sizeof(data(ctx)->ks1)); + memcpy(&data(ctx)->ks3, &data(ctx)->ks1, + sizeof(data(ctx)->ks1)); return 1; - } +} -static int des_ede3_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { +static int +des_ede3_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ DES_cblock *deskey = (DES_cblock *)key; + #ifdef KSSL_DEBUG { - int i; - printf("des_ede3_init_key(ctx=%lx)\n", ctx); - printf("\tKEY= "); - for(i=0;i<24;i++) printf("%02X",key[i]); printf("\n"); - printf("\t IV= "); - for(i=0;i<8;i++) printf("%02X",iv[i]); printf("\n"); + int i; + printf("des_ede3_init_key(ctx=%lx)\n", ctx); + printf("\tKEY= "); + for(i=0;i<24;i++) printf("%02X",key[i]); printf("\n"); + printf("\t IV= "); + for(i=0;i<8;i++) printf("%02X",iv[i]); printf("\n"); } #endif /* KSSL_DEBUG */ #ifdef EVP_CHECK_DES_KEY - if (DES_set_key_checked(&deskey[0],&data(ctx)->ks1) - || DES_set_key_checked(&deskey[1],&data(ctx)->ks2) - || DES_set_key_checked(&deskey[2],&data(ctx)->ks3)) - return 0; + if (DES_set_key_checked(&deskey[0], &data(ctx)->ks1) || + DES_set_key_checked(&deskey[1], &data(ctx)->ks2) || + DES_set_key_checked(&deskey[2], &data(ctx)->ks3)) + return 0; #else - DES_set_key_unchecked(&deskey[0],&data(ctx)->ks1); - DES_set_key_unchecked(&deskey[1],&data(ctx)->ks2); - DES_set_key_unchecked(&deskey[2],&data(ctx)->ks3); + DES_set_key_unchecked(&deskey[0], &data(ctx)->ks1); + DES_set_key_unchecked(&deskey[1], &data(ctx)->ks2); + DES_set_key_unchecked(&deskey[2], &data(ctx)->ks3); #endif return 1; - } - -static int des3_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) - { +} +static int +des3_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) +{ DES_cblock *deskey = ptr; - switch(type) - { + switch (type) { case EVP_CTRL_RAND_KEY: if (RAND_bytes(ptr, c->key_len) <= 0) return 0; @@ -300,15 +300,17 @@ static int des3_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) default: return -1; - } } +} -const EVP_CIPHER *EVP_des_ede(void) +const EVP_CIPHER * +EVP_des_ede(void) { return &des_ede_ecb; } -const EVP_CIPHER *EVP_des_ede3(void) +const EVP_CIPHER * +EVP_des_ede3(void) { return &des_ede3_ecb; } diff --git a/lib/libssl/src/crypto/evp/e_idea.c b/lib/libssl/src/crypto/evp/e_idea.c index 806b0803600..8b8a647a8cc 100644 --- a/lib/libssl/src/crypto/evp/e_idea.c +++ b/lib/libssl/src/crypto/evp/e_idea.c @@ -5,21 +5,21 @@ * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. - * + * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). - * + * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -34,10 +34,10 @@ * Eric Young (eay@cryptsoft.com)" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). - * 4. If you include any Windows specific code (or a derivative thereof) from + * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" - * + * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -49,7 +49,7 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * + * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence @@ -66,53 +66,56 @@ #include static int idea_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv,int enc); + const unsigned char *iv, int enc); /* NB idea_ecb_encrypt doesn't take an 'encrypt' argument so we treat it as a special - * case + * case */ -static int idea_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) +static int +idea_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) { BLOCK_CIPHER_ecb_loop() - idea_ecb_encrypt(in + i, out + i, ctx->cipher_data); + idea_ecb_encrypt(in + i, out + i, ctx->cipher_data); return 1; } /* Can't use IMPLEMENT_BLOCK_CIPHER because idea_ecb_encrypt is different */ -typedef struct - { +typedef struct { IDEA_KEY_SCHEDULE ks; - } EVP_IDEA_KEY; +} EVP_IDEA_KEY; BLOCK_CIPHER_func_cbc(idea, idea, EVP_IDEA_KEY, ks) BLOCK_CIPHER_func_ofb(idea, idea, 64, EVP_IDEA_KEY, ks) BLOCK_CIPHER_func_cfb(idea, idea, 64, EVP_IDEA_KEY, ks) BLOCK_CIPHER_defs(idea, IDEA_KEY_SCHEDULE, NID_idea, 8, 16, 8, 64, - 0, idea_init_key, NULL, - EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv, NULL) + 0, idea_init_key, NULL, + EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv, NULL) -static int idea_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { - if(!enc) { - if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_OFB_MODE) enc = 1; - else if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_CFB_MODE) enc = 1; +static int +idea_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ + if (!enc) { + if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_OFB_MODE) + enc = 1; + else if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_CFB_MODE) + enc = 1; } - if (enc) idea_set_encrypt_key(key,ctx->cipher_data); - else - { + if (enc) + idea_set_encrypt_key(key, ctx->cipher_data); + else { IDEA_KEY_SCHEDULE tmp; - idea_set_encrypt_key(key,&tmp); - idea_set_decrypt_key(&tmp,ctx->cipher_data); + idea_set_encrypt_key(key, &tmp); + idea_set_decrypt_key(&tmp, ctx->cipher_data); OPENSSL_cleanse((unsigned char *)&tmp, - sizeof(IDEA_KEY_SCHEDULE)); - } - return 1; + sizeof(IDEA_KEY_SCHEDULE)); } + return 1; +} #endif diff --git a/lib/libssl/src/crypto/evp/e_null.c b/lib/libssl/src/crypto/evp/e_null.c index 98a78499f96..d94751a07a5 100644 --- a/lib/libssl/src/crypto/evp/e_null.c +++ b/lib/libssl/src/crypto/evp/e_null.c @@ -5,21 +5,21 @@ * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. - * + * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). - * + * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -34,10 +34,10 @@ * Eric Young (eay@cryptsoft.com)" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). - * 4. If you include any Windows specific code (or a derivative thereof) from + * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" - * + * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -49,7 +49,7 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * + * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence @@ -62,13 +62,13 @@ #include static int null_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv,int enc); + const unsigned char *iv, int enc); static int null_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl); -static const EVP_CIPHER n_cipher= - { + const unsigned char *in, size_t inl); + +static const EVP_CIPHER n_cipher = { NID_undef, - 1,0,0, + 1, 0, 0, 0, null_init_key, null_cipher, @@ -78,24 +78,27 @@ static const EVP_CIPHER n_cipher= NULL, NULL, NULL - }; +}; -const EVP_CIPHER *EVP_enc_null(void) - { - return(&n_cipher); - } +const EVP_CIPHER * +EVP_enc_null(void) +{ + return (&n_cipher); +} -static int null_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { +static int +null_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ /* memset(&(ctx->c),0,sizeof(ctx->c));*/ return 1; - } +} -static int null_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) - { +static int +null_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) +{ if (in != out) - memcpy((char *)out,(const char *)in,inl); + memcpy((char *)out, (const char *)in, inl); return 1; - } +} diff --git a/lib/libssl/src/crypto/evp/e_old.c b/lib/libssl/src/crypto/evp/e_old.c index 1642af4869d..c27b61a4bfc 100644 --- a/lib/libssl/src/crypto/evp/e_old.c +++ b/lib/libssl/src/crypto/evp/e_old.c @@ -10,7 +10,7 @@ * are met: * * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. + * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in @@ -71,55 +71,88 @@ static void *dummy = &dummy; #ifndef OPENSSL_NO_BF #undef EVP_bf_cfb const EVP_CIPHER *EVP_bf_cfb(void); -const EVP_CIPHER *EVP_bf_cfb(void) { return EVP_bf_cfb64(); } +const EVP_CIPHER *EVP_bf_cfb(void) +{ + return EVP_bf_cfb64(); +} #endif #ifndef OPENSSL_NO_DES #undef EVP_des_cfb const EVP_CIPHER *EVP_des_cfb(void); -const EVP_CIPHER *EVP_des_cfb(void) { return EVP_des_cfb64(); } +const EVP_CIPHER *EVP_des_cfb(void) +{ + return EVP_des_cfb64(); +} #undef EVP_des_ede3_cfb const EVP_CIPHER *EVP_des_ede3_cfb(void); -const EVP_CIPHER *EVP_des_ede3_cfb(void) { return EVP_des_ede3_cfb64(); } +const EVP_CIPHER *EVP_des_ede3_cfb(void) +{ + return EVP_des_ede3_cfb64(); +} #undef EVP_des_ede_cfb const EVP_CIPHER *EVP_des_ede_cfb(void); -const EVP_CIPHER *EVP_des_ede_cfb(void) { return EVP_des_ede_cfb64(); } +const EVP_CIPHER *EVP_des_ede_cfb(void) +{ + return EVP_des_ede_cfb64(); +} #endif #ifndef OPENSSL_NO_IDEA #undef EVP_idea_cfb const EVP_CIPHER *EVP_idea_cfb(void); -const EVP_CIPHER *EVP_idea_cfb(void) { return EVP_idea_cfb64(); } +const EVP_CIPHER *EVP_idea_cfb(void) +{ + return EVP_idea_cfb64(); +} #endif #ifndef OPENSSL_NO_RC2 #undef EVP_rc2_cfb const EVP_CIPHER *EVP_rc2_cfb(void); -const EVP_CIPHER *EVP_rc2_cfb(void) { return EVP_rc2_cfb64(); } +const EVP_CIPHER *EVP_rc2_cfb(void) +{ + return EVP_rc2_cfb64(); +} #endif #ifndef OPENSSL_NO_CAST #undef EVP_cast5_cfb const EVP_CIPHER *EVP_cast5_cfb(void); -const EVP_CIPHER *EVP_cast5_cfb(void) { return EVP_cast5_cfb64(); } +const EVP_CIPHER *EVP_cast5_cfb(void) +{ + return EVP_cast5_cfb64(); +} #endif #ifndef OPENSSL_NO_RC5 #undef EVP_rc5_32_12_16_cfb const EVP_CIPHER *EVP_rc5_32_12_16_cfb(void); -const EVP_CIPHER *EVP_rc5_32_12_16_cfb(void) { return EVP_rc5_32_12_16_cfb64(); } +const EVP_CIPHER *EVP_rc5_32_12_16_cfb(void) +{ + return EVP_rc5_32_12_16_cfb64(); +} #endif #ifndef OPENSSL_NO_AES #undef EVP_aes_128_cfb const EVP_CIPHER *EVP_aes_128_cfb(void); -const EVP_CIPHER *EVP_aes_128_cfb(void) { return EVP_aes_128_cfb128(); } +const EVP_CIPHER *EVP_aes_128_cfb(void) +{ + return EVP_aes_128_cfb128(); +} #undef EVP_aes_192_cfb const EVP_CIPHER *EVP_aes_192_cfb(void); -const EVP_CIPHER *EVP_aes_192_cfb(void) { return EVP_aes_192_cfb128(); } +const EVP_CIPHER *EVP_aes_192_cfb(void) +{ + return EVP_aes_192_cfb128(); +} #undef EVP_aes_256_cfb const EVP_CIPHER *EVP_aes_256_cfb(void); -const EVP_CIPHER *EVP_aes_256_cfb(void) { return EVP_aes_256_cfb128(); } +const EVP_CIPHER *EVP_aes_256_cfb(void) +{ + return EVP_aes_256_cfb128(); +} #endif #endif diff --git a/lib/libssl/src/crypto/evp/e_rc2.c b/lib/libssl/src/crypto/evp/e_rc2.c index d4c33b58d4d..f6f45048903 100644 --- a/lib/libssl/src/crypto/evp/e_rc2.c +++ b/lib/libssl/src/crypto/evp/e_rc2.c @@ -5,21 +5,21 @@ * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. - * + * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). - * + * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -34,10 +34,10 @@ * Eric Young (eay@cryptsoft.com)" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). - * 4. If you include any Windows specific code (or a derivative thereof) from + * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" - * + * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -49,7 +49,7 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * + * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence @@ -67,37 +67,35 @@ #include static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv,int enc); + const unsigned char *iv, int enc); static int rc2_meth_to_magic(EVP_CIPHER_CTX *ctx); static int rc2_magic_to_meth(int i); static int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type); static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type); static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr); -typedef struct - { +typedef struct { int key_bits; /* effective key bits */ RC2_KEY ks; /* key schedule */ - } EVP_RC2_KEY; +} EVP_RC2_KEY; #define data(ctx) ((EVP_RC2_KEY *)(ctx)->cipher_data) IMPLEMENT_BLOCK_CIPHER(rc2, ks, RC2, EVP_RC2_KEY, NID_rc2, - 8, - RC2_KEY_LENGTH, 8, 64, - EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT, - rc2_init_key, NULL, - rc2_set_asn1_type_and_iv, rc2_get_asn1_type_and_iv, - rc2_ctrl) + 8, + RC2_KEY_LENGTH, 8, 64, + EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT, + rc2_init_key, NULL, + rc2_set_asn1_type_and_iv, rc2_get_asn1_type_and_iv, + rc2_ctrl) #define RC2_40_MAGIC 0xa0 #define RC2_64_MAGIC 0x78 #define RC2_128_MAGIC 0x3a -static const EVP_CIPHER r2_64_cbc_cipher= - { +static const EVP_CIPHER r2_64_cbc_cipher = { NID_rc2_64_cbc, - 8,8 /* 64 bit */,8, + 8, 8 /* 64 bit */, 8, EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT, rc2_init_key, rc2_cbc_cipher, @@ -107,12 +105,11 @@ static const EVP_CIPHER r2_64_cbc_cipher= rc2_get_asn1_type_and_iv, rc2_ctrl, NULL - }; +}; -static const EVP_CIPHER r2_40_cbc_cipher= - { +static const EVP_CIPHER r2_40_cbc_cipher = { NID_rc2_40_cbc, - 8,5 /* 40 bit */,8, + 8, 5 /* 40 bit */, 8, EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT, rc2_init_key, rc2_cbc_cipher, @@ -122,93 +119,105 @@ static const EVP_CIPHER r2_40_cbc_cipher= rc2_get_asn1_type_and_iv, rc2_ctrl, NULL - }; - -const EVP_CIPHER *EVP_rc2_64_cbc(void) - { - return(&r2_64_cbc_cipher); - } - -const EVP_CIPHER *EVP_rc2_40_cbc(void) - { - return(&r2_40_cbc_cipher); - } - -static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { - RC2_set_key(&data(ctx)->ks,EVP_CIPHER_CTX_key_length(ctx), - key,data(ctx)->key_bits); +}; + +const EVP_CIPHER * +EVP_rc2_64_cbc(void) +{ + return (&r2_64_cbc_cipher); +} + +const EVP_CIPHER * +EVP_rc2_40_cbc(void) +{ + return (&r2_40_cbc_cipher); +} + +static int +rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ + RC2_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx), + key, data(ctx)->key_bits); return 1; - } +} -static int rc2_meth_to_magic(EVP_CIPHER_CTX *e) - { +static int +rc2_meth_to_magic(EVP_CIPHER_CTX *e) +{ int i; EVP_CIPHER_CTX_ctrl(e, EVP_CTRL_GET_RC2_KEY_BITS, 0, &i); - if (i == 128) return(RC2_128_MAGIC); - else if (i == 64) return(RC2_64_MAGIC); - else if (i == 40) return(RC2_40_MAGIC); - else return(0); - } - -static int rc2_magic_to_meth(int i) - { - if (i == RC2_128_MAGIC) return 128; - else if (i == RC2_64_MAGIC) return 64; - else if (i == RC2_40_MAGIC) return 40; + if (i == 128) + return (RC2_128_MAGIC); + else if (i == 64) + return (RC2_64_MAGIC); + else if (i == 40) + return (RC2_40_MAGIC); else - { - EVPerr(EVP_F_RC2_MAGIC_TO_METH,EVP_R_UNSUPPORTED_KEY_SIZE); - return(0); - } + return (0); +} + +static int +rc2_magic_to_meth(int i) +{ + if (i == RC2_128_MAGIC) + return 128; + else if (i == RC2_64_MAGIC) + return 64; + else if (i == RC2_40_MAGIC) + return 40; + else { + EVPerr(EVP_F_RC2_MAGIC_TO_METH, EVP_R_UNSUPPORTED_KEY_SIZE); + return (0); } +} -static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type) - { - long num=0; - int i=0; +static int +rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type) +{ + long num = 0; + int i = 0; int key_bits; unsigned int l; unsigned char iv[EVP_MAX_IV_LENGTH]; - if (type != NULL) - { - l=EVP_CIPHER_CTX_iv_length(c); + if (type != NULL) { + l = EVP_CIPHER_CTX_iv_length(c); OPENSSL_assert(l <= sizeof(iv)); - i=ASN1_TYPE_get_int_octetstring(type,&num,iv,l); + i = ASN1_TYPE_get_int_octetstring(type, &num, iv, l); if (i != (int)l) - return(-1); - key_bits =rc2_magic_to_meth((int)num); + return (-1); + key_bits = rc2_magic_to_meth((int)num); if (!key_bits) - return(-1); - if(i > 0 && !EVP_CipherInit_ex(c, NULL, NULL, NULL, iv, -1)) + return (-1); + if (i > 0 && !EVP_CipherInit_ex(c, NULL, NULL, NULL, iv, -1)) return -1; - EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_RC2_KEY_BITS, key_bits, NULL); + EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_RC2_KEY_BITS, + key_bits, NULL); EVP_CIPHER_CTX_set_key_length(c, key_bits / 8); - } - return(i); } + return (i); +} -static int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type) - { +static int +rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type) +{ long num; - int i=0,j; + int i = 0, j; - if (type != NULL) - { - num=rc2_meth_to_magic(c); - j=EVP_CIPHER_CTX_iv_length(c); - i=ASN1_TYPE_set_int_octetstring(type,num,c->oiv,j); - } - return(i); + if (type != NULL) { + num = rc2_meth_to_magic(c); + j = EVP_CIPHER_CTX_iv_length(c); + i = ASN1_TYPE_set_int_octetstring(type, num, c->oiv, j); } + return (i); +} -static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) - { - switch(type) - { +static int +rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) +{ + switch (type) { case EVP_CTRL_INIT: data(c)->key_bits = EVP_CIPHER_CTX_key_length(c) * 8; return 1; @@ -216,14 +225,14 @@ static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) case EVP_CTRL_GET_RC2_KEY_BITS: *(int *)ptr = data(c)->key_bits; return 1; - + case EVP_CTRL_SET_RC2_KEY_BITS: - if(arg > 0) - { + if (arg > 0) { data(c)->key_bits = arg; return 1; - } + } return 0; + #ifdef PBE_PRF_TEST case EVP_CTRL_PBE_PRF_NID: *(int *)ptr = NID_hmacWithMD5; @@ -232,7 +241,7 @@ static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) default: return -1; - } } +} #endif diff --git a/lib/libssl/src/crypto/evp/e_rc4.c b/lib/libssl/src/crypto/evp/e_rc4.c index b4f6bda82d4..f66885f70d3 100644 --- a/lib/libssl/src/crypto/evp/e_rc4.c +++ b/lib/libssl/src/crypto/evp/e_rc4.c @@ -5,21 +5,21 @@ * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. - * + * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). - * + * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -34,10 +34,10 @@ * Eric Young (eay@cryptsoft.com)" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). - * 4. If you include any Windows specific code (or a derivative thereof) from + * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" - * + * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -49,7 +49,7 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * + * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence @@ -69,21 +69,20 @@ /* FIXME: surely this is available elsewhere? */ #define EVP_RC4_KEY_SIZE 16 -typedef struct - { +typedef struct { RC4_KEY ks; /* working key */ - } EVP_RC4_KEY; +} EVP_RC4_KEY; #define data(ctx) ((EVP_RC4_KEY *)(ctx)->cipher_data) static int rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv,int enc); + const unsigned char *iv, int enc); static int rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl); -static const EVP_CIPHER r4_cipher= - { + const unsigned char *in, size_t inl); + +static const EVP_CIPHER r4_cipher = { NID_rc4, - 1,EVP_RC4_KEY_SIZE,0, + 1, EVP_RC4_KEY_SIZE, 0, EVP_CIPH_VARIABLE_LENGTH, rc4_init_key, rc4_cipher, @@ -93,45 +92,47 @@ static const EVP_CIPHER r4_cipher= NULL, NULL, NULL - }; +}; -static const EVP_CIPHER r4_40_cipher= - { +static const EVP_CIPHER r4_40_cipher = { NID_rc4_40, - 1,5 /* 40 bit */,0, + 1, 5 /* 40 bit */, 0, EVP_CIPH_VARIABLE_LENGTH, rc4_init_key, rc4_cipher, NULL, sizeof(EVP_RC4_KEY), - NULL, + NULL, NULL, NULL, NULL - }; +}; -const EVP_CIPHER *EVP_rc4(void) - { - return(&r4_cipher); - } +const EVP_CIPHER * +EVP_rc4(void) +{ + return (&r4_cipher); +} -const EVP_CIPHER *EVP_rc4_40(void) - { - return(&r4_40_cipher); - } +const EVP_CIPHER * +EVP_rc4_40(void) +{ + return (&r4_40_cipher); +} -static int rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { - RC4_set_key(&data(ctx)->ks,EVP_CIPHER_CTX_key_length(ctx), - key); +static int +rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ + RC4_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx), key); return 1; - } +} -static int rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) - { - RC4(&data(ctx)->ks,inl,in,out); +static int +rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) +{ + RC4(&data(ctx)->ks, inl, in, out); return 1; - } +} #endif diff --git a/lib/libssl/src/crypto/evp/e_rc4_hmac_md5.c b/lib/libssl/src/crypto/evp/e_rc4_hmac_md5.c index 56563191ba1..d4655c56d94 100644 --- a/lib/libssl/src/crypto/evp/e_rc4_hmac_md5.c +++ b/lib/libssl/src/crypto/evp/e_rc4_hmac_md5.c @@ -68,37 +68,35 @@ /* FIXME: surely this is available elsewhere? */ #define EVP_RC4_KEY_SIZE 16 -typedef struct - { - RC4_KEY ks; - MD5_CTX head,tail,md; - size_t payload_length; - } EVP_RC4_HMAC_MD5; +typedef struct { + RC4_KEY ks; + MD5_CTX head, tail, md; + size_t payload_length; +} EVP_RC4_HMAC_MD5; #define NO_PAYLOAD_LENGTH ((size_t)-1) void rc4_md5_enc (RC4_KEY *key, const void *in0, void *out, - MD5_CTX *ctx,const void *inp,size_t blocks); + MD5_CTX *ctx, const void *inp, size_t blocks); #define data(ctx) ((EVP_RC4_HMAC_MD5 *)(ctx)->cipher_data) -static int rc4_hmac_md5_init_key(EVP_CIPHER_CTX *ctx, - const unsigned char *inkey, - const unsigned char *iv, int enc) - { +static int +rc4_hmac_md5_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *inkey, + const unsigned char *iv, int enc) +{ EVP_RC4_HMAC_MD5 *key = data(ctx); - RC4_set_key(&key->ks,EVP_CIPHER_CTX_key_length(ctx), - inkey); + RC4_set_key(&key->ks, EVP_CIPHER_CTX_key_length(ctx), inkey); MD5_Init(&key->head); /* handy when benchmarking */ key->tail = key->head; - key->md = key->head; + key->md = key->head; key->payload_length = NO_PAYLOAD_LENGTH; return 1; - } +} #if !defined(OPENSSL_NO_ASM) && ( \ defined(__x86_64) || defined(__x86_64__) || \ @@ -113,173 +111,184 @@ static int rc4_hmac_md5_init_key(EVP_CIPHER_CTX *ctx, #define md5_off 0 #endif -static int rc4_hmac_md5_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t len) - { +static int +rc4_hmac_md5_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t len) +{ EVP_RC4_HMAC_MD5 *key = data(ctx); #if defined(STITCHED_CALL) size_t rc4_off = 32-1-(key->ks.x&(32-1)), /* 32 is $MOD from rc4_md5-x86_64.pl */ - md5_off = MD5_CBLOCK-key->md.num, - blocks; + md5_off = MD5_CBLOCK - key->md.num, + blocks; unsigned int l; extern unsigned int OPENSSL_ia32cap_P[]; #endif size_t plen = key->payload_length; - if (plen!=NO_PAYLOAD_LENGTH && len!=(plen+MD5_DIGEST_LENGTH)) return 0; + if (plen != NO_PAYLOAD_LENGTH && len != (plen + MD5_DIGEST_LENGTH)) + return 0; if (ctx->encrypt) { - if (plen==NO_PAYLOAD_LENGTH) plen = len; + if (plen == NO_PAYLOAD_LENGTH) + plen = len; #if defined(STITCHED_CALL) /* cipher has to "fall behind" */ - if (rc4_off>md5_off) md5_off+=MD5_CBLOCK; + if (rc4_off > md5_off) + md5_off += MD5_CBLOCK; - if (plen>md5_off && (blocks=(plen-md5_off)/MD5_CBLOCK) && - (OPENSSL_ia32cap_P[0]&(1<<20))==0) { - MD5_Update(&key->md,in,md5_off); - RC4(&key->ks,rc4_off,in,out); + if (plen > md5_off && + (blocks = (plen - md5_off) / MD5_CBLOCK) && + (OPENSSL_ia32cap_P[0]&(1 << 20)) == 0) { + MD5_Update(&key->md, in, md5_off); + RC4(&key->ks, rc4_off, in, out); - rc4_md5_enc(&key->ks,in+rc4_off,out+rc4_off, - &key->md,in+md5_off,blocks); + rc4_md5_enc(&key->ks, in + rc4_off, out + rc4_off, + &key->md, in + md5_off, blocks); blocks *= MD5_CBLOCK; rc4_off += blocks; md5_off += blocks; - key->md.Nh += blocks>>29; - key->md.Nl += blocks<<=3; - if (key->md.Nl<(unsigned int)blocks) key->md.Nh++; + key->md.Nh += blocks >> 29; + key->md.Nl += blocks <<= 3; + if (key->md.Nl < (unsigned int)blocks) + key->md.Nh++; } else { rc4_off = 0; md5_off = 0; } #endif - MD5_Update(&key->md,in+md5_off,plen-md5_off); + MD5_Update(&key->md, in + md5_off, plen - md5_off); if (plen!=len) { /* "TLS" mode of operation */ - if (in!=out) - memcpy(out+rc4_off,in+rc4_off,plen-rc4_off); + if (in != out) + memcpy(out + rc4_off, in + rc4_off, + plen - rc4_off); /* calculate HMAC and append it to payload */ - MD5_Final(out+plen,&key->md); + MD5_Final(out + plen, &key->md); key->md = key->tail; - MD5_Update(&key->md,out+plen,MD5_DIGEST_LENGTH); - MD5_Final(out+plen,&key->md); + MD5_Update(&key->md, out + plen, MD5_DIGEST_LENGTH); + MD5_Final(out + plen, &key->md); + /* encrypt HMAC at once */ - RC4(&key->ks,len-rc4_off,out+rc4_off,out+rc4_off); + RC4(&key->ks, len - rc4_off, out + rc4_off, + out + rc4_off); } else { - RC4(&key->ks,len-rc4_off,in+rc4_off,out+rc4_off); + RC4(&key->ks, len - rc4_off, in + rc4_off, + out + rc4_off); } } else { unsigned char mac[MD5_DIGEST_LENGTH]; #if defined(STITCHED_CALL) /* digest has to "fall behind" */ - if (md5_off>rc4_off) rc4_off += 2*MD5_CBLOCK; - else rc4_off += MD5_CBLOCK; - - if (len>rc4_off && (blocks=(len-rc4_off)/MD5_CBLOCK) && - (OPENSSL_ia32cap_P[0]&(1<<20))==0) { - RC4(&key->ks,rc4_off,in,out); - MD5_Update(&key->md,out,md5_off); - - rc4_md5_enc(&key->ks,in+rc4_off,out+rc4_off, - &key->md,out+md5_off,blocks); + if (md5_off > rc4_off) + rc4_off += 2*MD5_CBLOCK; + else + rc4_off += MD5_CBLOCK; + + if (len > rc4_off && (blocks = (len - rc4_off) / MD5_CBLOCK) && + (OPENSSL_ia32cap_P[0] & (1 << 20)) == 0) { + RC4(&key->ks, rc4_off, in, out); + MD5_Update(&key->md, out, md5_off); + + rc4_md5_enc(&key->ks, in + rc4_off, out + rc4_off, + &key->md, out + md5_off, blocks); blocks *= MD5_CBLOCK; rc4_off += blocks; md5_off += blocks; - l = (key->md.Nl+(blocks<<3))&0xffffffffU; - if (lmd.Nl) key->md.Nh++; - key->md.Nl = l; - key->md.Nh += blocks>>29; + l = (key->md.Nl + (blocks << 3)) & 0xffffffffU; + if (l < key->md.Nl) + key->md.Nh++; + key->md.Nl = l; + key->md.Nh += blocks >> 29; } else { - md5_off=0; - rc4_off=0; + md5_off = 0; + rc4_off = 0; } #endif /* decrypt HMAC at once */ - RC4(&key->ks,len-rc4_off,in+rc4_off,out+rc4_off); + RC4(&key->ks, len - rc4_off, in + rc4_off, out + rc4_off); if (plen!=NO_PAYLOAD_LENGTH) { /* "TLS" mode of operation */ - MD5_Update(&key->md,out+md5_off,plen-md5_off); + MD5_Update(&key->md, out + md5_off, plen - md5_off); /* calculate HMAC and verify it */ - MD5_Final(mac,&key->md); + MD5_Final(mac, &key->md); key->md = key->tail; - MD5_Update(&key->md,mac,MD5_DIGEST_LENGTH); - MD5_Final(mac,&key->md); + MD5_Update(&key->md, mac, MD5_DIGEST_LENGTH); + MD5_Final(mac, &key->md); - if (memcmp(out+plen,mac,MD5_DIGEST_LENGTH)) + if (memcmp(out + plen, mac, MD5_DIGEST_LENGTH)) return 0; } else { - MD5_Update(&key->md,out+md5_off,len-md5_off); + MD5_Update(&key->md, out + md5_off, len - md5_off); } } key->payload_length = NO_PAYLOAD_LENGTH; return 1; - } +} -static int rc4_hmac_md5_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) - { +static int +rc4_hmac_md5_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) +{ EVP_RC4_HMAC_MD5 *key = data(ctx); - switch (type) - { + switch (type) { case EVP_CTRL_AEAD_SET_MAC_KEY: { - unsigned int i; - unsigned char hmac_key[64]; + unsigned int i; + unsigned char hmac_key[64]; - memset (hmac_key,0,sizeof(hmac_key)); + memset (hmac_key, 0, sizeof(hmac_key)); - if (arg > (int)sizeof(hmac_key)) { - MD5_Init(&key->head); - MD5_Update(&key->head,ptr,arg); - MD5_Final(hmac_key,&key->head); - } else { - memcpy(hmac_key,ptr,arg); - } + if (arg > (int)sizeof(hmac_key)) { + MD5_Init(&key->head); + MD5_Update(&key->head, ptr, arg); + MD5_Final(hmac_key, &key->head); + } else { + memcpy(hmac_key, ptr, arg); + } - for (i=0;ihead); - MD5_Update(&key->head,hmac_key,sizeof(hmac_key)); + for (i = 0; i < sizeof(hmac_key); i++) + hmac_key[i] ^= 0x36; /* ipad */ + MD5_Init(&key->head); + MD5_Update(&key->head, hmac_key, sizeof(hmac_key)); - for (i=0;itail); - MD5_Update(&key->tail,hmac_key,sizeof(hmac_key)); + for (i = 0; i < sizeof(hmac_key); i++) + hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */ + MD5_Init(&key->tail); + MD5_Update(&key->tail, hmac_key, sizeof(hmac_key)); - return 1; + return 1; } case EVP_CTRL_AEAD_TLS1_AAD: { - unsigned char *p=ptr; - unsigned int len=p[arg-2]<<8|p[arg-1]; - - if (!ctx->encrypt) - { - len -= MD5_DIGEST_LENGTH; - p[arg-2] = len>>8; - p[arg-1] = len; + unsigned char *p = ptr; + unsigned int len = p[arg - 2] << 8 | p[arg - 1]; + + if (!ctx->encrypt) { + len -= MD5_DIGEST_LENGTH; + p[arg - 2] = len >> 8; + p[arg - 1] = len; } - key->payload_length=len; - key->md = key->head; - MD5_Update(&key->md,p,arg); + key->payload_length = len; + key->md = key->head; + MD5_Update(&key->md, p, arg); - return MD5_DIGEST_LENGTH; + return MD5_DIGEST_LENGTH; } default: return -1; - } } +} -static EVP_CIPHER r4_hmac_md5_cipher= - { +static EVP_CIPHER r4_hmac_md5_cipher = { #ifdef NID_rc4_hmac_md5 NID_rc4_hmac_md5, #else NID_undef, #endif - 1,EVP_RC4_KEY_SIZE,0, + 1, EVP_RC4_KEY_SIZE, 0, EVP_CIPH_STREAM_CIPHER|EVP_CIPH_VARIABLE_LENGTH|EVP_CIPH_FLAG_AEAD_CIPHER, rc4_hmac_md5_init_key, rc4_hmac_md5_cipher, @@ -289,10 +298,11 @@ static EVP_CIPHER r4_hmac_md5_cipher= NULL, rc4_hmac_md5_ctrl, NULL - }; +}; -const EVP_CIPHER *EVP_rc4_hmac_md5(void) - { - return(&r4_hmac_md5_cipher); - } +const EVP_CIPHER * +EVP_rc4_hmac_md5(void) +{ + return (&r4_hmac_md5_cipher); +} #endif diff --git a/lib/libssl/src/crypto/evp/e_rc5.c b/lib/libssl/src/crypto/evp/e_rc5.c index 19a10c64024..efbd03735e6 100644 --- a/lib/libssl/src/crypto/evp/e_rc5.c +++ b/lib/libssl/src/crypto/evp/e_rc5.c @@ -5,21 +5,21 @@ * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. - * + * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). - * + * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -34,10 +34,10 @@ * Eric Young (eay@cryptsoft.com)" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). - * 4. If you include any Windows specific code (or a derivative thereof) from + * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" - * + * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -49,7 +49,7 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * + * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence @@ -67,27 +67,26 @@ #include static int r_32_12_16_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv,int enc); + const unsigned char *iv, int enc); static int rc5_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr); -typedef struct - { +typedef struct { int rounds; /* number of rounds */ RC5_32_KEY ks; /* key schedule */ - } EVP_RC5_KEY; +} EVP_RC5_KEY; #define data(ctx) EVP_C_DATA(EVP_RC5_KEY,ctx) IMPLEMENT_BLOCK_CIPHER(rc5_32_12_16, ks, RC5_32, EVP_RC5_KEY, NID_rc5, - 8, RC5_32_KEY_LENGTH, 8, 64, - EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT, - r_32_12_16_init_key, NULL, - NULL, NULL, rc5_ctrl) + 8, RC5_32_KEY_LENGTH, 8, 64, + EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT, + r_32_12_16_init_key, NULL, + NULL, NULL, rc5_ctrl) -static int rc5_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) - { - switch(type) - { +static int +rc5_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) +{ + switch (type) { case EVP_CTRL_INIT: data(c)->rounds = RC5_12_ROUNDS; return 1; @@ -95,10 +94,9 @@ static int rc5_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) case EVP_CTRL_GET_RC5_ROUNDS: *(int *)ptr = data(c)->rounds; return 1; - + case EVP_CTRL_SET_RC5_ROUNDS: - switch(arg) - { + switch (arg) { case RC5_8_ROUNDS: case RC5_12_ROUNDS: case RC5_16_ROUNDS: @@ -106,21 +104,23 @@ static int rc5_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) return 1; default: - EVPerr(EVP_F_RC5_CTRL, EVP_R_UNSUPORTED_NUMBER_OF_ROUNDS); + EVPerr(EVP_F_RC5_CTRL, + EVP_R_UNSUPORTED_NUMBER_OF_ROUNDS); return 0; - } + } default: return -1; - } } +} -static int r_32_12_16_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { - RC5_32_set_key(&data(ctx)->ks,EVP_CIPHER_CTX_key_length(ctx), - key,data(ctx)->rounds); +static int +r_32_12_16_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ + RC5_32_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx), key, + data(ctx)->rounds); return 1; - } +} #endif diff --git a/lib/libssl/src/crypto/evp/e_xcbc_d.c b/lib/libssl/src/crypto/evp/e_xcbc_d.c index 250e88c8c5d..7313e4d225c 100644 --- a/lib/libssl/src/crypto/evp/e_xcbc_d.c +++ b/lib/libssl/src/crypto/evp/e_xcbc_d.c @@ -5,21 +5,21 @@ * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. - * + * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). - * + * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -34,10 +34,10 @@ * Eric Young (eay@cryptsoft.com)" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). - * 4. If you include any Windows specific code (or a derivative thereof) from + * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" - * + * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -49,7 +49,7 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * + * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence @@ -67,24 +67,22 @@ #include static int desx_cbc_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv,int enc); + const unsigned char *iv, int enc); static int desx_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl); + const unsigned char *in, size_t inl); -typedef struct - { - DES_key_schedule ks;/* key schedule */ - DES_cblock inw; - DES_cblock outw; - } DESX_CBC_KEY; +typedef struct { + DES_key_schedule ks;/* key schedule */ + DES_cblock inw; + DES_cblock outw; +} DESX_CBC_KEY; #define data(ctx) ((DESX_CBC_KEY *)(ctx)->cipher_data) -static const EVP_CIPHER d_xcbc_cipher= - { +static const EVP_CIPHER d_xcbc_cipher = { NID_desx_cbc, - 8,24,8, + 8, 24, 8, EVP_CIPH_CBC_MODE, desx_cbc_init_key, desx_cbc_cipher, @@ -94,45 +92,43 @@ static const EVP_CIPHER d_xcbc_cipher= EVP_CIPHER_get_asn1_iv, NULL, NULL - }; +}; -const EVP_CIPHER *EVP_desx_cbc(void) - { - return(&d_xcbc_cipher); - } - -static int desx_cbc_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, - const unsigned char *iv, int enc) - { +const EVP_CIPHER * +EVP_desx_cbc(void) +{ + return (&d_xcbc_cipher); +} + +static int +desx_cbc_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ DES_cblock *deskey = (DES_cblock *)key; - DES_set_key_unchecked(deskey,&data(ctx)->ks); - memcpy(&data(ctx)->inw[0],&key[8],8); - memcpy(&data(ctx)->outw[0],&key[16],8); + DES_set_key_unchecked(deskey, &data(ctx)->ks); + memcpy(&data(ctx)->inw[0], &key[8], 8); + memcpy(&data(ctx)->outw[0], &key[16], 8); return 1; - } +} -static int desx_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t inl) - { - while (inl>=EVP_MAXCHUNK) - { - DES_xcbc_encrypt(in,out,(long)EVP_MAXCHUNK,&data(ctx)->ks, - (DES_cblock *)&(ctx->iv[0]), - &data(ctx)->inw, - &data(ctx)->outw, - ctx->encrypt); - inl-=EVP_MAXCHUNK; - in +=EVP_MAXCHUNK; - out+=EVP_MAXCHUNK; - } +static int +desx_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) +{ + while (inl >= EVP_MAXCHUNK) { + DES_xcbc_encrypt(in, out, (long)EVP_MAXCHUNK, &data(ctx)->ks, + (DES_cblock *)&(ctx->iv[0]), &data(ctx)->inw, + &data(ctx)->outw, ctx->encrypt); + inl -= EVP_MAXCHUNK; + in += EVP_MAXCHUNK; + out += EVP_MAXCHUNK; + } if (inl) - DES_xcbc_encrypt(in,out,(long)inl,&data(ctx)->ks, - (DES_cblock *)&(ctx->iv[0]), - &data(ctx)->inw, - &data(ctx)->outw, - ctx->encrypt); + DES_xcbc_encrypt(in, out, (long)inl, &data(ctx)->ks, + (DES_cblock *)&(ctx->iv[0]), &data(ctx)->inw, + &data(ctx)->outw, ctx->encrypt); return 1; - } +} #endif -- 2.20.1