1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2018-2022 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci/*
11e1051a39Sopenharmony_ci * CMAC low level APIs are deprecated for public use, but still ok for internal
12e1051a39Sopenharmony_ci * use.
13e1051a39Sopenharmony_ci */
14e1051a39Sopenharmony_ci#include "internal/deprecated.h"
15e1051a39Sopenharmony_ci
16e1051a39Sopenharmony_ci#include <openssl/core_dispatch.h>
17e1051a39Sopenharmony_ci#include <openssl/core_names.h>
18e1051a39Sopenharmony_ci#include <openssl/params.h>
19e1051a39Sopenharmony_ci#include <openssl/evp.h>
20e1051a39Sopenharmony_ci#include <openssl/cmac.h>
21e1051a39Sopenharmony_ci
22e1051a39Sopenharmony_ci#include "prov/implementations.h"
23e1051a39Sopenharmony_ci#include "prov/provider_ctx.h"
24e1051a39Sopenharmony_ci#include "prov/provider_util.h"
25e1051a39Sopenharmony_ci#include "prov/providercommon.h"
26e1051a39Sopenharmony_ci
27e1051a39Sopenharmony_ci/*
28e1051a39Sopenharmony_ci * Forward declaration of everything implemented here.  This is not strictly
29e1051a39Sopenharmony_ci * necessary for the compiler, but provides an assurance that the signatures
30e1051a39Sopenharmony_ci * of the functions in the dispatch table are correct.
31e1051a39Sopenharmony_ci */
32e1051a39Sopenharmony_cistatic OSSL_FUNC_mac_newctx_fn cmac_new;
33e1051a39Sopenharmony_cistatic OSSL_FUNC_mac_dupctx_fn cmac_dup;
34e1051a39Sopenharmony_cistatic OSSL_FUNC_mac_freectx_fn cmac_free;
35e1051a39Sopenharmony_cistatic OSSL_FUNC_mac_gettable_ctx_params_fn cmac_gettable_ctx_params;
36e1051a39Sopenharmony_cistatic OSSL_FUNC_mac_get_ctx_params_fn cmac_get_ctx_params;
37e1051a39Sopenharmony_cistatic OSSL_FUNC_mac_settable_ctx_params_fn cmac_settable_ctx_params;
38e1051a39Sopenharmony_cistatic OSSL_FUNC_mac_set_ctx_params_fn cmac_set_ctx_params;
39e1051a39Sopenharmony_cistatic OSSL_FUNC_mac_init_fn cmac_init;
40e1051a39Sopenharmony_cistatic OSSL_FUNC_mac_update_fn cmac_update;
41e1051a39Sopenharmony_cistatic OSSL_FUNC_mac_final_fn cmac_final;
42e1051a39Sopenharmony_ci
43e1051a39Sopenharmony_ci/* local CMAC data */
44e1051a39Sopenharmony_ci
45e1051a39Sopenharmony_cistruct cmac_data_st {
46e1051a39Sopenharmony_ci    void *provctx;
47e1051a39Sopenharmony_ci    CMAC_CTX *ctx;
48e1051a39Sopenharmony_ci    PROV_CIPHER cipher;
49e1051a39Sopenharmony_ci};
50e1051a39Sopenharmony_ci
51e1051a39Sopenharmony_cistatic void *cmac_new(void *provctx)
52e1051a39Sopenharmony_ci{
53e1051a39Sopenharmony_ci    struct cmac_data_st *macctx;
54e1051a39Sopenharmony_ci
55e1051a39Sopenharmony_ci    if (!ossl_prov_is_running())
56e1051a39Sopenharmony_ci        return NULL;
57e1051a39Sopenharmony_ci
58e1051a39Sopenharmony_ci    if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL
59e1051a39Sopenharmony_ci        || (macctx->ctx = CMAC_CTX_new()) == NULL) {
60e1051a39Sopenharmony_ci        OPENSSL_free(macctx);
61e1051a39Sopenharmony_ci        macctx = NULL;
62e1051a39Sopenharmony_ci    } else {
63e1051a39Sopenharmony_ci        macctx->provctx = provctx;
64e1051a39Sopenharmony_ci    }
65e1051a39Sopenharmony_ci
66e1051a39Sopenharmony_ci    return macctx;
67e1051a39Sopenharmony_ci}
68e1051a39Sopenharmony_ci
69e1051a39Sopenharmony_cistatic void cmac_free(void *vmacctx)
70e1051a39Sopenharmony_ci{
71e1051a39Sopenharmony_ci    struct cmac_data_st *macctx = vmacctx;
72e1051a39Sopenharmony_ci
73e1051a39Sopenharmony_ci    if (macctx != NULL) {
74e1051a39Sopenharmony_ci        CMAC_CTX_free(macctx->ctx);
75e1051a39Sopenharmony_ci        ossl_prov_cipher_reset(&macctx->cipher);
76e1051a39Sopenharmony_ci        OPENSSL_free(macctx);
77e1051a39Sopenharmony_ci    }
78e1051a39Sopenharmony_ci}
79e1051a39Sopenharmony_ci
80e1051a39Sopenharmony_cistatic void *cmac_dup(void *vsrc)
81e1051a39Sopenharmony_ci{
82e1051a39Sopenharmony_ci    struct cmac_data_st *src = vsrc;
83e1051a39Sopenharmony_ci    struct cmac_data_st *dst;
84e1051a39Sopenharmony_ci
85e1051a39Sopenharmony_ci    if (!ossl_prov_is_running())
86e1051a39Sopenharmony_ci        return NULL;
87e1051a39Sopenharmony_ci
88e1051a39Sopenharmony_ci    dst = cmac_new(src->provctx);
89e1051a39Sopenharmony_ci    if (dst == NULL)
90e1051a39Sopenharmony_ci        return NULL;
91e1051a39Sopenharmony_ci    if (!CMAC_CTX_copy(dst->ctx, src->ctx)
92e1051a39Sopenharmony_ci        || !ossl_prov_cipher_copy(&dst->cipher, &src->cipher)) {
93e1051a39Sopenharmony_ci        cmac_free(dst);
94e1051a39Sopenharmony_ci        return NULL;
95e1051a39Sopenharmony_ci    }
96e1051a39Sopenharmony_ci    return dst;
97e1051a39Sopenharmony_ci}
98e1051a39Sopenharmony_ci
99e1051a39Sopenharmony_cistatic size_t cmac_size(void *vmacctx)
100e1051a39Sopenharmony_ci{
101e1051a39Sopenharmony_ci    struct cmac_data_st *macctx = vmacctx;
102e1051a39Sopenharmony_ci
103e1051a39Sopenharmony_ci    return EVP_CIPHER_CTX_get_block_size(CMAC_CTX_get0_cipher_ctx(macctx->ctx));
104e1051a39Sopenharmony_ci}
105e1051a39Sopenharmony_ci
106e1051a39Sopenharmony_cistatic int cmac_setkey(struct cmac_data_st *macctx,
107e1051a39Sopenharmony_ci                       const unsigned char *key, size_t keylen)
108e1051a39Sopenharmony_ci{
109e1051a39Sopenharmony_ci    int rv = CMAC_Init(macctx->ctx, key, keylen,
110e1051a39Sopenharmony_ci                       ossl_prov_cipher_cipher(&macctx->cipher),
111e1051a39Sopenharmony_ci                       ossl_prov_cipher_engine(&macctx->cipher));
112e1051a39Sopenharmony_ci    ossl_prov_cipher_reset(&macctx->cipher);
113e1051a39Sopenharmony_ci    return rv;
114e1051a39Sopenharmony_ci}
115e1051a39Sopenharmony_ci
116e1051a39Sopenharmony_cistatic int cmac_init(void *vmacctx, const unsigned char *key,
117e1051a39Sopenharmony_ci                     size_t keylen, const OSSL_PARAM params[])
118e1051a39Sopenharmony_ci{
119e1051a39Sopenharmony_ci    struct cmac_data_st *macctx = vmacctx;
120e1051a39Sopenharmony_ci
121e1051a39Sopenharmony_ci    if (!ossl_prov_is_running() || !cmac_set_ctx_params(macctx, params))
122e1051a39Sopenharmony_ci        return 0;
123e1051a39Sopenharmony_ci    if (key != NULL)
124e1051a39Sopenharmony_ci        return cmac_setkey(macctx, key, keylen);
125e1051a39Sopenharmony_ci    /* Reinitialize the CMAC context */
126e1051a39Sopenharmony_ci    return CMAC_Init(macctx->ctx, NULL, 0, NULL, NULL);
127e1051a39Sopenharmony_ci}
128e1051a39Sopenharmony_ci
129e1051a39Sopenharmony_cistatic int cmac_update(void *vmacctx, const unsigned char *data,
130e1051a39Sopenharmony_ci                       size_t datalen)
131e1051a39Sopenharmony_ci{
132e1051a39Sopenharmony_ci    struct cmac_data_st *macctx = vmacctx;
133e1051a39Sopenharmony_ci
134e1051a39Sopenharmony_ci    return CMAC_Update(macctx->ctx, data, datalen);
135e1051a39Sopenharmony_ci}
136e1051a39Sopenharmony_ci
137e1051a39Sopenharmony_cistatic int cmac_final(void *vmacctx, unsigned char *out, size_t *outl,
138e1051a39Sopenharmony_ci                      size_t outsize)
139e1051a39Sopenharmony_ci{
140e1051a39Sopenharmony_ci    struct cmac_data_st *macctx = vmacctx;
141e1051a39Sopenharmony_ci
142e1051a39Sopenharmony_ci    if (!ossl_prov_is_running())
143e1051a39Sopenharmony_ci        return 0;
144e1051a39Sopenharmony_ci
145e1051a39Sopenharmony_ci    return CMAC_Final(macctx->ctx, out, outl);
146e1051a39Sopenharmony_ci}
147e1051a39Sopenharmony_ci
148e1051a39Sopenharmony_cistatic const OSSL_PARAM known_gettable_ctx_params[] = {
149e1051a39Sopenharmony_ci    OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
150e1051a39Sopenharmony_ci    OSSL_PARAM_size_t(OSSL_MAC_PARAM_BLOCK_SIZE, NULL),
151e1051a39Sopenharmony_ci    OSSL_PARAM_END
152e1051a39Sopenharmony_ci};
153e1051a39Sopenharmony_cistatic const OSSL_PARAM *cmac_gettable_ctx_params(ossl_unused void *ctx,
154e1051a39Sopenharmony_ci                                                  ossl_unused void *provctx)
155e1051a39Sopenharmony_ci{
156e1051a39Sopenharmony_ci    return known_gettable_ctx_params;
157e1051a39Sopenharmony_ci}
158e1051a39Sopenharmony_ci
159e1051a39Sopenharmony_cistatic int cmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
160e1051a39Sopenharmony_ci{
161e1051a39Sopenharmony_ci    OSSL_PARAM *p;
162e1051a39Sopenharmony_ci
163e1051a39Sopenharmony_ci    if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL
164e1051a39Sopenharmony_ci            && !OSSL_PARAM_set_size_t(p, cmac_size(vmacctx)))
165e1051a39Sopenharmony_ci        return 0;
166e1051a39Sopenharmony_ci
167e1051a39Sopenharmony_ci    if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_BLOCK_SIZE)) != NULL
168e1051a39Sopenharmony_ci            && !OSSL_PARAM_set_size_t(p, cmac_size(vmacctx)))
169e1051a39Sopenharmony_ci        return 0;
170e1051a39Sopenharmony_ci
171e1051a39Sopenharmony_ci    return 1;
172e1051a39Sopenharmony_ci}
173e1051a39Sopenharmony_ci
174e1051a39Sopenharmony_cistatic const OSSL_PARAM known_settable_ctx_params[] = {
175e1051a39Sopenharmony_ci    OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_CIPHER, NULL, 0),
176e1051a39Sopenharmony_ci    OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
177e1051a39Sopenharmony_ci    OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
178e1051a39Sopenharmony_ci    OSSL_PARAM_END
179e1051a39Sopenharmony_ci};
180e1051a39Sopenharmony_cistatic const OSSL_PARAM *cmac_settable_ctx_params(ossl_unused void *ctx,
181e1051a39Sopenharmony_ci                                                  ossl_unused void *provctx)
182e1051a39Sopenharmony_ci{
183e1051a39Sopenharmony_ci    return known_settable_ctx_params;
184e1051a39Sopenharmony_ci}
185e1051a39Sopenharmony_ci
186e1051a39Sopenharmony_ci/*
187e1051a39Sopenharmony_ci * ALL parameters should be set before init().
188e1051a39Sopenharmony_ci */
189e1051a39Sopenharmony_cistatic int cmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
190e1051a39Sopenharmony_ci{
191e1051a39Sopenharmony_ci    struct cmac_data_st *macctx = vmacctx;
192e1051a39Sopenharmony_ci    OSSL_LIB_CTX *ctx = PROV_LIBCTX_OF(macctx->provctx);
193e1051a39Sopenharmony_ci    const OSSL_PARAM *p;
194e1051a39Sopenharmony_ci
195e1051a39Sopenharmony_ci    if (params == NULL)
196e1051a39Sopenharmony_ci        return 1;
197e1051a39Sopenharmony_ci
198e1051a39Sopenharmony_ci    if (!ossl_prov_cipher_load_from_params(&macctx->cipher, params, ctx))
199e1051a39Sopenharmony_ci        return 0;
200e1051a39Sopenharmony_ci
201e1051a39Sopenharmony_ci    if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
202e1051a39Sopenharmony_ci        if (p->data_type != OSSL_PARAM_OCTET_STRING)
203e1051a39Sopenharmony_ci            return 0;
204e1051a39Sopenharmony_ci        return cmac_setkey(macctx, p->data, p->data_size);
205e1051a39Sopenharmony_ci    }
206e1051a39Sopenharmony_ci    return 1;
207e1051a39Sopenharmony_ci}
208e1051a39Sopenharmony_ci
209e1051a39Sopenharmony_ciconst OSSL_DISPATCH ossl_cmac_functions[] = {
210e1051a39Sopenharmony_ci    { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))cmac_new },
211e1051a39Sopenharmony_ci    { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))cmac_dup },
212e1051a39Sopenharmony_ci    { OSSL_FUNC_MAC_FREECTX, (void (*)(void))cmac_free },
213e1051a39Sopenharmony_ci    { OSSL_FUNC_MAC_INIT, (void (*)(void))cmac_init },
214e1051a39Sopenharmony_ci    { OSSL_FUNC_MAC_UPDATE, (void (*)(void))cmac_update },
215e1051a39Sopenharmony_ci    { OSSL_FUNC_MAC_FINAL, (void (*)(void))cmac_final },
216e1051a39Sopenharmony_ci    { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
217e1051a39Sopenharmony_ci      (void (*)(void))cmac_gettable_ctx_params },
218e1051a39Sopenharmony_ci    { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))cmac_get_ctx_params },
219e1051a39Sopenharmony_ci    { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
220e1051a39Sopenharmony_ci      (void (*)(void))cmac_settable_ctx_params },
221e1051a39Sopenharmony_ci    { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))cmac_set_ctx_params },
222e1051a39Sopenharmony_ci    { 0, NULL }
223e1051a39Sopenharmony_ci};
224