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