1 /* fips/aes/fips_gcmtest.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2011 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
55 #define OPENSSL_FIPSAPI
56 #include <openssl/opensslconf.h>
61 int main(int argc
, char **argv
)
63 printf("No FIPS GCM support\n");
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>
78 static void gcmtest(FILE *in
, FILE *out
, int encrypt
)
82 char *keyword
, *value
;
83 int keylen
= -1, ivlen
= -1, aadlen
= -1, taglen
= -1, ptlen
= -1;
86 unsigned char *key
= NULL
, *iv
= NULL
, *aad
= NULL
, *tag
= NULL
;
87 unsigned char *ct
= NULL
, *pt
= NULL
;
89 const EVP_CIPHER
*gcm
= NULL
;
90 FIPS_cipher_ctx_init(&ctx
);
92 while(fgets(buf
,sizeof buf
,in
) != NULL
)
95 if (!parse_line(&keyword
, &value
, lbuf
, buf
))
97 if(!strcmp(keyword
,"[Keylen"))
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();
108 fprintf(stderr
, "Unsupported keylen %d\n",
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"))
123 key
= hex2bin_m(value
, &l
);
126 fprintf(stderr
, "Inconsistent Key length\n");
130 else if(!strcmp(keyword
,"IV"))
132 iv
= hex2bin_m(value
, &l
);
135 fprintf(stderr
, "Inconsistent IV length\n");
139 else if(!strcmp(keyword
,"PT"))
141 pt
= hex2bin_m(value
, &l
);
144 fprintf(stderr
, "Inconsistent PT length\n");
148 else if(!strcmp(keyword
,"CT"))
150 ct
= hex2bin_m(value
, &l
);
153 fprintf(stderr
, "Inconsistent CT length\n");
157 else if(!strcmp(keyword
,"AAD"))
159 aad
= hex2bin_m(value
, &l
);
162 fprintf(stderr
, "Inconsistent AAD length\n");
166 else if(!strcmp(keyword
,"Tag"))
168 tag
= hex2bin_m(value
, &l
);
171 fprintf(stderr
, "Inconsistent Tag length\n");
175 if (encrypt
&& pt
&& aad
&& (iv
|| encrypt
==1))
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);
184 static unsigned char iv_fixed
[4] = {1,2,3,4};
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
,
191 if (!FIPS_cipher_ctx_ctrl(&ctx
,
192 EVP_CTRL_GCM_IV_GEN
, 0, iv
))
194 fprintf(stderr
, "IV gen error\n");
197 OutputValue("IV", iv
, ivlen
, out
, 0);
200 FIPS_cipherinit(&ctx
, NULL
, key
, iv
, 1);
204 FIPS_cipher(&ctx
, NULL
, aad
, aadlen
);
207 ct
= OPENSSL_malloc(ptlen
);
208 rv
= FIPS_cipher(&ctx
, ct
, pt
, ptlen
);
210 FIPS_cipher(&ctx
, NULL
, NULL
, 0);
211 FIPS_cipher_ctx_ctrl(&ctx
, EVP_CTRL_GCM_GET_TAG
,
213 OutputValue("CT", ct
, ptlen
, out
, 0);
214 OutputValue("Tag", tag
, taglen
, out
, 0);
227 iv
= aad
= ct
= pt
= key
= tag
= NULL
;
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
);
238 FIPS_cipher(&ctx
, NULL
, aad
, aadlen
);
241 pt
= OPENSSL_malloc(ptlen
);
242 rv
= FIPS_cipher(&ctx
, pt
, ct
, ptlen
);
244 rv
= FIPS_cipher(&ctx
, NULL
, NULL
, 0);
246 fprintf(out
, "FAIL" RESP_EOL
);
248 OutputValue("PT", pt
, ptlen
, out
, 0);
261 iv
= aad
= ct
= pt
= key
= tag
= NULL
;
266 static void xtstest(FILE *in
, FILE *out
)
270 char *keyword
, *value
;
275 unsigned char *key
= NULL
, *iv
= NULL
;
276 unsigned char *inbuf
= NULL
, *outbuf
= NULL
;
278 const EVP_CIPHER
*xts
= NULL
;
279 FIPS_cipher_ctx_init(&ctx
);
281 while(fgets(buf
,sizeof buf
,in
) != NULL
)
284 if (buf
[0] == '[' && strlen(buf
) >= 9)
286 if(!strncmp(buf
,"[ENCRYPT]", 9))
288 else if(!strncmp(buf
,"[DECRYPT]", 9))
291 if (!parse_line(&keyword
, &value
, lbuf
, buf
))
293 else if(!strcmp(keyword
,"Key"))
295 key
= hex2bin_m(value
, &l
);
297 xts
= EVP_aes_128_xts();
299 xts
= EVP_aes_256_xts();
302 fprintf(stderr
, "Inconsistent Key length\n");
306 else if(!strcmp(keyword
,"i"))
308 iv
= hex2bin_m(value
, &l
);
311 fprintf(stderr
, "Inconsistent i length\n");
315 else if(encrypt
&& !strcmp(keyword
,"PT"))
317 inbuf
= hex2bin_m(value
, &l
);
320 else if(!encrypt
&& !strcmp(keyword
,"CT"))
322 inbuf
= hex2bin_m(value
, &l
);
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);
332 OPENSSL_free(outbuf
);
335 iv
= key
= inbuf
= outbuf
= NULL
;
340 static void ccmtest(FILE *in
, FILE *out
)
344 char *keyword
, *value
;
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;
352 const EVP_CIPHER
*ccm
= NULL
;
353 FIPS_cipher_ctx_init(&ctx
);
355 while(fgets(buf
,sizeof buf
,in
) != NULL
)
360 if (!parse_line(&keyword
, &value
, lbuf
, buf
))
363 /* If surrounded by square brackets zap them */
364 if (keyword
[0] == '[')
367 p
= strchr(value
, ']');
371 /* See if we have a comma separated list of parameters
372 * if so copy rest of line back to buffer and redo later.
374 p
= strchr(value
, ',');
382 if (!strcmp(keyword
,"Plen"))
384 else if (!strcmp(keyword
,"Nlen"))
386 else if (!strcmp(keyword
,"Tlen"))
388 else if (!strcmp(keyword
,"Alen"))
392 if (!strcmp(keyword
,"Key"))
396 Key
= hex2bin_m(value
, &l
);
398 ccm
= EVP_aes_128_ccm();
400 ccm
= EVP_aes_192_ccm();
402 ccm
= EVP_aes_256_ccm();
405 fprintf(stderr
, "Inconsistent Key length\n");
409 else if (!strcmp(keyword
,"Nonce"))
413 Nonce
= hex2bin_m(value
, &l
);
416 fprintf(stderr
, "Inconsistent nonce length\n");
420 else if (!strcmp(keyword
,"Payload") && !decr
)
422 Payload
= hex2bin_m(value
, &l
);
423 if (Plen
&& l
!= Plen
)
425 fprintf(stderr
, "Inconsistent Payload length\n");
429 else if (!strcmp(keyword
,"Adata"))
431 Adata
= hex2bin_m(value
, &l
);
432 if (Alen
&& l
!= Alen
)
434 fprintf(stderr
, "Inconsistent Payload length\n");
438 else if (!strcmp(keyword
,"CT") && decr
)
440 CT
= hex2bin_m(value
, &l
);
441 if (l
!= (Plen
+ Tlen
))
443 fprintf(stderr
, "Inconsistent CT length\n");
449 FIPS_cipherinit(&ctx
, ccm
, NULL
, NULL
, 1);
450 FIPS_cipher_ctx_ctrl(&ctx
, EVP_CTRL_CCM_SET_IVLEN
, Nlen
, 0);
451 FIPS_cipher_ctx_ctrl(&ctx
, EVP_CTRL_CCM_SET_TAG
, Tlen
, 0);
452 FIPS_cipherinit(&ctx
, NULL
, Key
, Nonce
, 1);
454 FIPS_cipher(&ctx
, NULL
, NULL
, Plen
);
455 FIPS_cipher(&ctx
, NULL
, Adata
, Alen
);
456 CT
= OPENSSL_malloc(Plen
+ Tlen
);
457 FIPS_cipher(&ctx
, CT
, Payload
, Plen
);
458 FIPS_cipher_ctx_ctrl(&ctx
, EVP_CTRL_CCM_GET_TAG
, Tlen
,
460 OutputValue("CT", CT
, Plen
+ Tlen
, out
, 0);
462 OPENSSL_free(Payload
);
468 int len
= Plen
== 0 ? 1: Plen
;
469 FIPS_cipherinit(&ctx
, ccm
, NULL
, NULL
, 0);
470 FIPS_cipher_ctx_ctrl(&ctx
, EVP_CTRL_CCM_SET_IVLEN
, Nlen
, 0);
471 FIPS_cipher_ctx_ctrl(&ctx
, EVP_CTRL_CCM_SET_TAG
,
473 FIPS_cipherinit(&ctx
, NULL
, Key
, Nonce
, 0);
474 FIPS_cipher(&ctx
, NULL
, NULL
, Plen
);
475 FIPS_cipher(&ctx
, NULL
, Adata
, Alen
);
476 Payload
= OPENSSL_malloc(len
);
477 rv
= FIPS_cipher(&ctx
, Payload
, CT
, Plen
);
482 fputs("Result = Pass" RESP_EOL
, out
);
483 OutputValue("Payload", Payload
, len
, out
, 0);
486 fputs("Result = Fail" RESP_EOL
, out
);
488 OPENSSL_free(Payload
);
496 FIPS_cipher_ctx_cleanup(&ctx
);
500 int fips_gcmtest_main(int argc
, char **argv
)
502 int main(int argc
, char **argv
)
506 int xts
= 0, ccm
= 0;
510 in
= fopen(argv
[2], "r");
513 fprintf(stderr
, "Error opening input file\n");
516 out
= fopen(argv
[3], "w");
519 fprintf(stderr
, "Error opening output file\n");
530 fprintf(stderr
,"%s [-encrypt|-decrypt]\n",argv
[0]);
534 if(!strcmp(argv
[1],"-encrypt"))
536 else if(!strcmp(argv
[1],"-encryptIVext"))
538 else if(!strcmp(argv
[1],"-decrypt"))
540 else if(!strcmp(argv
[1],"-ccm"))
542 else if(!strcmp(argv
[1],"-xts"))
546 fprintf(stderr
,"Don't know how to %s.\n",argv
[1]);
555 gcmtest(in
, out
, encrypt
);