2 * Copyright 2018-2019 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
14 #include <openssl/core_names.h>
15 #include <openssl/des.h>
16 #include <openssl/evp.h>
17 #include <openssl/kdf.h>
19 #include "internal/cryptlib.h"
20 #include "crypto/evp.h"
21 #include "internal/numbers.h"
22 #include "prov/implementations.h"
23 #include "prov/provider_ctx.h"
24 #include "prov/provider_util.h"
25 #include "prov/providercommonerr.h"
27 /* KRB5 KDF defined in RFC 3961, Section 5.1 */
29 static OSSL_OP_kdf_newctx_fn krb5kdf_new;
30 static OSSL_OP_kdf_freectx_fn krb5kdf_free;
31 static OSSL_OP_kdf_reset_fn krb5kdf_reset;
32 static OSSL_OP_kdf_derive_fn krb5kdf_derive;
33 static OSSL_OP_kdf_settable_ctx_params_fn krb5kdf_settable_ctx_params;
34 static OSSL_OP_kdf_set_ctx_params_fn krb5kdf_set_ctx_params;
35 static OSSL_OP_kdf_gettable_ctx_params_fn krb5kdf_gettable_ctx_params;
36 static OSSL_OP_kdf_get_ctx_params_fn krb5kdf_get_ctx_params;
38 static int KRB5KDF(const EVP_CIPHER *cipher, ENGINE *engine,
39 const unsigned char *key, size_t key_len,
40 const unsigned char *constant, size_t constant_len,
41 unsigned char *okey, size_t okey_len);
48 unsigned char *constant;
52 static void *krb5kdf_new(void *provctx)
56 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
57 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
58 ctx->provctx = provctx;
62 static void krb5kdf_free(void *vctx)
64 KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx;
72 static void krb5kdf_reset(void *vctx)
74 KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx;
76 ossl_prov_cipher_reset(&ctx->cipher);
77 OPENSSL_clear_free(ctx->key, ctx->key_len);
78 OPENSSL_clear_free(ctx->constant, ctx->constant_len);
79 memset(ctx, 0, sizeof(*ctx));
82 static int krb5kdf_set_membuf(unsigned char **dst, size_t *dst_len,
85 OPENSSL_clear_free(*dst, *dst_len);
87 return OSSL_PARAM_get_octet_string(p, (void **)dst, 0, dst_len);
90 static int krb5kdf_derive(void *vctx, unsigned char *key,
93 KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx;
94 const EVP_CIPHER *cipher = ossl_prov_cipher_cipher(&ctx->cipher);
95 ENGINE *engine = ossl_prov_cipher_engine(&ctx->cipher);
98 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CIPHER);
101 if (ctx->key == NULL) {
102 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
105 if (ctx->constant == NULL) {
106 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CONSTANT);
109 return KRB5KDF(cipher, engine, ctx->key, ctx->key_len,
110 ctx->constant, ctx->constant_len,
114 static int krb5kdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
117 KRB5KDF_CTX *ctx = vctx;
118 OPENSSL_CTX *provctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx);
120 if (!ossl_prov_cipher_load_from_params(&ctx->cipher, params, provctx))
123 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY)) != NULL)
124 if (!krb5kdf_set_membuf(&ctx->key, &ctx->key_len, p))
127 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_CONSTANT))
129 if (!krb5kdf_set_membuf(&ctx->constant, &ctx->constant_len, p))
135 static const OSSL_PARAM *krb5kdf_settable_ctx_params(void)
137 static const OSSL_PARAM known_settable_ctx_params[] = {
138 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
139 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_CIPHER, NULL, 0),
140 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
141 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_CONSTANT, NULL, 0),
144 return known_settable_ctx_params;
147 static int krb5kdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
149 KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx;
150 const EVP_CIPHER *cipher;
154 cipher = ossl_prov_cipher_cipher(&ctx->cipher);
156 len = EVP_CIPHER_key_length(cipher);
158 len = EVP_MAX_KEY_LENGTH;
160 if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
161 return OSSL_PARAM_set_size_t(p, len);
165 static const OSSL_PARAM *krb5kdf_gettable_ctx_params(void)
167 static const OSSL_PARAM known_gettable_ctx_params[] = {
168 OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
171 return known_gettable_ctx_params;
174 const OSSL_DISPATCH kdf_krb5kdf_functions[] = {
175 { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))krb5kdf_new },
176 { OSSL_FUNC_KDF_FREECTX, (void(*)(void))krb5kdf_free },
177 { OSSL_FUNC_KDF_RESET, (void(*)(void))krb5kdf_reset },
178 { OSSL_FUNC_KDF_DERIVE, (void(*)(void))krb5kdf_derive },
179 { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
180 (void(*)(void))krb5kdf_settable_ctx_params },
181 { OSSL_FUNC_KDF_SET_CTX_PARAMS,
182 (void(*)(void))krb5kdf_set_ctx_params },
183 { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
184 (void(*)(void))krb5kdf_gettable_ctx_params },
185 { OSSL_FUNC_KDF_GET_CTX_PARAMS,
186 (void(*)(void))krb5kdf_get_ctx_params },
190 #ifndef OPENSSL_NO_DES
192 * DES3 is a special case, it requires a random-to-key function and its
193 * input truncated to 21 bytes of the 24 produced by the cipher.
196 static int fixup_des3_key(unsigned char *key)
198 unsigned char *cblock;
201 for (i = 2; i >= 0; i--) {
202 cblock = &key[i * 8];
203 memmove(cblock, &key[i * 7], 7);
205 for (j = 0; j < 7; j++)
206 cblock[7] |= (cblock[j] & 1) << (j + 1);
207 DES_set_odd_parity((DES_cblock *)cblock);
210 /* fail if keys are such that triple des degrades to single des */
211 if (CRYPTO_memcmp(&key[0], &key[8], 8) == 0 ||
212 CRYPTO_memcmp(&key[8], &key[16], 8) == 0) {
221 * N-fold(K) where blocksize is N, and constant_len is K
222 * Note: Here |= denotes concatenation
228 * s |= constant rot 13*(r-1))
232 * block += s[N(k-1)..(N-1)k] (one's complement addition)
234 * Optimizing for space we compute:
235 * for each l in L-1 -> 0:
236 * s[l] = (constant rot 13*(l/K))[l%k]
237 * block[l % N] += s[l] (with carry)
238 * finally add carry if any
240 static void n_fold(unsigned char *block, unsigned int blocksize,
241 const unsigned char *constant, size_t constant_len)
243 unsigned int tmp, gcd, remainder, lcm, carry;
246 if (constant_len == blocksize) {
247 memcpy(block, constant, constant_len);
251 /* Least Common Multiple of lengths: LCM(a,b)*/
253 remainder = constant_len;
254 /* Calculate Great Common Divisor first GCD(a,b) */
255 while (remainder != 0) {
256 tmp = gcd % remainder;
260 /* resulting a is the GCD, LCM(a,b) = |a*b|/GCD(a,b) */
261 lcm = blocksize * constant_len / gcd;
263 /* now spread out the bits */
264 memset(block, 0, blocksize);
266 /* last to first to be able to bring carry forward */
268 for (l = lcm - 1; l >= 0; l--) {
269 unsigned int rotbits, rshift, rbyte;
271 /* destination byte in block is l % N */
273 /* Our virtual s buffer is R = L/K long (K = constant_len) */
274 /* So we rotate backwards from R-1 to 0 (none) rotations */
275 rotbits = 13 * (l / constant_len);
276 /* find the byte on s where rotbits falls onto */
277 rbyte = l - (rotbits / 8);
278 /* calculate how much shift on that byte */
279 rshift = rotbits & 0x07;
280 /* rbyte % constant_len gives us the unrotated byte in the
281 * constant buffer, get also the previous byte then
282 * appropriately shift them to get the rotated byte we need */
283 tmp = (constant[(rbyte-1) % constant_len] << (8 - rshift)
284 | constant[rbyte % constant_len] >> rshift)
286 /* add with carry to any value placed by previous passes */
287 tmp += carry + block[b];
288 block[b] = tmp & 0xff;
289 /* save any carry that may be left */
293 /* if any carry is left at the end, add it through the number */
294 for (b = blocksize - 1; b >= 0 && carry != 0; b--) {
296 block[b] = carry & 0xff;
301 static int cipher_init(EVP_CIPHER_CTX *ctx,
302 const EVP_CIPHER *cipher, ENGINE *engine,
303 const unsigned char *key, size_t key_len)
307 ret = EVP_EncryptInit_ex(ctx, cipher, engine, key, NULL);
310 /* set the key len for the odd variable key len cipher */
311 klen = EVP_CIPHER_CTX_key_length(ctx);
312 if (key_len != (size_t)klen) {
313 ret = EVP_CIPHER_CTX_set_key_length(ctx, key_len);
317 /* we never want padding, either the length requested is a multiple of
318 * the cipher block size or we are passed a cipher that can cope with
319 * partial blocks via techniques like cipher text stealing */
320 ret = EVP_CIPHER_CTX_set_padding(ctx, 0);
328 static int KRB5KDF(const EVP_CIPHER *cipher, ENGINE *engine,
329 const unsigned char *key, size_t key_len,
330 const unsigned char *constant, size_t constant_len,
331 unsigned char *okey, size_t okey_len)
333 EVP_CIPHER_CTX *ctx = NULL;
334 unsigned char block[EVP_MAX_BLOCK_LENGTH * 2];
335 unsigned char *plainblock, *cipherblock;
339 #ifndef OPENSSL_NO_DES
340 int des3_no_fixup = 0;
344 if (key_len != okey_len) {
345 #ifndef OPENSSL_NO_DES
346 /* special case for 3des, where the caller may be requesting
347 * the random raw key, instead of the fixed up key */
348 if (EVP_CIPHER_nid(cipher) == NID_des_ede3_cbc &&
349 key_len == 24 && okey_len == 21) {
353 ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_OUTPUT_BUFFER_SIZE);
355 #ifndef OPENSSL_NO_DES
360 ctx = EVP_CIPHER_CTX_new();
364 ret = cipher_init(ctx, cipher, engine, key, key_len);
368 /* Initialize input block */
369 blocksize = EVP_CIPHER_CTX_block_size(ctx);
371 if (constant_len > blocksize) {
372 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CONSTANT_LENGTH);
377 n_fold(block, blocksize, constant, constant_len);
379 cipherblock = block + EVP_MAX_BLOCK_LENGTH;
381 for (osize = 0; osize < okey_len; osize += cipherlen) {
384 ret = EVP_EncryptUpdate(ctx, cipherblock, &olen,
385 plainblock, blocksize);
389 ret = EVP_EncryptFinal_ex(ctx, cipherblock, &olen);
393 ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_FINAL_BLOCK_LENGTH);
398 /* write cipherblock out */
399 if (cipherlen > okey_len - osize)
400 cipherlen = okey_len - osize;
401 memcpy(okey + osize, cipherblock, cipherlen);
403 if (okey_len > osize + cipherlen) {
404 /* we need to reinitialize cipher context per spec */
405 ret = EVP_CIPHER_CTX_reset(ctx);
408 ret = cipher_init(ctx, cipher, engine, key, key_len);
412 /* also swap block offsets so last ciphertext becomes new
414 plainblock = cipherblock;
415 if (cipherblock == block) {
416 cipherblock += EVP_MAX_BLOCK_LENGTH;
423 #ifndef OPENSSL_NO_DES
424 if (EVP_CIPHER_nid(cipher) == NID_des_ede3_cbc && !des3_no_fixup) {
425 ret = fixup_des3_key(okey);
427 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);
436 EVP_CIPHER_CTX_free(ctx);
437 OPENSSL_cleanse(block, EVP_MAX_BLOCK_LENGTH * 2);