f19e014927c4b1ef6ae2d8e28e8d686a72799955
[openssl.git] / providers / implementations / kdfs / x942kdf.c
1 /*
2  * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright (c) 2019, Oracle and/or its affiliates.  All rights reserved.
4  *
5  * Licensed under the Apache License 2.0 (the "License").  You may not use
6  * this file except in compliance with the License.  You can obtain a copy
7  * in the file LICENSE in the source distribution or at
8  * https://www.openssl.org/source/license.html
9  */
10
11 #include "e_os.h"
12 #include <openssl/core_names.h>
13 #include <openssl/core_dispatch.h>
14 #include <openssl/err.h>
15 #include <openssl/evp.h>
16 #include <openssl/params.h>
17 #include "internal/packet.h"
18 #include "internal/der.h"
19 #include "prov/provider_ctx.h"
20 #include "prov/providercommon.h"
21 #include "prov/providercommonerr.h"
22 #include "prov/implementations.h"
23 #include "prov/provider_util.h"
24 #include "prov/der_wrap.h"
25
26 #define X942KDF_MAX_INLEN (1 << 30)
27
28 static OSSL_FUNC_kdf_newctx_fn x942kdf_new;
29 static OSSL_FUNC_kdf_freectx_fn x942kdf_free;
30 static OSSL_FUNC_kdf_reset_fn x942kdf_reset;
31 static OSSL_FUNC_kdf_derive_fn x942kdf_derive;
32 static OSSL_FUNC_kdf_settable_ctx_params_fn x942kdf_settable_ctx_params;
33 static OSSL_FUNC_kdf_set_ctx_params_fn x942kdf_set_ctx_params;
34 static OSSL_FUNC_kdf_gettable_ctx_params_fn x942kdf_gettable_ctx_params;
35 static OSSL_FUNC_kdf_get_ctx_params_fn x942kdf_get_ctx_params;
36
37 typedef struct {
38     void *provctx;
39     PROV_DIGEST digest;
40     unsigned char *secret;
41     size_t secret_len;
42     unsigned char *ukm;
43     size_t ukm_len;
44     size_t dkm_len;
45     const unsigned char *cek_oid;
46     size_t cek_oid_len;
47 } KDF_X942;
48
49 /*
50  * A table of allowed wrapping algorithms, oids and the associated output
51  * lengths.
52  * NOTE: RC2wrap and camellia128_wrap have been removed as there are no
53  * corresponding ciphers for these operations.
54  */
55 static const struct {
56     const char *name;
57     const unsigned char *oid;
58     size_t oid_len;
59     size_t keklen; /* size in bytes */
60 } kek_algs[] = {
61     { "AES-128-WRAP", ossl_der_oid_id_aes128_wrap, DER_OID_SZ_id_aes128_wrap,
62       16 },
63     { "AES-192-WRAP", ossl_der_oid_id_aes192_wrap, DER_OID_SZ_id_aes192_wrap,
64       24 },
65     { "AES-256-WRAP", ossl_der_oid_id_aes256_wrap, DER_OID_SZ_id_aes256_wrap,
66       32 },
67 #ifndef FIPS_MODULE
68     { "DES3-WRAP", ossl_der_oid_id_alg_CMS3DESwrap,
69       DER_OID_SZ_id_alg_CMS3DESwrap, 24 },
70 #endif
71 };
72
73 static int find_alg_id(OSSL_LIB_CTX *libctx, const char *algname,
74                        const char *propq, size_t *id)
75 {
76     int ret = 1;
77     size_t i;
78     EVP_CIPHER *cipher;
79
80     cipher = EVP_CIPHER_fetch(libctx, algname, propq);
81     if (cipher != NULL) {
82         for (i = 0; i < OSSL_NELEM(kek_algs); i++) {
83             if (EVP_CIPHER_is_a(cipher, kek_algs[i].name)) {
84                 *id = i;
85                 goto end;
86             }
87         }
88     }
89     ret = 0;
90     ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_CEK_ALG);
91 end:
92     EVP_CIPHER_free(cipher);
93     return ret;
94 }
95
96 static int DER_w_keyinfo(WPACKET *pkt,
97                          const unsigned char *der_oid, size_t der_oidlen,
98                          unsigned char **pcounter)
99 {
100     return ossl_DER_w_begin_sequence(pkt, -1)
101            /* Store the initial value of 1 into the counter */
102            && ossl_DER_w_octet_string_uint32(pkt, -1, 1)
103            /* Remember where we stored the counter in the buffer */
104            && (pcounter == NULL
105                || (*pcounter = WPACKET_get_curr(pkt)) != NULL)
106            && ossl_DER_w_precompiled(pkt, -1, der_oid, der_oidlen)
107            && ossl_DER_w_end_sequence(pkt, -1);
108 }
109
110 static int der_encode_sharedinfo(WPACKET *pkt, unsigned char *buf, size_t buflen,
111                                  const unsigned char *der_oid, size_t der_oidlen,
112                                  const unsigned char *ukm, size_t ukmlen,
113                                  uint32_t keylen_bits, unsigned char **pcounter)
114 {
115     return (buf != NULL ? WPACKET_init_der(pkt, buf, buflen) :
116                           WPACKET_init_null_der(pkt))
117            && ossl_DER_w_begin_sequence(pkt, -1)
118            && ossl_DER_w_octet_string_uint32(pkt, 2, keylen_bits)
119            && (ukm == NULL || ossl_DER_w_octet_string(pkt, 0, ukm, ukmlen))
120            && DER_w_keyinfo(pkt, der_oid, der_oidlen, pcounter)
121            && ossl_DER_w_end_sequence(pkt, -1)
122            && WPACKET_finish(pkt);
123 }
124
125 /*
126  * Encode the other info structure.
127  *
128  *  RFC2631 Section 2.1.2 Contains the following definition for otherinfo
129  *
130  *  OtherInfo ::= SEQUENCE {
131  *      keyInfo KeySpecificInfo,
132  *      partyAInfo [0] OCTET STRING OPTIONAL,
133  *      suppPubInfo [2] OCTET STRING
134  *  }
135  *  Note suppPubInfo is the key length (in bits) (stored into 4 bytes)
136  *
137  *
138  *  KeySpecificInfo ::= SEQUENCE {
139  *      algorithm OBJECT IDENTIFIER,
140  *      counter OCTET STRING SIZE (4..4)
141  *  }
142  *
143  * |keylen| is the length (in bytes) of the generated KEK. It is stored into
144  * suppPubInfo (in bits).
145  * |cek_oid| The oid of the key wrapping algorithm.
146  * |cek_oidlen| The length (in bytes) of the key wrapping algorithm oid,
147  * |ukm| is the optional user keying material that is stored into partyAInfo. It
148  * can be NULL.
149  * |ukmlen| is the user keying material length (in bytes).
150  * |der| is the returned encoded data. It must be freed by the caller.
151  * |der_len| is the returned size of the encoded data.
152  * |out_ctr| returns a pointer to the counter data which is embedded inside the
153  * encoded data. This allows the counter bytes to be updated without re-encoding.
154  *
155  * Returns: 1 if successfully encoded, or 0 otherwise.
156  * Assumptions: |der|, |der_len| & |out_ctr| are not NULL.
157  */
158 static int x942_encode_otherinfo(size_t keylen,
159                                  const unsigned char *cek_oid, size_t cek_oidlen,
160                                  const unsigned char *ukm, size_t ukmlen,
161                                  unsigned char **der, size_t *der_len,
162                                  unsigned char **out_ctr)
163 {
164     int ret = 0;
165     unsigned char *pcounter = NULL, *der_buf = NULL;
166     size_t der_buflen = 0;
167     WPACKET pkt;
168     uint32_t keylen_bits;
169
170     /* keylenbits must fit into 4 bytes */
171     if (keylen > 0xFFFFFF)
172         return 0;
173     keylen_bits = 8 * keylen;
174
175     /* Calculate the size of the buffer */
176     if (!der_encode_sharedinfo(&pkt, NULL, 0, cek_oid, cek_oidlen, ukm, ukmlen,
177                                keylen_bits, NULL)
178         || !WPACKET_get_total_written(&pkt, &der_buflen))
179         goto err;
180     WPACKET_cleanup(&pkt);
181     /* Alloc the buffer */
182     der_buf = OPENSSL_zalloc(der_buflen);
183     if (der_buf == NULL)
184         goto err;
185     /* Encode into the buffer */
186     if (!der_encode_sharedinfo(&pkt, der_buf, der_buflen, cek_oid, cek_oidlen,
187                                ukm, ukmlen, keylen_bits, &pcounter))
188         goto err;
189     /*
190      * Since we allocated the exact size required, the buffer should point to the
191      * start of the alllocated buffer at this point.
192      */
193     if (WPACKET_get_curr(&pkt) != der_buf)
194         goto err;
195
196     /*
197      * The data for the DER encoded octet string of a 32 bit counter = 1
198      * should be 04 04 00 00 00 01
199      * So just check the header is correct and skip over it.
200      * This counter will be incremented in the kdf update loop.
201      */
202     if (pcounter == NULL
203         || pcounter[0] != 0x04
204         || pcounter[1] != 0x04)
205         goto err;
206     *out_ctr = (pcounter + 2);
207     *der = der_buf;
208     *der_len = der_buflen;
209     ret = 1;
210 err:
211     WPACKET_cleanup(&pkt);
212     return ret;
213 }
214
215 static int x942kdf_hash_kdm(const EVP_MD *kdf_md,
216                             const unsigned char *z, size_t z_len,
217                             const unsigned char *other, size_t other_len,
218                             unsigned char *ctr,
219                             unsigned char *derived_key, size_t derived_key_len)
220 {
221     int ret = 0, hlen;
222     size_t counter, out_len, len = derived_key_len;
223     unsigned char mac[EVP_MAX_MD_SIZE];
224     unsigned char *out = derived_key;
225     EVP_MD_CTX *ctx = NULL, *ctx_init = NULL;
226
227     if (z_len > X942KDF_MAX_INLEN || other_len > X942KDF_MAX_INLEN
228             || derived_key_len > X942KDF_MAX_INLEN
229             || derived_key_len == 0) {
230         ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
231         return 0;
232     }
233
234     hlen = EVP_MD_size(kdf_md);
235     if (hlen <= 0)
236         return 0;
237     out_len = (size_t)hlen;
238
239     ctx = EVP_MD_CTX_create();
240     ctx_init = EVP_MD_CTX_create();
241     if (ctx == NULL || ctx_init == NULL)
242         goto end;
243
244     if (!EVP_DigestInit(ctx_init, kdf_md))
245         goto end;
246
247     for (counter = 1;; counter++) {
248         /* updating the ctr modifies 4 bytes in the 'other' buffer */
249         ctr[0] = (unsigned char)((counter >> 24) & 0xff);
250         ctr[1] = (unsigned char)((counter >> 16) & 0xff);
251         ctr[2] = (unsigned char)((counter >> 8) & 0xff);
252         ctr[3] = (unsigned char)(counter & 0xff);
253
254         if (!EVP_MD_CTX_copy_ex(ctx, ctx_init)
255             || !EVP_DigestUpdate(ctx, z, z_len)
256             || !EVP_DigestUpdate(ctx, other, other_len))
257             goto end;
258         if (len >= out_len) {
259             if (!EVP_DigestFinal_ex(ctx, out, NULL))
260                 goto end;
261             out += out_len;
262             len -= out_len;
263             if (len == 0)
264                 break;
265         } else {
266             if (!EVP_DigestFinal_ex(ctx, mac, NULL))
267                 goto end;
268             memcpy(out, mac, len);
269             break;
270         }
271     }
272     ret = 1;
273 end:
274     EVP_MD_CTX_free(ctx);
275     EVP_MD_CTX_free(ctx_init);
276     OPENSSL_cleanse(mac, sizeof(mac));
277     return ret;
278 }
279
280 static void *x942kdf_new(void *provctx)
281 {
282     KDF_X942 *ctx;
283
284     if (!ossl_prov_is_running())
285         return 0;
286
287     if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
288         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
289     ctx->provctx = provctx;
290     return ctx;
291 }
292
293 static void x942kdf_reset(void *vctx)
294 {
295     KDF_X942 *ctx = (KDF_X942 *)vctx;
296     void *provctx = ctx->provctx;
297
298     ossl_prov_digest_reset(&ctx->digest);
299     OPENSSL_clear_free(ctx->secret, ctx->secret_len);
300     OPENSSL_clear_free(ctx->ukm, ctx->ukm_len);
301     memset(ctx, 0, sizeof(*ctx));
302     ctx->provctx = provctx;
303 }
304
305 static void x942kdf_free(void *vctx)
306 {
307     KDF_X942 *ctx = (KDF_X942 *)vctx;
308
309     if (ctx != NULL) {
310         x942kdf_reset(ctx);
311         OPENSSL_free(ctx);
312     }
313 }
314
315 static int x942kdf_set_buffer(unsigned char **out, size_t *out_len,
316                               const OSSL_PARAM *p)
317 {
318     if (p->data_size == 0 || p->data == NULL)
319         return 1;
320
321     OPENSSL_free(*out);
322     *out = NULL;
323     return OSSL_PARAM_get_octet_string(p, (void **)out, 0, out_len);
324 }
325
326 static size_t x942kdf_size(KDF_X942 *ctx)
327 {
328     int len;
329     const EVP_MD *md = ossl_prov_digest_md(&ctx->digest);
330
331     if (md == NULL) {
332         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
333         return 0;
334     }
335     len = EVP_MD_size(md);
336     return (len <= 0) ? 0 : (size_t)len;
337 }
338
339 static int x942kdf_derive(void *vctx, unsigned char *key, size_t keylen)
340 {
341     KDF_X942 *ctx = (KDF_X942 *)vctx;
342     const EVP_MD *md;
343     int ret = 0;
344     unsigned char *ctr;
345     unsigned char *der = NULL;
346     size_t der_len = 0;
347
348     if (!ossl_prov_is_running())
349         return 0;
350
351     if (ctx->secret == NULL) {
352         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
353         return 0;
354     }
355     md = ossl_prov_digest_md(&ctx->digest);
356     if (md == NULL) {
357         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
358         return 0;
359     }
360     if (ctx->cek_oid == NULL || ctx->cek_oid_len == 0) {
361         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CEK_ALG);
362         return 0;
363     }
364     if (ctx->ukm != NULL && ctx->ukm_len >= X942KDF_MAX_INLEN) {
365         /*
366          * Note the ukm length MUST be 512 bits.
367          * For backwards compatibility the old check is being done.
368          */
369         ERR_raise(ERR_LIB_PROV, PROV_R_INAVLID_UKM_LENGTH);
370         return 0;
371     }
372     /* generate the otherinfo der */
373     if (!x942_encode_otherinfo(ctx->dkm_len,
374                                ctx->cek_oid, ctx->cek_oid_len,
375                                ctx->ukm, ctx->ukm_len,
376                                &der, &der_len, &ctr)) {
377         ERR_raise(ERR_LIB_PROV, PROV_R_BAD_ENCODING);
378         return 0;
379     }
380     ret = x942kdf_hash_kdm(md, ctx->secret, ctx->secret_len,
381                            der, der_len, ctr, key, keylen);
382     OPENSSL_free(der);
383     return ret;
384 }
385
386 static int x942kdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
387 {
388     const OSSL_PARAM *p, *pq;
389     KDF_X942 *ctx = vctx;
390     OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);
391     const char *propq = NULL;
392     size_t id;
393
394     if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
395         return 0;
396
397     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET)) != NULL
398         || (p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY)) != NULL)
399         if (!x942kdf_set_buffer(&ctx->secret, &ctx->secret_len, p))
400             return 0;
401
402     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_UKM)) != NULL)
403         if (!x942kdf_set_buffer(&ctx->ukm, &ctx->ukm_len, p))
404             return 0;
405
406     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_CEK_ALG)) != NULL) {
407         if (p->data_type != OSSL_PARAM_UTF8_STRING)
408             return 0;
409         pq = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_PROPERTIES);
410         /*
411          * We already grab the properties during ossl_prov_digest_load_from_params()
412          * so there is no need to check the validity again..
413          */
414         if (pq != NULL)
415             propq = p->data;
416         if (find_alg_id(provctx, p->data, propq, &id) == 0)
417             return 0;
418         ctx->cek_oid = kek_algs[id].oid;
419         ctx->cek_oid_len = kek_algs[id].oid_len;
420         ctx->dkm_len = kek_algs[id].keklen;
421     }
422     return 1;
423 }
424
425 static const OSSL_PARAM *x942kdf_settable_ctx_params(ossl_unused void *provctx)
426 {
427     static const OSSL_PARAM known_settable_ctx_params[] = {
428         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
429         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
430         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0),
431         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
432         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_UKM, NULL, 0),
433         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_CEK_ALG, NULL, 0),
434         OSSL_PARAM_END
435     };
436     return known_settable_ctx_params;
437 }
438
439 static int x942kdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
440 {
441     KDF_X942 *ctx = (KDF_X942 *)vctx;
442     OSSL_PARAM *p;
443
444     if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
445         return OSSL_PARAM_set_size_t(p, x942kdf_size(ctx));
446     return -2;
447 }
448
449 static const OSSL_PARAM *x942kdf_gettable_ctx_params(ossl_unused void *provctx)
450 {
451     static const OSSL_PARAM known_gettable_ctx_params[] = {
452         OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
453         OSSL_PARAM_END
454     };
455     return known_gettable_ctx_params;
456 }
457
458 const OSSL_DISPATCH ossl_kdf_x942_kdf_functions[] = {
459     { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))x942kdf_new },
460     { OSSL_FUNC_KDF_FREECTX, (void(*)(void))x942kdf_free },
461     { OSSL_FUNC_KDF_RESET, (void(*)(void))x942kdf_reset },
462     { OSSL_FUNC_KDF_DERIVE, (void(*)(void))x942kdf_derive },
463     { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
464       (void(*)(void))x942kdf_settable_ctx_params },
465     { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))x942kdf_set_ctx_params },
466     { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
467       (void(*)(void))x942kdf_gettable_ctx_params },
468     { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))x942kdf_get_ctx_params },
469     { 0, NULL }
470 };