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