diff --git a/lib/hcrypto/evp-cc.c b/lib/hcrypto/evp-cc.c index 8ba1ba7f5..7a5cf9184 100644 --- a/lib/hcrypto/evp-cc.c +++ b/lib/hcrypto/evp-cc.c @@ -87,43 +87,6 @@ cc_do_cipher(EVP_CIPHER_CTX *ctx, return 1; } -static int -cc_do_cfb8_cipher(EVP_CIPHER_CTX *ctx, - unsigned char *out, - const unsigned char *in, - unsigned int size) -{ - struct cc_key *cc = ctx->cipher_data; - CCCryptorStatus ret; - size_t moved; - unsigned int i; - - for (i = 0; i < size; i++) { - unsigned char oiv[EVP_MAX_IV_LENGTH + 1]; - - assert(ctx->cipher->iv_len + 1 <= sizeof(oiv)); - memcpy(oiv, ctx->iv, ctx->cipher->iv_len); - - ret = CCCryptorUpdate(cc->href, ctx->iv, ctx->cipher->iv_len, - ctx->iv, ctx->cipher->iv_len, &moved); - if (ret) - return 0; - - if (moved != ctx->cipher->iv_len) - return 0; - - if (!ctx->encrypt) - oiv[ctx->cipher->iv_len] = in[i]; - out[i] = in[i] ^ ctx->iv[0]; - if (ctx->encrypt) - oiv[ctx->cipher->iv_len] = out[i]; - - memcpy(ctx->iv, &oiv[1], ctx->cipher->iv_len); - } - - return 1; -} - static int cc_cleanup(EVP_CIPHER_CTX *ctx) { @@ -134,10 +97,13 @@ cc_cleanup(EVP_CIPHER_CTX *ctx) } static int -init_cc_key(int encp, CCAlgorithm alg, CCOptions opts, const void *key, - size_t keylen, const void *iv, CCCryptorRef *ref) +init_cc_key(int encp, unsigned long flags, + CCAlgorithm alg, const void *key, size_t keylen, + const void *iv, CCCryptorRef *ref) { CCOperation op = encp ? kCCEncrypt : kCCDecrypt; + CCMode mode; + CCModeOptions options = 0; CCCryptorStatus ret; if (*ref) { @@ -149,8 +115,22 @@ init_cc_key(int encp, CCAlgorithm alg, CCOptions opts, const void *key, } if (key) { - ret = CCCryptorCreate(op, alg, opts, key, keylen, iv, ref); - if (ret) + switch (flags & EVP_CIPH_MODE) { + case EVP_CIPH_STREAM_CIPHER: + mode = kCCModeRC4; + break; + case EVP_CIPH_CFB8_MODE: + mode = kCCModeCFB8; + break; + default: + mode = kCCModeCBC; + break; + } + + ret = CCCryptorCreateWithMode(op, mode, alg, ccNoPadding, + iv, key, keylen, NULL, 0, 0, + options, ref); + if (ret) return 0; } @@ -164,7 +144,8 @@ cc_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx, int encp) { struct cc_key *cc = ctx->cipher_data; - return init_cc_key(encp, kCCAlgorithm3DES, 0, key, kCCKeySize3DES, iv, &cc->href); + return init_cc_key(encp, ctx->cipher->flags, kCCAlgorithm3DES, + key, kCCKeySize3DES, iv, &cc->href); } #endif /* HAVE_COMMONCRYPTO_COMMONCRYPTOR_H */ @@ -214,7 +195,8 @@ cc_des_cbc_init(EVP_CIPHER_CTX *ctx, int encp) { struct cc_key *cc = ctx->cipher_data; - return init_cc_key(encp, kCCAlgorithmDES, 0, key, kCCBlockSizeDES, iv, &cc->href); + return init_cc_key(encp, ctx->cipher->flags, kCCAlgorithmDES, + key, kCCBlockSizeDES, iv, &cc->href); } #endif @@ -263,7 +245,8 @@ cc_aes_cbc_init(EVP_CIPHER_CTX *ctx, int encp) { struct cc_key *cc = ctx->cipher_data; - return init_cc_key(encp, kCCAlgorithmAES128, 0, key, ctx->cipher->key_len, iv, &cc->href); + return init_cc_key(encp, ctx->cipher->flags, kCCAlgorithmAES128, + key, ctx->cipher->key_len, iv, &cc->href); } #endif @@ -378,9 +361,7 @@ cc_aes_cfb8_init(EVP_CIPHER_CTX *ctx, int encp) { struct cc_key *cc = ctx->cipher_data; - if (iv) - memcpy(ctx->iv, iv, ctx->cipher->iv_len); - return init_cc_key(1, kCCAlgorithmAES128, kCCOptionECBMode, + return init_cc_key(encp, ctx->cipher->flags, kCCAlgorithmAES128, key, ctx->cipher->key_len, NULL, &cc->href); } #endif @@ -404,7 +385,7 @@ EVP_cc_aes_128_cfb8(void) kCCBlockSizeAES128, EVP_CIPH_CFB8_MODE|EVP_CIPH_ALWAYS_CALL_INIT, cc_aes_cfb8_init, - cc_do_cfb8_cipher, + cc_do_cipher, cc_cleanup, sizeof(struct cc_key), NULL, @@ -437,7 +418,7 @@ EVP_cc_aes_192_cfb8(void) kCCBlockSizeAES128, EVP_CIPH_CFB8_MODE|EVP_CIPH_ALWAYS_CALL_INIT, cc_aes_cfb8_init, - cc_do_cfb8_cipher, + cc_do_cipher, cc_cleanup, sizeof(struct cc_key), NULL, @@ -470,7 +451,7 @@ EVP_cc_aes_256_cfb8(void) kCCBlockSizeAES128, EVP_CIPH_CFB8_MODE|EVP_CIPH_ALWAYS_CALL_INIT, cc_aes_cfb8_init, - cc_do_cfb8_cipher, + cc_do_cipher, cc_cleanup, sizeof(struct cc_key), NULL, @@ -496,7 +477,8 @@ cc_rc2_cbc_init(EVP_CIPHER_CTX *ctx, int encp) { struct cc_key *cc = ctx->cipher_data; - return init_cc_key(encp, kCCAlgorithmRC2, 0, key, ctx->cipher->key_len, iv, &cc->href); + return init_cc_key(encp, ctx->cipher->flags, kCCAlgorithmRC2, + key, ctx->cipher->key_len, iv, &cc->href); } #endif @@ -728,6 +710,56 @@ EVP_cc_sha256(void) #endif } +/** + * The CommonCrypto sha384 provider + * + * @ingroup hcrypto_evp + */ + +const EVP_MD * +EVP_cc_sha384(void) +{ +#ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H + static const struct hc_evp_md sha384 = { + CC_SHA384_DIGEST_LENGTH, + CC_SHA384_BLOCK_BYTES, + sizeof(CC_SHA512_CTX), + (hc_evp_md_init)CC_SHA384_Init, + (hc_evp_md_update)CC_SHA384_Update, + (hc_evp_md_final)CC_SHA384_Final, + (hc_evp_md_cleanup)NULL + }; + return &sha384; +#else + return NULL; +#endif +} + +/** + * The CommonCrypto sha512 provider + * + * @ingroup hcrypto_evp + */ + +const EVP_MD * +EVP_cc_sha512(void) +{ +#ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H + static const struct hc_evp_md sha512 = { + CC_SHA512_DIGEST_LENGTH, + CC_SHA512_BLOCK_BYTES, + sizeof(CC_SHA512_CTX), + (hc_evp_md_init)CC_SHA512_Init, + (hc_evp_md_update)CC_SHA512_Update, + (hc_evp_md_final)CC_SHA512_Final, + (hc_evp_md_cleanup)NULL + }; + return &sha512; +#else + return NULL; +#endif +} + /** * The Camellia-128 cipher type - CommonCrypto * @@ -783,7 +815,8 @@ cc_rc4_init(EVP_CIPHER_CTX *ctx, int encp) { struct cc_key *cc = ctx->cipher_data; - return init_cc_key(encp, kCCAlgorithmRC4, 0, key, ctx->key_len, iv, &cc->href); + return init_cc_key(encp, ctx->cipher->flags, kCCAlgorithmRC4, + key, ctx->key_len, iv, &cc->href); } #endif diff --git a/lib/hcrypto/evp-cc.h b/lib/hcrypto/evp-cc.h index 9249bb226..4d131de01 100644 --- a/lib/hcrypto/evp-cc.h +++ b/lib/hcrypto/evp-cc.h @@ -42,6 +42,8 @@ #define EVP_cc_md5 hc_EVP_cc_md5 #define EVP_cc_sha1 hc_EVP_cc_sha1 #define EVP_cc_sha256 hc_EVP_cc_sha256 +#define EVP_cc_sha384 hc_EVP_cc_sha384 +#define EVP_cc_sha512 hc_EVP_cc_sha512 #define EVP_cc_des_cbc hc_EVP_cc_des_cbc #define EVP_cc_des_ede3_cbc hc_EVP_cc_des_ede3_cbc #define EVP_cc_aes_128_cbc hc_EVP_cc_aes_128_cbc @@ -70,6 +72,8 @@ const EVP_MD * EVP_cc_md4(void); const EVP_MD * EVP_cc_md5(void); const EVP_MD * EVP_cc_sha1(void); const EVP_MD * EVP_cc_sha256(void); +const EVP_MD * EVP_cc_sha384(void); +const EVP_MD * EVP_cc_sha512(void); const EVP_CIPHER * EVP_cc_rc2_cbc(void); const EVP_CIPHER * EVP_cc_rc2_40_cbc(void);