Following the license change, modify the boilerplates in crypto/srp/
[openssl.git] / crypto / cmac / cm_meth.c
1 /*
2  * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <stdio.h>
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"
17
18 /* local CMAC pkey structure */
19
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 */
25     CMAC_CTX *ctx;
26 };
27
28 static EVP_MAC_IMPL *cmac_new(void)
29 {
30     EVP_MAC_IMPL *cctx;
31
32     if ((cctx = OPENSSL_zalloc(sizeof(*cctx))) == NULL
33         || (cctx->ctx = CMAC_CTX_new()) == NULL) {
34         OPENSSL_free(cctx);
35         cctx = NULL;
36     }
37
38     return cctx;
39 }
40
41 static void cmac_free(EVP_MAC_IMPL *cctx)
42 {
43     if (cctx != NULL) {
44         CMAC_CTX_free(cctx->ctx);
45         OPENSSL_free(cctx);
46     }
47 }
48
49 static int cmac_copy(EVP_MAC_IMPL *cdst, EVP_MAC_IMPL *csrc)
50 {
51     if (!CMAC_CTX_copy(cdst->ctx, csrc->ctx))
52         return 0;
53
54     cdst->tmpengine = csrc->tmpengine;
55     cdst->tmpcipher = csrc->tmpcipher;
56     return 1;
57 }
58
59 static size_t cmac_size(EVP_MAC_IMPL *cctx)
60 {
61     return EVP_CIPHER_CTX_block_size(CMAC_CTX_get0_cipher_ctx(cctx->ctx));
62 }
63
64 static int cmac_init(EVP_MAC_IMPL *cctx)
65 {
66     int rv = CMAC_Init(cctx->ctx, NULL, 0, cctx->tmpcipher,
67                        (ENGINE *)cctx->tmpengine);
68     cctx->tmpcipher = NULL;
69     cctx->tmpengine = NULL;
70
71     return rv;
72 }
73
74 static int cmac_update(EVP_MAC_IMPL *cctx, const unsigned char *data,
75                        size_t datalen)
76 {
77     return CMAC_Update(cctx->ctx, data, datalen);
78 }
79
80 static int cmac_final(EVP_MAC_IMPL *cctx, unsigned char *out)
81 {
82     size_t hlen;
83
84     return CMAC_Final(cctx->ctx, out, &hlen);
85 }
86
87 static int cmac_ctrl(EVP_MAC_IMPL *cctx, int cmd, va_list args)
88 {
89     switch (cmd) {
90     case EVP_MAC_CTRL_SET_KEY:
91         {
92             const unsigned char *key = va_arg(args, const unsigned char *);
93             size_t keylen = va_arg(args, size_t);
94             int rv = CMAC_Init(cctx->ctx, key, keylen, cctx->tmpcipher,
95                                (ENGINE *)cctx->tmpengine);
96
97             cctx->tmpcipher = NULL;
98             cctx->tmpengine = NULL;
99
100             return rv;
101         }
102         break;
103     case EVP_MAC_CTRL_SET_CIPHER:
104         cctx->tmpcipher = va_arg(args, const EVP_CIPHER *);
105         break;
106     case EVP_MAC_CTRL_SET_ENGINE:
107         cctx->tmpengine = va_arg(args, const ENGINE *);
108         break;
109     default:
110         return -2;
111     }
112     return 1;
113 }
114
115 static int cmac_ctrl_int(EVP_MAC_IMPL *hctx, int cmd, ...)
116 {
117     int rv;
118     va_list args;
119
120     va_start(args, cmd);
121     rv = cmac_ctrl(hctx, cmd, args);
122     va_end(args);
123
124     return rv;
125 }
126
127 static int cmac_ctrl_str_cb(void *hctx, int cmd, void *buf, size_t buflen)
128 {
129     return cmac_ctrl_int(hctx, cmd, buf, buflen);
130 }
131
132 static int cmac_ctrl_str(EVP_MAC_IMPL *cctx, const char *type,
133                          const char *value)
134 {
135     if (!value)
136         return 0;
137     if (strcmp(type, "cipher") == 0) {
138         const EVP_CIPHER *c = EVP_get_cipherbyname(value);
139
140         if (c == NULL)
141             return 0;
142         return cmac_ctrl_int(cctx, EVP_MAC_CTRL_SET_CIPHER, c);
143     }
144     if (strcmp(type, "key") == 0)
145         return EVP_str2ctrl(cmac_ctrl_str_cb, cctx, EVP_MAC_CTRL_SET_KEY,
146                             value);
147     if (strcmp(type, "hexkey") == 0)
148         return EVP_hex2ctrl(cmac_ctrl_str_cb, cctx, EVP_MAC_CTRL_SET_KEY,
149                             value);
150     return -2;
151 }
152
153 const EVP_MAC cmac_meth = {
154     EVP_MAC_CMAC,
155     cmac_new,
156     cmac_copy,
157     cmac_free,
158     cmac_size,
159     cmac_init,
160     cmac_update,
161     cmac_final,
162     cmac_ctrl,
163     cmac_ctrl_str
164 };