Fix EVP CCM decrypt. Add decrypt support to algorithm test program.
[openssl.git] / fips / aes / fips_gcmtest.c
1 /* fips/aes/fips_gcmtest.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project.
4  */
5 /* ====================================================================
6  * Copyright (c) 2011 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  */
53
54
55 #define OPENSSL_FIPSAPI
56 #include <openssl/opensslconf.h>
57
58 #ifndef OPENSSL_FIPS
59 #include <stdio.h>
60
61 int main(int argc, char **argv)
62 {
63     printf("No FIPS GCM support\n");
64     return(0);
65 }
66 #else
67
68 #include <openssl/bn.h>
69 #include <openssl/dsa.h>
70 #include <openssl/fips.h>
71 #include <openssl/err.h>
72 #include <openssl/evp.h>
73 #include <string.h>
74 #include <ctype.h>
75
76 #include "fips_utl.h"
77
78 static void gcmtest(FILE *in, FILE *out, int encrypt)
79         {
80         char buf[2048];
81         char lbuf[2048];
82         char *keyword, *value;
83         int keylen = -1, ivlen = -1, aadlen = -1, taglen = -1, ptlen = -1;
84         int rv;
85         long l;
86         unsigned char *key = NULL, *iv = NULL, *aad = NULL, *tag = NULL;
87         unsigned char *ct = NULL, *pt = NULL;
88         EVP_CIPHER_CTX ctx;
89         const EVP_CIPHER *gcm = NULL;
90         FIPS_cipher_ctx_init(&ctx);
91
92         while(fgets(buf,sizeof buf,in) != NULL)
93                 {
94                 fputs(buf,out);
95                 if (!parse_line(&keyword, &value, lbuf, buf))
96                         continue;
97                 if(!strcmp(keyword,"[Keylen"))
98                         {
99                         keylen = atoi(value);
100                         if (keylen == 128)
101                                 gcm = EVP_aes_128_gcm();
102                         else if (keylen == 192)
103                                 gcm = EVP_aes_192_gcm();
104                         else if (keylen == 256)
105                                 gcm = EVP_aes_256_gcm();
106                         else 
107                                 {
108                                 fprintf(stderr, "Unsupported keylen %d\n",
109                                                         keylen);
110                                 }
111                         keylen >>= 3;
112                         }
113                 else if (!strcmp(keyword, "[IVlen"))
114                         ivlen = atoi(value) >> 3;
115                 else if (!strcmp(keyword, "[AADlen"))
116                         aadlen = atoi(value) >> 3;
117                 else if (!strcmp(keyword, "[Taglen"))
118                         taglen = atoi(value) >> 3;
119                 else if (!strcmp(keyword, "[PTlen"))
120                         ptlen = atoi(value) >> 3;
121                 else if(!strcmp(keyword,"Key"))
122                         {
123                         key = hex2bin_m(value, &l);
124                         if (l != keylen)
125                                 {
126                                 fprintf(stderr, "Inconsistent Key length\n");
127                                 exit(1);
128                                 }
129                         }
130                 else if(!strcmp(keyword,"IV"))
131                         {
132                         iv = hex2bin_m(value, &l);
133                         if (l != ivlen)
134                                 {
135                                 fprintf(stderr, "Inconsistent IV length\n");
136                                 exit(1);
137                                 }
138                         }
139                 else if(!strcmp(keyword,"PT"))
140                         {
141                         pt = hex2bin_m(value, &l);
142                         if (l != ptlen)
143                                 {
144                                 fprintf(stderr, "Inconsistent PT length\n");
145                                 exit(1);
146                                 }
147                         }
148                 else if(!strcmp(keyword,"CT"))
149                         {
150                         ct = hex2bin_m(value, &l);
151                         if (l != ptlen)
152                                 {
153                                 fprintf(stderr, "Inconsistent CT length\n");
154                                 exit(1);
155                                 }
156                         }
157                 else if(!strcmp(keyword,"AAD"))
158                         {
159                         aad = hex2bin_m(value, &l);
160                         if (l != aadlen)
161                                 {
162                                 fprintf(stderr, "Inconsistent AAD length\n");
163                                 exit(1);
164                                 }
165                         }
166                 else if(!strcmp(keyword,"Tag"))
167                         {
168                         tag = hex2bin_m(value, &l);
169                         if (l != taglen)
170                                 {
171                                 fprintf(stderr, "Inconsistent Tag length\n");
172                                 exit(1);
173                                 }
174                         }
175                 if (encrypt && pt && aad && (iv || encrypt==1))
176                         {
177                         tag = OPENSSL_malloc(taglen);
178                         FIPS_cipherinit(&ctx, gcm, NULL, NULL, 1);
179                         /* Relax FIPS constraints for testing */
180                         M_EVP_CIPHER_CTX_set_flags(&ctx, EVP_CIPH_FLAG_NON_FIPS_ALLOW);
181                         FIPS_cipher_ctx_ctrl(&ctx, EVP_CTRL_GCM_SET_IVLEN, ivlen, 0);
182                         if (encrypt == 1)
183                                 {
184                                 static unsigned char iv_fixed[4] = {1,2,3,4};
185                                 if (!iv)
186                                         iv = OPENSSL_malloc(ivlen);
187                                 FIPS_cipherinit(&ctx, NULL, key, NULL, 1);
188                                 FIPS_cipher_ctx_ctrl(&ctx,
189                                                 EVP_CTRL_GCM_SET_IV_FIXED,
190                                                 4, iv_fixed);
191                                 if (!FIPS_cipher_ctx_ctrl(&ctx,
192                                         EVP_CTRL_GCM_IV_GEN, 0, iv))
193                                         {
194                                         fprintf(stderr, "IV gen error\n");
195                                         exit(1);
196                                         }
197                                 OutputValue("IV", iv, ivlen, out, 0);
198                                 }
199                         else
200                                 FIPS_cipherinit(&ctx, NULL, key, iv, 1);
201
202
203                         if (aadlen)
204                                 FIPS_cipher(&ctx, NULL, aad, aadlen);
205                         if (ptlen)
206                                 {
207                                 ct = OPENSSL_malloc(ptlen);
208                                 rv = FIPS_cipher(&ctx, ct, pt, ptlen);
209                                 }
210                         FIPS_cipher(&ctx, NULL, NULL, 0);
211                         FIPS_cipher_ctx_ctrl(&ctx, EVP_CTRL_GCM_GET_TAG,
212                                                                 taglen, tag);   
213                         OutputValue("CT", ct, ptlen, out, 0);
214                         OutputValue("Tag", tag, taglen, out, 0);
215                         if (iv)
216                                 OPENSSL_free(iv);
217                         if (aad)
218                                 OPENSSL_free(aad);
219                         if (ct)
220                                 OPENSSL_free(ct);
221                         if (pt)
222                                 OPENSSL_free(pt);
223                         if (key)
224                                 OPENSSL_free(key);
225                         if (tag)
226                                 OPENSSL_free(tag);
227                         iv = aad = ct = pt = key = tag = NULL;
228                         }       
229                 if (!encrypt && tag)
230                         {
231                         FIPS_cipherinit(&ctx, gcm, NULL, NULL, 0);
232                         /* Relax FIPS constraints for testing */
233                         M_EVP_CIPHER_CTX_set_flags(&ctx, EVP_CIPH_FLAG_NON_FIPS_ALLOW);
234                         FIPS_cipher_ctx_ctrl(&ctx, EVP_CTRL_GCM_SET_IVLEN, ivlen, 0);
235                         FIPS_cipherinit(&ctx, NULL, key, iv, 0);
236                         FIPS_cipher_ctx_ctrl(&ctx, EVP_CTRL_GCM_SET_TAG, taglen, tag);
237                         if (aadlen)
238                                 FIPS_cipher(&ctx, NULL, aad, aadlen);
239                         if (ptlen)
240                                 {
241                                 pt = OPENSSL_malloc(ptlen);
242                                 rv = FIPS_cipher(&ctx, pt, ct, ptlen);
243                                 }
244                         rv = FIPS_cipher(&ctx, NULL, NULL, 0);
245                         if (rv < 0)
246                                 fprintf(out, "FAIL\n");
247                         else
248                                 OutputValue("PT", pt, ptlen, out, 0);
249                         if (iv)
250                                 OPENSSL_free(iv);
251                         if (aad)
252                                 OPENSSL_free(aad);
253                         if (ct)
254                                 OPENSSL_free(ct);
255                         if (pt)
256                                 OPENSSL_free(pt);
257                         if (key)
258                                 OPENSSL_free(key);
259                         if (tag)
260                                 OPENSSL_free(tag);
261                         iv = aad = ct = pt = key = tag = NULL;
262                         }
263                 }
264         }
265
266 static void xtstest(FILE *in, FILE *out)
267         {
268         char buf[2048];
269         char lbuf[2048];
270         char *keyword, *value;
271         int inlen;
272         int encrypt = 0;
273         int rv;
274         long l;
275         unsigned char *key = NULL, *iv = NULL;
276         unsigned char *inbuf = NULL, *outbuf = NULL;
277         EVP_CIPHER_CTX ctx;
278         const EVP_CIPHER *xts = NULL;
279         FIPS_cipher_ctx_init(&ctx);
280
281         while(fgets(buf,sizeof buf,in) != NULL)
282                 {
283                 fputs(buf,out);
284                 if (buf[0] == '[' && strlen(buf) >= 9)
285                         {
286                         if(!strncmp(buf,"[ENCRYPT]", 9))
287                                 encrypt = 1;
288                         else if(!strncmp(buf,"[DECRYPT]", 9))
289                                 encrypt = 0;
290                         }
291                 if  (!parse_line(&keyword, &value, lbuf, buf))
292                         continue;
293                 else if(!strcmp(keyword,"Key"))
294                         {
295                         key = hex2bin_m(value, &l);
296                         if (l == 32)
297                                 xts = EVP_aes_128_xts();
298                         else if (l == 64)
299                                 xts = EVP_aes_256_xts();
300                         else
301                                 {
302                                 fprintf(stderr, "Inconsistent Key length\n");
303                                 exit(1);
304                                 }
305                         }
306                 else if(!strcmp(keyword,"i"))
307                         {
308                         iv = hex2bin_m(value, &l);
309                         if (l != 16)
310                                 {
311                                 fprintf(stderr, "Inconsistent i length\n");
312                                 exit(1);
313                                 }
314                         }
315                 else if(encrypt && !strcmp(keyword,"PT"))
316                         {
317                         inbuf = hex2bin_m(value, &l);
318                         inlen = l;
319                         }
320                 else if(!encrypt && !strcmp(keyword,"CT"))
321                         {
322                         inbuf = hex2bin_m(value, &l);
323                         inlen = l;
324                         }
325                 if (inbuf)
326                         {
327                         FIPS_cipherinit(&ctx, xts, key, iv, encrypt);
328                         outbuf = OPENSSL_malloc(inlen);
329                         rv = FIPS_cipher(&ctx, outbuf, inbuf, inlen);
330                         OutputValue(encrypt ? "CT":"PT", outbuf, inlen, out, 0);
331                         OPENSSL_free(inbuf);
332                         OPENSSL_free(outbuf);
333                         OPENSSL_free(key);
334                         OPENSSL_free(iv);
335                         iv = key = inbuf = outbuf = NULL;
336                         }       
337                 }
338         }
339
340 static void ccmtest(FILE *in, FILE *out)
341         {
342         char buf[2048];
343         char lbuf[2048];
344         char *keyword, *value;
345         long l;
346         unsigned char *Key = NULL, *Nonce = NULL;
347         unsigned char *Adata = NULL, *Payload = NULL;
348         unsigned char *CT = NULL;
349         int Plen = -1, Nlen = -1, Tlen = -1, Alen = -1;
350         int decr = 0;
351         EVP_CIPHER_CTX ctx;
352         const EVP_CIPHER *ccm = NULL;
353         FIPS_cipher_ctx_init(&ctx);
354
355         while(fgets(buf,sizeof buf,in) != NULL)
356                 {
357                 char *p;
358                 fputs(buf,out);
359                 redo:
360                 if (!parse_line(&keyword, &value, lbuf, buf))
361                         continue;
362
363                 /* If surrounded by square brackets zap them */
364                 if (keyword[0] == '[')
365                         {
366                         keyword++;
367                         p = strchr(value, ']');
368                         if (p)
369                                 *p = 0;
370                         }
371                 /* See if we have a comma separated list of parameters
372                  * if so copy rest of line back to buffer and redo later.
373                  */
374                 p = strchr(value, ',');
375                 if (p)
376                         {
377                         *p = 0;
378                         strcpy(buf, p + 1);
379                         decr = 1;
380                         }
381                 if (!strcmp(keyword,"Plen"))
382                         Plen = atoi(value);
383                 else if (!strcmp(keyword,"Nlen"))
384                         Nlen = atoi(value);
385                 else if (!strcmp(keyword,"Tlen"))
386                         Tlen = atoi(value);
387                 else if (!strcmp(keyword,"Alen"))
388                         Alen = atoi(value);
389                 if (p)
390                         goto redo;
391                 if (!strcmp(keyword,"Key"))
392                         {
393                         if (Key)
394                                 OPENSSL_free(Key);
395                         Key = hex2bin_m(value, &l);
396                         if (l == 16)
397                                 ccm = EVP_aes_128_ccm();
398                         else if (l == 24)
399                                 ccm = EVP_aes_192_ccm();
400                         else if (l == 32)
401                                 ccm = EVP_aes_256_ccm();
402                         else
403                                 {
404                                 fprintf(stderr, "Inconsistent Key length\n");
405                                 exit(1);
406                                 }
407                         }
408                 else if (!strcmp(keyword,"Nonce"))
409                         {
410                         if (Nonce)
411                                 OPENSSL_free(Nonce);
412                         Nonce = hex2bin_m(value, &l);
413                         if (l != Nlen)
414                                 {
415                                 fprintf(stderr, "Inconsistent nonce length\n");
416                                 exit(1);
417                                 }
418                         }
419                 else if (!strcmp(keyword,"Payload") && !decr)
420                         {
421                         Payload = hex2bin_m(value, &l);
422                         if (Plen && l != Plen)
423                                 {
424                                 fprintf(stderr, "Inconsistent Payload length\n");
425                                 exit(1);
426                                 }
427                         }
428                 else if (!strcmp(keyword,"Adata"))
429                         {
430                         Adata = hex2bin_m(value, &l);
431                         if (Alen && l != Alen)
432                                 {
433                                 fprintf(stderr, "Inconsistent Payload length\n");
434                                 exit(1);
435                                 }
436                         }
437                 else if (!strcmp(keyword,"CT") && decr)
438                         {
439                         CT = hex2bin_m(value, &l);
440                         if (l != (Plen + Tlen))
441                                 {
442                                 fprintf(stderr, "Inconsistent CT length\n");
443                                 exit(1);
444                                 }
445                         }
446                 if (Payload)
447                         {
448                         FIPS_cipherinit(&ctx, ccm, NULL, NULL, 1);
449                         FIPS_cipher_ctx_ctrl(&ctx, EVP_CTRL_CCM_SET_IVLEN, Nlen, 0);
450                         FIPS_cipher_ctx_ctrl(&ctx, EVP_CTRL_CCM_SET_TAG, Tlen, 0);
451                         FIPS_cipherinit(&ctx, NULL, Key, Nonce, 1);
452
453                         FIPS_cipher(&ctx, NULL, NULL, Plen);
454                         FIPS_cipher(&ctx, NULL, Adata, Alen);
455                         CT = OPENSSL_malloc(Plen + Tlen);
456                         FIPS_cipher(&ctx, CT, Payload, Plen);
457                         FIPS_cipher_ctx_ctrl(&ctx, EVP_CTRL_CCM_GET_TAG, Tlen,
458                                                 CT + Plen);
459                         OutputValue("CT", CT, Plen + Tlen, out, 0);
460                         OPENSSL_free(CT);
461                         OPENSSL_free(Payload);
462                         CT = Payload = NULL;
463                         }
464                 if (CT)
465                         {
466                         int rv;
467                         int len = Plen == 0 ? 1: Plen;
468                         FIPS_cipherinit(&ctx, ccm, NULL, NULL, 0);
469                         FIPS_cipher_ctx_ctrl(&ctx, EVP_CTRL_CCM_SET_IVLEN, Nlen, 0);
470                         FIPS_cipher_ctx_ctrl(&ctx, EVP_CTRL_CCM_SET_TAG,
471                                                 Tlen, CT + Plen);
472                         FIPS_cipherinit(&ctx, NULL, Key, Nonce, 0);
473                         FIPS_cipher(&ctx, NULL, NULL, Plen);
474                         FIPS_cipher(&ctx, NULL, Adata, Alen);
475                         Payload = OPENSSL_malloc(len);
476                         rv = FIPS_cipher(&ctx, Payload, CT, Plen);
477                         if (rv >= 0)
478                                 {
479                                 if (rv == 0)
480                                         Payload[0] = 0;
481                                 fputs("Result = Pass\n", out);
482                                 OutputValue("Payload", Payload, len, out, 0);
483                                 }
484                         else
485                                 fputs("Result = Fail\n", out);
486                         OPENSSL_free(CT);
487                         OPENSSL_free(Payload);
488                         CT = Payload = NULL;
489                         }
490                 }
491         if (Key)
492                 OPENSSL_free(Key);
493         if (Nonce)
494                 OPENSSL_free(Nonce);
495         FIPS_cipher_ctx_cleanup(&ctx);
496         }
497
498 int main(int argc,char **argv)
499         {
500         int encrypt;
501         int xts = 0, ccm = 0;
502         FILE *in, *out;
503         if (argc == 4)
504                 {
505                 in = fopen(argv[2], "r");
506                 if (!in)
507                         {
508                         fprintf(stderr, "Error opening input file\n");
509                         exit(1);
510                         }
511                 out = fopen(argv[3], "w");
512                 if (!out)
513                         {
514                         fprintf(stderr, "Error opening output file\n");
515                         exit(1);
516                         }
517                 }
518         else if (argc == 2)
519                 {
520                 in = stdin;
521                 out = stdout;
522                 }
523         else
524                 {
525                 fprintf(stderr,"%s [-encrypt|-decrypt]\n",argv[0]);
526                 exit(1);
527                 }
528         fips_algtest_init();
529         if(!strcmp(argv[1],"-encrypt"))
530                 encrypt = 1;
531         else if(!strcmp(argv[1],"-encryptIVext"))
532                 encrypt = 2;
533         else if(!strcmp(argv[1],"-decrypt"))
534                 encrypt = 0;
535         else if(!strcmp(argv[1],"-ccm"))
536                 ccm = 1;
537         else if(!strcmp(argv[1],"-xts"))
538                 xts = 1;
539         else
540                 {
541                 fprintf(stderr,"Don't know how to %s.\n",argv[1]);
542                 exit(1);
543                 }
544
545         if (ccm)
546                 ccmtest(in, out);
547         else if (xts)
548                 xtstest(in, out);
549         else
550                 gcmtest(in, out, encrypt);
551
552         if (argc == 4)
553                 {
554                 fclose(in);
555                 fclose(out);
556                 }
557
558         return 0;
559 }
560
561 #endif