1*12720SWyllys.Ingersoll@Sun.COM /*
2*12720SWyllys.Ingersoll@Sun.COM  * CDDL HEADER START
3*12720SWyllys.Ingersoll@Sun.COM  *
4*12720SWyllys.Ingersoll@Sun.COM  * The contents of this file are subject to the terms of the
5*12720SWyllys.Ingersoll@Sun.COM  * Common Development and Distribution License (the "License").
6*12720SWyllys.Ingersoll@Sun.COM  * You may not use this file except in compliance with the License.
7*12720SWyllys.Ingersoll@Sun.COM  *
8*12720SWyllys.Ingersoll@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*12720SWyllys.Ingersoll@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*12720SWyllys.Ingersoll@Sun.COM  * See the License for the specific language governing permissions
11*12720SWyllys.Ingersoll@Sun.COM  * and limitations under the License.
12*12720SWyllys.Ingersoll@Sun.COM  *
13*12720SWyllys.Ingersoll@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*12720SWyllys.Ingersoll@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*12720SWyllys.Ingersoll@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*12720SWyllys.Ingersoll@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*12720SWyllys.Ingersoll@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*12720SWyllys.Ingersoll@Sun.COM  *
19*12720SWyllys.Ingersoll@Sun.COM  * CDDL HEADER END
20*12720SWyllys.Ingersoll@Sun.COM  */
21*12720SWyllys.Ingersoll@Sun.COM 
22*12720SWyllys.Ingersoll@Sun.COM /*
23*12720SWyllys.Ingersoll@Sun.COM  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24*12720SWyllys.Ingersoll@Sun.COM  */
25*12720SWyllys.Ingersoll@Sun.COM 
26*12720SWyllys.Ingersoll@Sun.COM /**
27*12720SWyllys.Ingersoll@Sun.COM  * \file KMSAgentKnownAnswerTests.cpp
28*12720SWyllys.Ingersoll@Sun.COM  */
29*12720SWyllys.Ingersoll@Sun.COM 
30*12720SWyllys.Ingersoll@Sun.COM #if defined(K_SOLARIS_PLATFORM) && !defined(SOLARIS10)
31*12720SWyllys.Ingersoll@Sun.COM #include <aes_impl.h>
32*12720SWyllys.Ingersoll@Sun.COM #define AES_MAXKEYBYTES AES_MAX_KEY_BYTES
33*12720SWyllys.Ingersoll@Sun.COM #define	AES_MAXKEYBITS AES_MAXBITS
34*12720SWyllys.Ingersoll@Sun.COM #else
35*12720SWyllys.Ingersoll@Sun.COM #include "rijndael.h"
36*12720SWyllys.Ingersoll@Sun.COM #endif
37*12720SWyllys.Ingersoll@Sun.COM #include "KMSAgentCryptoUtilities.h"
38*12720SWyllys.Ingersoll@Sun.COM #include "KMSAgentStringUtilities.h"
39*12720SWyllys.Ingersoll@Sun.COM 
40*12720SWyllys.Ingersoll@Sun.COM #ifdef METAWARE
41*12720SWyllys.Ingersoll@Sun.COM #include "debug.h"
42*12720SWyllys.Ingersoll@Sun.COM #include "sizet.h"
43*12720SWyllys.Ingersoll@Sun.COM typedef unsigned char uint8_t;
44*12720SWyllys.Ingersoll@Sun.COM typedef unsigned short uint16_t;
45*12720SWyllys.Ingersoll@Sun.COM typedef unsigned int uint32_t;
46*12720SWyllys.Ingersoll@Sun.COM typedef unsigned long long uint64_t;
47*12720SWyllys.Ingersoll@Sun.COM #endif
48*12720SWyllys.Ingersoll@Sun.COM 
49*12720SWyllys.Ingersoll@Sun.COM #include "KMSAgentAESKeyWrap.h"
50*12720SWyllys.Ingersoll@Sun.COM #include "KMSAgentKnownAnswerTests.h"
51*12720SWyllys.Ingersoll@Sun.COM 
KnownAnswerTestAESKeyWrap(void)52*12720SWyllys.Ingersoll@Sun.COM int KnownAnswerTestAESKeyWrap (void)
53*12720SWyllys.Ingersoll@Sun.COM {
54*12720SWyllys.Ingersoll@Sun.COM 
55*12720SWyllys.Ingersoll@Sun.COM     /*
56*12720SWyllys.Ingersoll@Sun.COM      * Test Vectors from RFC3394 for 256 bit KEK and 256 bit Key
57*12720SWyllys.Ingersoll@Sun.COM      *  Wrap  Input:
58*12720SWyllys.Ingersoll@Sun.COM            KEK:
59*12720SWyllys.Ingersoll@Sun.COM              000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F
60*12720SWyllys.Ingersoll@Sun.COM            Key Data:
61*12720SWyllys.Ingersoll@Sun.COM              00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F
62*12720SWyllys.Ingersoll@Sun.COM 
63*12720SWyllys.Ingersoll@Sun.COM            Output:
64*12720SWyllys.Ingersoll@Sun.COM            Ciphertext  28C9F404C4B810F4 CBCCB35CFB87F826 3F5786E2D80ED326
65*12720SWyllys.Ingersoll@Sun.COM                        CBC7F0E71A99F43B FB988B9B7A02DD21
66*12720SWyllys.Ingersoll@Sun.COM 
67*12720SWyllys.Ingersoll@Sun.COM            Unwrap:
68*12720SWyllys.Ingersoll@Sun.COM            Plaintext  A6A6A6A6A6A6A6A6 0011223344556677 8899AABBCCDDEEFF
69*12720SWyllys.Ingersoll@Sun.COM                       0001020304050607 08090A0B0C0D0E0F
70*12720SWyllys.Ingersoll@Sun.COM 
71*12720SWyllys.Ingersoll@Sun.COM            Output:
72*12720SWyllys.Ingersoll@Sun.COM            Key Data:
73*12720SWyllys.Ingersoll@Sun.COM                 00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F
74*12720SWyllys.Ingersoll@Sun.COM 
75*12720SWyllys.Ingersoll@Sun.COM      */
76*12720SWyllys.Ingersoll@Sun.COM 
77*12720SWyllys.Ingersoll@Sun.COM     static char sKEK[] = "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F";
78*12720SWyllys.Ingersoll@Sun.COM     static char sKey[] = "00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F";
79*12720SWyllys.Ingersoll@Sun.COM     static char sKnownCiphertext[] = "28C9F404C4B810F4CBCCB35CFB87F8263F5786E2D80ED326CBC7F0E71A99F43BFB988B9B7A02DD21";
80*12720SWyllys.Ingersoll@Sun.COM 
81*12720SWyllys.Ingersoll@Sun.COM     //#ifdef KAT_DEBUG
82*12720SWyllys.Ingersoll@Sun.COM     //    printf("\nAES Key Wrap Test using Test Vectors from RFC 3394 for 256b KEK and 256b Key\n\n");
83*12720SWyllys.Ingersoll@Sun.COM     //    printf("KEK=%s\n", sKEK);
84*12720SWyllys.Ingersoll@Sun.COM     //    printf("Key=%s\n", sKey);
85*12720SWyllys.Ingersoll@Sun.COM     //#endif
86*12720SWyllys.Ingersoll@Sun.COM 
87*12720SWyllys.Ingersoll@Sun.COM     // key-encryption key
88*12720SWyllys.Ingersoll@Sun.COM     unsigned char acKEK[AES_MAXKEYBYTES];
89*12720SWyllys.Ingersoll@Sun.COM 
90*12720SWyllys.Ingersoll@Sun.COM     // plaintext key
91*12720SWyllys.Ingersoll@Sun.COM     unsigned char acKey[AES_MAXKEYBYTES];
92*12720SWyllys.Ingersoll@Sun.COM 
93*12720SWyllys.Ingersoll@Sun.COM     // the wrapped key includes an extra 64bits for the integrity check register
94*12720SWyllys.Ingersoll@Sun.COM     unsigned char acWrappedKey[AES_MAXKEYBYTES + 8];
95*12720SWyllys.Ingersoll@Sun.COM     unsigned char acUnWrappedKey[AES_MAXKEYBYTES];
96*12720SWyllys.Ingersoll@Sun.COM     unsigned char acExpectedWrappedKey[AES_MAXKEYBYTES + 8];
97*12720SWyllys.Ingersoll@Sun.COM 
98*12720SWyllys.Ingersoll@Sun.COM     if ((size_t) ConvertUTF8HexStringToBinary(
99*12720SWyllys.Ingersoll@Sun.COM         sKnownCiphertext,
100*12720SWyllys.Ingersoll@Sun.COM         acExpectedWrappedKey) != strlen(sKnownCiphertext) / 2)
101*12720SWyllys.Ingersoll@Sun.COM     {
102*12720SWyllys.Ingersoll@Sun.COM         return -1;
103*12720SWyllys.Ingersoll@Sun.COM     }
104*12720SWyllys.Ingersoll@Sun.COM 
105*12720SWyllys.Ingersoll@Sun.COM     if (ConvertUTF8HexStringToBinary(
106*12720SWyllys.Ingersoll@Sun.COM         sKEK,
107*12720SWyllys.Ingersoll@Sun.COM         acKEK) != AES_MAXKEYBYTES)
108*12720SWyllys.Ingersoll@Sun.COM     {
109*12720SWyllys.Ingersoll@Sun.COM         return -1;
110*12720SWyllys.Ingersoll@Sun.COM     }
111*12720SWyllys.Ingersoll@Sun.COM 
112*12720SWyllys.Ingersoll@Sun.COM     if (ConvertUTF8HexStringToBinary(
113*12720SWyllys.Ingersoll@Sun.COM         sKey,
114*12720SWyllys.Ingersoll@Sun.COM         acKey) != AES_MAXKEYBYTES)
115*12720SWyllys.Ingersoll@Sun.COM     {
116*12720SWyllys.Ingersoll@Sun.COM         return -1;
117*12720SWyllys.Ingersoll@Sun.COM     }
118*12720SWyllys.Ingersoll@Sun.COM 
119*12720SWyllys.Ingersoll@Sun.COM     // for 256 bit Key n=64
120*12720SWyllys.Ingersoll@Sun.COM     aes_key_wrap(acKEK, sizeof (acKEK), acKey,
121*12720SWyllys.Ingersoll@Sun.COM             4, acWrappedKey);
122*12720SWyllys.Ingersoll@Sun.COM 
123*12720SWyllys.Ingersoll@Sun.COM     if (memcmp(acWrappedKey, acExpectedWrappedKey, sizeof (acWrappedKey)) != 0)
124*12720SWyllys.Ingersoll@Sun.COM     {
125*12720SWyllys.Ingersoll@Sun.COM         return -1;
126*12720SWyllys.Ingersoll@Sun.COM     }
127*12720SWyllys.Ingersoll@Sun.COM 
128*12720SWyllys.Ingersoll@Sun.COM     if (aes_key_unwrap(acKEK, sizeof (acKEK), acWrappedKey,
129*12720SWyllys.Ingersoll@Sun.COM         acUnWrappedKey, 4) != 0)
130*12720SWyllys.Ingersoll@Sun.COM     {
131*12720SWyllys.Ingersoll@Sun.COM         return -1;
132*12720SWyllys.Ingersoll@Sun.COM     }
133*12720SWyllys.Ingersoll@Sun.COM 
134*12720SWyllys.Ingersoll@Sun.COM     if (memcmp(acKey, acUnWrappedKey, sizeof (acKey)) != 0)
135*12720SWyllys.Ingersoll@Sun.COM     {
136*12720SWyllys.Ingersoll@Sun.COM         return -1;
137*12720SWyllys.Ingersoll@Sun.COM     }
138*12720SWyllys.Ingersoll@Sun.COM 
139*12720SWyllys.Ingersoll@Sun.COM     return 0;
140*12720SWyllys.Ingersoll@Sun.COM }
141*12720SWyllys.Ingersoll@Sun.COM 
AES_ECB_TestExecution(const char * const i_sPlainText,const char * const i_sKnownCypherText,const unsigned char * const i_pKey)142*12720SWyllys.Ingersoll@Sun.COM static int AES_ECB_TestExecution (
143*12720SWyllys.Ingersoll@Sun.COM                                   const char * const i_sPlainText,
144*12720SWyllys.Ingersoll@Sun.COM                                   const char * const i_sKnownCypherText,
145*12720SWyllys.Ingersoll@Sun.COM                                   const unsigned char * const i_pKey)
146*12720SWyllys.Ingersoll@Sun.COM {
147*12720SWyllys.Ingersoll@Sun.COM     unsigned char acPlainText[256];
148*12720SWyllys.Ingersoll@Sun.COM     unsigned char acCypherText[sizeof (acPlainText)];
149*12720SWyllys.Ingersoll@Sun.COM     unsigned char acKnownCypherText[sizeof (acPlainText)];
150*12720SWyllys.Ingersoll@Sun.COM     unsigned char acDecryptedCypherText[sizeof (acPlainText)];
151*12720SWyllys.Ingersoll@Sun.COM     memset(acDecryptedCypherText, 0, sizeof (acDecryptedCypherText));
152*12720SWyllys.Ingersoll@Sun.COM 
153*12720SWyllys.Ingersoll@Sun.COM #ifdef KAT_DEBUG
154*12720SWyllys.Ingersoll@Sun.COM     char sComputedCypherText[256];
155*12720SWyllys.Ingersoll@Sun.COM #endif
156*12720SWyllys.Ingersoll@Sun.COM 
157*12720SWyllys.Ingersoll@Sun.COM #if defined(K_SOLARIS_PLATFORM) && !defined(SOLARIS10)
158*12720SWyllys.Ingersoll@Sun.COM     void *ks;
159*12720SWyllys.Ingersoll@Sun.COM     size_t ks_size;
160*12720SWyllys.Ingersoll@Sun.COM #else
161*12720SWyllys.Ingersoll@Sun.COM     rijndael_ctx ctx;
162*12720SWyllys.Ingersoll@Sun.COM #endif
163*12720SWyllys.Ingersoll@Sun.COM 
164*12720SWyllys.Ingersoll@Sun.COM     if ((size_t) ConvertUTF8HexStringToBinary(
165*12720SWyllys.Ingersoll@Sun.COM         i_sPlainText,
166*12720SWyllys.Ingersoll@Sun.COM         acPlainText) != strlen(i_sPlainText) / 2)
167*12720SWyllys.Ingersoll@Sun.COM     {
168*12720SWyllys.Ingersoll@Sun.COM         return -1;
169*12720SWyllys.Ingersoll@Sun.COM     }
170*12720SWyllys.Ingersoll@Sun.COM     if ((size_t) ConvertUTF8HexStringToBinary(
171*12720SWyllys.Ingersoll@Sun.COM         i_sKnownCypherText,
172*12720SWyllys.Ingersoll@Sun.COM         acKnownCypherText) != strlen(i_sKnownCypherText) / 2)
173*12720SWyllys.Ingersoll@Sun.COM     {
174*12720SWyllys.Ingersoll@Sun.COM         return -1;
175*12720SWyllys.Ingersoll@Sun.COM     }
176*12720SWyllys.Ingersoll@Sun.COM 
177*12720SWyllys.Ingersoll@Sun.COM #if defined(K_SOLARIS_PLATFORM) && !defined(SOLARIS10)
178*12720SWyllys.Ingersoll@Sun.COM 	ks = aes_alloc_keysched(&ks_size, 0);
179*12720SWyllys.Ingersoll@Sun.COM 	if (ks == NULL)
180*12720SWyllys.Ingersoll@Sun.COM 		return (-1);
181*12720SWyllys.Ingersoll@Sun.COM 	aes_init_keysched(i_pKey, AES_MAXKEYBITS, ks);
182*12720SWyllys.Ingersoll@Sun.COM 	(void) aes_encrypt_block(ks, acPlainText, acCypherText);
183*12720SWyllys.Ingersoll@Sun.COM #else
184*12720SWyllys.Ingersoll@Sun.COM     rijndael_set_key_enc_only(&ctx, (uint8_t *) i_pKey, AES_MAXKEYBITS);
185*12720SWyllys.Ingersoll@Sun.COM 
186*12720SWyllys.Ingersoll@Sun.COM     rijndael_encrypt(&ctx, acPlainText, (uint8_t *) acCypherText);
187*12720SWyllys.Ingersoll@Sun.COM #endif
188*12720SWyllys.Ingersoll@Sun.COM 
189*12720SWyllys.Ingersoll@Sun.COM #ifdef KAT_DEBUG
190*12720SWyllys.Ingersoll@Sun.COM     ConvertBinaryToUTF8HexString(sComputedCypherText,
191*12720SWyllys.Ingersoll@Sun.COM             acCypherText,
192*12720SWyllys.Ingersoll@Sun.COM             strlen(i_sPlainText) / 2);
193*12720SWyllys.Ingersoll@Sun.COM     printf("PlainText=%s\n", i_sPlainText);
194*12720SWyllys.Ingersoll@Sun.COM     printf("CypherText=%s\n", sComputedCypherText);
195*12720SWyllys.Ingersoll@Sun.COM #endif
196*12720SWyllys.Ingersoll@Sun.COM 
197*12720SWyllys.Ingersoll@Sun.COM     if (memcmp(acCypherText, acKnownCypherText, strlen(i_sKnownCypherText) / 2) != 0)
198*12720SWyllys.Ingersoll@Sun.COM     {
199*12720SWyllys.Ingersoll@Sun.COM #if defined(K_SOLARIS_PLATFORM) && !defined(SOLARIS10)
200*12720SWyllys.Ingersoll@Sun.COM 	free(ks);
201*12720SWyllys.Ingersoll@Sun.COM #endif
202*12720SWyllys.Ingersoll@Sun.COM         return -1;
203*12720SWyllys.Ingersoll@Sun.COM     }
204*12720SWyllys.Ingersoll@Sun.COM 
205*12720SWyllys.Ingersoll@Sun.COM #if defined(K_SOLARIS_PLATFORM) && !defined(SOLARIS10)
206*12720SWyllys.Ingersoll@Sun.COM 	aes_init_keysched(i_pKey, AES_MAXKEYBITS, ks);
207*12720SWyllys.Ingersoll@Sun.COM 	(void) aes_decrypt_block(ks, acCypherText, acDecryptedCypherText);
208*12720SWyllys.Ingersoll@Sun.COM 	free(ks);
209*12720SWyllys.Ingersoll@Sun.COM #else
210*12720SWyllys.Ingersoll@Sun.COM     rijndael_set_key(&ctx, (uint8_t *) i_pKey, AES_MAXKEYBITS);
211*12720SWyllys.Ingersoll@Sun.COM     rijndael_decrypt(&ctx, (uint8_t *) acCypherText, acDecryptedCypherText);
212*12720SWyllys.Ingersoll@Sun.COM #endif
213*12720SWyllys.Ingersoll@Sun.COM 
214*12720SWyllys.Ingersoll@Sun.COM     if (memcmp(acPlainText, acDecryptedCypherText, strlen(i_sPlainText) / 2) != 0)
215*12720SWyllys.Ingersoll@Sun.COM     {
216*12720SWyllys.Ingersoll@Sun.COM         return -1;
217*12720SWyllys.Ingersoll@Sun.COM     }
218*12720SWyllys.Ingersoll@Sun.COM 
219*12720SWyllys.Ingersoll@Sun.COM     return 0;
220*12720SWyllys.Ingersoll@Sun.COM 
221*12720SWyllys.Ingersoll@Sun.COM }
222*12720SWyllys.Ingersoll@Sun.COM 
KnownAnswerTestAESECB_GFSbox(void)223*12720SWyllys.Ingersoll@Sun.COM static int KnownAnswerTestAESECB_GFSbox (void)
224*12720SWyllys.Ingersoll@Sun.COM {
225*12720SWyllys.Ingersoll@Sun.COM     /*
226*12720SWyllys.Ingersoll@Sun.COM      *  Test Vectors from AES Algorithm Validation Suite(AESAVS)
227*12720SWyllys.Ingersoll@Sun.COM      */
228*12720SWyllys.Ingersoll@Sun.COM     unsigned char acKey[AES_MAXKEYBYTES];
229*12720SWyllys.Ingersoll@Sun.COM     memset(acKey, 0, sizeof (acKey));
230*12720SWyllys.Ingersoll@Sun.COM 
231*12720SWyllys.Ingersoll@Sun.COM     /*
232*12720SWyllys.Ingersoll@Sun.COM         # CAVS 6.1
233*12720SWyllys.Ingersoll@Sun.COM         # Config info for Sun 1820 AES
234*12720SWyllys.Ingersoll@Sun.COM         # AESVS GFSbox test data for ECB
235*12720SWyllys.Ingersoll@Sun.COM         # State : Encrypt and Decrypt
236*12720SWyllys.Ingersoll@Sun.COM         # Key Length : 256
237*12720SWyllys.Ingersoll@Sun.COM         # Generated on Wed Aug 13 13:39:06 2008
238*12720SWyllys.Ingersoll@Sun.COM      */
239*12720SWyllys.Ingersoll@Sun.COM     const size_t GFSboxCount = 5;
240*12720SWyllys.Ingersoll@Sun.COM     static char sPlainText[GFSboxCount][33];
241*12720SWyllys.Ingersoll@Sun.COM     static char sKnownCypherText[GFSboxCount][33];
242*12720SWyllys.Ingersoll@Sun.COM     strcpy(sPlainText[0], "014730f80ac625fe84f026c60bfd547d");
243*12720SWyllys.Ingersoll@Sun.COM     strcpy(sPlainText[1], "0b24af36193ce4665f2825d7b4749c98");
244*12720SWyllys.Ingersoll@Sun.COM     strcpy(sPlainText[2], "761c1fe41a18acf20d241650611d90f1");
245*12720SWyllys.Ingersoll@Sun.COM     strcpy(sPlainText[3], "8a560769d605868ad80d819bdba03771");
246*12720SWyllys.Ingersoll@Sun.COM     strcpy(sPlainText[4], "91fbef2d15a97816060bee1feaa49afe");
247*12720SWyllys.Ingersoll@Sun.COM 
248*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKnownCypherText[0], "5c9d844ed46f9885085e5d6a4f94c7d7");
249*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKnownCypherText[1], "a9ff75bd7cf6613d3731c77c3b6d0c04");
250*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKnownCypherText[2], "623a52fcea5d443e48d9181ab32c7421" );
251*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKnownCypherText[3], "38f2c7ae10612415d27ca190d27da8b4" );
252*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKnownCypherText[4], "1bc704f1bce135ceb810341b216d7abe" );
253*12720SWyllys.Ingersoll@Sun.COM 
254*12720SWyllys.Ingersoll@Sun.COM 
255*12720SWyllys.Ingersoll@Sun.COM     for (size_t i = 0; i < GFSboxCount; i++)
256*12720SWyllys.Ingersoll@Sun.COM     {
257*12720SWyllys.Ingersoll@Sun.COM         if (AES_ECB_TestExecution(sPlainText[i], sKnownCypherText[i], acKey) != 0)
258*12720SWyllys.Ingersoll@Sun.COM         {
259*12720SWyllys.Ingersoll@Sun.COM #ifdef KAT_DEBUG
260*12720SWyllys.Ingersoll@Sun.COM             printf("GFSbox[%d]: failed\n", i);
261*12720SWyllys.Ingersoll@Sun.COM #endif
262*12720SWyllys.Ingersoll@Sun.COM             return -1;
263*12720SWyllys.Ingersoll@Sun.COM         }
264*12720SWyllys.Ingersoll@Sun.COM #ifdef KAT_DEBUG
265*12720SWyllys.Ingersoll@Sun.COM         printf("GFSbox[%d]: passed\n", i);
266*12720SWyllys.Ingersoll@Sun.COM #endif
267*12720SWyllys.Ingersoll@Sun.COM     }
268*12720SWyllys.Ingersoll@Sun.COM     return 0;
269*12720SWyllys.Ingersoll@Sun.COM }
270*12720SWyllys.Ingersoll@Sun.COM 
KnownAnswerTestAESECB_KeySbox(void)271*12720SWyllys.Ingersoll@Sun.COM static int KnownAnswerTestAESECB_KeySbox (void)
272*12720SWyllys.Ingersoll@Sun.COM {
273*12720SWyllys.Ingersoll@Sun.COM     unsigned char acKey[AES_MAXKEYBYTES];
274*12720SWyllys.Ingersoll@Sun.COM     memset(acKey, 0, sizeof (acKey));
275*12720SWyllys.Ingersoll@Sun.COM 
276*12720SWyllys.Ingersoll@Sun.COM     /*
277*12720SWyllys.Ingersoll@Sun.COM         # CAVS 6.1
278*12720SWyllys.Ingersoll@Sun.COM         # Config info for Sun 1820 AES
279*12720SWyllys.Ingersoll@Sun.COM         # AESVS KeySbox test data for ECB
280*12720SWyllys.Ingersoll@Sun.COM         # State : Encrypt and Decrypt
281*12720SWyllys.Ingersoll@Sun.COM         # Key Length : 256
282*12720SWyllys.Ingersoll@Sun.COM         # Generated on Wed Aug 13 13:39:07 2008
283*12720SWyllys.Ingersoll@Sun.COM      */
284*12720SWyllys.Ingersoll@Sun.COM     const size_t KeySboxCount = 16;
285*12720SWyllys.Ingersoll@Sun.COM     static char sKey[KeySboxCount][65];
286*12720SWyllys.Ingersoll@Sun.COM     static char sKnownCypherText[KeySboxCount][33];
287*12720SWyllys.Ingersoll@Sun.COM     static char sPlainText[] = "00000000000000000000000000000000";
288*12720SWyllys.Ingersoll@Sun.COM 
289*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKey[0], "c47b0294dbbbee0fec4757f22ffeee3587ca4730c3d33b691df38bab076bc558");
290*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKey[1], "28d46cffa158533194214a91e712fc2b45b518076675affd910edeca5f41ac64");
291*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKey[2], "c1cc358b449909a19436cfbb3f852ef8bcb5ed12ac7058325f56e6099aab1a1c");
292*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKey[3], "984ca75f4ee8d706f46c2d98c0bf4a45f5b00d791c2dfeb191b5ed8e420fd627");
293*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKey[4], "b43d08a447ac8609baadae4ff12918b9f68fc1653f1269222f123981ded7a92f");
294*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKey[5], "1d85a181b54cde51f0e098095b2962fdc93b51fe9b88602b3f54130bf76a5bd9");
295*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKey[6], "dc0eba1f2232a7879ded34ed8428eeb8769b056bbaf8ad77cb65c3541430b4cf");
296*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKey[7], "f8be9ba615c5a952cabbca24f68f8593039624d524c816acda2c9183bd917cb9");
297*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKey[8], "797f8b3d176dac5b7e34a2d539c4ef367a16f8635f6264737591c5c07bf57a3e");
298*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKey[9], "6838d40caf927749c13f0329d331f448e202c73ef52c5f73a37ca635d4c47707");
299*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKey[10], "ccd1bc3c659cd3c59bc437484e3c5c724441da8d6e90ce556cd57d0752663bbc");
300*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKey[11], "13428b5e4c005e0636dd338405d173ab135dec2a25c22c5df0722d69dcc43887");
301*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKey[12], "07eb03a08d291d1b07408bf3512ab40c91097ac77461aad4bb859647f74f00ee");
302*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKey[13], "90143ae20cd78c5d8ebdd6cb9dc1762427a96c78c639bccc41a61424564eafe1");
303*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKey[14], "b7a5794d52737475d53d5a377200849be0260a67a2b22ced8bbef12882270d07");
304*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKey[15], "fca02f3d5011cfc5c1e23165d413a049d4526a991827424d896fe3435e0bf68e");
305*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKnownCypherText[0], "46f2fb342d6f0ab477476fc501242c5f");
306*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKnownCypherText[1], "4bf3b0a69aeb6657794f2901b1440ad4");
307*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKnownCypherText[2], "352065272169abf9856843927d0674fd");
308*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKnownCypherText[3], "4307456a9e67813b452e15fa8fffe398");
309*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKnownCypherText[4], "4663446607354989477a5c6f0f007ef4");
310*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKnownCypherText[5], "531c2c38344578b84d50b3c917bbb6e1");
311*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKnownCypherText[6], "fc6aec906323480005c58e7e1ab004ad");
312*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKnownCypherText[7], "a3944b95ca0b52043584ef02151926a8");
313*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKnownCypherText[8], "a74289fe73a4c123ca189ea1e1b49ad5");
314*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKnownCypherText[9], "b91d4ea4488644b56cf0812fa7fcf5fc");
315*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKnownCypherText[10], "304f81ab61a80c2e743b94d5002a126b");
316*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKnownCypherText[11], "649a71545378c783e368c9ade7114f6c");
317*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKnownCypherText[12], "47cb030da2ab051dfc6c4bf6910d12bb");
318*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKnownCypherText[13], "798c7c005dee432b2c8ea5dfa381ecc3");
319*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKnownCypherText[14], "637c31dc2591a07636f646b72daabbe7");
320*12720SWyllys.Ingersoll@Sun.COM     strcpy(sKnownCypherText[15], "179a49c712154bbffbe6e7a84a18e220");
321*12720SWyllys.Ingersoll@Sun.COM 
322*12720SWyllys.Ingersoll@Sun.COM     for (size_t i = 0; i < KeySboxCount; i++)
323*12720SWyllys.Ingersoll@Sun.COM     {
324*12720SWyllys.Ingersoll@Sun.COM #ifdef KAT_DEBUG
325*12720SWyllys.Ingersoll@Sun.COM         printf("KeySbox[%d]: \n", i);
326*12720SWyllys.Ingersoll@Sun.COM #endif
327*12720SWyllys.Ingersoll@Sun.COM         unsigned char acKey[256];
328*12720SWyllys.Ingersoll@Sun.COM         if ((size_t) ConvertUTF8HexStringToBinary(
329*12720SWyllys.Ingersoll@Sun.COM             sKey[i],
330*12720SWyllys.Ingersoll@Sun.COM             acKey) != strlen(sKey[i]) / 2)
331*12720SWyllys.Ingersoll@Sun.COM         {
332*12720SWyllys.Ingersoll@Sun.COM #ifdef KAT_DEBUG
333*12720SWyllys.Ingersoll@Sun.COM             printf("KeySbox[%d]: failed hex to binary conversion\n", i);
334*12720SWyllys.Ingersoll@Sun.COM #endif
335*12720SWyllys.Ingersoll@Sun.COM             return -1;
336*12720SWyllys.Ingersoll@Sun.COM         }
337*12720SWyllys.Ingersoll@Sun.COM         if (AES_ECB_TestExecution(sPlainText, sKnownCypherText[i], acKey) != 0)
338*12720SWyllys.Ingersoll@Sun.COM         {
339*12720SWyllys.Ingersoll@Sun.COM #ifdef KAT_DEBUG
340*12720SWyllys.Ingersoll@Sun.COM             printf("KeySbox[%d]: failed test\n", i);
341*12720SWyllys.Ingersoll@Sun.COM #endif
342*12720SWyllys.Ingersoll@Sun.COM             return -1;
343*12720SWyllys.Ingersoll@Sun.COM         }
344*12720SWyllys.Ingersoll@Sun.COM #ifdef KAT_DEBUG
345*12720SWyllys.Ingersoll@Sun.COM         printf("KeySbox[%d]: passed\n", i);
346*12720SWyllys.Ingersoll@Sun.COM #endif
347*12720SWyllys.Ingersoll@Sun.COM     }
348*12720SWyllys.Ingersoll@Sun.COM     return 0;
349*12720SWyllys.Ingersoll@Sun.COM }
350*12720SWyllys.Ingersoll@Sun.COM 
KnownAnswerTestHMACSHA1(void)351*12720SWyllys.Ingersoll@Sun.COM int KnownAnswerTestHMACSHA1 (void)
352*12720SWyllys.Ingersoll@Sun.COM {
353*12720SWyllys.Ingersoll@Sun.COM     /* Test Data from RFC2202 */
354*12720SWyllys.Ingersoll@Sun.COM     const static char sKey[] = "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b";
355*12720SWyllys.Ingersoll@Sun.COM     unsigned char acKey[HMAC_LENGTH];
356*12720SWyllys.Ingersoll@Sun.COM     const static char sPlainText[] = "Hi There";
357*12720SWyllys.Ingersoll@Sun.COM     const static char sCypherText[] = "b617318655057264e28bc0b6fb378c8ef146be00";
358*12720SWyllys.Ingersoll@Sun.COM     const unsigned char* aBuffersToHMAC[1];
359*12720SWyllys.Ingersoll@Sun.COM     int aBuffersToHMACSize[1];
360*12720SWyllys.Ingersoll@Sun.COM     unsigned char acCypherText[HMAC_LENGTH];
361*12720SWyllys.Ingersoll@Sun.COM     unsigned char acComputedCypherText[HMAC_LENGTH];
362*12720SWyllys.Ingersoll@Sun.COM     if ((size_t) ConvertUTF8HexStringToBinary(
363*12720SWyllys.Ingersoll@Sun.COM         sKey,
364*12720SWyllys.Ingersoll@Sun.COM         acKey) != sizeof (acKey))
365*12720SWyllys.Ingersoll@Sun.COM     {
366*12720SWyllys.Ingersoll@Sun.COM #ifdef KAT_DEBUG
367*12720SWyllys.Ingersoll@Sun.COM         printf("HMAC-SHA1: failed hex to binary conversion for Key\n");
368*12720SWyllys.Ingersoll@Sun.COM #endif
369*12720SWyllys.Ingersoll@Sun.COM         return -1;
370*12720SWyllys.Ingersoll@Sun.COM     }
371*12720SWyllys.Ingersoll@Sun.COM     if ((size_t) ConvertUTF8HexStringToBinary(
372*12720SWyllys.Ingersoll@Sun.COM         sCypherText,
373*12720SWyllys.Ingersoll@Sun.COM         acCypherText) != sizeof (acCypherText))
374*12720SWyllys.Ingersoll@Sun.COM     {
375*12720SWyllys.Ingersoll@Sun.COM #ifdef KAT_DEBUG
376*12720SWyllys.Ingersoll@Sun.COM         printf("HMAC-SHA1: failed hex to binary conversion for CypherText\n");
377*12720SWyllys.Ingersoll@Sun.COM #endif
378*12720SWyllys.Ingersoll@Sun.COM         return -1;
379*12720SWyllys.Ingersoll@Sun.COM     }
380*12720SWyllys.Ingersoll@Sun.COM 
381*12720SWyllys.Ingersoll@Sun.COM     aBuffersToHMAC[0] = (unsigned char *) sPlainText;
382*12720SWyllys.Ingersoll@Sun.COM     aBuffersToHMACSize[0] = strlen(sPlainText);
383*12720SWyllys.Ingersoll@Sun.COM 
384*12720SWyllys.Ingersoll@Sun.COM     if (!HMACBuffers(
385*12720SWyllys.Ingersoll@Sun.COM         1,
386*12720SWyllys.Ingersoll@Sun.COM         aBuffersToHMAC,
387*12720SWyllys.Ingersoll@Sun.COM         aBuffersToHMACSize,
388*12720SWyllys.Ingersoll@Sun.COM         acKey,
389*12720SWyllys.Ingersoll@Sun.COM         sizeof (acKey),
390*12720SWyllys.Ingersoll@Sun.COM         acComputedCypherText))
391*12720SWyllys.Ingersoll@Sun.COM     {
392*12720SWyllys.Ingersoll@Sun.COM #ifdef KAT_DEBUG
393*12720SWyllys.Ingersoll@Sun.COM         printf("HMAC-SHA1: failed in HMACBuffers\n");
394*12720SWyllys.Ingersoll@Sun.COM #endif
395*12720SWyllys.Ingersoll@Sun.COM         return -1;
396*12720SWyllys.Ingersoll@Sun.COM     }
397*12720SWyllys.Ingersoll@Sun.COM     if (memcmp(acCypherText, acComputedCypherText, sizeof (acCypherText)) != 0)
398*12720SWyllys.Ingersoll@Sun.COM     {
399*12720SWyllys.Ingersoll@Sun.COM #ifdef KAT_DEBUG
400*12720SWyllys.Ingersoll@Sun.COM         printf("HMAC-SHA1: failed comparison with expected cycphertext\n");
401*12720SWyllys.Ingersoll@Sun.COM #endif
402*12720SWyllys.Ingersoll@Sun.COM         return -1;
403*12720SWyllys.Ingersoll@Sun.COM     }
404*12720SWyllys.Ingersoll@Sun.COM 
405*12720SWyllys.Ingersoll@Sun.COM     return 0;
406*12720SWyllys.Ingersoll@Sun.COM }
407*12720SWyllys.Ingersoll@Sun.COM 
408*12720SWyllys.Ingersoll@Sun.COM 
KnownAnswerTestAESECB(void)409*12720SWyllys.Ingersoll@Sun.COM int KnownAnswerTestAESECB (void)
410*12720SWyllys.Ingersoll@Sun.COM {
411*12720SWyllys.Ingersoll@Sun.COM     if (KnownAnswerTestAESECB_GFSbox() != 0)
412*12720SWyllys.Ingersoll@Sun.COM     {
413*12720SWyllys.Ingersoll@Sun.COM #ifdef KAT_DEBUG
414*12720SWyllys.Ingersoll@Sun.COM         printf("GFSbox: test suite failed\n");
415*12720SWyllys.Ingersoll@Sun.COM #endif
416*12720SWyllys.Ingersoll@Sun.COM         return -1;
417*12720SWyllys.Ingersoll@Sun.COM     }
418*12720SWyllys.Ingersoll@Sun.COM 
419*12720SWyllys.Ingersoll@Sun.COM     if (KnownAnswerTestAESECB_KeySbox() != 0)
420*12720SWyllys.Ingersoll@Sun.COM     {
421*12720SWyllys.Ingersoll@Sun.COM #ifdef KAT_DEBUG
422*12720SWyllys.Ingersoll@Sun.COM         printf("KeySbox: test suite failed\n");
423*12720SWyllys.Ingersoll@Sun.COM #endif
424*12720SWyllys.Ingersoll@Sun.COM         return -1;
425*12720SWyllys.Ingersoll@Sun.COM     }
426*12720SWyllys.Ingersoll@Sun.COM 
427*12720SWyllys.Ingersoll@Sun.COM     return 0;
428*12720SWyllys.Ingersoll@Sun.COM }
429*12720SWyllys.Ingersoll@Sun.COM 
430*12720SWyllys.Ingersoll@Sun.COM #ifdef STAND_ALONE_TEST
431*12720SWyllys.Ingersoll@Sun.COM 
main()432*12720SWyllys.Ingersoll@Sun.COM int main ()
433*12720SWyllys.Ingersoll@Sun.COM {
434*12720SWyllys.Ingersoll@Sun.COM     // Known Answer Test on AES Key Wrap code
435*12720SWyllys.Ingersoll@Sun.COM     if (KnownAnswerTestAESKeyWrap() != 0)
436*12720SWyllys.Ingersoll@Sun.COM     {
437*12720SWyllys.Ingersoll@Sun.COM         return -1;
438*12720SWyllys.Ingersoll@Sun.COM     }
439*12720SWyllys.Ingersoll@Sun.COM 
440*12720SWyllys.Ingersoll@Sun.COM     if (KnownAnswerTestAESECB() != 0)
441*12720SWyllys.Ingersoll@Sun.COM     {
442*12720SWyllys.Ingersoll@Sun.COM         return -1;
443*12720SWyllys.Ingersoll@Sun.COM     }
444*12720SWyllys.Ingersoll@Sun.COM 
445*12720SWyllys.Ingersoll@Sun.COM     if (KnownAnswerTestHMACSHA1() != 0)
446*12720SWyllys.Ingersoll@Sun.COM     {
447*12720SWyllys.Ingersoll@Sun.COM         return -1;
448*12720SWyllys.Ingersoll@Sun.COM     }
449*12720SWyllys.Ingersoll@Sun.COM 
450*12720SWyllys.Ingersoll@Sun.COM     return 0;
451*12720SWyllys.Ingersoll@Sun.COM }
452*12720SWyllys.Ingersoll@Sun.COM #endif
453*12720SWyllys.Ingersoll@Sun.COM 
454*12720SWyllys.Ingersoll@Sun.COM 
455