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;
70 static void krb5kdf_reset(void *vctx)
72 KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx;
74 ossl_prov_cipher_reset(&ctx->cipher);
75 OPENSSL_clear_free(ctx->key, ctx->key_len);
76 OPENSSL_clear_free(ctx->constant, ctx->constant_len);
77 memset(ctx, 0, sizeof(*ctx));
80 static int krb5kdf_set_membuf(unsigned char **dst, size_t *dst_len,
83 OPENSSL_clear_free(*dst, *dst_len);
85 return OSSL_PARAM_get_octet_string(p, (void **)dst, 0, dst_len);
88 static int krb5kdf_derive(void *vctx, unsigned char *key,
91 KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx;
92 const EVP_CIPHER *cipher = ossl_prov_cipher_cipher(&ctx->cipher);
93 ENGINE *engine = ossl_prov_cipher_engine(&ctx->cipher);
96 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CIPHER);
99 if (ctx->key == NULL) {
100 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
103 if (ctx->constant == NULL) {
104 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CONSTANT);
107 return KRB5KDF(cipher, engine, ctx->key, ctx->key_len,
108 ctx->constant, ctx->constant_len,
112 static int krb5kdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
115 KRB5KDF_CTX *ctx = vctx;
116 OPENSSL_CTX *provctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx);
118 if (!ossl_prov_cipher_load_from_params(&ctx->cipher, params, provctx))
121 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY)) != NULL)
122 if (!krb5kdf_set_membuf(&ctx->key, &ctx->key_len, p))
125 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_CONSTANT))
127 if (!krb5kdf_set_membuf(&ctx->constant, &ctx->constant_len, p))
133 static const OSSL_PARAM *krb5kdf_settable_ctx_params(void)
135 static const OSSL_PARAM known_settable_ctx_params[] = {
136 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
137 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_CIPHER, NULL, 0),
138 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
139 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_CONSTANT, NULL, 0),
142 return known_settable_ctx_params;
145 static int krb5kdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
147 KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx;
148 const EVP_CIPHER *cipher;
152 cipher = ossl_prov_cipher_cipher(&ctx->cipher);
154 len = EVP_CIPHER_key_length(cipher);
156 len = EVP_MAX_KEY_LENGTH;
158 if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
159 return OSSL_PARAM_set_size_t(p, len);
163 static const OSSL_PARAM *krb5kdf_gettable_ctx_params(void)
165 static const OSSL_PARAM known_gettable_ctx_params[] = {
166 OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
169 return known_gettable_ctx_params;
172 const OSSL_DISPATCH kdf_krb5kdf_functions[] = {
173 { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))krb5kdf_new },
174 { OSSL_FUNC_KDF_FREECTX, (void(*)(void))krb5kdf_free },
175 { OSSL_FUNC_KDF_RESET, (void(*)(void))krb5kdf_reset },
176 { OSSL_FUNC_KDF_DERIVE, (void(*)(void))krb5kdf_derive },
177 { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
178 (void(*)(void))krb5kdf_settable_ctx_params },
179 { OSSL_FUNC_KDF_SET_CTX_PARAMS,
180 (void(*)(void))krb5kdf_set_ctx_params },
181 { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
182 (void(*)(void))krb5kdf_gettable_ctx_params },
183 { OSSL_FUNC_KDF_GET_CTX_PARAMS,
184 (void(*)(void))krb5kdf_get_ctx_params },
188 #ifndef OPENSSL_NO_DES
190 * DES3 is a special case, it requires a random-to-key function and its
191 * input truncated to 21 bytes of the 24 produced by the cipher.
194 static int fixup_des3_key(unsigned char *key)
196 unsigned char *cblock;
199 for (i = 2; i >= 0; i--) {
200 cblock = &key[i * 8];
201 memmove(cblock, &key[i * 7], 7);
203 for (j = 0; j < 7; j++)
204 cblock[7] |= (cblock[j] & 1) << (j + 1);
205 DES_set_odd_parity((DES_cblock *)cblock);
208 /* fail if keys are such that triple des degrades to single des */
209 if (CRYPTO_memcmp(&key[0], &key[8], 8) == 0 ||
210 CRYPTO_memcmp(&key[8], &key[16], 8) == 0) {
219 * N-fold(K) where blocksize is N, and constant_len is K
220 * Note: Here |= denotes concatenation
226 * s |= constant rot 13*(r-1))
230 * block += s[N(k-1)..(N-1)k] (one's complement addition)
232 * Optimizing for space we compute:
233 * for each l in L-1 -> 0:
234 * s[l] = (constant rot 13*(l/K))[l%k]
235 * block[l % N] += s[l] (with carry)
236 * finally add carry if any
238 static void n_fold(unsigned char *block, unsigned int blocksize,
239 const unsigned char *constant, size_t constant_len)
241 unsigned int tmp, gcd, remainder, lcm, carry;
244 if (constant_len == blocksize) {
245 memcpy(block, constant, constant_len);
249 /* Least Common Multiple of lengths: LCM(a,b)*/
251 remainder = constant_len;
252 /* Calculate Great Common Divisor first GCD(a,b) */
253 while (remainder != 0) {
254 tmp = gcd % remainder;
258 /* resulting a is the GCD, LCM(a,b) = |a*b|/GCD(a,b) */
259 lcm = blocksize * constant_len / gcd;
261 /* now spread out the bits */
262 memset(block, 0, blocksize);
264 /* last to first to be able to bring carry forward */
266 for (l = lcm - 1; l >= 0; l--) {
267 unsigned int rotbits, rshift, rbyte;
269 /* destination byte in block is l % N */
271 /* Our virtual s buffer is R = L/K long (K = constant_len) */
272 /* So we rotate backwards from R-1 to 0 (none) rotations */
273 rotbits = 13 * (l / constant_len);
274 /* find the byte on s where rotbits falls onto */
275 rbyte = l - (rotbits / 8);
276 /* calculate how much shift on that byte */
277 rshift = rotbits & 0x07;
278 /* rbyte % constant_len gives us the unrotated byte in the
279 * constant buffer, get also the previous byte then
280 * appropriately shift them to get the rotated byte we need */
281 tmp = (constant[(rbyte-1) % constant_len] << (8 - rshift)
282 | constant[rbyte % constant_len] >> rshift)
284 /* add with carry to any value placed by previous passes */
285 tmp += carry + block[b];
286 block[b] = tmp & 0xff;
287 /* save any carry that may be left */
291 /* if any carry is left at the end, add it through the number */
292 for (b = blocksize - 1; b >= 0 && carry != 0; b--) {
294 block[b] = carry & 0xff;
299 static int cipher_init(EVP_CIPHER_CTX *ctx,
300 const EVP_CIPHER *cipher, ENGINE *engine,
301 const unsigned char *key, size_t key_len)
305 ret = EVP_EncryptInit_ex(ctx, cipher, engine, key, NULL);
308 /* set the key len for the odd variable key len cipher */
309 klen = EVP_CIPHER_CTX_key_length(ctx);
310 if (key_len != (size_t)klen) {
311 ret = EVP_CIPHER_CTX_set_key_length(ctx, key_len);
315 /* we never want padding, either the length requested is a multiple of
316 * the cipher block size or we are passed a cipher that can cope with
317 * partial blocks via techniques like cipher text stealing */
318 ret = EVP_CIPHER_CTX_set_padding(ctx, 0);
326 static int KRB5KDF(const EVP_CIPHER *cipher, ENGINE *engine,
327 const unsigned char *key, size_t key_len,
328 const unsigned char *constant, size_t constant_len,
329 unsigned char *okey, size_t okey_len)
331 EVP_CIPHER_CTX *ctx = NULL;
332 unsigned char block[EVP_MAX_BLOCK_LENGTH * 2];
333 unsigned char *plainblock, *cipherblock;
337 int des3_no_fixup = 0;
340 if (key_len != okey_len) {
341 /* special case for 3des, where the caller may be requesting
342 * the random raw key, instead of the fixed up key */
343 if (EVP_CIPHER_nid(cipher) == NID_des_ede3_cbc &&
344 key_len == 24 && okey_len == 21) {
347 ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_OUTPUT_BUFFER_SIZE);
352 ctx = EVP_CIPHER_CTX_new();
356 ret = cipher_init(ctx, cipher, engine, key, key_len);
360 /* Initialize input block */
361 blocksize = EVP_CIPHER_CTX_block_size(ctx);
363 if (constant_len > blocksize) {
364 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CONSTANT_LENGTH);
369 n_fold(block, blocksize, constant, constant_len);
371 cipherblock = block + EVP_MAX_BLOCK_LENGTH;
373 for (osize = 0; osize < okey_len; osize += cipherlen) {
376 ret = EVP_EncryptUpdate(ctx, cipherblock, &olen,
377 plainblock, blocksize);
381 ret = EVP_EncryptFinal_ex(ctx, cipherblock, &olen);
385 ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_FINAL_BLOCK_LENGTH);
390 /* write cipherblock out */
391 if (cipherlen > okey_len - osize)
392 cipherlen = okey_len - osize;
393 memcpy(okey + osize, cipherblock, cipherlen);
395 if (okey_len > osize + cipherlen) {
396 /* we need to reinitialize cipher context per spec */
397 ret = EVP_CIPHER_CTX_reset(ctx);
400 ret = cipher_init(ctx, cipher, engine, key, key_len);
404 /* also swap block offsets so last ciphertext becomes new
406 plainblock = cipherblock;
407 if (cipherblock == block) {
408 cipherblock += EVP_MAX_BLOCK_LENGTH;
415 #ifndef OPENSSL_NO_DES
416 if (EVP_CIPHER_nid(cipher) == NID_des_ede3_cbc && !des3_no_fixup) {
417 ret = fixup_des3_key(okey);
419 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);
428 EVP_CIPHER_CTX_free(ctx);
429 OPENSSL_cleanse(block, EVP_MAX_BLOCK_LENGTH * 2);