Add FIPS flags to AES ciphers and SHA* digests.
[openssl.git] / crypto / evp / e_aes.c
1 /* ====================================================================
2  * Copyright (c) 2001 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  *    openssl-core@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  */
50
51 #define OPENSSL_FIPSAPI
52
53 #include <openssl/opensslconf.h>
54 #ifndef OPENSSL_NO_AES
55 #include <openssl/evp.h>
56 #include <openssl/err.h>
57 #include <string.h>
58 #include <assert.h>
59 #include <openssl/aes.h>
60 #include "evp_locl.h"
61 #include <openssl/modes.h>
62 #include <openssl/rand.h>
63
64 static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
65                                         const unsigned char *iv, int enc);
66
67 typedef struct
68         {
69         AES_KEY ks;
70         } EVP_AES_KEY;
71
72 #define data(ctx)       EVP_C_DATA(EVP_AES_KEY,ctx)
73
74 IMPLEMENT_BLOCK_CIPHER(aes_128, ks, AES, EVP_AES_KEY,
75                        NID_aes_128, 16, 16, 16, 128,
76                        EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1,
77                        aes_init_key, NULL, NULL, NULL, NULL)
78 IMPLEMENT_BLOCK_CIPHER(aes_192, ks, AES, EVP_AES_KEY,
79                        NID_aes_192, 16, 24, 16, 128,
80                        EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1,
81                        aes_init_key, NULL, NULL, NULL, NULL)
82 IMPLEMENT_BLOCK_CIPHER(aes_256, ks, AES, EVP_AES_KEY,
83                        NID_aes_256, 16, 32, 16, 128,
84                        EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1,
85                        aes_init_key, NULL, NULL, NULL, NULL)
86
87 #define IMPLEMENT_AES_CFBR(ksize,cbits) IMPLEMENT_CFBR(aes,AES,EVP_AES_KEY,ks,ksize,cbits,16,EVP_CIPH_FLAG_FIPS)
88
89 IMPLEMENT_AES_CFBR(128,1)
90 IMPLEMENT_AES_CFBR(192,1)
91 IMPLEMENT_AES_CFBR(256,1)
92
93 IMPLEMENT_AES_CFBR(128,8)
94 IMPLEMENT_AES_CFBR(192,8)
95 IMPLEMENT_AES_CFBR(256,8)
96
97 static int aes_counter (EVP_CIPHER_CTX *ctx, unsigned char *out,
98                 const unsigned char *in, size_t len)
99 {
100         unsigned int num;
101         num = ctx->num;
102 #ifdef AES_CTR_ASM
103         void AES_ctr32_encrypt(const unsigned char *in, unsigned char *out,
104                         size_t blocks, const AES_KEY *key,
105                         const unsigned char ivec[AES_BLOCK_SIZE]);
106
107         CRYPTO_ctr128_encrypt_ctr32(in,out,len,
108                 &((EVP_AES_KEY *)ctx->cipher_data)->ks,
109                 ctx->iv,ctx->buf,&num,(ctr128_f)AES_ctr32_encrypt);
110 #else
111         CRYPTO_ctr128_encrypt(in,out,len,
112                 &((EVP_AES_KEY *)ctx->cipher_data)->ks,
113                 ctx->iv,ctx->buf,&num,(block128_f)AES_encrypt);
114 #endif
115         ctx->num = (size_t)num;
116         return 1;
117 }
118
119 static const EVP_CIPHER aes_128_ctr_cipher=
120         {
121         NID_aes_128_ctr,1,16,16,
122         EVP_CIPH_CTR_MODE|EVP_CIPH_FLAG_FIPS,
123         aes_init_key,
124         aes_counter,
125         NULL,
126         sizeof(EVP_AES_KEY),
127         NULL,
128         NULL,
129         NULL,
130         NULL
131         };
132
133 const EVP_CIPHER *EVP_aes_128_ctr (void)
134 {       return &aes_128_ctr_cipher;     }
135
136 static const EVP_CIPHER aes_192_ctr_cipher=
137         {
138         NID_aes_192_ctr,1,24,16,
139         EVP_CIPH_CTR_MODE|EVP_CIPH_FLAG_FIPS,
140         aes_init_key,
141         aes_counter,
142         NULL,
143         sizeof(EVP_AES_KEY),
144         NULL,
145         NULL,
146         NULL,
147         NULL
148         };
149
150 const EVP_CIPHER *EVP_aes_192_ctr (void)
151 {       return &aes_192_ctr_cipher;     }
152
153 static const EVP_CIPHER aes_256_ctr_cipher=
154         {
155         NID_aes_256_ctr,1,32,16,
156         EVP_CIPH_CTR_MODE|EVP_CIPH_FLAG_FIPS,
157         aes_init_key,
158         aes_counter,
159         NULL,
160         sizeof(EVP_AES_KEY),
161         NULL,
162         NULL,
163         NULL,
164         NULL
165         };
166
167 const EVP_CIPHER *EVP_aes_256_ctr (void)
168 {       return &aes_256_ctr_cipher;     }
169
170 static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
171                    const unsigned char *iv, int enc)
172         {
173         int ret;
174
175         if (((ctx->cipher->flags & EVP_CIPH_MODE) == EVP_CIPH_ECB_MODE
176             || (ctx->cipher->flags & EVP_CIPH_MODE) == EVP_CIPH_CBC_MODE)
177             && !enc) 
178                 ret=AES_set_decrypt_key(key, ctx->key_len * 8, ctx->cipher_data);
179         else
180                 ret=AES_set_encrypt_key(key, ctx->key_len * 8, ctx->cipher_data);
181
182         if(ret < 0)
183                 {
184                 EVPerr(EVP_F_AES_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
185                 return 0;
186                 }
187
188         return 1;
189         }
190
191 typedef struct
192         {
193         /* AES key schedule to use */
194         AES_KEY ks;
195         /* Set if key initialised */
196         int key_set;
197         /* Set if an iv is set */
198         int iv_set;
199         /* Pointer to GCM128_CTX: FIXME actual structure later */
200         GCM128_CONTEXT *gcm;
201         /* Temporary IV store */
202         unsigned char *iv;
203         /* IV length */
204         int ivlen;
205         /* Tag to verify */
206         unsigned char tag[16];
207         int taglen;
208         /* It is OK to generate IVs */
209         int iv_gen;
210         } EVP_AES_GCM_CTX;
211
212 static int aes_gcm_cleanup(EVP_CIPHER_CTX *c)
213         {
214         EVP_AES_GCM_CTX *gctx = c->cipher_data;
215         if (gctx->gcm)
216                 CRYPTO_gcm128_release(gctx->gcm);
217         if (gctx->iv != c->iv)
218                 OPENSSL_free(gctx->iv);
219         return 1;
220         }
221
222 /* increment counter (64-bit int) by 1 */
223 static void ctr64_inc(unsigned char *counter) {
224         int n=8;
225         unsigned char  c;
226
227         do {
228                 --n;
229                 c = counter[n];
230                 ++c;
231                 counter[n] = c;
232                 if (c) return;
233         } while (n);
234 }
235
236 static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
237         {
238         EVP_AES_GCM_CTX *gctx = c->cipher_data;
239         switch (type)
240                 {
241         case EVP_CTRL_INIT:
242                 gctx->gcm = NULL;
243                 gctx->key_set = 0;
244                 gctx->iv_set = 0;
245                 gctx->ivlen = c->cipher->iv_len;
246                 gctx->iv = c->iv;
247                 gctx->taglen = -1;
248                 gctx->iv_gen = 0;
249                 return 1;
250
251         case EVP_CTRL_GCM_SET_IVLEN:
252                 if (arg <= 0)
253                         return 0;
254 #ifdef OPENSSL_FIPS
255                 if (FIPS_mode() && !(c->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW)
256                                                  && arg < 12)
257                         return 0;
258 #endif
259                 /* Allocate memory for IV if needed */
260                 if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen))
261                         {
262                         if (gctx->iv != c->iv)
263                                 OPENSSL_free(gctx->iv);
264                         gctx->iv = OPENSSL_malloc(arg);
265                         if (!gctx->iv)
266                                 return 0;
267                         }
268                 gctx->ivlen = arg;
269                 return 1;
270
271         case EVP_CTRL_GCM_SET_TAG:
272                 if (arg <= 0 || arg > 16 || c->encrypt)
273                         return 0;
274                 memcpy(gctx->tag, ptr, arg);
275                 gctx->taglen = arg;
276                 return 1;
277
278         case EVP_CTRL_GCM_GET_TAG:
279                 if (arg <= 0 || arg > 16 || !c->encrypt || gctx->taglen < 0)
280                         return 0;
281                 memcpy(ptr, gctx->tag, arg);
282                 return 1;
283
284         case EVP_CTRL_GCM_SET_IV_FIXED:
285                 /* Special case: -1 length restores whole IV */
286                 if (arg == -1)
287                         {
288                         memcpy(gctx->iv, ptr, gctx->ivlen);
289                         gctx->iv_gen = 1;
290                         return 1;
291                         }
292                 /* Fixed field must be at least 4 bytes and invocation field
293                  * at least 8.
294                  */
295                 if ((arg < 4) || (gctx->ivlen - arg) < 8)
296                         return 0;
297                 if (arg)
298                         memcpy(gctx->iv, ptr, arg);
299                 if (RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0)
300                         return 0;
301                 gctx->iv_gen = 1;
302                 return 1;
303
304         case EVP_CTRL_GCM_IV_GEN:
305                 if (gctx->iv_gen == 0 || gctx->key_set == 0)
306                         return 0;
307                 CRYPTO_gcm128_setiv(gctx->gcm, gctx->iv, gctx->ivlen);
308                 memcpy(ptr, gctx->iv, gctx->ivlen);
309                 /* Invocation field will be at least 8 bytes in size and
310                  * so no need to check wrap around or increment more than
311                  * last 8 bytes.
312                  */
313                 ctr64_inc(gctx->iv + gctx->ivlen - 8);
314                 gctx->iv_set = 1;
315                 return 1;
316
317         default:
318                 return -1;
319
320                 }
321         }
322
323 static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
324                         const unsigned char *iv, int enc)
325         {
326         EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
327         if (!iv && !key)
328                 return 1;
329         if (key)
330                 {
331                 AES_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks);
332                 if (!gctx->gcm)
333                         {
334                         gctx->gcm =
335                                 CRYPTO_gcm128_new(&gctx->ks, (block128_f)AES_encrypt);
336                         if (!gctx->gcm)
337                                 return 0;
338                         }
339                 else
340                         CRYPTO_gcm128_init(gctx->gcm, &gctx->ks, (block128_f)AES_encrypt);
341                 /* If we have an iv can set it directly, otherwise use
342                  * saved IV.
343                  */
344                 if (iv == NULL && gctx->iv_set)
345                         iv = gctx->iv;
346                 if (iv)
347                         {
348                         CRYPTO_gcm128_setiv(gctx->gcm, iv, gctx->ivlen);
349                         gctx->iv_set = 1;
350                         }
351                 gctx->key_set = 1;
352                 }
353         else
354                 {
355                 /* If key set use IV, otherwise copy */
356                 if (gctx->key_set)
357                         CRYPTO_gcm128_setiv(gctx->gcm, iv, gctx->ivlen);
358                 else
359                         memcpy(gctx->iv, iv, gctx->ivlen);
360                 gctx->iv_set = 1;
361                 gctx->iv_gen = 0;
362                 }
363         return 1;
364         }
365
366 static int aes_gcm(EVP_CIPHER_CTX *ctx, unsigned char *out,
367                 const unsigned char *in, size_t len)
368         {
369         EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
370         /* If not set up, return error */
371         if (!gctx->iv_set && !gctx->key_set)
372                 return -1;
373         if (!ctx->encrypt && gctx->taglen < 0)
374                 return -1;
375         if (in)
376                 {
377                 if (out == NULL)
378                         {
379                         if (CRYPTO_gcm128_aad(gctx->gcm, in, len))
380                                 return -1;
381                         }
382                 else if (ctx->encrypt)
383                         {
384                         if (CRYPTO_gcm128_encrypt(gctx->gcm, in, out, len))
385                                 return -1;
386                         }
387                 else
388                         {
389                         if (CRYPTO_gcm128_decrypt(gctx->gcm, in, out, len))
390                                 return -1;
391                         }
392                 return len;
393                 }
394         else
395                 {
396                 if (!ctx->encrypt)
397                         {
398                         if (CRYPTO_gcm128_finish(gctx->gcm,
399                                         gctx->tag, gctx->taglen) != 0)
400                                 return -1;
401                         gctx->iv_set = 0;
402                         return 0;
403                         }
404                 CRYPTO_gcm128_tag(gctx->gcm, gctx->tag, 16);
405                 gctx->taglen = 16;
406                 /* Don't reuse the IV */
407                 gctx->iv_set = 0;
408                 return 0;
409                 }
410
411         }
412
413 static const EVP_CIPHER aes_128_gcm_cipher=
414         {
415         NID_aes_128_gcm,1,16,12,
416         EVP_CIPH_GCM_MODE|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1
417                 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
418                 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT
419                 | EVP_CIPH_FLAG_FIPS,
420         aes_gcm_init_key,
421         aes_gcm,
422         aes_gcm_cleanup,
423         sizeof(EVP_AES_GCM_CTX),
424         NULL,
425         NULL,
426         aes_gcm_ctrl,
427         NULL
428         };
429
430 const EVP_CIPHER *EVP_aes_128_gcm (void)
431 {       return &aes_128_gcm_cipher;     }
432
433 static const EVP_CIPHER aes_192_gcm_cipher=
434         {
435         NID_aes_128_gcm,1,24,12,
436         EVP_CIPH_GCM_MODE|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1
437                 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
438                 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT
439                 | EVP_CIPH_FLAG_FIPS,
440         aes_gcm_init_key,
441         aes_gcm,
442         aes_gcm_cleanup,
443         sizeof(EVP_AES_GCM_CTX),
444         NULL,
445         NULL,
446         aes_gcm_ctrl,
447         NULL
448         };
449
450 const EVP_CIPHER *EVP_aes_192_gcm (void)
451 {       return &aes_192_gcm_cipher;     }
452
453 static const EVP_CIPHER aes_256_gcm_cipher=
454         {
455         NID_aes_128_gcm,1,32,12,
456         EVP_CIPH_GCM_MODE|EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_DEFAULT_ASN1
457                 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
458                 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT
459                 | EVP_CIPH_FLAG_FIPS,
460         aes_gcm_init_key,
461         aes_gcm,
462         aes_gcm_cleanup,
463         sizeof(EVP_AES_GCM_CTX),
464         NULL,
465         NULL,
466         aes_gcm_ctrl,
467         NULL
468         };
469
470 const EVP_CIPHER *EVP_aes_256_gcm (void)
471 {       return &aes_256_gcm_cipher;     }
472                 
473 #endif