diff options
author | Luke Howard <lukeh@padl.com> | 2010-05-17 12:56:58 +0000 |
---|---|---|
committer | Luke Howard <lukeh@padl.com> | 2010-05-17 12:56:58 +0000 |
commit | ef9ec4caad17b4e81dae9d6d95c24ceef49eb68e (patch) | |
tree | 077643743ce3b51267975e6a533934db152477bc | |
parent | 951a56f7c087ccec84151b4a0f7526cf16b66c1e (diff) | |
download | krb5-ef9ec4caad17b4e81dae9d6d95c24ceef49eb68e.zip krb5-ef9ec4caad17b4e81dae9d6d95c24ceef49eb68e.tar.gz krb5-ef9ec4caad17b4e81dae9d6d95c24ceef49eb68e.tar.bz2 |
separate CCM from CCM with derived keys
git-svn-id: svn://anonsvn.mit.edu/krb5/users/lhoward/camellia-ccm@24049 dc483132-0cff-0310-8789-dd5450dbe970
-rw-r--r-- | src/lib/crypto/krb/dk/dk.h | 28 | ||||
-rw-r--r-- | src/lib/crypto/krb/dk/dk_ccm.c | 178 | ||||
-rw-r--r-- | src/lib/crypto/krb/etypes.c | 16 | ||||
-rw-r--r-- | src/lib/crypto/krb/prf/ccm_prf.c | 4 | ||||
-rw-r--r-- | src/lib/crypto/krb/prf/prf_int.h | 4 |
5 files changed, 139 insertions, 91 deletions
diff --git a/src/lib/crypto/krb/dk/dk.h b/src/lib/crypto/krb/dk/dk.h index 1f17332..5d07332 100644 --- a/src/lib/crypto/krb/dk/dk.h +++ b/src/lib/crypto/krb/dk/dk.h @@ -94,24 +94,24 @@ krb5int_derive_random(const struct krb5_enc_provider *enc, const krb5_data *in_constant); unsigned int -krb5int_ccm_crypto_length(const struct krb5_keytypes *ktp, - krb5_cryptotype type); +krb5int_dk_ccm_crypto_length(const struct krb5_keytypes *ktp, + krb5_cryptotype type); krb5_error_code -krb5int_ccm_encrypt(const struct krb5_keytypes *ktp, - krb5_key key, - krb5_keyusage usage, - const krb5_data *ivec, - krb5_crypto_iov *data, - size_t num_data); +krb5int_dk_ccm_encrypt(const struct krb5_keytypes *ktp, + krb5_key key, + krb5_keyusage usage, + const krb5_data *ivec, + krb5_crypto_iov *data, + size_t num_data); krb5_error_code -krb5int_ccm_decrypt(const struct krb5_keytypes *ktp, - krb5_key key, - krb5_keyusage usage, - const krb5_data *ivec, - krb5_crypto_iov *data, - size_t num_data); +krb5int_dk_ccm_decrypt(const struct krb5_keytypes *ktp, + krb5_key key, + krb5_keyusage usage, + const krb5_data *ivec, + krb5_crypto_iov *data, + size_t num_data); krb5_error_code krb5int_dk_cmac_checksum(const struct krb5_cksumtypes *ctp, diff --git a/src/lib/crypto/krb/dk/dk_ccm.c b/src/lib/crypto/krb/dk/dk_ccm.c index 628e9b0..5209bba 100644 --- a/src/lib/crypto/krb/dk/dk_ccm.c +++ b/src/lib/crypto/krb/dk/dk_ccm.c @@ -71,8 +71,8 @@ #define CCM_FLAG_RESERVED 0x80 unsigned int -krb5int_ccm_crypto_length(const struct krb5_keytypes *ktp, - krb5_cryptotype type) +krb5int_dk_ccm_crypto_length(const struct krb5_keytypes *ktp, + krb5_cryptotype type) { unsigned int length; @@ -88,7 +88,7 @@ krb5int_ccm_crypto_length(const struct krb5_keytypes *ktp, length = ktp->enc->block_size; break; default: - assert(0 && "invalid cryptotype passed to krb5int_ccm_crypto_length"); + assert(0 && "invalid cryptotype passed to ccm_crypto_length"); length = ~0; break; } @@ -245,7 +245,7 @@ valid_payload_length_p(const struct krb5_keytypes *ktp, q = 15 - n; - maxblocks = (1UL << (8 * q)); + maxblocks = (1U << (8 * q)); nblocks = 1; /* tag */ nblocks += (payload_len + block_size - 1) / block_size; @@ -253,19 +253,16 @@ valid_payload_length_p(const struct krb5_keytypes *ktp, return (nblocks <= maxblocks); } -krb5_error_code -krb5int_ccm_encrypt(const struct krb5_keytypes *ktp, - krb5_key key, - krb5_keyusage usage, - const krb5_data *state, - krb5_crypto_iov *data, - size_t num_data) +static krb5_error_code +ccm_encrypt(const struct krb5_keytypes *ktp, + krb5_key kc, + krb5_keyusage usage, + const krb5_data *state, + krb5_crypto_iov *data, + size_t num_data) { krb5_error_code ret; - unsigned char constantdata[K5CLENGTH]; - krb5_data d1; krb5_crypto_iov *header, *trailer, *sign_data = NULL; - krb5_key kc = NULL; size_t i, num_sign_data = 0; unsigned int header_len; unsigned int trailer_len; @@ -278,14 +275,18 @@ krb5int_ccm_encrypt(const struct krb5_keytypes *ktp, header_len = ktp->crypto_length(ktp, KRB5_CRYPTO_TYPE_HEADER); header = krb5int_c_locate_iov(data, num_data, KRB5_CRYPTO_TYPE_HEADER); - if (header == NULL || header->data.length < header_len) - return KRB5_BAD_MSIZE; + if (header == NULL || header->data.length < header_len) { + ret = KRB5_BAD_MSIZE; + goto cleanup; + } trailer_len = ktp->crypto_length(ktp, KRB5_CRYPTO_TYPE_TRAILER); trailer = krb5int_c_locate_iov(data, num_data, KRB5_CRYPTO_TYPE_TRAILER); - if (trailer == NULL || trailer->data.length < trailer_len) - return KRB5_BAD_MSIZE; + if (trailer == NULL || trailer->data.length < trailer_len) { + ret = KRB5_BAD_MSIZE; + goto cleanup; + } for (i = 0; i < num_data; i++) { krb5_crypto_iov *iov = &data[i]; @@ -305,8 +306,10 @@ krb5int_ccm_encrypt(const struct krb5_keytypes *ktp, } } - if (!valid_payload_length_p(ktp, header_len, payload_len)) - return KRB5_BAD_MSIZE; + if (!valid_payload_length_p(ktp, header_len, payload_len)) { + ret = KRB5_BAD_MSIZE; + goto cleanup; + } header->data.length = header_len; @@ -352,19 +355,6 @@ krb5int_ccm_encrypt(const struct krb5_keytypes *ktp, sign_data[num_sign_data++] = data[i]; } - d1 = make_data((char *)constantdata, K5CLENGTH); - - d1.data[0] = (usage >> 24) & 0xFF; - d1.data[1] = (usage >> 16) & 0xFF; - d1.data[2] = (usage >> 8 ) & 0xFF; - d1.data[3] = (usage ) & 0xFF; - - d1.data[4] = 0xCC; - - ret = krb5int_derive_key(ktp->enc, key, &kc, &d1); - if (ret != 0) - goto cleanup; - assert(ktp->enc->encrypt != NULL); assert(ktp->enc->cbc_mac != NULL); @@ -394,25 +384,55 @@ krb5int_ccm_encrypt(const struct krb5_keytypes *ktp, memcpy(state->data, counter.data, counter.length); cleanup: - krb5_k_free_key(NULL, kc); free(sign_data); return ret; } krb5_error_code -krb5int_ccm_decrypt(const struct krb5_keytypes *ktp, - krb5_key key, - krb5_keyusage usage, - const krb5_data *state, - krb5_crypto_iov *data, - size_t num_data) +krb5int_dk_ccm_encrypt(const struct krb5_keytypes *ktp, + krb5_key key, + krb5_keyusage usage, + const krb5_data *state, + krb5_crypto_iov *data, + size_t num_data) { - krb5_error_code ret; unsigned char constantdata[K5CLENGTH]; + krb5_error_code ret; + krb5_key kc; krb5_data d1; + + d1.data = (char *)constantdata; + d1.length = K5CLENGTH; + + d1.data[0] = (usage >> 24) & 0xFF; + d1.data[1] = (usage >> 16) & 0xFF; + d1.data[2] = (usage >> 8 ) & 0xFF; + d1.data[3] = (usage ) & 0xFF; + + d1.data[4] = 0xCC; + + ret = krb5int_derive_key(ktp->enc, key, &kc, &d1); + if (ret != 0) + return ret; + + ret = ccm_encrypt(ktp, kc, usage, state, data, num_data); + + krb5_k_free_key(NULL, kc); + + return ret; +} + +static krb5_error_code +ccm_decrypt(const struct krb5_keytypes *ktp, + krb5_key kc, + krb5_keyusage usage, + const krb5_data *state, + krb5_crypto_iov *data, + size_t num_data) +{ + krb5_error_code ret; krb5_crypto_iov *header, *trailer, *sign_data = NULL; - krb5_key kc = NULL; size_t i, num_sign_data = 0; unsigned int header_len; unsigned int trailer_len; @@ -426,14 +446,18 @@ krb5int_ccm_decrypt(const struct krb5_keytypes *ktp, header_len = ktp->crypto_length(ktp, KRB5_CRYPTO_TYPE_HEADER); header = krb5int_c_locate_iov(data, num_data, KRB5_CRYPTO_TYPE_HEADER); - if (header == NULL || header->data.length != header_len) - return KRB5_BAD_MSIZE; + if (header == NULL || header->data.length != header_len) { + ret = KRB5_BAD_MSIZE; + goto cleanup; + } trailer_len = ktp->crypto_length(ktp, KRB5_CRYPTO_TYPE_TRAILER); trailer = krb5int_c_locate_iov(data, num_data, KRB5_CRYPTO_TYPE_TRAILER); - if (trailer == NULL || trailer->data.length != trailer_len) - return KRB5_BAD_MSIZE; + if (trailer == NULL || trailer->data.length != trailer_len) { + ret = KRB5_BAD_MSIZE; + goto cleanup; + } for (i = 0; i < num_data; i++) { krb5_crypto_iov *iov = &data[i]; @@ -446,16 +470,20 @@ krb5int_ccm_decrypt(const struct krb5_keytypes *ktp, adata_len += iov->data.length; break; case KRB5_CRYPTO_TYPE_PADDING: - if (iov->data.length != 0) - return KRB5_BAD_MSIZE; + if (iov->data.length != 0) { + ret = KRB5_BAD_MSIZE; + goto cleanup; + } break; default: break; } } - if (!valid_payload_length_p(ktp, header_len, payload_len)) - return KRB5_BAD_MSIZE; + if (!valid_payload_length_p(ktp, header_len, payload_len)) { + ret = KRB5_BAD_MSIZE; + goto cleanup; + } /* Initialize counter block */ ret = format_Ctr0(&counter, &header->data, state, header_len); @@ -484,21 +512,8 @@ krb5int_ccm_decrypt(const struct krb5_keytypes *ktp, goto cleanup; num_sign_data++; - d1.data = (char *)constantdata; - d1.length = K5CLENGTH; - - d1.data[0] = (usage >> 24) & 0xFF; - d1.data[1] = (usage >> 16) & 0xFF; - d1.data[2] = (usage >> 8 ) & 0xFF; - d1.data[3] = (usage ) & 0xFF; - - d1.data[4] = 0xCC; - - ret = krb5int_derive_key(ktp->enc, key, &kc, &d1); - if (ret != 0) - goto cleanup; - assert(ktp->enc->decrypt != NULL); + assert(ktp->enc->cbc_mac != NULL); made_cksum.data = k5alloc(trailer_len, &ret); if (made_cksum.data == NULL) @@ -547,10 +562,43 @@ krb5int_ccm_decrypt(const struct krb5_keytypes *ktp, memcpy(state->data, counter.data, counter.length); cleanup: - krb5_k_free_key(NULL, kc); free(made_cksum.data); free(sign_data); return ret; } +krb5_error_code +krb5int_dk_ccm_decrypt(const struct krb5_keytypes *ktp, + krb5_key key, + krb5_keyusage usage, + const krb5_data *state, + krb5_crypto_iov *data, + size_t num_data) +{ + unsigned char constantdata[K5CLENGTH]; + krb5_error_code ret; + krb5_key kc; + krb5_data d1; + + d1.data = (char *)constantdata; + d1.length = K5CLENGTH; + + d1.data[0] = (usage >> 24) & 0xFF; + d1.data[1] = (usage >> 16) & 0xFF; + d1.data[2] = (usage >> 8 ) & 0xFF; + d1.data[3] = (usage ) & 0xFF; + + d1.data[4] = 0xCC; + + ret = krb5int_derive_key(ktp->enc, key, &kc, &d1); + if (ret != 0) + return ret; + + ret = ccm_decrypt(ktp, kc, usage, state, data, num_data); + + krb5_k_free_key(NULL, kc); + + return ret; +} + diff --git a/src/lib/crypto/krb/etypes.c b/src/lib/crypto/krb/etypes.c index 595dc1c..aa14dc6 100644 --- a/src/lib/crypto/krb/etypes.c +++ b/src/lib/crypto/krb/etypes.c @@ -159,9 +159,9 @@ const struct krb5_keytypes krb5int_enctypes_list[] = { "AES-128 CCM mode with 128-bit MAC", &krb5int_enc_aes128_ctr, NULL, 16, - krb5int_ccm_crypto_length, krb5int_ccm_encrypt, krb5int_ccm_decrypt, + krb5int_dk_ccm_crypto_length, krb5int_dk_ccm_encrypt, krb5int_dk_ccm_decrypt, krb5int_aes_ccm_string_to_key, - krb5int_ccm_prf, + krb5int_dk_ccm_prf, CKSUMTYPE_CMAC_128_AES128, 0 /*flags*/ }, { ENCTYPE_AES256_CCM_128, @@ -169,9 +169,9 @@ const struct krb5_keytypes krb5int_enctypes_list[] = { "AES-256 CCM mode with 128-bit MAC", &krb5int_enc_aes256_ctr, NULL, 16, - krb5int_ccm_crypto_length, krb5int_ccm_encrypt, krb5int_ccm_decrypt, + krb5int_dk_ccm_crypto_length, krb5int_dk_ccm_encrypt, krb5int_dk_ccm_decrypt, krb5int_aes_ccm_string_to_key, - krb5int_ccm_prf, + krb5int_dk_ccm_prf, CKSUMTYPE_CMAC_128_AES256, 0 /*flags */ }, #endif /* AES_CCM */ @@ -200,9 +200,9 @@ const struct krb5_keytypes krb5int_enctypes_list[] = { "Camellia-128 CCM mode with 128-bit MAC", &krb5int_enc_camellia128_ctr, NULL, 16, - krb5int_ccm_crypto_length, krb5int_ccm_encrypt, krb5int_ccm_decrypt, + krb5int_dk_ccm_crypto_length, krb5int_dk_ccm_encrypt, krb5int_dk_ccm_decrypt, krb5int_camellia_ccm_string_to_key, - krb5int_ccm_prf, + krb5int_dk_ccm_prf, CKSUMTYPE_CMAC_128_CAMELLIA128, 0 /*flags*/ }, { ENCTYPE_CAMELLIA256_CCM_128, @@ -210,9 +210,9 @@ const struct krb5_keytypes krb5int_enctypes_list[] = { "Camellia-256 CCM mode with 128-bit MAC", &krb5int_enc_camellia256_ctr, NULL, 16, - krb5int_ccm_crypto_length, krb5int_ccm_encrypt, krb5int_ccm_decrypt, + krb5int_dk_ccm_crypto_length, krb5int_dk_ccm_encrypt, krb5int_dk_ccm_decrypt, krb5int_camellia_ccm_string_to_key, - krb5int_ccm_prf, + krb5int_dk_ccm_prf, CKSUMTYPE_CMAC_128_CAMELLIA256, 0 /*flags */ }, }; diff --git a/src/lib/crypto/krb/prf/ccm_prf.c b/src/lib/crypto/krb/prf/ccm_prf.c index f600e52..17b4dc9 100644 --- a/src/lib/crypto/krb/prf/ccm_prf.c +++ b/src/lib/crypto/krb/prf/ccm_prf.c @@ -34,8 +34,8 @@ #include <dk.h> krb5_error_code -krb5int_ccm_prf(const struct krb5_keytypes *ktp, krb5_key key, - const krb5_data *in, krb5_data *out) +krb5int_dk_ccm_prf(const struct krb5_keytypes *ktp, krb5_key key, + const krb5_data *in, krb5_data *out) { krb5_crypto_iov iov; krb5_data prfconst = make_data("prf", 3); diff --git a/src/lib/crypto/krb/prf/prf_int.h b/src/lib/crypto/krb/prf/prf_int.h index 4549ee3..25c4da6 100644 --- a/src/lib/crypto/krb/prf/prf_int.h +++ b/src/lib/crypto/krb/prf/prf_int.h @@ -44,7 +44,7 @@ krb5int_dk_prf(const struct krb5_keytypes *ktp, krb5_key key, const krb5_data *in, krb5_data *out); krb5_error_code -krb5int_ccm_prf(const struct krb5_keytypes *ktp, krb5_key key, - const krb5_data *in, krb5_data *out); +krb5int_dk_ccm_prf(const struct krb5_keytypes *ktp, krb5_key key, + const krb5_data *in, krb5_data *out); #endif /*PRF_INTERNAL_DEFS*/ |