]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/cmac/cmac.c
Add RSA encrypt demo
[thirdparty/openssl.git] / crypto / cmac / cmac.c
CommitLineData
0f113f3e 1/*
f5afac4b 2 * Copyright 2010-2021 The OpenSSL Project Authors. All Rights Reserved.
8c968e03 3 *
8de396f8 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
4f22f405
RS
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
8c968e03
DSH
8 */
9
a6d572e6
P
10/*
11 * CMAC low level APIs are deprecated for public use, but still ok for internal
12 * use.
13 */
14#include "internal/deprecated.h"
15
8c968e03
DSH
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
b39fc560 19#include "internal/cryptlib.h"
8c968e03 20#include <openssl/cmac.h>
7de2b9c4 21#include <openssl/err.h>
8c968e03 22
0f113f3e
MC
23struct CMAC_CTX_st {
24 /* Cipher context to use */
846ec07d 25 EVP_CIPHER_CTX *cctx;
0f113f3e
MC
26 /* Keys k1 and k2 */
27 unsigned char k1[EVP_MAX_BLOCK_LENGTH];
28 unsigned char k2[EVP_MAX_BLOCK_LENGTH];
29 /* Temporary block */
30 unsigned char tbl[EVP_MAX_BLOCK_LENGTH];
31 /* Last (possibly partial) block */
32 unsigned char last_block[EVP_MAX_BLOCK_LENGTH];
33 /* Number of bytes in last block: -1 means context not initialised */
34 int nlast_block;
35};
8c968e03
DSH
36
37/* Make temporary keys K1 and K2 */
38
03cf7e78 39static void make_kn(unsigned char *k1, const unsigned char *l, int bl)
0f113f3e
MC
40{
41 int i;
42 unsigned char c = l[0], carry = c >> 7, cnext;
03cf7e78 43
0f113f3e
MC
44 /* Shift block to left, including carry */
45 for (i = 0; i < bl - 1; i++, c = cnext)
46 k1[i] = (c << 1) | ((cnext = l[i + 1]) >> 7);
03cf7e78 47
0f113f3e
MC
48 /* If MSB set fixup with R */
49 k1[i] = (c << 1) ^ ((0 - carry) & (bl == 16 ? 0x87 : 0x1b));
50}
8c968e03
DSH
51
52CMAC_CTX *CMAC_CTX_new(void)
0f113f3e
MC
53{
54 CMAC_CTX *ctx;
b4faea50 55
7de2b9c4 56 if ((ctx = OPENSSL_malloc(sizeof(*ctx))) == NULL) {
9311d0c4 57 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
0f113f3e 58 return NULL;
7de2b9c4 59 }
846ec07d
RL
60 ctx->cctx = EVP_CIPHER_CTX_new();
61 if (ctx->cctx == NULL) {
62 OPENSSL_free(ctx);
63 return NULL;
64 }
0f113f3e
MC
65 ctx->nlast_block = -1;
66 return ctx;
67}
8c968e03
DSH
68
69void CMAC_CTX_cleanup(CMAC_CTX *ctx)
0f113f3e 70{
15d95dd7 71 EVP_CIPHER_CTX_reset(ctx->cctx);
0f113f3e
MC
72 OPENSSL_cleanse(ctx->tbl, EVP_MAX_BLOCK_LENGTH);
73 OPENSSL_cleanse(ctx->k1, EVP_MAX_BLOCK_LENGTH);
74 OPENSSL_cleanse(ctx->k2, EVP_MAX_BLOCK_LENGTH);
75 OPENSSL_cleanse(ctx->last_block, EVP_MAX_BLOCK_LENGTH);
76 ctx->nlast_block = -1;
77}
8c968e03
DSH
78
79EVP_CIPHER_CTX *CMAC_CTX_get0_cipher_ctx(CMAC_CTX *ctx)
0f113f3e 80{
846ec07d 81 return ctx->cctx;
0f113f3e 82}
8c968e03
DSH
83
84void CMAC_CTX_free(CMAC_CTX *ctx)
0f113f3e 85{
efa7dd64
RS
86 if (!ctx)
87 return;
0f113f3e 88 CMAC_CTX_cleanup(ctx);
5c6c4c5c 89 EVP_CIPHER_CTX_free(ctx->cctx);
0f113f3e
MC
90 OPENSSL_free(ctx);
91}
8c968e03 92
c8ef656d 93int CMAC_CTX_copy(CMAC_CTX *out, const CMAC_CTX *in)
0f113f3e
MC
94{
95 int bl;
d5f85429 96
0f113f3e
MC
97 if (in->nlast_block == -1)
98 return 0;
ed576acd 99 if ((bl = EVP_CIPHER_CTX_get_block_size(in->cctx)) < 0)
d5f85429 100 return 0;
846ec07d 101 if (!EVP_CIPHER_CTX_copy(out->cctx, in->cctx))
0f113f3e 102 return 0;
0f113f3e
MC
103 memcpy(out->k1, in->k1, bl);
104 memcpy(out->k2, in->k2, bl);
105 memcpy(out->tbl, in->tbl, bl);
106 memcpy(out->last_block, in->last_block, bl);
107 out->nlast_block = in->nlast_block;
108 return 1;
109}
c8ef656d 110
0f113f3e
MC
111int CMAC_Init(CMAC_CTX *ctx, const void *key, size_t keylen,
112 const EVP_CIPHER *cipher, ENGINE *impl)
113{
114 static const unsigned char zero_iv[EVP_MAX_BLOCK_LENGTH] = { 0 };
d5f85429 115
0f113f3e
MC
116 /* All zeros means restart */
117 if (!key && !cipher && !impl && keylen == 0) {
118 /* Not initialised */
119 if (ctx->nlast_block == -1)
120 return 0;
846ec07d 121 if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, zero_iv))
0f113f3e 122 return 0;
ed576acd 123 memset(ctx->tbl, 0, EVP_CIPHER_CTX_get_block_size(ctx->cctx));
0f113f3e
MC
124 ctx->nlast_block = 0;
125 return 1;
126 }
0d4fb843 127 /* Initialise context */
b896d943
MC
128 if (cipher != NULL) {
129 /* Ensure we can't use this ctx until we also have a key */
130 ctx->nlast_block = -1;
131 if (!EVP_EncryptInit_ex(ctx->cctx, cipher, impl, NULL, NULL))
132 return 0;
133 }
0f113f3e 134 /* Non-NULL key means initialisation complete */
b896d943 135 if (key != NULL) {
0f113f3e 136 int bl;
d5f85429 137
b896d943
MC
138 /* If anything fails then ensure we can't use this ctx */
139 ctx->nlast_block = -1;
f6c95e46 140 if (!EVP_CIPHER_CTX_get0_cipher(ctx->cctx))
0f113f3e 141 return 0;
846ec07d 142 if (!EVP_CIPHER_CTX_set_key_length(ctx->cctx, keylen))
0f113f3e 143 return 0;
846ec07d 144 if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, key, zero_iv))
0f113f3e 145 return 0;
ed576acd 146 if ((bl = EVP_CIPHER_CTX_get_block_size(ctx->cctx)) < 0)
d5f85429 147 return 0;
154ea425 148 if (EVP_Cipher(ctx->cctx, ctx->tbl, zero_iv, bl) <= 0)
0f113f3e
MC
149 return 0;
150 make_kn(ctx->k1, ctx->tbl, bl);
151 make_kn(ctx->k2, ctx->k1, bl);
152 OPENSSL_cleanse(ctx->tbl, bl);
153 /* Reset context again ready for first data block */
846ec07d 154 if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, zero_iv))
0f113f3e
MC
155 return 0;
156 /* Zero tbl so resume works */
157 memset(ctx->tbl, 0, bl);
158 ctx->nlast_block = 0;
159 }
160 return 1;
161}
8c968e03
DSH
162
163int CMAC_Update(CMAC_CTX *ctx, const void *in, size_t dlen)
0f113f3e
MC
164{
165 const unsigned char *data = in;
d5f85429
RL
166 int bl;
167
0f113f3e
MC
168 if (ctx->nlast_block == -1)
169 return 0;
170 if (dlen == 0)
171 return 1;
ed576acd 172 if ((bl = EVP_CIPHER_CTX_get_block_size(ctx->cctx)) < 0)
d5f85429 173 return 0;
0f113f3e
MC
174 /* Copy into partial block if we need to */
175 if (ctx->nlast_block > 0) {
176 size_t nleft;
d5f85429 177
0f113f3e
MC
178 nleft = bl - ctx->nlast_block;
179 if (dlen < nleft)
180 nleft = dlen;
181 memcpy(ctx->last_block + ctx->nlast_block, data, nleft);
182 dlen -= nleft;
183 ctx->nlast_block += nleft;
184 /* If no more to process return */
185 if (dlen == 0)
186 return 1;
187 data += nleft;
188 /* Else not final block so encrypt it */
154ea425 189 if (EVP_Cipher(ctx->cctx, ctx->tbl, ctx->last_block, bl) <= 0)
0f113f3e
MC
190 return 0;
191 }
192 /* Encrypt all but one of the complete blocks left */
d5f85429 193 while (dlen > (size_t)bl) {
154ea425 194 if (EVP_Cipher(ctx->cctx, ctx->tbl, data, bl) <= 0)
0f113f3e
MC
195 return 0;
196 dlen -= bl;
197 data += bl;
198 }
199 /* Copy any data left to last block buffer */
200 memcpy(ctx->last_block, data, dlen);
201 ctx->nlast_block = dlen;
202 return 1;
8c968e03 203
0f113f3e 204}
8c968e03 205
c8ef656d 206int CMAC_Final(CMAC_CTX *ctx, unsigned char *out, size_t *poutlen)
0f113f3e
MC
207{
208 int i, bl, lb;
d5f85429 209
0f113f3e
MC
210 if (ctx->nlast_block == -1)
211 return 0;
ed576acd 212 if ((bl = EVP_CIPHER_CTX_get_block_size(ctx->cctx)) < 0)
d5f85429 213 return 0;
f6dead1b
RH
214 if (poutlen != NULL)
215 *poutlen = (size_t)bl;
0f113f3e
MC
216 if (!out)
217 return 1;
218 lb = ctx->nlast_block;
219 /* Is last block complete? */
220 if (lb == bl) {
221 for (i = 0; i < bl; i++)
222 out[i] = ctx->last_block[i] ^ ctx->k1[i];
223 } else {
224 ctx->last_block[lb] = 0x80;
225 if (bl - lb > 1)
226 memset(ctx->last_block + lb + 1, 0, bl - lb - 1);
227 for (i = 0; i < bl; i++)
228 out[i] = ctx->last_block[i] ^ ctx->k2[i];
229 }
846ec07d 230 if (!EVP_Cipher(ctx->cctx, out, out, bl)) {
0f113f3e
MC
231 OPENSSL_cleanse(out, bl);
232 return 0;
233 }
234 return 1;
235}
c8ef656d
DSH
236
237int CMAC_resume(CMAC_CTX *ctx)
0f113f3e
MC
238{
239 if (ctx->nlast_block == -1)
240 return 0;
241 /*
0d4fb843 242 * The buffer "tbl" contains the last fully encrypted block which is the
0f113f3e 243 * last IV (or all zeroes if no last encrypted block). The last block has
0d4fb843 244 * not been modified since CMAC_final(). So reinitialising using the last
0f113f3e
MC
245 * decrypted block will allow CMAC to continue after calling
246 * CMAC_Final().
247 */
512fdfdf 248 return EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, ctx->tbl);
0f113f3e 249}