]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/cmac/cm_meth.c
2 * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
11 #include "internal/cryptlib.h"
12 #include <openssl/x509.h>
13 #include <openssl/x509v3.h>
14 #include <openssl/evp.h>
15 #include <openssl/cmac.h>
16 #include "internal/evp_int.h"
18 /* local CMAC pkey structure */
20 /* typedef EVP_MAC_IMPL */
21 struct evp_mac_impl_st
{
22 /* tmpcipher and tmpengine are set to NULL after a CMAC_Init call */
23 const EVP_CIPHER
*tmpcipher
; /* cached CMAC cipher */
24 const ENGINE
*tmpengine
; /* cached CMAC cipher engine */
28 static EVP_MAC_IMPL
*cmac_new(void)
32 if ((cctx
= OPENSSL_zalloc(sizeof(*cctx
))) == NULL
33 || (cctx
->ctx
= CMAC_CTX_new()) == NULL
) {
41 static void cmac_free(EVP_MAC_IMPL
*cctx
)
44 CMAC_CTX_free(cctx
->ctx
);
49 static EVP_MAC_IMPL
*cmac_dup(const EVP_MAC_IMPL
*csrc
)
51 EVP_MAC_IMPL
*cdst
= cmac_new();
56 if (!CMAC_CTX_copy(cdst
->ctx
, csrc
->ctx
)) {
61 cdst
->tmpengine
= csrc
->tmpengine
;
62 cdst
->tmpcipher
= csrc
->tmpcipher
;
67 static size_t cmac_size(EVP_MAC_IMPL
*cctx
)
69 return EVP_CIPHER_CTX_block_size(CMAC_CTX_get0_cipher_ctx(cctx
->ctx
));
72 static int cmac_init(EVP_MAC_IMPL
*cctx
)
74 int rv
= CMAC_Init(cctx
->ctx
, NULL
, 0, cctx
->tmpcipher
,
75 (ENGINE
*)cctx
->tmpengine
);
76 cctx
->tmpcipher
= NULL
;
77 cctx
->tmpengine
= NULL
;
82 static int cmac_update(EVP_MAC_IMPL
*cctx
, const unsigned char *data
,
85 return CMAC_Update(cctx
->ctx
, data
, datalen
);
88 static int cmac_final(EVP_MAC_IMPL
*cctx
, unsigned char *out
)
92 return CMAC_Final(cctx
->ctx
, out
, &hlen
);
95 static int cmac_ctrl(EVP_MAC_IMPL
*cctx
, int cmd
, va_list args
)
98 case EVP_MAC_CTRL_SET_KEY
:
100 const unsigned char *key
= va_arg(args
, const unsigned char *);
101 size_t keylen
= va_arg(args
, size_t);
102 int rv
= CMAC_Init(cctx
->ctx
, key
, keylen
, cctx
->tmpcipher
,
103 (ENGINE
*)cctx
->tmpengine
);
105 cctx
->tmpcipher
= NULL
;
106 cctx
->tmpengine
= NULL
;
111 case EVP_MAC_CTRL_SET_CIPHER
:
112 cctx
->tmpcipher
= va_arg(args
, const EVP_CIPHER
*);
114 case EVP_MAC_CTRL_SET_ENGINE
:
115 cctx
->tmpengine
= va_arg(args
, const ENGINE
*);
123 static int cmac_ctrl_int(EVP_MAC_IMPL
*hctx
, int cmd
, ...)
129 rv
= cmac_ctrl(hctx
, cmd
, args
);
135 static int cmac_ctrl_str_cb(void *hctx
, int cmd
, void *buf
, size_t buflen
)
137 return cmac_ctrl_int(hctx
, cmd
, buf
, buflen
);
140 static int cmac_ctrl_str(EVP_MAC_IMPL
*cctx
, const char *type
,
145 if (strcmp(type
, "cipher") == 0) {
146 const EVP_CIPHER
*c
= EVP_get_cipherbyname(value
);
150 return cmac_ctrl_int(cctx
, EVP_MAC_CTRL_SET_CIPHER
, c
);
152 if (strcmp(type
, "key") == 0)
153 return EVP_str2ctrl(cmac_ctrl_str_cb
, cctx
, EVP_MAC_CTRL_SET_KEY
,
155 if (strcmp(type
, "hexkey") == 0)
156 return EVP_hex2ctrl(cmac_ctrl_str_cb
, cctx
, EVP_MAC_CTRL_SET_KEY
,
161 const EVP_MAC cmac_meth
= {