2 * Copyright 2018-2020 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>
25 #include "internal/cryptlib.h"
26 #include "crypto/evp.h"
27 #include "internal/numbers.h"
28 #include "prov/implementations.h"
29 #include "prov/provider_ctx.h"
30 #include "prov/provider_util.h"
31 #include "prov/providercommonerr.h"
33 /* KRB5 KDF defined in RFC 3961, Section 5.1 */
35 static OSSL_FUNC_kdf_newctx_fn krb5kdf_new;
36 static OSSL_FUNC_kdf_freectx_fn krb5kdf_free;
37 static OSSL_FUNC_kdf_reset_fn krb5kdf_reset;
38 static OSSL_FUNC_kdf_derive_fn krb5kdf_derive;
39 static OSSL_FUNC_kdf_settable_ctx_params_fn krb5kdf_settable_ctx_params;
40 static OSSL_FUNC_kdf_set_ctx_params_fn krb5kdf_set_ctx_params;
41 static OSSL_FUNC_kdf_gettable_ctx_params_fn krb5kdf_gettable_ctx_params;
42 static OSSL_FUNC_kdf_get_ctx_params_fn krb5kdf_get_ctx_params;
44 static int KRB5KDF(const EVP_CIPHER *cipher, ENGINE *engine,
45 const unsigned char *key, size_t key_len,
46 const unsigned char *constant, size_t constant_len,
47 unsigned char *okey, size_t okey_len);
54 unsigned char *constant;
58 static void *krb5kdf_new(void *provctx)
62 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
63 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
64 ctx->provctx = provctx;
68 static void krb5kdf_free(void *vctx)
70 KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx;
78 static void krb5kdf_reset(void *vctx)
80 KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx;
81 void *provctx = ctx->provctx;
83 ossl_prov_cipher_reset(&ctx->cipher);
84 OPENSSL_clear_free(ctx->key, ctx->key_len);
85 OPENSSL_clear_free(ctx->constant, ctx->constant_len);
86 memset(ctx, 0, sizeof(*ctx));
87 ctx->provctx = provctx;
90 static int krb5kdf_set_membuf(unsigned char **dst, size_t *dst_len,
93 OPENSSL_clear_free(*dst, *dst_len);
95 return OSSL_PARAM_get_octet_string(p, (void **)dst, 0, dst_len);
98 static int krb5kdf_derive(void *vctx, unsigned char *key,
101 KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx;
102 const EVP_CIPHER *cipher = ossl_prov_cipher_cipher(&ctx->cipher);
103 ENGINE *engine = ossl_prov_cipher_engine(&ctx->cipher);
105 if (cipher == NULL) {
106 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CIPHER);
109 if (ctx->key == NULL) {
110 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
113 if (ctx->constant == NULL) {
114 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CONSTANT);
117 return KRB5KDF(cipher, engine, ctx->key, ctx->key_len,
118 ctx->constant, ctx->constant_len,
122 static int krb5kdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
125 KRB5KDF_CTX *ctx = vctx;
126 OPENSSL_CTX *provctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx);
128 if (!ossl_prov_cipher_load_from_params(&ctx->cipher, params, provctx))
131 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY)) != NULL)
132 if (!krb5kdf_set_membuf(&ctx->key, &ctx->key_len, p))
135 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_CONSTANT))
137 if (!krb5kdf_set_membuf(&ctx->constant, &ctx->constant_len, p))
143 static const OSSL_PARAM *krb5kdf_settable_ctx_params(void *provctx)
145 static const OSSL_PARAM known_settable_ctx_params[] = {
146 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
147 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_CIPHER, NULL, 0),
148 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
149 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_CONSTANT, NULL, 0),
152 return known_settable_ctx_params;
155 static int krb5kdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
157 KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx;
158 const EVP_CIPHER *cipher;
162 cipher = ossl_prov_cipher_cipher(&ctx->cipher);
164 len = EVP_CIPHER_key_length(cipher);
166 len = EVP_MAX_KEY_LENGTH;
168 if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
169 return OSSL_PARAM_set_size_t(p, len);
173 static const OSSL_PARAM *krb5kdf_gettable_ctx_params(void *provctx)
175 static const OSSL_PARAM known_gettable_ctx_params[] = {
176 OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
179 return known_gettable_ctx_params;
182 const OSSL_DISPATCH kdf_krb5kdf_functions[] = {
183 { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))krb5kdf_new },
184 { OSSL_FUNC_KDF_FREECTX, (void(*)(void))krb5kdf_free },
185 { OSSL_FUNC_KDF_RESET, (void(*)(void))krb5kdf_reset },
186 { OSSL_FUNC_KDF_DERIVE, (void(*)(void))krb5kdf_derive },
187 { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
188 (void(*)(void))krb5kdf_settable_ctx_params },
189 { OSSL_FUNC_KDF_SET_CTX_PARAMS,
190 (void(*)(void))krb5kdf_set_ctx_params },
191 { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
192 (void(*)(void))krb5kdf_gettable_ctx_params },
193 { OSSL_FUNC_KDF_GET_CTX_PARAMS,
194 (void(*)(void))krb5kdf_get_ctx_params },
198 #ifndef OPENSSL_NO_DES
200 * DES3 is a special case, it requires a random-to-key function and its
201 * input truncated to 21 bytes of the 24 produced by the cipher.
204 static int fixup_des3_key(unsigned char *key)
206 unsigned char *cblock;
209 for (i = 2; i >= 0; i--) {
210 cblock = &key[i * 8];
211 memmove(cblock, &key[i * 7], 7);
213 for (j = 0; j < 7; j++)
214 cblock[7] |= (cblock[j] & 1) << (j + 1);
215 DES_set_odd_parity((DES_cblock *)cblock);
218 /* fail if keys are such that triple des degrades to single des */
219 if (CRYPTO_memcmp(&key[0], &key[8], 8) == 0 ||
220 CRYPTO_memcmp(&key[8], &key[16], 8) == 0) {
229 * N-fold(K) where blocksize is N, and constant_len is K
230 * Note: Here |= denotes concatenation
236 * s |= constant rot 13*(r-1))
240 * block += s[N(k-1)..(N-1)k] (one's complement addition)
242 * Optimizing for space we compute:
243 * for each l in L-1 -> 0:
244 * s[l] = (constant rot 13*(l/K))[l%k]
245 * block[l % N] += s[l] (with carry)
246 * finally add carry if any
248 static void n_fold(unsigned char *block, unsigned int blocksize,
249 const unsigned char *constant, size_t constant_len)
251 unsigned int tmp, gcd, remainder, lcm, carry;
254 if (constant_len == blocksize) {
255 memcpy(block, constant, constant_len);
259 /* Least Common Multiple of lengths: LCM(a,b)*/
261 remainder = constant_len;
262 /* Calculate Great Common Divisor first GCD(a,b) */
263 while (remainder != 0) {
264 tmp = gcd % remainder;
268 /* resulting a is the GCD, LCM(a,b) = |a*b|/GCD(a,b) */
269 lcm = blocksize * constant_len / gcd;
271 /* now spread out the bits */
272 memset(block, 0, blocksize);
274 /* last to first to be able to bring carry forward */
276 for (l = lcm - 1; l >= 0; l--) {
277 unsigned int rotbits, rshift, rbyte;
279 /* destination byte in block is l % N */
281 /* Our virtual s buffer is R = L/K long (K = constant_len) */
282 /* So we rotate backwards from R-1 to 0 (none) rotations */
283 rotbits = 13 * (l / constant_len);
284 /* find the byte on s where rotbits falls onto */
285 rbyte = l - (rotbits / 8);
286 /* calculate how much shift on that byte */
287 rshift = rotbits & 0x07;
288 /* rbyte % constant_len gives us the unrotated byte in the
289 * constant buffer, get also the previous byte then
290 * appropriately shift them to get the rotated byte we need */
291 tmp = (constant[(rbyte-1) % constant_len] << (8 - rshift)
292 | constant[rbyte % constant_len] >> rshift)
294 /* add with carry to any value placed by previous passes */
295 tmp += carry + block[b];
296 block[b] = tmp & 0xff;
297 /* save any carry that may be left */
301 /* if any carry is left at the end, add it through the number */
302 for (b = blocksize - 1; b >= 0 && carry != 0; b--) {
304 block[b] = carry & 0xff;
309 static int cipher_init(EVP_CIPHER_CTX *ctx,
310 const EVP_CIPHER *cipher, ENGINE *engine,
311 const unsigned char *key, size_t key_len)
315 ret = EVP_EncryptInit_ex(ctx, cipher, engine, key, NULL);
318 /* set the key len for the odd variable key len cipher */
319 klen = EVP_CIPHER_CTX_key_length(ctx);
320 if (key_len != (size_t)klen) {
321 ret = EVP_CIPHER_CTX_set_key_length(ctx, key_len);
325 /* we never want padding, either the length requested is a multiple of
326 * the cipher block size or we are passed a cipher that can cope with
327 * partial blocks via techniques like cipher text stealing */
328 ret = EVP_CIPHER_CTX_set_padding(ctx, 0);
336 static int KRB5KDF(const EVP_CIPHER *cipher, ENGINE *engine,
337 const unsigned char *key, size_t key_len,
338 const unsigned char *constant, size_t constant_len,
339 unsigned char *okey, size_t okey_len)
341 EVP_CIPHER_CTX *ctx = NULL;
342 unsigned char block[EVP_MAX_BLOCK_LENGTH * 2];
343 unsigned char *plainblock, *cipherblock;
347 #ifndef OPENSSL_NO_DES
348 int des3_no_fixup = 0;
352 if (key_len != okey_len) {
353 #ifndef OPENSSL_NO_DES
354 /* special case for 3des, where the caller may be requesting
355 * the random raw key, instead of the fixed up key */
356 if (EVP_CIPHER_nid(cipher) == NID_des_ede3_cbc &&
357 key_len == 24 && okey_len == 21) {
361 ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_OUTPUT_BUFFER_SIZE);
363 #ifndef OPENSSL_NO_DES
368 ctx = EVP_CIPHER_CTX_new();
372 ret = cipher_init(ctx, cipher, engine, key, key_len);
376 /* Initialize input block */
377 blocksize = EVP_CIPHER_CTX_block_size(ctx);
379 if (constant_len > blocksize) {
380 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CONSTANT_LENGTH);
385 n_fold(block, blocksize, constant, constant_len);
387 cipherblock = block + EVP_MAX_BLOCK_LENGTH;
389 for (osize = 0; osize < okey_len; osize += cipherlen) {
392 ret = EVP_EncryptUpdate(ctx, cipherblock, &olen,
393 plainblock, blocksize);
397 ret = EVP_EncryptFinal_ex(ctx, cipherblock, &olen);
401 ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_FINAL_BLOCK_LENGTH);
406 /* write cipherblock out */
407 if (cipherlen > okey_len - osize)
408 cipherlen = okey_len - osize;
409 memcpy(okey + osize, cipherblock, cipherlen);
411 if (okey_len > osize + cipherlen) {
412 /* we need to reinitialize cipher context per spec */
413 ret = EVP_CIPHER_CTX_reset(ctx);
416 ret = cipher_init(ctx, cipher, engine, key, key_len);
420 /* also swap block offsets so last ciphertext becomes new
422 plainblock = cipherblock;
423 if (cipherblock == block) {
424 cipherblock += EVP_MAX_BLOCK_LENGTH;
431 #ifndef OPENSSL_NO_DES
432 if (EVP_CIPHER_nid(cipher) == NID_des_ede3_cbc && !des3_no_fixup) {
433 ret = fixup_des3_key(okey);
435 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);
444 EVP_CIPHER_CTX_free(ctx);
445 OPENSSL_cleanse(block, EVP_MAX_BLOCK_LENGTH * 2);