diff options
author | Hiroki Sato <hrs@FreeBSD.org> | 2018-04-04 04:21:19 +0000 |
---|---|---|
committer | Hiroki Sato <hrs@FreeBSD.org> | 2018-04-04 04:21:19 +0000 |
commit | d684f11da759490a8d98d7b790796106285f4084 (patch) | |
tree | 27b7356df710fdf1440fe2c23154b8121e99f2ab /lib/hcrypto/evp-cc.c | |
parent | f52d4664e3f68828c06f85bfc1afa271e3e04713 (diff) |
Import Heimdal 7.5.0.vendor/heimdal/7.5.0
Notes
Notes:
svn path=/vendor-crypto/heimdal/dist/; revision=331978
svn path=/vendor-crypto/heimdal/7.5.0/; revision=331979; tag=vendor/heimdal/7.5.0
Diffstat (limited to 'lib/hcrypto/evp-cc.c')
-rw-r--r-- | lib/hcrypto/evp-cc.c | 203 |
1 files changed, 145 insertions, 58 deletions
diff --git a/lib/hcrypto/evp-cc.c b/lib/hcrypto/evp-cc.c index bd084a25e20d..4a377f1c3bb1 100644 --- a/lib/hcrypto/evp-cc.c +++ b/lib/hcrypto/evp-cc.c @@ -37,12 +37,9 @@ #ifdef __APPLE__ -#include "config.h" +#include <config.h> +#include <roken.h> -#include <sys/types.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> #include <assert.h> #ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H @@ -53,6 +50,7 @@ #endif #include <evp.h> +#include <evp-hcrypto.h> #include <evp-cc.h> /* @@ -88,43 +86,6 @@ cc_do_cipher(EVP_CIPHER_CTX *ctx, } 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) { struct cc_key *cc = ctx->cipher_data; @@ -134,10 +95,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) { @@ -148,9 +112,26 @@ init_cc_key(int encp, CCAlgorithm alg, CCOptions opts, const void *key, CCCryptorRelease(*ref); } - ret = CCCryptorCreate(op, alg, opts, key, keylen, iv, ref); - if (ret) - return 0; + if (key) { + 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; + } + return 1; } @@ -161,13 +142,14 @@ 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 */ /** - * The tripple DES cipher type (Apple CommonCrypto provider) + * The triple DES cipher type (Apple CommonCrypto provider) * * @return the DES-EDE3-CBC EVP_CIPHER pointer. * @@ -194,6 +176,8 @@ EVP_cc_des_ede3_cbc(void) NULL }; return &des_ede3_cbc; +#elif HCRYPTO_FALLBACK + return EVP_hcrypto_des_ede3_cbc(); #else return NULL; #endif @@ -211,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 @@ -243,6 +228,8 @@ EVP_cc_des_cbc(void) NULL }; return &des_ede3_cbc; +#elif HCRYPTO_FALLBACK + return EVP_hcrypto_des_cbc(); #else return NULL; #endif @@ -260,7 +247,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 @@ -292,6 +280,8 @@ EVP_cc_aes_128_cbc(void) NULL }; return &c; +#elif HCRYPTO_FALLBACK + return EVP_hcrypto_aes_128_cbc(); #else return NULL; #endif @@ -325,6 +315,8 @@ EVP_cc_aes_192_cbc(void) NULL }; return &c; +#elif HCRYPTO_FALLBACK + return EVP_hcrypto_aes_192_cbc(); #else return NULL; #endif @@ -358,6 +350,8 @@ EVP_cc_aes_256_cbc(void) NULL }; return &c; +#elif HCRYPTO_FALLBACK + return EVP_hcrypto_aes_256_cbc(); #else return NULL; #endif @@ -375,8 +369,7 @@ cc_aes_cfb8_init(EVP_CIPHER_CTX *ctx, int encp) { struct cc_key *cc = ctx->cipher_data; - 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 @@ -400,7 +393,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, @@ -409,6 +402,8 @@ EVP_cc_aes_128_cfb8(void) NULL }; return &c; +#elif HCRYPTO_FALLBACK + return EVP_hcrypto_aes_128_cfb8(); #else return NULL; #endif @@ -433,7 +428,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, @@ -442,6 +437,8 @@ EVP_cc_aes_192_cfb8(void) NULL }; return &c; +#elif HCRYPTO_FALLBACK + return EVP_hcrypto_aes_192_cfb8(); #else return NULL; #endif @@ -466,7 +463,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, @@ -475,6 +472,8 @@ EVP_cc_aes_256_cfb8(void) NULL }; return &c; +#elif HCRYPTO_FALLBACK + return EVP_hcrypto_aes_256_cfb8(); #else return NULL; #endif @@ -492,7 +491,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 @@ -525,6 +525,8 @@ EVP_cc_rc2_cbc(void) NULL }; return &rc2_cbc; +#elif HCRYPTO_FALLBACK + return EVP_hcrypto_rc2_cbc(); #else return NULL; #endif @@ -559,6 +561,8 @@ EVP_cc_rc2_40_cbc(void) NULL }; return &rc2_40_cbc; +#elif HCRYPTO_FALLBACK + return EVP_hcrypto_rc2_40_cbc(); #else return NULL; #endif @@ -594,6 +598,8 @@ EVP_cc_rc2_64_cbc(void) NULL }; return &rc2_64_cbc; +#elif HCRYPTO_FALLBACK + return EVP_hcrypto_rc2_64_cbc(); #else return NULL; #endif @@ -619,6 +625,8 @@ EVP_cc_md2(void) (hc_evp_md_cleanup)NULL }; return &md2; +#elif HCRYPTO_FALLBACK + return EVP_hcrypto_md2(); #else return NULL; #endif @@ -644,6 +652,8 @@ EVP_cc_md4(void) (hc_evp_md_cleanup)NULL }; return &md4; +#elif HCRYPTO_FALLBACK + return EVP_hcrypto_md4(); #else return NULL; #endif @@ -669,6 +679,8 @@ EVP_cc_md5(void) (hc_evp_md_cleanup)NULL }; return &md5; +#elif HCRYPTO_FALLBACK + return EVP_hcrypto_md5(); #else return NULL; #endif @@ -694,6 +706,8 @@ EVP_cc_sha1(void) (hc_evp_md_cleanup)NULL }; return &sha1; +#elif HCRYPTO_FALLBACK + return EVP_hcrypto_sha1(); #else return NULL; #endif @@ -719,6 +733,62 @@ EVP_cc_sha256(void) (hc_evp_md_cleanup)NULL }; return &sha256; +#elif HCRYPTO_FALLBACK + return EVP_hcrypto_sha256(); +#else + return NULL; +#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; +#elif HCRYPTO_FALLBACK + return EVP_hcrypto_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; +#elif HCRYPTO_FALLBACK + return EVP_hcrypto_sha512(); #else return NULL; #endif @@ -735,7 +805,11 @@ EVP_cc_sha256(void) const EVP_CIPHER * EVP_cc_camellia_128_cbc(void) { +#if HCRYPTO_FALLBACK + return EVP_hcrypto_camellia_128_cbc(); +#else return NULL; +#endif } /** @@ -749,7 +823,11 @@ EVP_cc_camellia_128_cbc(void) const EVP_CIPHER * EVP_cc_camellia_192_cbc(void) { +#if HCRYPTO_FALLBACK + return EVP_hcrypto_camellia_192_cbc(); +#else return NULL; +#endif } /** @@ -763,7 +841,11 @@ EVP_cc_camellia_192_cbc(void) const EVP_CIPHER * EVP_cc_camellia_256_cbc(void) { +#if HCRYPTO_FALLBACK + return EVP_hcrypto_camellia_256_cbc(); +#else return NULL; +#endif } #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H @@ -779,7 +861,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 @@ -813,6 +896,8 @@ EVP_cc_rc4(void) NULL }; return &rc4; +#elif HCRYPTO_FALLBACK + return EVP_hcrypto_rc4(); #else return NULL; #endif @@ -847,6 +932,8 @@ EVP_cc_rc4_40(void) NULL }; return &rc4_40; +#elif HCRYPTO_FALLBACK + return EVP_hcrypto_rc4_40(); #else return NULL; #endif |