Added EVP_KDF (similiar to the EVP_MAC)
[openssl.git] / crypto / evp / kdf_lib.c
1 /*
2  * Copyright 2018-2019 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright (c) 2018-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 <stdio.h>
12 #include <stdlib.h>
13 #include "internal/cryptlib.h"
14 #include <openssl/engine.h>
15 #include <openssl/evp.h>
16 #include <openssl/x509v3.h>
17 #include <openssl/kdf.h>
18 #include "internal/asn1_int.h"
19 #include "internal/evp_int.h"
20 #include "internal/numbers.h"
21 #include "evp_locl.h"
22
23 EVP_KDF_CTX *EVP_KDF_CTX_new(const EVP_KDF *kdf)
24 {
25     EVP_KDF_CTX *ctx = OPENSSL_zalloc(sizeof(EVP_KDF_CTX));
26
27     if (ctx == NULL || (ctx->impl = kdf->new()) == NULL) {
28         EVPerr(EVP_F_EVP_KDF_CTX_NEW, ERR_R_MALLOC_FAILURE);
29         OPENSSL_free(ctx);
30         ctx = NULL;
31     } else {
32         ctx->meth = kdf;
33     }
34     return ctx;
35 }
36
37 EVP_KDF_CTX *EVP_KDF_CTX_new_id(int id)
38 {
39     const EVP_KDF *kdf = EVP_get_kdfbynid(id);
40
41     if (kdf == NULL)
42         return NULL;
43     return EVP_KDF_CTX_new(kdf);
44 }
45
46 int EVP_KDF_nid(const EVP_KDF *kdf)
47 {
48     return kdf->type;
49 }
50
51 const EVP_KDF *EVP_KDF_CTX_kdf(EVP_KDF_CTX *ctx)
52 {
53     return ctx->meth;
54 }
55
56 void EVP_KDF_CTX_free(EVP_KDF_CTX *ctx)
57 {
58     if (ctx == NULL)
59         return;
60
61     ctx->meth->free(ctx->impl);
62     OPENSSL_free(ctx);
63 }
64
65 void EVP_KDF_reset(EVP_KDF_CTX *ctx)
66 {
67     if (ctx == NULL)
68         return;
69
70     if (ctx->meth->reset != NULL)
71         ctx->meth->reset(ctx->impl);
72 }
73
74 int EVP_KDF_ctrl(EVP_KDF_CTX *ctx, int cmd, ...)
75 {
76     int ret;
77     va_list args;
78
79     va_start(args, cmd);
80     ret = EVP_KDF_vctrl(ctx, cmd, args);
81     va_end(args);
82
83     if (ret == -2)
84         EVPerr(EVP_F_EVP_KDF_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
85
86     return ret;
87 }
88
89 int EVP_KDF_vctrl(EVP_KDF_CTX *ctx, int cmd, va_list args)
90 {
91     if (ctx == NULL)
92         return 0;
93
94     return ctx->meth->ctrl(ctx->impl, cmd, args);
95 }
96
97 int EVP_KDF_ctrl_str(EVP_KDF_CTX *ctx, const char *type, const char *value)
98 {
99     int ret;
100
101     if (ctx == NULL)
102         return 0;
103
104     if (ctx->meth->ctrl_str == NULL) {
105         EVPerr(EVP_F_EVP_KDF_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
106         return -2;
107     }
108
109     ret = ctx->meth->ctrl_str(ctx->impl, type, value);
110     if (ret == -2)
111         EVPerr(EVP_F_EVP_KDF_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
112
113     return ret;
114 }
115
116 size_t EVP_KDF_size(EVP_KDF_CTX *ctx)
117 {
118     if (ctx == NULL)
119         return 0;
120
121     if (ctx->meth->size == NULL)
122         return SIZE_MAX;
123
124     return ctx->meth->size(ctx->impl);
125 }
126
127 int EVP_KDF_derive(EVP_KDF_CTX *ctx, unsigned char *key, size_t keylen)
128 {
129     if (ctx == NULL)
130         return 0;
131
132     return ctx->meth->derive(ctx->impl, key, keylen);
133 }