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