Constify some input parameters.
[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_free(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     OPENSSL_free(ctx);
82 }
83
84 int CMAC_CTX_copy(CMAC_CTX *out, const CMAC_CTX *in)
85 {
86     int bl;
87     if (in->nlast_block == -1)
88         return 0;
89     if (!EVP_CIPHER_CTX_copy(out->cctx, in->cctx))
90         return 0;
91     bl = EVP_CIPHER_CTX_block_size(in->cctx);
92     memcpy(out->k1, in->k1, bl);
93     memcpy(out->k2, in->k2, bl);
94     memcpy(out->tbl, in->tbl, bl);
95     memcpy(out->last_block, in->last_block, bl);
96     out->nlast_block = in->nlast_block;
97     return 1;
98 }
99
100 int CMAC_Init(CMAC_CTX *ctx, const void *key, size_t keylen,
101               const EVP_CIPHER *cipher, ENGINE *impl)
102 {
103     static const unsigned char zero_iv[EVP_MAX_BLOCK_LENGTH] = { 0 };
104     /* All zeros means restart */
105     if (!key && !cipher && !impl && keylen == 0) {
106         /* Not initialised */
107         if (ctx->nlast_block == -1)
108             return 0;
109         if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, zero_iv))
110             return 0;
111         memset(ctx->tbl, 0, EVP_CIPHER_CTX_block_size(ctx->cctx));
112         ctx->nlast_block = 0;
113         return 1;
114     }
115     /* Initialise context */
116     if (cipher && !EVP_EncryptInit_ex(ctx->cctx, cipher, impl, NULL, NULL))
117         return 0;
118     /* Non-NULL key means initialisation complete */
119     if (key) {
120         int bl;
121         if (!EVP_CIPHER_CTX_cipher(ctx->cctx))
122             return 0;
123         if (!EVP_CIPHER_CTX_set_key_length(ctx->cctx, keylen))
124             return 0;
125         if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, key, zero_iv))
126             return 0;
127         bl = EVP_CIPHER_CTX_block_size(ctx->cctx);
128         if (!EVP_Cipher(ctx->cctx, ctx->tbl, zero_iv, bl))
129             return 0;
130         make_kn(ctx->k1, ctx->tbl, bl);
131         make_kn(ctx->k2, ctx->k1, bl);
132         OPENSSL_cleanse(ctx->tbl, bl);
133         /* Reset context again ready for first data block */
134         if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, zero_iv))
135             return 0;
136         /* Zero tbl so resume works */
137         memset(ctx->tbl, 0, bl);
138         ctx->nlast_block = 0;
139     }
140     return 1;
141 }
142
143 int CMAC_Update(CMAC_CTX *ctx, const void *in, size_t dlen)
144 {
145     const unsigned char *data = in;
146     size_t bl;
147     if (ctx->nlast_block == -1)
148         return 0;
149     if (dlen == 0)
150         return 1;
151     bl = EVP_CIPHER_CTX_block_size(ctx->cctx);
152     /* Copy into partial block if we need to */
153     if (ctx->nlast_block > 0) {
154         size_t nleft;
155         nleft = bl - ctx->nlast_block;
156         if (dlen < nleft)
157             nleft = dlen;
158         memcpy(ctx->last_block + ctx->nlast_block, data, nleft);
159         dlen -= nleft;
160         ctx->nlast_block += nleft;
161         /* If no more to process return */
162         if (dlen == 0)
163             return 1;
164         data += nleft;
165         /* Else not final block so encrypt it */
166         if (!EVP_Cipher(ctx->cctx, ctx->tbl, ctx->last_block, bl))
167             return 0;
168     }
169     /* Encrypt all but one of the complete blocks left */
170     while (dlen > bl) {
171         if (!EVP_Cipher(ctx->cctx, ctx->tbl, data, bl))
172             return 0;
173         dlen -= bl;
174         data += bl;
175     }
176     /* Copy any data left to last block buffer */
177     memcpy(ctx->last_block, data, dlen);
178     ctx->nlast_block = dlen;
179     return 1;
180
181 }
182
183 int CMAC_Final(CMAC_CTX *ctx, unsigned char *out, size_t *poutlen)
184 {
185     int i, bl, lb;
186     if (ctx->nlast_block == -1)
187         return 0;
188     bl = EVP_CIPHER_CTX_block_size(ctx->cctx);
189     *poutlen = (size_t)bl;
190     if (!out)
191         return 1;
192     lb = ctx->nlast_block;
193     /* Is last block complete? */
194     if (lb == bl) {
195         for (i = 0; i < bl; i++)
196             out[i] = ctx->last_block[i] ^ ctx->k1[i];
197     } else {
198         ctx->last_block[lb] = 0x80;
199         if (bl - lb > 1)
200             memset(ctx->last_block + lb + 1, 0, bl - lb - 1);
201         for (i = 0; i < bl; i++)
202             out[i] = ctx->last_block[i] ^ ctx->k2[i];
203     }
204     if (!EVP_Cipher(ctx->cctx, out, out, bl)) {
205         OPENSSL_cleanse(out, bl);
206         return 0;
207     }
208     return 1;
209 }
210
211 int CMAC_resume(CMAC_CTX *ctx)
212 {
213     if (ctx->nlast_block == -1)
214         return 0;
215     /*
216      * The buffer "tbl" contains the last fully encrypted block which is the
217      * last IV (or all zeroes if no last encrypted block). The last block has
218      * not been modified since CMAC_final(). So reinitialising using the last
219      * decrypted block will allow CMAC to continue after calling
220      * CMAC_Final().
221      */
222     return EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, ctx->tbl);
223 }