• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ====================================================================
2  * Copyright (c) 2010 The OpenSSL Project.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *    the documentation and/or other materials provided with the
14  *    distribution.
15  *
16  * 3. All advertising materials mentioning features or use of this
17  *    software must display the following acknowledgment:
18  *    "This product includes software developed by the OpenSSL Project
19  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
20  *
21  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22  *    endorse or promote products derived from this software without
23  *    prior written permission. For written permission, please contact
24  *    licensing@OpenSSL.org.
25  *
26  * 5. Products derived from this software may not be called "OpenSSL"
27  *    nor may "OpenSSL" appear in their names without prior written
28  *    permission of the OpenSSL Project.
29  *
30  * 6. Redistributions of any form whatsoever must retain the following
31  *    acknowledgment:
32  *    "This product includes software developed by the OpenSSL Project
33  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46  * OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ==================================================================== */
48 
49 #include <openssl/cmac.h>
50 
51 #include <assert.h>
52 #include <string.h>
53 
54 #include <openssl/aes.h>
55 #include <openssl/cipher.h>
56 #include <openssl/mem.h>
57 
58 #include "../../internal.h"
59 #include "../service_indicator/internal.h"
60 
61 
62 struct cmac_ctx_st {
63   EVP_CIPHER_CTX cipher_ctx;
64   // k1 and k2 are the CMAC subkeys. See
65   // https://tools.ietf.org/html/rfc4493#section-2.3
66   uint8_t k1[AES_BLOCK_SIZE];
67   uint8_t k2[AES_BLOCK_SIZE];
68   // Last (possibly partial) scratch
69   uint8_t block[AES_BLOCK_SIZE];
70   // block_used contains the number of valid bytes in |block|.
71   unsigned block_used;
72 };
73 
CMAC_CTX_init(CMAC_CTX * ctx)74 static void CMAC_CTX_init(CMAC_CTX *ctx) {
75   EVP_CIPHER_CTX_init(&ctx->cipher_ctx);
76 }
77 
CMAC_CTX_cleanup(CMAC_CTX * ctx)78 static void CMAC_CTX_cleanup(CMAC_CTX *ctx) {
79   EVP_CIPHER_CTX_cleanup(&ctx->cipher_ctx);
80   OPENSSL_cleanse(ctx->k1, sizeof(ctx->k1));
81   OPENSSL_cleanse(ctx->k2, sizeof(ctx->k2));
82   OPENSSL_cleanse(ctx->block, sizeof(ctx->block));
83 }
84 
AES_CMAC(uint8_t out[16],const uint8_t * key,size_t key_len,const uint8_t * in,size_t in_len)85 int AES_CMAC(uint8_t out[16], const uint8_t *key, size_t key_len,
86              const uint8_t *in, size_t in_len) {
87   const EVP_CIPHER *cipher;
88   switch (key_len) {
89     // WARNING: this code assumes that all supported key sizes are FIPS
90     // Approved.
91     case 16:
92       cipher = EVP_aes_128_cbc();
93       break;
94     case 32:
95       cipher = EVP_aes_256_cbc();
96       break;
97     default:
98       return 0;
99   }
100 
101   size_t scratch_out_len;
102   CMAC_CTX ctx;
103   CMAC_CTX_init(&ctx);
104 
105   // We have to verify that all the CMAC services actually succeed before
106   // updating the indicator state, so we lock the state here.
107   FIPS_service_indicator_lock_state();
108   const int ok = CMAC_Init(&ctx, key, key_len, cipher, NULL /* engine */) &&
109                  CMAC_Update(&ctx, in, in_len) &&
110                  CMAC_Final(&ctx, out, &scratch_out_len);
111   FIPS_service_indicator_unlock_state();
112 
113   if (ok) {
114     FIPS_service_indicator_update_state();
115   }
116   CMAC_CTX_cleanup(&ctx);
117   return ok;
118 }
119 
CMAC_CTX_new(void)120 CMAC_CTX *CMAC_CTX_new(void) {
121   CMAC_CTX *ctx = OPENSSL_malloc(sizeof(*ctx));
122   if (ctx != NULL) {
123     CMAC_CTX_init(ctx);
124   }
125   return ctx;
126 }
127 
CMAC_CTX_free(CMAC_CTX * ctx)128 void CMAC_CTX_free(CMAC_CTX *ctx) {
129   if (ctx == NULL) {
130     return;
131   }
132 
133   CMAC_CTX_cleanup(ctx);
134   OPENSSL_free(ctx);
135 }
136 
CMAC_CTX_copy(CMAC_CTX * out,const CMAC_CTX * in)137 int CMAC_CTX_copy(CMAC_CTX *out, const CMAC_CTX *in) {
138   if (!EVP_CIPHER_CTX_copy(&out->cipher_ctx, &in->cipher_ctx)) {
139     return 0;
140   }
141   OPENSSL_memcpy(out->k1, in->k1, AES_BLOCK_SIZE);
142   OPENSSL_memcpy(out->k2, in->k2, AES_BLOCK_SIZE);
143   OPENSSL_memcpy(out->block, in->block, AES_BLOCK_SIZE);
144   out->block_used = in->block_used;
145   return 1;
146 }
147 
148 // binary_field_mul_x_128 treats the 128 bits at |in| as an element of GF(2¹²⁸)
149 // with a hard-coded reduction polynomial and sets |out| as x times the input.
150 //
151 // See https://tools.ietf.org/html/rfc4493#section-2.3
binary_field_mul_x_128(uint8_t out[16],const uint8_t in[16])152 static void binary_field_mul_x_128(uint8_t out[16], const uint8_t in[16]) {
153   unsigned i;
154 
155   // Shift |in| to left, including carry.
156   for (i = 0; i < 15; i++) {
157     out[i] = (in[i] << 1) | (in[i+1] >> 7);
158   }
159 
160   // If MSB set fixup with R.
161   const uint8_t carry = in[0] >> 7;
162   out[i] = (in[i] << 1) ^ ((0 - carry) & 0x87);
163 }
164 
165 // binary_field_mul_x_64 behaves like |binary_field_mul_x_128| but acts on an
166 // element of GF(2⁶⁴).
167 //
168 // See https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-38b.pdf
binary_field_mul_x_64(uint8_t out[8],const uint8_t in[8])169 static void binary_field_mul_x_64(uint8_t out[8], const uint8_t in[8]) {
170   unsigned i;
171 
172   // Shift |in| to left, including carry.
173   for (i = 0; i < 7; i++) {
174     out[i] = (in[i] << 1) | (in[i+1] >> 7);
175   }
176 
177   // If MSB set fixup with R.
178   const uint8_t carry = in[0] >> 7;
179   out[i] = (in[i] << 1) ^ ((0 - carry) & 0x1b);
180 }
181 
182 static const uint8_t kZeroIV[AES_BLOCK_SIZE] = {0};
183 
CMAC_Init(CMAC_CTX * ctx,const void * key,size_t key_len,const EVP_CIPHER * cipher,ENGINE * engine)184 int CMAC_Init(CMAC_CTX *ctx, const void *key, size_t key_len,
185               const EVP_CIPHER *cipher, ENGINE *engine) {
186   int ret = 0;
187   uint8_t scratch[AES_BLOCK_SIZE];
188 
189   // We have to avoid the underlying AES-CBC |EVP_CIPHER| services updating the
190   // indicator state, so we lock the state here.
191   FIPS_service_indicator_lock_state();
192 
193   size_t block_size = EVP_CIPHER_block_size(cipher);
194   if ((block_size != AES_BLOCK_SIZE && block_size != 8 /* 3-DES */) ||
195       EVP_CIPHER_key_length(cipher) != key_len ||
196       !EVP_EncryptInit_ex(&ctx->cipher_ctx, cipher, NULL, key, kZeroIV) ||
197       !EVP_Cipher(&ctx->cipher_ctx, scratch, kZeroIV, block_size) ||
198       // Reset context again ready for first data.
199       !EVP_EncryptInit_ex(&ctx->cipher_ctx, NULL, NULL, NULL, kZeroIV)) {
200     goto out;
201   }
202 
203   if (block_size == AES_BLOCK_SIZE) {
204     binary_field_mul_x_128(ctx->k1, scratch);
205     binary_field_mul_x_128(ctx->k2, ctx->k1);
206   } else {
207     binary_field_mul_x_64(ctx->k1, scratch);
208     binary_field_mul_x_64(ctx->k2, ctx->k1);
209   }
210   ctx->block_used = 0;
211   ret = 1;
212 
213 out:
214   FIPS_service_indicator_unlock_state();
215   return ret;
216 }
217 
CMAC_Reset(CMAC_CTX * ctx)218 int CMAC_Reset(CMAC_CTX *ctx) {
219   ctx->block_used = 0;
220   return EVP_EncryptInit_ex(&ctx->cipher_ctx, NULL, NULL, NULL, kZeroIV);
221 }
222 
CMAC_Update(CMAC_CTX * ctx,const uint8_t * in,size_t in_len)223 int CMAC_Update(CMAC_CTX *ctx, const uint8_t *in, size_t in_len) {
224   int ret = 0;
225 
226   // We have to avoid the underlying AES-CBC |EVP_Cipher| services updating the
227   // indicator state, so we lock the state here.
228   FIPS_service_indicator_lock_state();
229 
230   size_t block_size = EVP_CIPHER_CTX_block_size(&ctx->cipher_ctx);
231   assert(block_size <= AES_BLOCK_SIZE);
232   uint8_t scratch[AES_BLOCK_SIZE];
233 
234   if (ctx->block_used > 0) {
235     size_t todo = block_size - ctx->block_used;
236     if (in_len < todo) {
237       todo = in_len;
238     }
239 
240     OPENSSL_memcpy(ctx->block + ctx->block_used, in, todo);
241     in += todo;
242     in_len -= todo;
243     ctx->block_used += todo;
244 
245     // If |in_len| is zero then either |ctx->block_used| is less than
246     // |block_size|, in which case we can stop here, or |ctx->block_used| is
247     // exactly |block_size| but there's no more data to process. In the latter
248     // case we don't want to process this block now because it might be the last
249     // block and that block is treated specially.
250     if (in_len == 0) {
251       ret = 1;
252       goto out;
253     }
254 
255     assert(ctx->block_used == block_size);
256 
257     if (!EVP_Cipher(&ctx->cipher_ctx, scratch, ctx->block, block_size)) {
258       goto out;
259     }
260   }
261 
262   // Encrypt all but one of the remaining blocks.
263   while (in_len > block_size) {
264     if (!EVP_Cipher(&ctx->cipher_ctx, scratch, in, block_size)) {
265       goto out;
266     }
267     in += block_size;
268     in_len -= block_size;
269   }
270 
271   OPENSSL_memcpy(ctx->block, in, in_len);
272   ctx->block_used = in_len;
273   ret = 1;
274 
275 out:
276   FIPS_service_indicator_unlock_state();
277   return ret;
278 }
279 
CMAC_Final(CMAC_CTX * ctx,uint8_t * out,size_t * out_len)280 int CMAC_Final(CMAC_CTX *ctx, uint8_t *out, size_t *out_len) {
281   int ret = 0;
282   size_t block_size = EVP_CIPHER_CTX_block_size(&ctx->cipher_ctx);
283   assert(block_size <= AES_BLOCK_SIZE);
284 
285   // We have to avoid the underlying AES-CBC |EVP_Cipher| services updating the
286   // indicator state, so we lock the state here.
287   FIPS_service_indicator_lock_state();
288 
289   *out_len = block_size;
290   if (out == NULL) {
291     ret = 1;
292     goto out;
293   }
294 
295   const uint8_t *mask = ctx->k1;
296 
297   if (ctx->block_used != block_size) {
298     // If the last block is incomplete, terminate it with a single 'one' bit
299     // followed by zeros.
300     ctx->block[ctx->block_used] = 0x80;
301     OPENSSL_memset(ctx->block + ctx->block_used + 1, 0,
302                    block_size - (ctx->block_used + 1));
303 
304     mask = ctx->k2;
305   }
306 
307   for (unsigned i = 0; i < block_size; i++) {
308     out[i] = ctx->block[i] ^ mask[i];
309   }
310   ret = EVP_Cipher(&ctx->cipher_ctx, out, out, block_size);
311 
312 out:
313   FIPS_service_indicator_unlock_state();
314   if (ret) {
315     FIPS_service_indicator_update_state();
316   }
317   return ret;
318 }
319