1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/crypto/krb/enc_etm.c - encrypt-then-mac construction for aes-sha2 */
3 /*
4 * Copyright (C) 2015 by the Massachusetts Institute of Technology.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30 * OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include "crypto_int.h"
34
35 unsigned int
krb5int_aes2_crypto_length(const struct krb5_keytypes * ktp,krb5_cryptotype type)36 krb5int_aes2_crypto_length(const struct krb5_keytypes *ktp,
37 krb5_cryptotype type)
38 {
39 switch (type) {
40 case KRB5_CRYPTO_TYPE_HEADER:
41 return ktp->enc->block_size;
42 case KRB5_CRYPTO_TYPE_PADDING:
43 return 0;
44 case KRB5_CRYPTO_TYPE_TRAILER:
45 case KRB5_CRYPTO_TYPE_CHECKSUM:
46 return ktp->hash->hashsize / 2;
47 default:
48 assert(0 && "invalid cryptotype passed to krb5int_aes2_crypto_length");
49 return 0;
50 }
51 }
52
53 /* Derive encryption and integrity keys for CMAC-using enctypes. */
54 static krb5_error_code
derive_keys(const struct krb5_keytypes * ktp,krb5_key key,krb5_keyusage usage,krb5_key * ke_out,krb5_data * ki_out)55 derive_keys(const struct krb5_keytypes *ktp, krb5_key key,
56 krb5_keyusage usage, krb5_key *ke_out, krb5_data *ki_out)
57 {
58 krb5_error_code ret;
59 uint8_t label[5];
60 krb5_data label_data = make_data(label, 5), ki = empty_data();
61 krb5_key ke = NULL;
62
63 *ke_out = NULL;
64 *ki_out = empty_data();
65
66 /* Derive the encryption key. */
67 store_32_be(usage, label);
68 label[4] = 0xAA;
69 ret = krb5int_derive_key(ktp->enc, ktp->hash, key, &ke, &label_data,
70 DERIVE_SP800_108_HMAC);
71 if (ret)
72 goto cleanup;
73
74 /* Derive the integrity key. */
75 label[4] = 0x55;
76 ret = alloc_data(&ki, ktp->hash->hashsize / 2);
77 if (ret)
78 goto cleanup;
79 ret = krb5int_derive_random(NULL, ktp->hash, key, &ki, &label_data,
80 DERIVE_SP800_108_HMAC);
81 if (ret)
82 goto cleanup;
83
84 *ke_out = ke;
85 ke = NULL;
86 *ki_out = ki;
87 ki = empty_data();
88
89 cleanup:
90 krb5_k_free_key(NULL, ke);
91 zapfree(ki.data, ki.length);
92 return ret;
93 }
94
95 /* Compute an HMAC checksum over the cipher state and data. Allocate enough
96 * space in *out for the checksum. */
97 static krb5_error_code
hmac_ivec_data(const struct krb5_keytypes * ktp,const krb5_data * ki,const krb5_data * ivec,krb5_crypto_iov * data,size_t num_data,krb5_data * out)98 hmac_ivec_data(const struct krb5_keytypes *ktp, const krb5_data *ki,
99 const krb5_data *ivec, krb5_crypto_iov *data, size_t num_data,
100 krb5_data *out)
101 {
102 krb5_error_code ret;
103 krb5_data zeroivec = empty_data();
104 krb5_crypto_iov *iovs = NULL;
105 krb5_keyblock kb = { 0 };
106
107 if (ivec == NULL) {
108 ret = ktp->enc->init_state(NULL, 0, &zeroivec);
109 if (ret)
110 goto cleanup;
111 ivec = &zeroivec;
112 }
113
114 /* Make a copy of data with an extra iov at the beginning for the ivec. */
115 iovs = k5calloc(num_data + 1, sizeof(*iovs), &ret);
116 if (iovs == NULL)
117 goto cleanup;
118 iovs[0].flags = KRB5_CRYPTO_TYPE_DATA;
119 iovs[0].data = *ivec;
120 memcpy(iovs + 1, data, num_data * sizeof(*iovs));
121
122 ret = alloc_data(out, ktp->hash->hashsize);
123 if (ret)
124 goto cleanup;
125 kb.length = ki->length;
126 kb.contents = (uint8_t *)ki->data;
127 ret = krb5int_hmac_keyblock(ktp->hash, &kb, iovs, num_data + 1, out);
128
129 cleanup:
130 if (zeroivec.data != NULL)
131 ktp->enc->free_state(&zeroivec);
132 free(iovs);
133 return ret;
134 }
135
136 krb5_error_code
krb5int_etm_encrypt(const struct krb5_keytypes * ktp,krb5_key key,krb5_keyusage usage,const krb5_data * ivec,krb5_crypto_iov * data,size_t num_data)137 krb5int_etm_encrypt(const struct krb5_keytypes *ktp, krb5_key key,
138 krb5_keyusage usage, const krb5_data *ivec,
139 krb5_crypto_iov *data, size_t num_data)
140 {
141 const struct krb5_enc_provider *enc = ktp->enc;
142 krb5_error_code ret;
143 krb5_data ivcopy = empty_data(), cksum = empty_data();
144 krb5_crypto_iov *header, *trailer, *padding;
145 krb5_key ke = NULL;
146 krb5_data ki = empty_data();
147 unsigned int trailer_len;
148
149 /* E(Confounder | Plaintext) | Checksum(IV | ciphertext) */
150
151 trailer_len = ktp->crypto_length(ktp, KRB5_CRYPTO_TYPE_TRAILER);
152
153 /* Validate header and trailer lengths, and zero out padding length. */
154 header = krb5int_c_locate_iov(data, num_data, KRB5_CRYPTO_TYPE_HEADER);
155 if (header == NULL || header->data.length < enc->block_size)
156 return KRB5_BAD_MSIZE;
157 trailer = krb5int_c_locate_iov(data, num_data, KRB5_CRYPTO_TYPE_TRAILER);
158 if (trailer == NULL || trailer->data.length < trailer_len)
159 return KRB5_BAD_MSIZE;
160 padding = krb5int_c_locate_iov(data, num_data, KRB5_CRYPTO_TYPE_PADDING);
161 if (padding != NULL)
162 padding->data.length = 0;
163
164 if (ivec != NULL) {
165 ret = alloc_data(&ivcopy, ivec->length);
166 if (ret)
167 goto cleanup;
168 memcpy(ivcopy.data, ivec->data, ivec->length);
169 }
170
171 /* Derive the encryption and integrity keys. */
172 ret = derive_keys(ktp, key, usage, &ke, &ki);
173 if (ret)
174 goto cleanup;
175
176 /* Generate confounder. */
177 header->data.length = enc->block_size;
178 ret = krb5_c_random_make_octets(NULL, &header->data);
179 if (ret)
180 goto cleanup;
181
182 /* Encrypt the plaintext (header | data). */
183 ret = enc->encrypt(ke, (ivec == NULL) ? NULL : &ivcopy, data, num_data);
184 if (ret)
185 goto cleanup;
186
187 /* HMAC the IV, confounder, and ciphertext with sign-only data. */
188 ret = hmac_ivec_data(ktp, &ki, ivec, data, num_data, &cksum);
189 if (ret)
190 goto cleanup;
191
192 /* Truncate the HMAC checksum to the trailer length. */
193 assert(trailer_len <= cksum.length);
194 memcpy(trailer->data.data, cksum.data, trailer_len);
195 trailer->data.length = trailer_len;
196
197 /* Copy out the updated ivec if desired. */
198 if (ivec != NULL)
199 memcpy(ivec->data, ivcopy.data, ivcopy.length);
200
201 cleanup:
202 krb5_k_free_key(NULL, ke);
203 zapfree(ki.data, ki.length);
204 free(cksum.data);
205 zapfree(ivcopy.data, ivcopy.length);
206 return ret;
207 }
208
209 krb5_error_code
krb5int_etm_decrypt(const struct krb5_keytypes * ktp,krb5_key key,krb5_keyusage usage,const krb5_data * ivec,krb5_crypto_iov * data,size_t num_data)210 krb5int_etm_decrypt(const struct krb5_keytypes *ktp, krb5_key key,
211 krb5_keyusage usage, const krb5_data *ivec,
212 krb5_crypto_iov *data, size_t num_data)
213 {
214 const struct krb5_enc_provider *enc = ktp->enc;
215 krb5_error_code ret;
216 krb5_data cksum = empty_data();
217 krb5_crypto_iov *header, *trailer;
218 krb5_key ke = NULL;
219 krb5_data ki = empty_data();
220 unsigned int trailer_len;
221
222 trailer_len = ktp->crypto_length(ktp, KRB5_CRYPTO_TYPE_TRAILER);
223
224 /* Validate header and trailer lengths. */
225 header = krb5int_c_locate_iov(data, num_data, KRB5_CRYPTO_TYPE_HEADER);
226 if (header == NULL || header->data.length != enc->block_size)
227 return KRB5_BAD_MSIZE;
228 trailer = krb5int_c_locate_iov(data, num_data, KRB5_CRYPTO_TYPE_TRAILER);
229 if (trailer == NULL || trailer->data.length != trailer_len)
230 return KRB5_BAD_MSIZE;
231
232 /* Derive the encryption and integrity keys. */
233 ret = derive_keys(ktp, key, usage, &ke, &ki);
234 if (ret)
235 goto cleanup;
236
237 /* HMAC the IV, confounder, and ciphertext with sign-only data. */
238 ret = hmac_ivec_data(ktp, &ki, ivec, data, num_data, &cksum);
239 if (ret)
240 goto cleanup;
241
242 /* Compare only the possibly truncated length. */
243 assert(trailer_len <= cksum.length);
244 if (k5_bcmp(cksum.data, trailer->data.data, trailer_len) != 0) {
245 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
246 goto cleanup;
247 }
248
249 /* Decrypt the ciphertext (header | data | padding). */
250 ret = enc->decrypt(ke, ivec, data, num_data);
251
252 cleanup:
253 krb5_k_free_key(NULL, ke);
254 zapfree(ki.data, ki.length);
255 zapfree(cksum.data, cksum.length);
256 return ret;
257 }
258