2 * Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved.
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
11 * DES low level APIs are deprecated for public use, but still ok for internal
12 * use. We access the DES_set_odd_parity(3) function here.
14 #include "internal/deprecated.h"
20 #include <openssl/core_names.h>
21 #include <openssl/des.h>
22 #include <openssl/evp.h>
23 #include <openssl/kdf.h>
24 #include <openssl/proverr.h>
26 #include "internal/cryptlib.h"
27 #include "crypto/evp.h"
28 #include "internal/numbers.h"
29 #include "prov/implementations.h"
30 #include "prov/provider_ctx.h"
31 #include "prov/provider_util.h"
32 #include "prov/providercommon.h"
34 /* KRB5 KDF defined in RFC 3961, Section 5.1 */
36 static OSSL_FUNC_kdf_newctx_fn krb5kdf_new;
37 static OSSL_FUNC_kdf_dupctx_fn krb5kdf_dup;
38 static OSSL_FUNC_kdf_freectx_fn krb5kdf_free;
39 static OSSL_FUNC_kdf_reset_fn krb5kdf_reset;
40 static OSSL_FUNC_kdf_derive_fn krb5kdf_derive;
41 static OSSL_FUNC_kdf_settable_ctx_params_fn krb5kdf_settable_ctx_params;
42 static OSSL_FUNC_kdf_set_ctx_params_fn krb5kdf_set_ctx_params;
43 static OSSL_FUNC_kdf_gettable_ctx_params_fn krb5kdf_gettable_ctx_params;
44 static OSSL_FUNC_kdf_get_ctx_params_fn krb5kdf_get_ctx_params;
46 static int KRB5KDF(const EVP_CIPHER *cipher, ENGINE *engine,
47 const unsigned char *key, size_t key_len,
48 const unsigned char *constant, size_t constant_len,
49 unsigned char *okey, size_t okey_len);
56 unsigned char *constant;
60 static void *krb5kdf_new(void *provctx)
64 if (!ossl_prov_is_running())
67 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
68 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
71 ctx->provctx = provctx;
75 static void krb5kdf_free(void *vctx)
77 KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx;
85 static void krb5kdf_reset(void *vctx)
87 KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx;
88 void *provctx = ctx->provctx;
90 ossl_prov_cipher_reset(&ctx->cipher);
91 OPENSSL_clear_free(ctx->key, ctx->key_len);
92 OPENSSL_clear_free(ctx->constant, ctx->constant_len);
93 memset(ctx, 0, sizeof(*ctx));
94 ctx->provctx = provctx;
97 static int krb5kdf_set_membuf(unsigned char **dst, size_t *dst_len,
100 OPENSSL_clear_free(*dst, *dst_len);
103 return OSSL_PARAM_get_octet_string(p, (void **)dst, 0, dst_len);
106 static void *krb5kdf_dup(void *vctx)
108 const KRB5KDF_CTX *src = (const KRB5KDF_CTX *)vctx;
111 dest = krb5kdf_new(src->provctx);
113 if (!ossl_prov_memdup(src->key, src->key_len,
114 &dest->key, &dest->key_len)
115 || !ossl_prov_memdup(src->constant, src->constant_len,
116 &dest->constant , &dest->constant_len)
117 || !ossl_prov_cipher_copy(&dest->cipher, &src->cipher))
127 static int krb5kdf_derive(void *vctx, unsigned char *key, size_t keylen,
128 const OSSL_PARAM params[])
130 KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx;
131 const EVP_CIPHER *cipher;
134 if (!ossl_prov_is_running() || !krb5kdf_set_ctx_params(ctx, params))
137 cipher = ossl_prov_cipher_cipher(&ctx->cipher);
138 if (cipher == NULL) {
139 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CIPHER);
142 if (ctx->key == NULL) {
143 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
146 if (ctx->constant == NULL) {
147 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CONSTANT);
150 engine = ossl_prov_cipher_engine(&ctx->cipher);
151 return KRB5KDF(cipher, engine, ctx->key, ctx->key_len,
152 ctx->constant, ctx->constant_len,
156 static int krb5kdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
159 KRB5KDF_CTX *ctx = vctx;
160 OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);
165 if (!ossl_prov_cipher_load_from_params(&ctx->cipher, params, provctx))
168 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY)) != NULL)
169 if (!krb5kdf_set_membuf(&ctx->key, &ctx->key_len, p))
172 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_CONSTANT))
174 if (!krb5kdf_set_membuf(&ctx->constant, &ctx->constant_len, p))
180 static const OSSL_PARAM *krb5kdf_settable_ctx_params(ossl_unused void *ctx,
181 ossl_unused void *provctx)
183 static const OSSL_PARAM known_settable_ctx_params[] = {
184 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
185 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_CIPHER, NULL, 0),
186 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
187 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_CONSTANT, NULL, 0),
190 return known_settable_ctx_params;
193 static int krb5kdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
195 KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx;
196 const EVP_CIPHER *cipher;
200 cipher = ossl_prov_cipher_cipher(&ctx->cipher);
202 len = EVP_CIPHER_get_key_length(cipher);
204 len = EVP_MAX_KEY_LENGTH;
206 if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
207 return OSSL_PARAM_set_size_t(p, len);
211 static const OSSL_PARAM *krb5kdf_gettable_ctx_params(ossl_unused void *ctx,
212 ossl_unused void *provctx)
214 static const OSSL_PARAM known_gettable_ctx_params[] = {
215 OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
218 return known_gettable_ctx_params;
221 const OSSL_DISPATCH ossl_kdf_krb5kdf_functions[] = {
222 { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))krb5kdf_new },
223 { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))krb5kdf_dup },
224 { OSSL_FUNC_KDF_FREECTX, (void(*)(void))krb5kdf_free },
225 { OSSL_FUNC_KDF_RESET, (void(*)(void))krb5kdf_reset },
226 { OSSL_FUNC_KDF_DERIVE, (void(*)(void))krb5kdf_derive },
227 { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
228 (void(*)(void))krb5kdf_settable_ctx_params },
229 { OSSL_FUNC_KDF_SET_CTX_PARAMS,
230 (void(*)(void))krb5kdf_set_ctx_params },
231 { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
232 (void(*)(void))krb5kdf_gettable_ctx_params },
233 { OSSL_FUNC_KDF_GET_CTX_PARAMS,
234 (void(*)(void))krb5kdf_get_ctx_params },
238 #ifndef OPENSSL_NO_DES
240 * DES3 is a special case, it requires a random-to-key function and its
241 * input truncated to 21 bytes of the 24 produced by the cipher.
244 static int fixup_des3_key(unsigned char *key)
246 unsigned char *cblock;
249 for (i = 2; i >= 0; i--) {
250 cblock = &key[i * 8];
251 memmove(cblock, &key[i * 7], 7);
253 for (j = 0; j < 7; j++)
254 cblock[7] |= (cblock[j] & 1) << (j + 1);
255 DES_set_odd_parity((DES_cblock *)cblock);
258 /* fail if keys are such that triple des degrades to single des */
259 if (CRYPTO_memcmp(&key[0], &key[8], 8) == 0 ||
260 CRYPTO_memcmp(&key[8], &key[16], 8) == 0) {
269 * N-fold(K) where blocksize is N, and constant_len is K
270 * Note: Here |= denotes concatenation
276 * s |= constant rot 13*(r-1))
280 * block += s[N(k-1)..(N-1)k] (one's complement addition)
282 * Optimizing for space we compute:
283 * for each l in L-1 -> 0:
284 * s[l] = (constant rot 13*(l/K))[l%k]
285 * block[l % N] += s[l] (with carry)
286 * finally add carry if any
288 static void n_fold(unsigned char *block, unsigned int blocksize,
289 const unsigned char *constant, size_t constant_len)
291 unsigned int tmp, gcd, remainder, lcm, carry;
294 if (constant_len == blocksize) {
295 memcpy(block, constant, constant_len);
299 /* Least Common Multiple of lengths: LCM(a,b)*/
301 remainder = constant_len;
302 /* Calculate Great Common Divisor first GCD(a,b) */
303 while (remainder != 0) {
304 tmp = gcd % remainder;
308 /* resulting a is the GCD, LCM(a,b) = |a*b|/GCD(a,b) */
309 lcm = blocksize * constant_len / gcd;
311 /* now spread out the bits */
312 memset(block, 0, blocksize);
314 /* last to first to be able to bring carry forward */
316 for (l = lcm - 1; l >= 0; l--) {
317 unsigned int rotbits, rshift, rbyte;
319 /* destination byte in block is l % N */
321 /* Our virtual s buffer is R = L/K long (K = constant_len) */
322 /* So we rotate backwards from R-1 to 0 (none) rotations */
323 rotbits = 13 * (l / constant_len);
324 /* find the byte on s where rotbits falls onto */
325 rbyte = l - (rotbits / 8);
326 /* calculate how much shift on that byte */
327 rshift = rotbits & 0x07;
328 /* rbyte % constant_len gives us the unrotated byte in the
329 * constant buffer, get also the previous byte then
330 * appropriately shift them to get the rotated byte we need */
331 tmp = (constant[(rbyte-1) % constant_len] << (8 - rshift)
332 | constant[rbyte % constant_len] >> rshift)
334 /* add with carry to any value placed by previous passes */
335 tmp += carry + block[b];
336 block[b] = tmp & 0xff;
337 /* save any carry that may be left */
341 /* if any carry is left at the end, add it through the number */
342 for (b = blocksize - 1; b >= 0 && carry != 0; b--) {
344 block[b] = carry & 0xff;
349 static int cipher_init(EVP_CIPHER_CTX *ctx,
350 const EVP_CIPHER *cipher, ENGINE *engine,
351 const unsigned char *key, size_t key_len)
355 ret = EVP_EncryptInit_ex(ctx, cipher, engine, key, NULL);
358 /* set the key len for the odd variable key len cipher */
359 klen = EVP_CIPHER_CTX_get_key_length(ctx);
360 if (key_len != (size_t)klen) {
361 ret = EVP_CIPHER_CTX_set_key_length(ctx, key_len);
365 /* we never want padding, either the length requested is a multiple of
366 * the cipher block size or we are passed a cipher that can cope with
367 * partial blocks via techniques like cipher text stealing */
368 ret = EVP_CIPHER_CTX_set_padding(ctx, 0);
376 static int KRB5KDF(const EVP_CIPHER *cipher, ENGINE *engine,
377 const unsigned char *key, size_t key_len,
378 const unsigned char *constant, size_t constant_len,
379 unsigned char *okey, size_t okey_len)
381 EVP_CIPHER_CTX *ctx = NULL;
382 unsigned char block[EVP_MAX_BLOCK_LENGTH * 2];
383 unsigned char *plainblock, *cipherblock;
387 #ifndef OPENSSL_NO_DES
388 int des3_no_fixup = 0;
392 if (key_len != okey_len) {
393 #ifndef OPENSSL_NO_DES
394 /* special case for 3des, where the caller may be requesting
395 * the random raw key, instead of the fixed up key */
396 if (EVP_CIPHER_get_nid(cipher) == NID_des_ede3_cbc &&
397 key_len == 24 && okey_len == 21) {
401 ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_OUTPUT_BUFFER_SIZE);
403 #ifndef OPENSSL_NO_DES
408 ctx = EVP_CIPHER_CTX_new();
412 ret = cipher_init(ctx, cipher, engine, key, key_len);
416 /* Initialize input block */
417 blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
419 if (constant_len > blocksize) {
420 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CONSTANT_LENGTH);
425 n_fold(block, blocksize, constant, constant_len);
427 cipherblock = block + EVP_MAX_BLOCK_LENGTH;
429 for (osize = 0; osize < okey_len; osize += cipherlen) {
432 ret = EVP_EncryptUpdate(ctx, cipherblock, &olen,
433 plainblock, blocksize);
437 ret = EVP_EncryptFinal_ex(ctx, cipherblock, &olen);
441 ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_FINAL_BLOCK_LENGTH);
446 /* write cipherblock out */
447 if (cipherlen > okey_len - osize)
448 cipherlen = okey_len - osize;
449 memcpy(okey + osize, cipherblock, cipherlen);
451 if (okey_len > osize + cipherlen) {
452 /* we need to reinitialize cipher context per spec */
453 ret = EVP_CIPHER_CTX_reset(ctx);
456 ret = cipher_init(ctx, cipher, engine, key, key_len);
460 /* also swap block offsets so last ciphertext becomes new
462 plainblock = cipherblock;
463 if (cipherblock == block) {
464 cipherblock += EVP_MAX_BLOCK_LENGTH;
471 #ifndef OPENSSL_NO_DES
472 if (EVP_CIPHER_get_nid(cipher) == NID_des_ede3_cbc && !des3_no_fixup) {
473 ret = fixup_des3_key(okey);
475 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);
484 EVP_CIPHER_CTX_free(ctx);
485 OPENSSL_cleanse(block, EVP_MAX_BLOCK_LENGTH * 2);