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