/src/openssl/providers/implementations/kdfs/krb5kdf.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2018-2024 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | |
11 | | /* |
12 | | * DES low level APIs are deprecated for public use, but still ok for internal |
13 | | * use. We access the DES_set_odd_parity(3) function here. |
14 | | */ |
15 | | #include "internal/deprecated.h" |
16 | | |
17 | | #include <stdlib.h> |
18 | | #include <stdarg.h> |
19 | | #include <string.h> |
20 | | |
21 | | #include <openssl/core_names.h> |
22 | | #include <openssl/des.h> |
23 | | #include <openssl/evp.h> |
24 | | #include <openssl/kdf.h> |
25 | | #include <openssl/proverr.h> |
26 | | |
27 | | #include "internal/cryptlib.h" |
28 | | #include "crypto/evp.h" |
29 | | #include "internal/numbers.h" |
30 | | #include "prov/implementations.h" |
31 | | #include "prov/provider_ctx.h" |
32 | | #include "prov/provider_util.h" |
33 | | #include "prov/providercommon.h" |
34 | | |
35 | | /* KRB5 KDF defined in RFC 3961, Section 5.1 */ |
36 | | |
37 | | static OSSL_FUNC_kdf_newctx_fn krb5kdf_new; |
38 | | static OSSL_FUNC_kdf_dupctx_fn krb5kdf_dup; |
39 | | static OSSL_FUNC_kdf_freectx_fn krb5kdf_free; |
40 | | static OSSL_FUNC_kdf_reset_fn krb5kdf_reset; |
41 | | static OSSL_FUNC_kdf_derive_fn krb5kdf_derive; |
42 | | static OSSL_FUNC_kdf_settable_ctx_params_fn krb5kdf_settable_ctx_params; |
43 | | static OSSL_FUNC_kdf_set_ctx_params_fn krb5kdf_set_ctx_params; |
44 | | static OSSL_FUNC_kdf_gettable_ctx_params_fn krb5kdf_gettable_ctx_params; |
45 | | static OSSL_FUNC_kdf_get_ctx_params_fn krb5kdf_get_ctx_params; |
46 | | |
47 | | static int KRB5KDF(const EVP_CIPHER *cipher, ENGINE *engine, |
48 | | const unsigned char *key, size_t key_len, |
49 | | const unsigned char *constant, size_t constant_len, |
50 | | unsigned char *okey, size_t okey_len); |
51 | | |
52 | | typedef struct { |
53 | | void *provctx; |
54 | | PROV_CIPHER cipher; |
55 | | unsigned char *key; |
56 | | size_t key_len; |
57 | | unsigned char *constant; |
58 | | size_t constant_len; |
59 | | } KRB5KDF_CTX; |
60 | | |
61 | | static void *krb5kdf_new(void *provctx) |
62 | 0 | { |
63 | 0 | KRB5KDF_CTX *ctx; |
64 | |
|
65 | 0 | if (!ossl_prov_is_running()) |
66 | 0 | return NULL; |
67 | | |
68 | 0 | if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) |
69 | 0 | return NULL; |
70 | 0 | ctx->provctx = provctx; |
71 | 0 | return ctx; |
72 | 0 | } |
73 | | |
74 | | static void krb5kdf_free(void *vctx) |
75 | 0 | { |
76 | 0 | KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx; |
77 | |
|
78 | 0 | if (ctx != NULL) { |
79 | 0 | krb5kdf_reset(ctx); |
80 | 0 | OPENSSL_free(ctx); |
81 | 0 | } |
82 | 0 | } |
83 | | |
84 | | static void krb5kdf_reset(void *vctx) |
85 | 0 | { |
86 | 0 | KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx; |
87 | 0 | void *provctx = ctx->provctx; |
88 | |
|
89 | 0 | ossl_prov_cipher_reset(&ctx->cipher); |
90 | 0 | OPENSSL_clear_free(ctx->key, ctx->key_len); |
91 | 0 | OPENSSL_clear_free(ctx->constant, ctx->constant_len); |
92 | 0 | memset(ctx, 0, sizeof(*ctx)); |
93 | 0 | ctx->provctx = provctx; |
94 | 0 | } |
95 | | |
96 | | static int krb5kdf_set_membuf(unsigned char **dst, size_t *dst_len, |
97 | | const OSSL_PARAM *p) |
98 | 0 | { |
99 | 0 | OPENSSL_clear_free(*dst, *dst_len); |
100 | 0 | *dst = NULL; |
101 | 0 | *dst_len = 0; |
102 | 0 | return OSSL_PARAM_get_octet_string(p, (void **)dst, 0, dst_len); |
103 | 0 | } |
104 | | |
105 | | static void *krb5kdf_dup(void *vctx) |
106 | 0 | { |
107 | 0 | const KRB5KDF_CTX *src = (const KRB5KDF_CTX *)vctx; |
108 | 0 | KRB5KDF_CTX *dest; |
109 | |
|
110 | 0 | dest = krb5kdf_new(src->provctx); |
111 | 0 | if (dest != NULL) { |
112 | 0 | if (!ossl_prov_memdup(src->key, src->key_len, |
113 | 0 | &dest->key, &dest->key_len) |
114 | 0 | || !ossl_prov_memdup(src->constant, src->constant_len, |
115 | 0 | &dest->constant , &dest->constant_len) |
116 | 0 | || !ossl_prov_cipher_copy(&dest->cipher, &src->cipher)) |
117 | 0 | goto err; |
118 | 0 | } |
119 | 0 | return dest; |
120 | | |
121 | 0 | err: |
122 | 0 | krb5kdf_free(dest); |
123 | 0 | return NULL; |
124 | 0 | } |
125 | | |
126 | | static int krb5kdf_derive(void *vctx, unsigned char *key, size_t keylen, |
127 | | const OSSL_PARAM params[]) |
128 | 0 | { |
129 | 0 | KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx; |
130 | 0 | const EVP_CIPHER *cipher; |
131 | 0 | ENGINE *engine; |
132 | |
|
133 | 0 | if (!ossl_prov_is_running() || !krb5kdf_set_ctx_params(ctx, params)) |
134 | 0 | return 0; |
135 | | |
136 | 0 | cipher = ossl_prov_cipher_cipher(&ctx->cipher); |
137 | 0 | if (cipher == NULL) { |
138 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CIPHER); |
139 | 0 | return 0; |
140 | 0 | } |
141 | 0 | if (ctx->key == NULL) { |
142 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY); |
143 | 0 | return 0; |
144 | 0 | } |
145 | 0 | if (ctx->constant == NULL) { |
146 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CONSTANT); |
147 | 0 | return 0; |
148 | 0 | } |
149 | 0 | engine = ossl_prov_cipher_engine(&ctx->cipher); |
150 | 0 | return KRB5KDF(cipher, engine, ctx->key, ctx->key_len, |
151 | 0 | ctx->constant, ctx->constant_len, |
152 | 0 | key, keylen); |
153 | 0 | } |
154 | | |
155 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
156 | | #ifndef krb5kdf_set_ctx_params_list |
157 | | static const OSSL_PARAM krb5kdf_set_ctx_params_list[] = { |
158 | | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0), |
159 | | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_CIPHER, NULL, 0), |
160 | | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0), |
161 | | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_CONSTANT, NULL, 0), |
162 | | OSSL_PARAM_END |
163 | | }; |
164 | | #endif |
165 | | |
166 | | #ifndef krb5kdf_set_ctx_params_st |
167 | | struct krb5kdf_set_ctx_params_st { |
168 | | OSSL_PARAM *cipher; |
169 | | OSSL_PARAM *cnst; |
170 | | OSSL_PARAM *engine; |
171 | | OSSL_PARAM *key; |
172 | | OSSL_PARAM *propq; |
173 | | }; |
174 | | #endif |
175 | | |
176 | | #ifndef krb5kdf_set_ctx_params_decoder |
177 | | static int krb5kdf_set_ctx_params_decoder |
178 | | (const OSSL_PARAM *p, struct krb5kdf_set_ctx_params_st *r) |
179 | 0 | { |
180 | 0 | const char *s; |
181 | |
|
182 | 0 | memset(r, 0, sizeof(*r)); |
183 | 0 | if (p != NULL) |
184 | 0 | for (; (s = p->key) != NULL; p++) |
185 | 0 | switch(s[0]) { |
186 | 0 | default: |
187 | 0 | break; |
188 | 0 | case 'c': |
189 | 0 | switch(s[1]) { |
190 | 0 | default: |
191 | 0 | break; |
192 | 0 | case 'i': |
193 | 0 | if (ossl_likely(strcmp("pher", s + 2) == 0)) { |
194 | | /* KDF_PARAM_CIPHER */ |
195 | 0 | if (ossl_unlikely(r->cipher != NULL)) { |
196 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
197 | 0 | "param %s is repeated", s); |
198 | 0 | return 0; |
199 | 0 | } |
200 | 0 | r->cipher = (OSSL_PARAM *)p; |
201 | 0 | } |
202 | 0 | break; |
203 | 0 | case 'o': |
204 | 0 | if (ossl_likely(strcmp("nstant", s + 2) == 0)) { |
205 | | /* KDF_PARAM_CONSTANT */ |
206 | 0 | if (ossl_unlikely(r->cnst != NULL)) { |
207 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
208 | 0 | "param %s is repeated", s); |
209 | 0 | return 0; |
210 | 0 | } |
211 | 0 | r->cnst = (OSSL_PARAM *)p; |
212 | 0 | } |
213 | 0 | } |
214 | 0 | break; |
215 | 0 | case 'e': |
216 | 0 | if (ossl_likely(strcmp("ngine", s + 1) == 0)) { |
217 | | /* ALG_PARAM_ENGINE */ |
218 | 0 | if (ossl_unlikely(r->engine != NULL)) { |
219 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
220 | 0 | "param %s is repeated", s); |
221 | 0 | return 0; |
222 | 0 | } |
223 | 0 | r->engine = (OSSL_PARAM *)p; |
224 | 0 | } |
225 | 0 | break; |
226 | 0 | case 'k': |
227 | 0 | if (ossl_likely(strcmp("ey", s + 1) == 0)) { |
228 | | /* KDF_PARAM_KEY */ |
229 | 0 | if (ossl_unlikely(r->key != NULL)) { |
230 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
231 | 0 | "param %s is repeated", s); |
232 | 0 | return 0; |
233 | 0 | } |
234 | 0 | r->key = (OSSL_PARAM *)p; |
235 | 0 | } |
236 | 0 | break; |
237 | 0 | case 'p': |
238 | 0 | if (ossl_likely(strcmp("roperties", s + 1) == 0)) { |
239 | | /* KDF_PARAM_PROPERTIES */ |
240 | 0 | if (ossl_unlikely(r->propq != NULL)) { |
241 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
242 | 0 | "param %s is repeated", s); |
243 | 0 | return 0; |
244 | 0 | } |
245 | 0 | r->propq = (OSSL_PARAM *)p; |
246 | 0 | } |
247 | 0 | } |
248 | 0 | return 1; |
249 | 0 | } |
250 | | #endif |
251 | | /* End of machine generated */ |
252 | | |
253 | | static int krb5kdf_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
254 | 0 | { |
255 | 0 | struct krb5kdf_set_ctx_params_st p; |
256 | 0 | KRB5KDF_CTX *ctx = vctx; |
257 | 0 | OSSL_LIB_CTX *provctx; |
258 | |
|
259 | 0 | if (ctx == NULL || !krb5kdf_set_ctx_params_decoder(params, &p)) |
260 | 0 | return 0; |
261 | | |
262 | 0 | provctx = PROV_LIBCTX_OF(ctx->provctx); |
263 | |
|
264 | 0 | if (!ossl_prov_cipher_load(&ctx->cipher, p.cipher, p.propq, p.engine, provctx)) |
265 | 0 | return 0; |
266 | | |
267 | 0 | if (p.key != NULL && !krb5kdf_set_membuf(&ctx->key, &ctx->key_len, p.key)) |
268 | 0 | return 0; |
269 | | |
270 | 0 | if (p.cnst != NULL |
271 | 0 | && !krb5kdf_set_membuf(&ctx->constant, &ctx->constant_len, p.cnst)) |
272 | 0 | return 0; |
273 | | |
274 | 0 | return 1; |
275 | 0 | } |
276 | | |
277 | | static const OSSL_PARAM *krb5kdf_settable_ctx_params(ossl_unused void *ctx, |
278 | | ossl_unused void *provctx) |
279 | 0 | { |
280 | 0 | return krb5kdf_set_ctx_params_list; |
281 | 0 | } |
282 | | |
283 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
284 | | #ifndef krb5kdf_get_ctx_params_list |
285 | | static const OSSL_PARAM krb5kdf_get_ctx_params_list[] = { |
286 | | OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL), |
287 | | OSSL_PARAM_END |
288 | | }; |
289 | | #endif |
290 | | |
291 | | #ifndef krb5kdf_get_ctx_params_st |
292 | | struct krb5kdf_get_ctx_params_st { |
293 | | OSSL_PARAM *size; |
294 | | }; |
295 | | #endif |
296 | | |
297 | | #ifndef krb5kdf_get_ctx_params_decoder |
298 | | static int krb5kdf_get_ctx_params_decoder |
299 | | (const OSSL_PARAM *p, struct krb5kdf_get_ctx_params_st *r) |
300 | 0 | { |
301 | 0 | const char *s; |
302 | |
|
303 | 0 | memset(r, 0, sizeof(*r)); |
304 | 0 | if (p != NULL) |
305 | 0 | for (; (s = p->key) != NULL; p++) |
306 | 0 | if (ossl_likely(strcmp("size", s + 0) == 0)) { |
307 | | /* KDF_PARAM_SIZE */ |
308 | 0 | if (ossl_unlikely(r->size != NULL)) { |
309 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
310 | 0 | "param %s is repeated", s); |
311 | 0 | return 0; |
312 | 0 | } |
313 | 0 | r->size = (OSSL_PARAM *)p; |
314 | 0 | } |
315 | 0 | return 1; |
316 | 0 | } |
317 | | #endif |
318 | | /* End of machine generated */ |
319 | | |
320 | | static int krb5kdf_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
321 | 0 | { |
322 | 0 | struct krb5kdf_get_ctx_params_st p; |
323 | 0 | KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx; |
324 | |
|
325 | 0 | if (ctx == NULL || !krb5kdf_get_ctx_params_decoder(params, &p)) |
326 | 0 | return 0; |
327 | | |
328 | 0 | if (p.size != NULL) { |
329 | 0 | const EVP_CIPHER *cipher = ossl_prov_cipher_cipher(&ctx->cipher); |
330 | 0 | size_t len; |
331 | |
|
332 | 0 | if (cipher != NULL) |
333 | 0 | len = EVP_CIPHER_get_key_length(cipher); |
334 | 0 | else |
335 | 0 | len = EVP_MAX_KEY_LENGTH; |
336 | |
|
337 | 0 | if (!OSSL_PARAM_set_size_t(p.size, len)) |
338 | 0 | return 0; |
339 | 0 | } |
340 | 0 | return 1; |
341 | 0 | } |
342 | | |
343 | | static const OSSL_PARAM *krb5kdf_gettable_ctx_params(ossl_unused void *ctx, |
344 | | ossl_unused void *provctx) |
345 | 0 | { |
346 | 0 | return krb5kdf_get_ctx_params_list; |
347 | 0 | } |
348 | | |
349 | | const OSSL_DISPATCH ossl_kdf_krb5kdf_functions[] = { |
350 | | { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))krb5kdf_new }, |
351 | | { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))krb5kdf_dup }, |
352 | | { OSSL_FUNC_KDF_FREECTX, (void(*)(void))krb5kdf_free }, |
353 | | { OSSL_FUNC_KDF_RESET, (void(*)(void))krb5kdf_reset }, |
354 | | { OSSL_FUNC_KDF_DERIVE, (void(*)(void))krb5kdf_derive }, |
355 | | { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, |
356 | | (void(*)(void))krb5kdf_settable_ctx_params }, |
357 | | { OSSL_FUNC_KDF_SET_CTX_PARAMS, |
358 | | (void(*)(void))krb5kdf_set_ctx_params }, |
359 | | { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, |
360 | | (void(*)(void))krb5kdf_gettable_ctx_params }, |
361 | | { OSSL_FUNC_KDF_GET_CTX_PARAMS, |
362 | | (void(*)(void))krb5kdf_get_ctx_params }, |
363 | | OSSL_DISPATCH_END |
364 | | }; |
365 | | |
366 | | #ifndef OPENSSL_NO_DES |
367 | | /* |
368 | | * DES3 is a special case, it requires a random-to-key function and its |
369 | | * input truncated to 21 bytes of the 24 produced by the cipher. |
370 | | * See RFC3961 6.3.1 |
371 | | */ |
372 | | static int fixup_des3_key(unsigned char *key) |
373 | 0 | { |
374 | 0 | unsigned char *cblock; |
375 | 0 | int i, j; |
376 | |
|
377 | 0 | for (i = 2; i >= 0; i--) { |
378 | 0 | cblock = &key[i * 8]; |
379 | 0 | memmove(cblock, &key[i * 7], 7); |
380 | 0 | cblock[7] = 0; |
381 | 0 | for (j = 0; j < 7; j++) |
382 | 0 | cblock[7] |= (cblock[j] & 1) << (j + 1); |
383 | 0 | DES_set_odd_parity((DES_cblock *)cblock); |
384 | 0 | } |
385 | | |
386 | | /* fail if keys are such that triple des degrades to single des */ |
387 | 0 | if (CRYPTO_memcmp(&key[0], &key[8], 8) == 0 || |
388 | 0 | CRYPTO_memcmp(&key[8], &key[16], 8) == 0) { |
389 | 0 | return 0; |
390 | 0 | } |
391 | | |
392 | 0 | return 1; |
393 | 0 | } |
394 | | #endif |
395 | | |
396 | | /* |
397 | | * N-fold(K) where blocksize is N, and constant_len is K |
398 | | * Note: Here |= denotes concatenation |
399 | | * |
400 | | * L = lcm(N,K) |
401 | | * R = L/K |
402 | | * |
403 | | * for r: 1 -> R |
404 | | * s |= constant rot 13*(r-1)) |
405 | | * |
406 | | * block = 0 |
407 | | * for k: 1 -> K |
408 | | * block += s[N(k-1)..(N-1)k] (ones'-complement addition) |
409 | | * |
410 | | * Optimizing for space we compute: |
411 | | * for each l in L-1 -> 0: |
412 | | * s[l] = (constant rot 13*(l/K))[l%k] |
413 | | * block[l % N] += s[l] (with carry) |
414 | | * finally add carry if any |
415 | | */ |
416 | | static void n_fold(unsigned char *block, unsigned int blocksize, |
417 | | const unsigned char *constant, unsigned int constant_len) |
418 | 0 | { |
419 | 0 | unsigned int tmp, gcd, remainder, lcm, carry; |
420 | 0 | int b, l; |
421 | |
|
422 | 0 | if (constant_len == blocksize) { |
423 | 0 | memcpy(block, constant, constant_len); |
424 | 0 | return; |
425 | 0 | } |
426 | | |
427 | | /* Least Common Multiple of lengths: LCM(a,b)*/ |
428 | 0 | gcd = blocksize; |
429 | 0 | remainder = constant_len; |
430 | | /* Calculate Great Common Divisor first GCD(a,b) */ |
431 | 0 | while (remainder != 0) { |
432 | 0 | tmp = gcd % remainder; |
433 | 0 | gcd = remainder; |
434 | 0 | remainder = tmp; |
435 | 0 | } |
436 | | /* resulting a is the GCD, LCM(a,b) = |a*b|/GCD(a,b) */ |
437 | 0 | lcm = blocksize * constant_len / gcd; |
438 | | |
439 | | /* now spread out the bits */ |
440 | 0 | memset(block, 0, blocksize); |
441 | | |
442 | | /* last to first to be able to bring carry forward */ |
443 | 0 | carry = 0; |
444 | 0 | for (l = lcm - 1; l >= 0; l--) { |
445 | 0 | unsigned int rotbits, rshift, rbyte; |
446 | | |
447 | | /* destination byte in block is l % N */ |
448 | 0 | b = l % blocksize; |
449 | | /* Our virtual s buffer is R = L/K long (K = constant_len) */ |
450 | | /* So we rotate backwards from R-1 to 0 (none) rotations */ |
451 | 0 | rotbits = 13 * (l / constant_len); |
452 | | /* find the byte on s where rotbits falls onto */ |
453 | 0 | rbyte = l - (rotbits / 8); |
454 | | /* calculate how much shift on that byte */ |
455 | 0 | rshift = rotbits & 0x07; |
456 | | /* rbyte % constant_len gives us the unrotated byte in the |
457 | | * constant buffer, get also the previous byte then |
458 | | * appropriately shift them to get the rotated byte we need */ |
459 | 0 | tmp = (constant[(rbyte-1) % constant_len] << (8 - rshift) |
460 | 0 | | constant[rbyte % constant_len] >> rshift) |
461 | 0 | & 0xff; |
462 | | /* add with carry to any value placed by previous passes */ |
463 | 0 | tmp += carry + block[b]; |
464 | 0 | block[b] = tmp & 0xff; |
465 | | /* save any carry that may be left */ |
466 | 0 | carry = tmp >> 8; |
467 | 0 | } |
468 | | |
469 | | /* if any carry is left at the end, add it through the number */ |
470 | 0 | for (b = blocksize - 1; b >= 0 && carry != 0; b--) { |
471 | 0 | carry += block[b]; |
472 | 0 | block[b] = carry & 0xff; |
473 | 0 | carry >>= 8; |
474 | 0 | } |
475 | 0 | } |
476 | | |
477 | | static int cipher_init(EVP_CIPHER_CTX *ctx, |
478 | | const EVP_CIPHER *cipher, ENGINE *engine, |
479 | | const unsigned char *key, size_t key_len) |
480 | 0 | { |
481 | 0 | int klen, ret; |
482 | |
|
483 | 0 | ret = EVP_EncryptInit_ex(ctx, cipher, engine, key, NULL); |
484 | 0 | if (!ret) |
485 | 0 | goto out; |
486 | | /* set the key len for the odd variable key len cipher */ |
487 | 0 | klen = EVP_CIPHER_CTX_get_key_length(ctx); |
488 | 0 | if (key_len != (size_t)klen) { |
489 | 0 | ret = EVP_CIPHER_CTX_set_key_length(ctx, (int)key_len); |
490 | 0 | if (ret <= 0) { |
491 | 0 | ret = 0; |
492 | 0 | goto out; |
493 | 0 | } |
494 | 0 | } |
495 | | /* we never want padding, either the length requested is a multiple of |
496 | | * the cipher block size or we are passed a cipher that can cope with |
497 | | * partial blocks via techniques like cipher text stealing */ |
498 | 0 | ret = EVP_CIPHER_CTX_set_padding(ctx, 0); |
499 | 0 | if (!ret) |
500 | 0 | goto out; |
501 | | |
502 | 0 | out: |
503 | 0 | return ret; |
504 | 0 | } |
505 | | |
506 | | static int KRB5KDF(const EVP_CIPHER *cipher, ENGINE *engine, |
507 | | const unsigned char *key, size_t key_len, |
508 | | const unsigned char *constant, size_t constant_len, |
509 | | unsigned char *okey, size_t okey_len) |
510 | 0 | { |
511 | 0 | EVP_CIPHER_CTX *ctx = NULL; |
512 | 0 | unsigned char block[EVP_MAX_BLOCK_LENGTH * 2]; |
513 | 0 | unsigned char *plainblock, *cipherblock; |
514 | 0 | size_t blocksize; |
515 | 0 | size_t cipherlen; |
516 | 0 | size_t osize; |
517 | 0 | #ifndef OPENSSL_NO_DES |
518 | 0 | int des3_no_fixup = 0; |
519 | 0 | #endif |
520 | 0 | int ret; |
521 | |
|
522 | 0 | if (key_len != okey_len) { |
523 | 0 | #ifndef OPENSSL_NO_DES |
524 | | /* special case for 3des, where the caller may be requesting |
525 | | * the random raw key, instead of the fixed up key */ |
526 | 0 | if (EVP_CIPHER_get_nid(cipher) == NID_des_ede3_cbc && |
527 | 0 | key_len == 24 && okey_len == 21) { |
528 | 0 | des3_no_fixup = 1; |
529 | 0 | } else { |
530 | 0 | #endif |
531 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_OUTPUT_BUFFER_SIZE); |
532 | 0 | return 0; |
533 | 0 | #ifndef OPENSSL_NO_DES |
534 | 0 | } |
535 | 0 | #endif |
536 | 0 | } |
537 | | |
538 | 0 | ctx = EVP_CIPHER_CTX_new(); |
539 | 0 | if (ctx == NULL) |
540 | 0 | return 0; |
541 | | |
542 | 0 | ret = cipher_init(ctx, cipher, engine, key, key_len); |
543 | 0 | if (!ret) |
544 | 0 | goto out; |
545 | | |
546 | | /* Initialize input block */ |
547 | 0 | blocksize = EVP_CIPHER_CTX_get_block_size(ctx); |
548 | |
|
549 | 0 | if (blocksize == 0) { |
550 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CIPHER); |
551 | 0 | ret = 0; |
552 | 0 | goto out; |
553 | 0 | } |
554 | | |
555 | 0 | if (constant_len > blocksize) { |
556 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CONSTANT_LENGTH); |
557 | 0 | ret = 0; |
558 | 0 | goto out; |
559 | 0 | } |
560 | | |
561 | 0 | n_fold(block, (unsigned int)blocksize, constant, (unsigned int)constant_len); |
562 | 0 | plainblock = block; |
563 | 0 | cipherblock = block + EVP_MAX_BLOCK_LENGTH; |
564 | |
|
565 | 0 | for (osize = 0; osize < okey_len; osize += cipherlen) { |
566 | 0 | int olen; |
567 | |
|
568 | 0 | ret = EVP_EncryptUpdate(ctx, cipherblock, &olen, |
569 | 0 | plainblock, (int)blocksize); |
570 | 0 | if (!ret) |
571 | 0 | goto out; |
572 | 0 | cipherlen = olen; |
573 | 0 | ret = EVP_EncryptFinal_ex(ctx, cipherblock, &olen); |
574 | 0 | if (!ret) |
575 | 0 | goto out; |
576 | 0 | if (olen != 0) { |
577 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_FINAL_BLOCK_LENGTH); |
578 | 0 | ret = 0; |
579 | 0 | goto out; |
580 | 0 | } |
581 | | |
582 | | /* write cipherblock out */ |
583 | 0 | if (cipherlen > okey_len - osize) |
584 | 0 | cipherlen = okey_len - osize; |
585 | 0 | memcpy(okey + osize, cipherblock, cipherlen); |
586 | |
|
587 | 0 | if (okey_len > osize + cipherlen) { |
588 | | /* we need to reinitialize cipher context per spec */ |
589 | 0 | ret = EVP_CIPHER_CTX_reset(ctx); |
590 | 0 | if (!ret) |
591 | 0 | goto out; |
592 | 0 | ret = cipher_init(ctx, cipher, engine, key, key_len); |
593 | 0 | if (!ret) |
594 | 0 | goto out; |
595 | | |
596 | | /* also swap block offsets so last ciphertext becomes new |
597 | | * plaintext */ |
598 | 0 | plainblock = cipherblock; |
599 | 0 | if (cipherblock == block) { |
600 | 0 | cipherblock += EVP_MAX_BLOCK_LENGTH; |
601 | 0 | } else { |
602 | 0 | cipherblock = block; |
603 | 0 | } |
604 | 0 | } |
605 | 0 | } |
606 | | |
607 | 0 | #ifndef OPENSSL_NO_DES |
608 | 0 | if (EVP_CIPHER_get_nid(cipher) == NID_des_ede3_cbc && !des3_no_fixup) { |
609 | 0 | ret = fixup_des3_key(okey); |
610 | 0 | if (!ret) { |
611 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY); |
612 | 0 | goto out; |
613 | 0 | } |
614 | 0 | } |
615 | 0 | #endif |
616 | | |
617 | 0 | ret = 1; |
618 | |
|
619 | 0 | out: |
620 | 0 | EVP_CIPHER_CTX_free(ctx); |
621 | 0 | OPENSSL_cleanse(block, EVP_MAX_BLOCK_LENGTH * 2); |
622 | 0 | return ret; |
623 | 0 | } |
624 | | |