cryptlib  3.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros
loadkey.c
Go to the documentation of this file.
1 /****************************************************************************
2 * *
3 * cryptlib Test Key Load Code *
4 * Copyright Peter Gutmann 1995-2007 *
5 * *
6 ****************************************************************************/
7 
8 #include "cryptlib.h"
9 #include "test/test.h"
10 
11 #if defined( __MVS__ ) || defined( __VMCMS__ )
12  /* Suspend conversion of literals to ASCII. */
13  #pragma convlit( suspend )
14 #endif /* IBM big iron */
15 #if defined( __ILEC400__ )
16  #pragma convert( 0 )
17 #endif /* IBM medium iron */
18 
19 /****************************************************************************
20 * *
21 * Key Data *
22 * *
23 ****************************************************************************/
24 
25 /* The keys for testing the RSA, DSA, and Elgamal implementations. The key
26  values may be extracted with the following code pasted into the
27  generateKey() function in ctx_dsa.c/ctx_rsa.c */
28 
29 #if 0
30 {
31 #include <stdio.h>
32 
34 int length, i;
35 
36 printf( "static const RSA_KEY FAR_BSS rsaTestKey = {\n" );
37 length = BN_bn2bin( &contextInfoPtr->ctxPKC->rsaParam_n, buffer );
38 printf( "\t/* n */\n\t%d,", BN_num_bits( &contextInfoPtr->ctxPKC->rsaParam_n ) );
39 for( i = 0; i < length; i++ )
40  { if( !( i % 8 ) ) printf( "\n\t " );
41  printf( "0x%02X, ", buffer[ i ] ); }
42 length = BN_bn2bin( &contextInfoPtr->ctxPKC->rsaParam_e, buffer );
43 printf( "\n\n\t/* e */\n\t%d,", BN_num_bits( &contextInfoPtr->ctxPKC->rsaParam_e ) );
44 for( i = 0; i < length; i++ )
45  { if( !( i % 8 ) ) printf( "\n\t " );
46  printf( "0x%02X, ", buffer[ i ] ); }
47 length = BN_bn2bin( &contextInfoPtr->ctxPKC->rsaParam_d, buffer );
48 printf( "\n\n\t/* d */\n\t%d,", BN_num_bits( &contextInfoPtr->ctxPKC->rsaParam_d ) );
49 for( i = 0; i < length; i++ )
50  { if( !( i % 8 ) ) printf( "\n\t " );
51  printf( "0x%02X, ", buffer[ i ] ); }
52 length = BN_bn2bin( &contextInfoPtr->ctxPKC->rsaParam_p, buffer );
53 printf( "\n\n\t/* p */\n\t%d,", BN_num_bits( &contextInfoPtr->ctxPKC->rsaParam_p ) );
54 for( i = 0; i < length; i++ )
55  { if( !( i % 8 ) ) printf( "\n\t " );
56  printf( "0x%02X, ", buffer[ i ] ); }
57 length = BN_bn2bin( &contextInfoPtr->ctxPKC->rsaParam_q, buffer );
58 printf( "\n\n\t/* q */\n\t%d,", BN_num_bits( &contextInfoPtr->ctxPKC->rsaParam_q ) );
59 for( i = 0; i < length; i++ )
60  { if( !( i % 8 ) ) printf( "\n\t " );
61  printf( "0x%02X, ", buffer[ i ] ); }
62 length = BN_bn2bin( &contextInfoPtr->ctxPKC->rsaParam_u, buffer );
63 printf( "\n\n\t/* u */\n\t%d,", BN_num_bits( &contextInfoPtr->ctxPKC->rsaParam_u ) );
64 for( i = 0; i < length; i++ )
65  { if( !( i % 8 ) ) printf( "\n\t " );
66  printf( "0x%02X, ", buffer[ i ] ); }
67 length = BN_bn2bin( &contextInfoPtr->ctxPKC->rsaParam_exponent1, buffer );
68 printf( "\n\n\t/* exponent1 */\n\t%d,", BN_num_bits( &contextInfoPtr->ctxPKC->rsaParam_exponent1 ) );
69 for( i = 0; i < length; i++ )
70  { if( !( i % 8 ) ) printf( "\n\t " );
71  printf( "0x%02X, ", buffer[ i ] ); }
72 length = BN_bn2bin( &contextInfoPtr->ctxPKC->rsaParam_exponent2, buffer );
73 printf( "\n\n\t/* exponent2 */\n\t%d,", BN_num_bits( &contextInfoPtr->ctxPKC->rsaParam_exponent2 ) );
74 for( i = 0; i < length; i++ )
75  { if( !( i % 8 ) ) printf( "\n\t " );
76  printf( "0x%02X, ", buffer[ i ] ); }
77 puts( "\n\t};" );
78 fflush( stdout );
79 }
80 #endif
81 #if 0
82 {
83 #include <stdio.h>
84 
85 BYTE buffer[ CRYPT_MAX_PKCSIZE ];
86 int length, i;
87 
88 printf( "static const DLP_KEY FAR_BSS dlpTestKey = {\n" );
89 length = BN_bn2bin( &contextInfoPtr->ctxPKC->dlpParam_p, buffer );
90 printf( "\t/* p */\n\t%d,", BN_num_bits( &contextInfoPtr->ctxPKC->dlpParam_p ) );
91 for( i = 0; i < length; i++ )
92  { if( !( i % 8 ) ) printf( "\n\t " );
93  printf( "0x%02X, ", buffer[ i ] ); }
94 length = BN_bn2bin( &contextInfoPtr->ctxPKC->dlpParam_q, buffer );
95 printf( "\n\n\t/* q */\n\t%d,", BN_num_bits( &contextInfoPtr->ctxPKC->dlpParam_q ) );
96 for( i = 0; i < length; i++ )
97  { if( !( i % 8 ) ) printf( "\n\t " );
98  printf( "0x%02X, ", buffer[ i ] ); }
99 length = BN_bn2bin( &contextInfoPtr->ctxPKC->dlpParam_g, buffer );
100 printf( "\n\n\t/* g */\n\t%d,", BN_num_bits( &contextInfoPtr->ctxPKC->dlpParam_g ) );
101 for( i = 0; i < length; i++ )
102  { if( !( i % 8 ) ) printf( "\n\t " );
103  printf( "0x%02X, ", buffer[ i ] ); }
104 length = BN_bn2bin( &contextInfoPtr->ctxPKC->dlpParam_x, buffer );
105 printf( "\n\n\t/* x */\n\t%d,", BN_num_bits( &contextInfoPtr->ctxPKC->dlpParam_x ) );
106 for( i = 0; i < length; i++ )
107  { if( !( i % 8 ) ) printf( "\n\t " );
108  printf( "0x%02X, ", buffer[ i ] ); }
109 length = BN_bn2bin( &contextInfoPtr->ctxPKC->dlpParam_y, buffer );
110 printf( "\n\n\t/* y */\n\t%d,", BN_num_bits( &contextInfoPtr->ctxPKC->dlpParam_y ) );
111 for( i = 0; i < length; i++ )
112  { if( !( i % 8 ) ) printf( "\n\t " );
113  printf( "0x%02X, ", buffer[ i ] ); }
114 puts( "\n\t};" );
115 fflush( stdout );
116 }
117 #endif
118 
119 typedef struct {
120  const int nLen; const BYTE n[ 256 ];
121  const int eLen; const BYTE e[ 3 ];
122  const int dLen; const BYTE d[ 256 ];
123  const int pLen; const BYTE p[ 128 ];
124  const int qLen; const BYTE q[ 128 ];
125  const int uLen; const BYTE u[ 128 ];
126  const int e1Len; const BYTE e1[ 128 ];
127  const int e2Len; const BYTE e2[ 128 ];
128  } RSA_KEY;
129 
130 static const RSA_KEY FAR_BSS rsa1024TestKey = {
131  /* n */
132  1024,
133  { 0x9C, 0x4D, 0x98, 0x18, 0x67, 0xF9, 0x45, 0xBC,
134  0xB6, 0x75, 0x53, 0x5D, 0x2C, 0xFA, 0x55, 0xE4,
135  0x51, 0x54, 0x9F, 0x0C, 0x16, 0xB1, 0xAF, 0x89,
136  0xF6, 0xF3, 0xE7, 0x78, 0xB1, 0x2B, 0x07, 0xFB,
137  0xDC, 0xDE, 0x64, 0x23, 0x34, 0x87, 0xDA, 0x0B,
138  0xE5, 0xB3, 0x17, 0x16, 0xA4, 0xE3, 0x7F, 0x23,
139  0xDF, 0x96, 0x16, 0x28, 0xA6, 0xD2, 0xF0, 0x0A,
140  0x59, 0xEE, 0x06, 0xB3, 0x76, 0x6C, 0x64, 0x19,
141  0xD9, 0x76, 0x41, 0x25, 0x66, 0xD1, 0x93, 0x51,
142  0x52, 0x06, 0x6B, 0x71, 0x50, 0x0E, 0xAB, 0x30,
143  0xA5, 0xC8, 0x41, 0xFC, 0x30, 0xBC, 0x32, 0xD7,
144  0x4B, 0x22, 0xF2, 0x45, 0x4C, 0x94, 0x68, 0xF1,
145  0x92, 0x8A, 0x4C, 0xF9, 0xD4, 0x5E, 0x87, 0x92,
146  0xA8, 0x54, 0x93, 0x92, 0x94, 0x48, 0xA4, 0xA3,
147  0xEE, 0x19, 0x7F, 0x6E, 0xD3, 0x14, 0xB1, 0x48,
148  0xCE, 0x93, 0xD1, 0xEA, 0x4C, 0xE1, 0x9D, 0xEF },
149 
150  /* e */
151  17,
152  { 0x01, 0x00, 0x01 },
153 
154  /* d */
155  1022,
156  { 0x37, 0xE2, 0x66, 0x67, 0x13, 0x85, 0xC4, 0xB1,
157  0x5C, 0x6B, 0x46, 0x8B, 0x21, 0xF1, 0xBF, 0x94,
158  0x0A, 0xA0, 0x3E, 0xDD, 0x8B, 0x9F, 0xAC, 0x2B,
159  0x9F, 0xE8, 0x44, 0xF2, 0x9A, 0x25, 0xD0, 0x8C,
160  0xF4, 0xC3, 0x6E, 0xFA, 0x47, 0x65, 0xEB, 0x48,
161  0x25, 0xB0, 0x8A, 0xA8, 0xC5, 0xFB, 0xB1, 0x11,
162  0x9A, 0x77, 0x87, 0x24, 0xB1, 0xC0, 0xE9, 0xA2,
163  0x49, 0xD5, 0x19, 0x00, 0x41, 0x6F, 0x2F, 0xBA,
164  0x9F, 0x28, 0x47, 0xF9, 0xB8, 0xBA, 0xFF, 0xF4,
165  0x8B, 0x20, 0xC9, 0xC9, 0x39, 0xAB, 0x52, 0x0E,
166  0x8A, 0x5A, 0xAF, 0xB3, 0xA3, 0x93, 0x4D, 0xBB,
167  0xFE, 0x62, 0x9B, 0x02, 0xCC, 0xA7, 0xB4, 0xAE,
168  0x86, 0x65, 0x88, 0x19, 0xD7, 0x44, 0xA7, 0xE4,
169  0x18, 0xB6, 0xCE, 0x01, 0xCD, 0xDF, 0x36, 0x81,
170  0xD5, 0xE1, 0x62, 0xF8, 0xD0, 0x27, 0xF1, 0x86,
171  0xA8, 0x58, 0xA7, 0xEB, 0x39, 0x79, 0x56, 0x41 },
172 
173  /* p */
174  512,
175  { 0xCF, 0xDA, 0xF9, 0x99, 0x6F, 0x05, 0x95, 0x84,
176  0x09, 0x90, 0xB3, 0xAB, 0x39, 0xB7, 0xDD, 0x1D,
177  0x7B, 0xFC, 0xFD, 0x10, 0x35, 0xA0, 0x18, 0x1D,
178  0x9A, 0x11, 0x30, 0x90, 0xD4, 0x3B, 0xF0, 0x5A,
179  0xC1, 0xA6, 0xF4, 0x53, 0xD0, 0x94, 0xA0, 0xED,
180  0xE0, 0xE4, 0xE0, 0x8E, 0x44, 0x18, 0x42, 0x42,
181  0xE1, 0x2C, 0x0D, 0xF7, 0x30, 0xE2, 0xB8, 0x09,
182  0x73, 0x50, 0x28, 0xF6, 0x55, 0x85, 0x57, 0x03 },
183 
184  /* q */
185  512,
186  { 0xC0, 0x81, 0xC4, 0x82, 0x6E, 0xF6, 0x1C, 0x92,
187  0x83, 0xEC, 0x17, 0xFB, 0x30, 0x98, 0xED, 0x6E,
188  0x89, 0x92, 0xB2, 0xA1, 0x21, 0x0D, 0xC1, 0x95,
189  0x49, 0x99, 0xD3, 0x79, 0xD3, 0xBD, 0x94, 0x93,
190  0xB9, 0x28, 0x68, 0xFF, 0xDE, 0xEB, 0xE8, 0xD2,
191  0x0B, 0xED, 0x7C, 0x08, 0xD0, 0xD5, 0x59, 0xE3,
192  0xC1, 0x76, 0xEA, 0xC1, 0xCD, 0xB6, 0x8B, 0x39,
193  0x4E, 0x29, 0x59, 0x5F, 0xFA, 0xCE, 0x83, 0xA5 },
194 
195  /* u */
196  511,
197  { 0x4B, 0x87, 0x97, 0x1F, 0x27, 0xED, 0xAA, 0xAF,
198  0x42, 0xF4, 0x57, 0x82, 0x3F, 0xEC, 0x80, 0xED,
199  0x1E, 0x91, 0xF8, 0xB4, 0x33, 0xDA, 0xEF, 0xC3,
200  0x03, 0x53, 0x0F, 0xCE, 0xB9, 0x5F, 0xE4, 0x29,
201  0xCC, 0xEE, 0x6A, 0x5E, 0x11, 0x0E, 0xFA, 0x66,
202  0x85, 0xDC, 0xFC, 0x48, 0x31, 0x0C, 0x00, 0x97,
203  0xC6, 0x0A, 0xF2, 0x34, 0x60, 0x6B, 0xF7, 0x68,
204  0x09, 0x4E, 0xCF, 0xB1, 0x9E, 0x33, 0x9A, 0x41 },
205 
206  /* exponent1 */
207  511,
208  { 0x6B, 0x2A, 0x0D, 0xF8, 0x22, 0x7A, 0x71, 0x8C,
209  0xE2, 0xD5, 0x9D, 0x1C, 0x91, 0xA4, 0x8F, 0x37,
210  0x0D, 0x5E, 0xF1, 0x26, 0x73, 0x4F, 0x78, 0x3F,
211  0x82, 0xD8, 0x8B, 0xFE, 0x8F, 0xBD, 0xDB, 0x7D,
212  0x1F, 0x4C, 0xB1, 0xB9, 0xA8, 0xD7, 0x88, 0x65,
213  0x3C, 0xC7, 0x24, 0x53, 0x95, 0x1E, 0x20, 0xC3,
214  0x94, 0x8E, 0x7F, 0x20, 0xCC, 0x2E, 0x88, 0x0E,
215  0x2F, 0x4A, 0xCB, 0xE3, 0xBD, 0x52, 0x02, 0xFB },
216 
217  /* exponent2 */
218  509,
219  { 0x10, 0x27, 0xD3, 0xD2, 0x0E, 0x75, 0xE1, 0x17,
220  0xFA, 0xB2, 0x49, 0xA0, 0xEF, 0x07, 0x26, 0x85,
221  0xEC, 0x4D, 0xBF, 0x67, 0xFE, 0x5A, 0x25, 0x30,
222  0xDE, 0x28, 0x66, 0xB3, 0x06, 0xAE, 0x16, 0x55,
223  0xFF, 0x68, 0x00, 0xC7, 0xD8, 0x71, 0x7B, 0xEC,
224  0x84, 0xCB, 0xBD, 0x69, 0x0F, 0xFD, 0x97, 0xB9,
225  0xA1, 0x76, 0xD5, 0x64, 0xC6, 0x5A, 0xD7, 0x7C,
226  0x4B, 0xAE, 0xF4, 0xAD, 0x35, 0x63, 0x37, 0x71 }
227  };
228 
229 static const RSA_KEY FAR_BSS rsa2048TestKey = {
230  /* n */
231  2048,
232  { 0xBC, 0xB9, 0x69, 0xBE, 0x8A, 0xDE, 0x29, 0x4B,
233  0xFC, 0x0C, 0xCA, 0x10, 0xBC, 0x16, 0x30, 0x9B,
234  0x78, 0xE2, 0xDB, 0x18, 0x61, 0x4F, 0x2C, 0xD8,
235  0x88, 0xFD, 0xC8, 0x59, 0xD0, 0x24, 0x95, 0x79,
236  0x95, 0x33, 0x8E, 0x41, 0xAB, 0x8E, 0x36, 0x6A,
237  0xB1, 0xAA, 0xE7, 0xA0, 0x06, 0x70, 0xA8, 0x47,
238  0xC3, 0xA7, 0xFB, 0x3F, 0xA6, 0x62, 0xC2, 0xD1,
239  0x9B, 0xA2, 0x7A, 0x93, 0x65, 0x02, 0x31, 0x5C,
240  0x61, 0x18, 0xF3, 0x61, 0x4E, 0x11, 0x1F, 0x77,
241  0xDB, 0x86, 0x11, 0x73, 0xA3, 0x7A, 0x30, 0x46,
242  0x10, 0xE3, 0x23, 0xE2, 0x9E, 0x41, 0x23, 0xA2,
243  0x32, 0x25, 0x1D, 0x9B, 0x15, 0x64, 0x52, 0x88,
244  0x97, 0x70, 0x3D, 0x83, 0x0A, 0xAC, 0x47, 0x2E,
245  0x0E, 0xFE, 0x56, 0xBA, 0x2E, 0x3A, 0xEB, 0xE7,
246  0x56, 0xDC, 0x60, 0xA2, 0xEA, 0xBF, 0xFC, 0xD0,
247  0xAD, 0xED, 0xB3, 0xC2, 0x6B, 0x0E, 0xAE, 0x00,
248  0x2A, 0x19, 0xE1, 0xFD, 0x85, 0x14, 0x53, 0xA6,
249  0xD4, 0xBE, 0x7D, 0xF0, 0xC8, 0x39, 0xFF, 0x07,
250  0x58, 0x79, 0x52, 0x1E, 0xE4, 0xF9, 0xC9, 0xE1,
251  0x82, 0xD1, 0xF6, 0x1B, 0x8B, 0x69, 0x26, 0x2E,
252  0x9F, 0x8D, 0xDE, 0xB5, 0x9A, 0x9C, 0xAF, 0x7A,
253  0x61, 0x00, 0xFC, 0x7B, 0xC1, 0x47, 0x34, 0xBB,
254  0x49, 0x1C, 0x99, 0x4D, 0x63, 0xC3, 0x47, 0xE8,
255  0xE9, 0x75, 0x08, 0xD0, 0xE2, 0x9D, 0xA6, 0x56,
256  0x64, 0x7D, 0xCE, 0x62, 0x7B, 0xE9, 0x4E, 0xE5,
257  0x25, 0xA2, 0xAF, 0x36, 0xC4, 0x95, 0x17, 0xA5,
258  0xE9, 0x52, 0x0D, 0xF6, 0x29, 0x1E, 0xAE, 0xC5,
259  0x6C, 0x43, 0x00, 0x02, 0xE3, 0x03, 0xE5, 0x2C,
260  0x12, 0x63, 0x7E, 0x5E, 0x97, 0x93, 0x7D, 0x35,
261  0xBB, 0x58, 0xD0, 0xD1, 0x74, 0x3B, 0xC0, 0x2D,
262  0x86, 0x66, 0x1B, 0x81, 0x45, 0x64, 0xE5, 0xE0,
263  0xAC, 0x39, 0x22, 0xEA, 0x25, 0x5B, 0x7D, 0x41 },
264 
265  /* e */
266  17,
267  { 0x01, 0x00, 0x01 },
268 
269  /* d */
270  2047,
271  { 0x4A, 0x93, 0x15, 0xD7, 0x06, 0x76, 0xDD, 0x68,
272  0xBA, 0x33, 0xAF, 0x91, 0x47, 0x51, 0x99, 0x69,
273  0x86, 0x2E, 0x56, 0x97, 0x5E, 0xB4, 0x73, 0xAB,
274  0x29, 0x0E, 0xE7, 0xA1, 0x9D, 0xDF, 0x26, 0xF5,
275  0xC1, 0x60, 0x7B, 0x01, 0x36, 0x32, 0x5F, 0x0C,
276  0x70, 0x22, 0x71, 0x9E, 0xC9, 0x8C, 0xB0, 0xE2,
277  0x92, 0xCD, 0x09, 0x3A, 0x50, 0x9C, 0x4C, 0x11,
278  0x99, 0xE7, 0x6D, 0x7A, 0x5A, 0xFA, 0xAF, 0xD8,
279  0xAA, 0x41, 0xBA, 0xC2, 0xA3, 0x9F, 0x9E, 0x88,
280  0xB5, 0x45, 0x58, 0x16, 0x12, 0xEE, 0x50, 0xAF,
281  0x0F, 0x33, 0x42, 0x55, 0xE5, 0x65, 0x26, 0x18,
282  0x8C, 0xAA, 0x1F, 0xF5, 0xCD, 0x51, 0x34, 0x4F,
283  0xE7, 0x4F, 0xA1, 0xEE, 0xEB, 0x43, 0xC2, 0x68,
284  0xCF, 0xE1, 0xEF, 0x11, 0x68, 0xFD, 0x3D, 0x84,
285  0xCF, 0xCD, 0x0B, 0x9E, 0xB5, 0x16, 0xA2, 0x67,
286  0x2F, 0xA3, 0x73, 0x5A, 0x0B, 0x05, 0xE4, 0xF9,
287  0x5B, 0x60, 0xE1, 0xEF, 0x7A, 0x12, 0x3C, 0xFC,
288  0xF7, 0xD6, 0x34, 0x09, 0x77, 0x6D, 0xF3, 0x70,
289  0x34, 0x16, 0x35, 0x26, 0xB4, 0xF8, 0x5F, 0x05,
290  0xB9, 0xB2, 0x7F, 0x07, 0x17, 0x62, 0x9E, 0x1C,
291  0x96, 0xCC, 0x9B, 0xF3, 0x19, 0xE5, 0xDA, 0x86,
292  0x0A, 0x91, 0x06, 0x8B, 0xB6, 0xCF, 0x21, 0xD8,
293  0x68, 0x9C, 0x5F, 0xF1, 0xA7, 0xF4, 0x80, 0x7B,
294  0x09, 0xC5, 0x64, 0x1D, 0x3D, 0xDD, 0x68, 0x66,
295  0x55, 0x6C, 0xDD, 0xED, 0x48, 0x43, 0x3B, 0x19,
296  0x84, 0x6F, 0x8E, 0x87, 0x3E, 0x6D, 0xEB, 0xE5,
297  0x28, 0x22, 0xEA, 0xE1, 0x3F, 0x44, 0xDF, 0x40,
298  0xE3, 0xED, 0xDB, 0x0D, 0x6C, 0x96, 0x5C, 0x41,
299  0xD7, 0x32, 0xF7, 0x12, 0x4A, 0xA3, 0x01, 0xD2,
300  0x23, 0x79, 0x0D, 0xE6, 0xAF, 0x33, 0x8D, 0xDE,
301  0x88, 0x32, 0x14, 0x56, 0x60, 0x03, 0x9A, 0x93,
302  0x05, 0x2E, 0xFC, 0xB4, 0x3F, 0xB9, 0xA2, 0x31 },
303 
304  /* p */
305  1024,
306  { 0xEE, 0x5B, 0x04, 0xEB, 0x5C, 0x16, 0x87, 0x89,
307  0x6C, 0xF7, 0xD4, 0xDC, 0x42, 0x6D, 0xCD, 0xCA,
308  0x3C, 0x9B, 0x7C, 0x4A, 0x94, 0x1F, 0xAD, 0xFA,
309  0xCC, 0x6F, 0xE1, 0x7E, 0xA0, 0x50, 0x48, 0xBF,
310  0xA7, 0x6A, 0x66, 0x66, 0x81, 0x1E, 0x79, 0xF9,
311  0xE8, 0x45, 0xBB, 0xF4, 0xC9, 0xE1, 0x85, 0x38,
312  0xDB, 0xF2, 0x7F, 0x77, 0x66, 0x42, 0xB4, 0xAD,
313  0xFA, 0xFC, 0xFF, 0xE4, 0x7D, 0xC6, 0xB0, 0x4B,
314  0xB2, 0xAD, 0x2A, 0x16, 0x2E, 0x22, 0x97, 0x8D,
315  0x98, 0xEF, 0x7C, 0x96, 0xA3, 0x7B, 0xF0, 0x5F,
316  0x40, 0x4E, 0xE7, 0x66, 0x06, 0x60, 0x25, 0x5B,
317  0x36, 0x2E, 0x6A, 0x37, 0xEF, 0xC7, 0xF6, 0xDC,
318  0x34, 0xC6, 0xC4, 0x40, 0x92, 0xC1, 0x20, 0x91,
319  0x5C, 0xB8, 0xEB, 0xBD, 0xA5, 0xF5, 0x7B, 0x54,
320  0x93, 0x20, 0x49, 0x02, 0x4F, 0xA4, 0x0A, 0x7F,
321  0xD4, 0x76, 0x6F, 0x6D, 0x92, 0x8B, 0xD0, 0xDD },
322 
323  /* q */
324  1024,
325  { 0xCA, 0xB1, 0xD9, 0xF4, 0x19, 0x4B, 0xDC, 0x6E,
326  0xF8, 0x88, 0xED, 0x8C, 0x93, 0x3F, 0x56, 0x79,
327  0xA4, 0x2E, 0x54, 0x3B, 0xD5, 0x69, 0xCD, 0x4F,
328  0xE0, 0x0C, 0x2B, 0x82, 0xEC, 0xFF, 0x7C, 0x05,
329  0x83, 0x7D, 0x68, 0xB6, 0x7B, 0x51, 0x8B, 0x00,
330  0x3F, 0x90, 0x63, 0x62, 0xA9, 0xFC, 0xA0, 0xBC,
331  0x47, 0xD9, 0x7B, 0x0E, 0xC3, 0x3A, 0x0C, 0x89,
332  0x96, 0x23, 0xAC, 0xBA, 0x09, 0xCF, 0x2D, 0xC2,
333  0x0A, 0x0C, 0xA1, 0xDD, 0x63, 0x82, 0x4B, 0xDC,
334  0x88, 0x0D, 0x9B, 0xF4, 0x9C, 0x0F, 0x5F, 0xA6,
335  0xB4, 0xA2, 0xFE, 0xFB, 0x08, 0xB7, 0x0E, 0xED,
336  0xA5, 0x04, 0x13, 0xAB, 0x6B, 0x66, 0xF1, 0x85,
337  0xD0, 0xB1, 0xAB, 0xDC, 0x2F, 0x7E, 0x61, 0xB1,
338  0x28, 0x87, 0xBD, 0xC6, 0xA3, 0xD5, 0xA0, 0x04,
339  0xD4, 0x9F, 0x49, 0xF9, 0xA9, 0xB4, 0xB1, 0x33,
340  0x8C, 0xFA, 0x62, 0xDE, 0x9A, 0x16, 0x85, 0xB5 },
341 
342  /* u */
343  1023,
344  { 0x5F, 0x6C, 0x5D, 0x2D, 0x73, 0x9C, 0x2B, 0x8C,
345  0x1C, 0x8B, 0x96, 0x6C, 0x84, 0xDB, 0x21, 0x86,
346  0xF1, 0xEA, 0x75, 0xE1, 0xC5, 0x96, 0x23, 0x38,
347  0x82, 0x02, 0x77, 0x8B, 0xFA, 0x79, 0xE3, 0xA2,
348  0x88, 0x0C, 0x38, 0x9C, 0x56, 0x44, 0xAA, 0x1C,
349  0x20, 0xB2, 0x9E, 0x9D, 0xD9, 0xAE, 0x95, 0x6B,
350  0x43, 0xB1, 0x4C, 0x38, 0x88, 0x9C, 0x12, 0x4E,
351  0x7C, 0x1F, 0xD1, 0x41, 0x6E, 0x11, 0xAA, 0x48,
352  0x7A, 0xAC, 0x56, 0xF0, 0x6B, 0xA3, 0x35, 0xE4,
353  0x6B, 0xC0, 0xF4, 0x33, 0x74, 0xC0, 0x1E, 0xC4,
354  0x04, 0x44, 0xF0, 0x24, 0x32, 0x83, 0xB0, 0xC7,
355  0xAC, 0x5C, 0x6D, 0x29, 0x87, 0x1A, 0xA7, 0x03,
356  0x7B, 0x61, 0x69, 0x18, 0xA1, 0x26, 0x00, 0x0A,
357  0x96, 0x3A, 0x56, 0x1A, 0xA0, 0xFC, 0xE2, 0x4C,
358  0x51, 0xE0, 0x8F, 0xE5, 0x68, 0x69, 0xEE, 0xE8,
359  0xA9, 0x1B, 0x24, 0x0D, 0x55, 0x55, 0x98, 0x72 },
360 
361  /* exponent1 */
362  1023,
363  { 0x71, 0xC3, 0x9F, 0xA5, 0x76, 0x5E, 0x8A, 0x72,
364  0x5D, 0x40, 0x2C, 0xA8, 0xB4, 0x4C, 0x14, 0x5C,
365  0xE2, 0x70, 0x93, 0xF2, 0x44, 0xA0, 0x9A, 0x39,
366  0x8A, 0x3A, 0x1C, 0x36, 0x83, 0xED, 0xCD, 0xCB,
367  0x2B, 0xEC, 0xEC, 0xD3, 0x1F, 0xED, 0x9B, 0xEA,
368  0x5B, 0xA2, 0x6D, 0x03, 0x79, 0x17, 0xDA, 0xAE,
369  0x38, 0xCC, 0x95, 0x6A, 0x37, 0xB4, 0xBE, 0xE8,
370  0x0B, 0x53, 0x96, 0x0F, 0x48, 0xB8, 0xFC, 0x8C,
371  0x24, 0xCB, 0xE1, 0xBA, 0x94, 0x6A, 0x8E, 0x4B,
372  0x57, 0x23, 0x77, 0x23, 0xAF, 0x04, 0x08, 0xC6,
373  0x6D, 0xBB, 0x3B, 0x56, 0xC6, 0xD4, 0x3D, 0x00,
374  0x3B, 0xEC, 0x0B, 0x66, 0x87, 0x5B, 0xB9, 0xC7,
375  0x80, 0xA9, 0x1E, 0x22, 0x73, 0xE9, 0x19, 0xD2,
376  0x47, 0x9F, 0x3B, 0x65, 0x59, 0x40, 0xC1, 0x8C,
377  0xAC, 0x6C, 0x4C, 0x6C, 0x7D, 0xF6, 0x9D, 0xCC,
378  0x45, 0x6C, 0x01, 0xE3, 0x1A, 0x7F, 0x01, 0x41 },
379 
380  /* exponent2 */
381  1022,
382  { 0x32, 0x07, 0x93, 0xF4, 0x5C, 0x0A, 0x0D, 0x6E,
383  0x96, 0x89, 0xB5, 0x98, 0x6C, 0xFF, 0xC5, 0x28,
384  0x61, 0x0D, 0xCE, 0x5C, 0xB6, 0x60, 0x56, 0xFC,
385  0xD0, 0x20, 0x30, 0xDD, 0x30, 0x02, 0x1F, 0x6A,
386  0x7C, 0xFA, 0x07, 0x4E, 0x83, 0x41, 0xAD, 0x3D,
387  0x72, 0x73, 0x01, 0x14, 0xE9, 0x40, 0x21, 0xAC,
388  0x57, 0x4F, 0xA6, 0xC0, 0x0E, 0x0F, 0xD5, 0xE8,
389  0x5F, 0xD6, 0x8E, 0x5B, 0x9C, 0xF0, 0x36, 0x2B,
390  0x1E, 0xAF, 0xDF, 0x83, 0xF5, 0x7B, 0xC3, 0x9D,
391  0xBB, 0x37, 0x20, 0xB7, 0x4F, 0x8D, 0xBB, 0xDE,
392  0x39, 0xD7, 0xC1, 0x77, 0xD4, 0xBE, 0xDA, 0x40,
393  0x6D, 0xEA, 0x83, 0xB0, 0x5B, 0xE2, 0x1C, 0xDB,
394  0x1A, 0x97, 0xC0, 0x03, 0xA8, 0xF5, 0x58, 0xC7,
395  0x91, 0x69, 0x6F, 0xBE, 0x07, 0xD2, 0x42, 0x9C,
396  0xEE, 0x9E, 0x22, 0x74, 0x2D, 0x1F, 0x1C, 0x5D,
397  0xCC, 0xFE, 0x40, 0x49, 0x11, 0x5F, 0x5D, 0xC1 }
398  };
399 
400 typedef struct {
401  const int pLen; const BYTE p[ 128 ];
402  const int qLen; const BYTE q[ 20 ];
403  const int gLen; const BYTE g[ 128 ];
404  const int xLen; const BYTE x[ 20 ];
405  const int yLen; const BYTE y[ 128 ];
406  } DLP_KEY;
407 
408 static const DLP_KEY FAR_BSS dlp1024TestKey = {
409  /* p */
410  1024,
411  { 0x04, 0x4C, 0xDD, 0x5D, 0xB6, 0xED, 0x23, 0xAE,
412  0xB2, 0xA7, 0x59, 0xE6, 0xF8, 0x3D, 0xA6, 0x27,
413  0x85, 0xF2, 0xFE, 0xE2, 0xE8, 0xF3, 0xDA, 0xA3,
414  0x7B, 0xD6, 0x48, 0xD4, 0x44, 0xCA, 0x6E, 0x10,
415  0x97, 0x6C, 0x1D, 0x6C, 0x39, 0xA7, 0x0C, 0x88,
416  0x8E, 0x1F, 0xDD, 0xF7, 0x59, 0x69, 0xDA, 0x36,
417  0xDD, 0xB8, 0x3E, 0x1A, 0xD2, 0x91, 0x3E, 0x30,
418  0xB1, 0xB5, 0xC2, 0xBC, 0xA9, 0xA3, 0xA5, 0xDE,
419  0xC7, 0xCF, 0x51, 0x2C, 0x1B, 0x89, 0xD0, 0x71,
420  0xE3, 0x71, 0xBB, 0x50, 0x86, 0x26, 0x32, 0x9F,
421  0xF5, 0x4A, 0x9C, 0xB1, 0x78, 0x7B, 0x47, 0x1F,
422  0x19, 0xC7, 0x26, 0x22, 0x15, 0x62, 0x71, 0xAB,
423  0xD7, 0x25, 0xA5, 0xE4, 0x68, 0x71, 0x93, 0x5D,
424  0x1F, 0x29, 0x01, 0x05, 0x9C, 0x57, 0x3A, 0x09,
425  0xB0, 0xB8, 0xE4, 0xD2, 0x37, 0x90, 0x36, 0x2F,
426  0xBF, 0x1E, 0x74, 0xB4, 0x6B, 0xE4, 0x66, 0x07 },
427 
428  /* q */
429  160,
430  { 0xFD, 0xD9, 0xC8, 0x5F, 0x73, 0x62, 0xC9, 0x79,
431  0xEF, 0xD5, 0x09, 0x07, 0x02, 0xE7, 0xF2, 0x90,
432  0x97, 0x13, 0x26, 0x1D },
433 
434  /* g */
435  1024,
436  { 0x02, 0x4E, 0xDD, 0x0D, 0x7F, 0x4D, 0xB1, 0x42,
437  0x01, 0x50, 0xE7, 0x9A, 0x65, 0x73, 0x8B, 0x31,
438  0x24, 0x6B, 0xC6, 0x74, 0xA7, 0x68, 0x26, 0x11,
439  0x06, 0x3C, 0x96, 0xA9, 0xA6, 0x23, 0x12, 0x79,
440  0xC4, 0xEE, 0x21, 0x88, 0xDD, 0xE3, 0xF0, 0x37,
441  0xCE, 0x3E, 0x54, 0x53, 0x57, 0x03, 0x30, 0xE4,
442  0xD3, 0xAB, 0x39, 0x4E, 0x39, 0xDC, 0xA2, 0x88,
443  0x82, 0xF6, 0xE8, 0xBA, 0xAC, 0xF5, 0x7D, 0x2F,
444  0x23, 0x9A, 0x09, 0x94, 0xB2, 0x89, 0xA2, 0xC9,
445  0x7C, 0xBE, 0x4D, 0x48, 0x0E, 0x59, 0x51, 0xB8,
446  0x7D, 0x99, 0x88, 0x79, 0xA8, 0x13, 0x0E, 0x12,
447  0x56, 0x9D, 0x4B, 0x2E, 0xE0, 0xE1, 0x37, 0x78,
448  0x6F, 0xCC, 0x4D, 0x97, 0xA9, 0x02, 0x0E, 0xD2,
449  0x43, 0x83, 0xEC, 0x4F, 0xC2, 0x70, 0xEF, 0x16,
450  0xDE, 0xBF, 0xBA, 0xD1, 0x6C, 0x8A, 0x36, 0xEE,
451  0x42, 0x41, 0xE9, 0xE7, 0x66, 0xAE, 0x46, 0x3B },
452 
453  /* x */
454  160,
455  { 0xD9, 0x41, 0x29, 0xF7, 0x40, 0x32, 0x09, 0x71,
456  0xB8, 0xE2, 0xB8, 0xCB, 0x74, 0x46, 0x0B, 0xD4,
457  0xF2, 0xAB, 0x54, 0xA1 },
458 
459  /* y */
460  1024,
461  { 0x01, 0x7E, 0x16, 0x5B, 0x65, 0x51, 0x0A, 0xDA,
462  0x82, 0x1A, 0xD9, 0xF4, 0x1E, 0x66, 0x6D, 0x7D,
463  0x23, 0xA6, 0x28, 0x2F, 0xE6, 0xC2, 0x03, 0x8E,
464  0x8C, 0xAB, 0xC2, 0x08, 0x87, 0xC9, 0xE8, 0x51,
465  0x0A, 0x37, 0x1E, 0xD4, 0x41, 0x7F, 0xA2, 0xC5,
466  0x48, 0x26, 0xB7, 0xF6, 0xC2, 0x6F, 0xB2, 0xF8,
467  0xF9, 0x43, 0x43, 0xF9, 0xDA, 0xAB, 0xA2, 0x59,
468  0x27, 0xBA, 0xC9, 0x1C, 0x8C, 0xAB, 0xC4, 0x90,
469  0x27, 0xE1, 0x10, 0x39, 0x6F, 0xD2, 0xCD, 0x7C,
470  0xD1, 0x0B, 0xFA, 0x28, 0xD2, 0x7A, 0x7B, 0x52,
471  0x8A, 0xA0, 0x5A, 0x0F, 0x10, 0xF7, 0xBA, 0xFD,
472  0x33, 0x0C, 0x3C, 0xCE, 0xE5, 0xF2, 0xF6, 0x92,
473  0xED, 0x04, 0xBF, 0xD3, 0xF8, 0x3D, 0x39, 0xCC,
474  0xAA, 0xCC, 0x0B, 0xB2, 0x6B, 0xD8, 0xB2, 0x8A,
475  0x5C, 0xCE, 0xDA, 0xF9, 0xE1, 0xA7, 0x23, 0x50,
476  0xDC, 0xCE, 0xA4, 0xD5, 0xA5, 0x4F, 0x08, 0x0F }
477  };
478 
479 /* The DH key uses cryptlib-internal mechanisms, the following data and
480  associated test can't be used with an unmodified version of cryptlib */
481 
482 #ifdef TEST_DH
483 
484 #define CRYPT_IATTRIBUTE_KEY_SPKI 8015
485 
486 static const BYTE FAR_BSS dh1024SPKI[] = {
487  0x30, 0x82, 0x01, 0x21,
488  0x30, 0x82, 0x01, 0x17,
489  0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3E, 0x02, 0x01,
490  0x30, 0x82, 0x01, 0x0A,
491  0x02, 0x81, 0x81, 0x00, /* p */
492  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
493  0xC9, 0x0F, 0xDA, 0xA2, 0x21, 0x68, 0xC2, 0x34,
494  0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1,
495  0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0x74,
496  0x02, 0x0B, 0xBE, 0xA6, 0x3B, 0x13, 0x9B, 0x22,
497  0x51, 0x4A, 0x08, 0x79, 0x8E, 0x34, 0x04, 0xDD,
498  0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43, 0x1B,
499  0x30, 0x2B, 0x0A, 0x6D, 0xF2, 0x5F, 0x14, 0x37,
500  0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51, 0xC2, 0x45,
501  0xE4, 0x85, 0xB5, 0x76, 0x62, 0x5E, 0x7E, 0xC6,
502  0xF4, 0x4C, 0x42, 0xE9, 0xA6, 0x37, 0xED, 0x6B,
503  0x0B, 0xFF, 0x5C, 0xB6, 0xF4, 0x06, 0xB7, 0xED,
504  0xEE, 0x38, 0x6B, 0xFB, 0x5A, 0x89, 0x9F, 0xA5,
505  0xAE, 0x9F, 0x24, 0x11, 0x7C, 0x4B, 0x1F, 0xE6,
506  0x49, 0x28, 0x66, 0x51, 0xEC, 0xE6, 0x53, 0x81,
507  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
508  0x02, 0x01, /* g */
509  0x02,
510  0x02, 0x81, 0x80, /* q */
511  0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
512  0xE4, 0x87, 0xED, 0x51, 0x10, 0xB4, 0x61, 0x1A,
513  0x62, 0x63, 0x31, 0x45, 0xC0, 0x6E, 0x0E, 0x68,
514  0x94, 0x81, 0x27, 0x04, 0x45, 0x33, 0xE6, 0x3A,
515  0x01, 0x05, 0xDF, 0x53, 0x1D, 0x89, 0xCD, 0x91,
516  0x28, 0xA5, 0x04, 0x3C, 0xC7, 0x1A, 0x02, 0x6E,
517  0xF7, 0xCA, 0x8C, 0xD9, 0xE6, 0x9D, 0x21, 0x8D,
518  0x98, 0x15, 0x85, 0x36, 0xF9, 0x2F, 0x8A, 0x1B,
519  0xA7, 0xF0, 0x9A, 0xB6, 0xB6, 0xA8, 0xE1, 0x22,
520  0xF2, 0x42, 0xDA, 0xBB, 0x31, 0x2F, 0x3F, 0x63,
521  0x7A, 0x26, 0x21, 0x74, 0xD3, 0x1B, 0xF6, 0xB5,
522  0x85, 0xFF, 0xAE, 0x5B, 0x7A, 0x03, 0x5B, 0xF6,
523  0xF7, 0x1C, 0x35, 0xFD, 0xAD, 0x44, 0xCF, 0xD2,
524  0xD7, 0x4F, 0x92, 0x08, 0xBE, 0x25, 0x8F, 0xF3,
525  0x24, 0x94, 0x33, 0x28, 0xF6, 0x73, 0x29, 0xC0,
526  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
527  0x03, 0x04, 0x00,
528  0x02, 0x01, 0x00 /* y */
529  };
530 #endif /* TEST_DH */
531 
532 typedef struct {
534  const int pLen; const BYTE p[ 66 ];
535  const int aLen; const BYTE a[ 66 ];
536  const int bLen; const BYTE b[ 66 ];
537  const int gxLen; const BYTE gx[ 66 ];
538  const int gyLen; const BYTE gy[ 66 ];
539  const int nLen; const BYTE n[ 66 ];
540  const int qxLen; const BYTE qx[ 66 ];
541  const int qyLen; const BYTE qy[ 66 ];
542  const int dLen; const BYTE d[ 66 ];
543  } ECC_KEY;
544 
545 /* NIST curve P-192 */
546 
547 static const ECC_KEY FAR_BSS eccP192TestKey = {
548 #if 0
550  /* p */
551  192,
552  { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
553  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
554  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
555  /* a */
556  192,
557  { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
558  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
559  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC },
560  /* b */
561  192,
562  { 0x64, 0x21, 0x05, 0x19, 0xE5, 0x9C, 0x80, 0xE7,
563  0x0F, 0xA7, 0xE9, 0xAB, 0x72, 0x24, 0x30, 0x49,
564  0xFE, 0xB8, 0xDE, 0xEC, 0xC1, 0x46, 0xB9, 0xB1 },
565  /* gx */
566  192,
567  { 0x18, 0x8D, 0xA8, 0x0E, 0xB0, 0x30, 0x90, 0xF6,
568  0x7C, 0xBF, 0x20, 0xEB, 0x43, 0xA1, 0x88, 0x00,
569  0xF4, 0xFF, 0x0A, 0xFD, 0x82, 0xFF, 0x10, 0x12 },
570  /* gy */
571  192,
572  { 0x07, 0x19, 0x2B, 0x95, 0xFF, 0xC8, 0xDA, 0x78,
573  0x63, 0x10, 0x11, 0xED, 0x6B, 0x24, 0xCD, 0xD5,
574  0x73, 0xF9, 0x77, 0xA1, 0x1E, 0x79, 0x48, 0x11 },
575  /* n */
576  192,
577  { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
578  0xFF, 0xFF, 0xFF, 0xFF, 0x99, 0xDE, 0xF8, 0x36,
579  0x14, 0x6B, 0xC9, 0xB1, 0xB4, 0xD2, 0x28, 0x31 },
580 #else
582  0, { 0 }, /* p */
583  0, { 0 }, /* a */
584  0, { 0 }, /* b */
585  0, { 0 }, /* gx */
586  0, { 0 }, /* gy */
587  0, { 0 }, /* n */
588 #endif /* 0 */
589  /* qx */
590  192,
591  { 0x43, 0x4F, 0x79, 0x78, 0xE7, 0x00, 0x55, 0x7F,
592  0x6C, 0xC7, 0x2B, 0xCA, 0x3B, 0xC5, 0xB7, 0xB2,
593  0xC3, 0x06, 0xEA, 0xD0, 0xBD, 0x57, 0x76, 0xD1 },
594  /* qy */
595  192,
596  { 0x67, 0xD4, 0xA9, 0x61, 0x1C, 0x2F, 0x81, 0x0E,
597  0x4E, 0x6C, 0xA4, 0x92, 0xC8, 0x4E, 0xBC, 0x9B,
598  0x7C, 0x8D, 0xF9, 0x98, 0x19, 0x2F, 0x8B, 0xA8 },
599  /* d */
600  192,
601  { 0x40, 0x79, 0x26, 0x9F, 0x12, 0x89, 0x90, 0xE9,
602  0x41, 0x11, 0xBF, 0x25, 0xDA, 0xD0, 0x58, 0x51,
603  0x49, 0x6B, 0x6C, 0xCF, 0x90, 0x9A, 0x76, 0x8D }
604  };
605 
606 /* NIST curve P-256 */
607 
608 static const ECC_KEY FAR_BSS eccP256TestKey = {
609 #if 0
611  /* p */
612  256,
613  { 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01,
614  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
615  0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
616  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
617  /* a */
618  256,
619  { 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01,
620  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
621  0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
622  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC },
623  /* b */
624  256,
625  { 0x5A, 0xC6, 0x35, 0xD8, 0xAA, 0x3A, 0x93, 0xE7,
626  0xB3, 0xEB, 0xBD, 0x55, 0x76, 0x98, 0x86, 0xBC,
627  0x65, 0x1D, 0x06, 0xB0, 0xCC, 0x53, 0xB0, 0xF6,
628  0x3B, 0xCE, 0x3C, 0x3E, 0x27, 0xD2, 0x60, 0x4B },
629  /* gx */
630  256,
631  { 0x6B, 0x17, 0xD1, 0xF2, 0xE1, 0x2C, 0x42, 0x47,
632  0xF8, 0xBC, 0xE6, 0xE5, 0x63, 0xA4, 0x40, 0xF2,
633  0x77, 0x03, 0x7D, 0x81, 0x2D, 0xEB, 0x33, 0xA0,
634  0xF4, 0xA1, 0x39, 0x45, 0xD8, 0x98, 0xC2, 0x96 },
635  /* gy */
636  256,
637  { 0x4F, 0xE3, 0x42, 0xE2, 0xFE, 0x1A, 0x7F, 0x9B,
638  0x8E, 0xE7, 0xEB, 0x4A, 0x7C, 0x0F, 0x9E, 0x16,
639  0x2B, 0xCE, 0x33, 0x57, 0x6B, 0x31, 0x5E, 0xCE,
640  0xCB, 0xB6, 0x40, 0x68, 0x37, 0xBF, 0x51, 0xF5 },
641  /* n */
642  256,
643  { 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
644  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
645  0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84,
646  0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51 },
647 #else
649  0, { 0 }, /* p */
650  0, { 0 }, /* a */
651  0, { 0 }, /* b */
652  0, { 0 }, /* gx */
653  0, { 0 }, /* gy */
654  0, { 0 }, /* n */
655 #endif /* 0 */
656  /* qx */
657  256,
658  { 0x26, 0x0C, 0xAB, 0x1F, 0xF2, 0x5E, 0x8F, 0x54,
659  0x1C, 0x52, 0x66, 0x4A, 0x1B, 0x23, 0x8F, 0x68,
660  0x0D, 0xEB, 0xCB, 0x0B, 0x4A, 0x4E, 0x4C, 0x88,
661  0xCA, 0x53, 0x1F, 0x32, 0xAB, 0x0F, 0x72, 0x56 },
662  /* qy */
663  256,
664  { 0x94, 0x56, 0xD7, 0x00, 0x6A, 0x50, 0x06, 0xC0,
665  0x87, 0x9B, 0x73, 0x0D, 0x3F, 0x16, 0x37, 0x42,
666  0xE8, 0x8A, 0xA0, 0x7F, 0x9F, 0x87, 0xD5, 0x29,
667  0xCF, 0x3C, 0x83, 0xC7, 0xC3, 0xE3, 0x93, 0x58 },
668  /* d */
669  256,
670  { 0xC6, 0x91, 0x9E, 0xD5, 0xF2, 0x84, 0xE0, 0x30,
671  0xD5, 0x7B, 0xA8, 0x13, 0x51, 0x0B, 0x50, 0x1C,
672  0x7D, 0x8E, 0x14, 0x66, 0xE2, 0xF1, 0x49, 0x97,
673  0x06, 0x49, 0x61, 0x67, 0xFA, 0xA3, 0xEA, 0x05 }
674  };
675 
676 /****************************************************************************
677 * *
678 * Key Load Routines *
679 * *
680 ****************************************************************************/
681 
682 /* Set the label for a device object */
683 
684 static BOOLEAN setLabel( const CRYPT_CONTEXT cryptContext, const C_STR label )
685  {
686  int status;
687 
688  status = cryptSetAttributeString( cryptContext, CRYPT_CTXINFO_LABEL,
689  label, paramStrlen( label ) );
690  if( status == CRYPT_ERROR_DUPLICATE )
691  {
692  printf( "A key object with the label '%s' already exists inside the\n"
693  "device. To perform this test, you need to delete the "
694  "existing object so\nthat cryptlib can create a new one, "
695  "line %d.\n", label, __LINE__ );
696  return( FALSE );
697  }
698  if( cryptStatusError( status ) )
699  {
700  printf( "Attempt to set object label failed with status %d, "
701  "line %d.\n", status, __LINE__ );
702  return( FALSE );
703  }
704 
705  return( TRUE );
706  }
707 
708 /* Load DH, RSA, DSA, and Elgamal PKC encrytion contexts */
709 
710 #ifdef TEST_DH
711 
712 typedef struct {
713  void *data; /* Data */
714  int length; /* Length */
715  } xMESSAGE_DATA;
716 
717 #define xsetMessageData( msgDataPtr, dataPtr, dataLength ) \
718  { \
719  ( msgDataPtr )->data = ( dataPtr ); \
720  ( msgDataPtr )->length = ( dataLength ); \
721  }
722 
723 BOOLEAN loadDHKey( const CRYPT_DEVICE cryptDevice,
724  CRYPT_CONTEXT *cryptContext )
725  {
726  const BOOLEAN isDevice = ( cryptDevice != CRYPT_UNUSED ) ? TRUE : FALSE;
727  int status;
728 
729  if( isDevice )
730  status = cryptDeviceCreateContext( cryptDevice, cryptContext,
731  CRYPT_ALGO_DH );
732  else
733  status = cryptCreateContext( cryptContext, CRYPT_UNUSED,
734  CRYPT_ALGO_DH );
735  if( cryptStatusError( status ) )
736  {
737  printf( "crypt%sCreateContext() failed with error code %d, "
738  "line %d.\n", isDevice ? "Device" : "", status, __LINE__ );
739  return( FALSE );
740  }
741  if( !setLabel( *cryptContext, "DH key" ) )
742  {
743  cryptDestroyContext( *cryptContext );
744  return( FALSE );
745  }
746  if( cryptStatusOK( status ) )
747  {
748  xMESSAGE_DATA msgData;
749 
750  xsetMessageData( &msgData, ( void * ) dh1024SPKI,
751  sizeof( dh1024SPKI ) );
752  status = krnlSendMessage( *cryptContext, IMESSAGE_SETATTRIBUTE_S,
753  &msgData, CRYPT_IATTRIBUTE_KEY_SPKI );
754  }
755  if( cryptStatusError( status ) )
756  {
757  printf( "DH key load failed, status = %d, line %d.\n", status,
758  __LINE__ );
759  cryptDestroyContext( *cryptContext );
760  return( FALSE );
761  }
762  return( TRUE );
763  }
764 #endif /* TEST_DH */
765 
766 static int loadRSAPublicKey( const CRYPT_DEVICE cryptDevice,
767  CRYPT_CONTEXT *cryptContext,
768  const C_STR cryptContextLabel,
769  CRYPT_PKCINFO_RSA *rsaKey,
770  const BOOLEAN isDevice,
771  const BOOLEAN useLargeKey )
772  {
773  const RSA_KEY *rsaKeyTemplate = useLargeKey ? \
774  &rsa2048TestKey : &rsa1024TestKey;
775  int status;
776 
777  if( isDevice )
778  status = cryptDeviceCreateContext( cryptDevice, cryptContext,
779  CRYPT_ALGO_RSA );
780  else
781  status = cryptCreateContext( cryptContext, CRYPT_UNUSED,
782  CRYPT_ALGO_RSA );
783  if( cryptStatusError( status ) )
784  {
785  printf( "crypt%sCreateContext() failed with error code %d, "
786  "line %d.\n", isDevice ? "Device" : "", status, __LINE__ );
787  return( status );
788  }
789  if( isDevice && !setLabel( *cryptContext, cryptContextLabel ) )
790  {
791  cryptDestroyContext( *cryptContext );
792  return( CRYPT_ERROR_FAILED );
793  }
795  cryptSetComponent( rsaKey->n, rsaKeyTemplate->n, rsaKeyTemplate->nLen );
796  cryptSetComponent( rsaKey->e, rsaKeyTemplate->e, rsaKeyTemplate->eLen );
797  status = cryptSetAttributeString( *cryptContext,
799  sizeof( CRYPT_PKCINFO_RSA ) );
800  cryptDestroyComponents( rsaKey );
801  if( cryptStatusError( status ) )
802  cryptDestroyContext( *cryptContext );
803  return( status );
804  }
805 
807  CRYPT_CONTEXT *cryptContext,
808  CRYPT_CONTEXT *decryptContext,
809  const C_STR cryptContextLabel,
810  const C_STR decryptContextLabel,
811  const BOOLEAN useLargeKey,
812  const BOOLEAN useMinimalKey )
813  {
814  CRYPT_PKCINFO_RSA *rsaKey;
815  const RSA_KEY *rsaKeyTemplate = useLargeKey ? \
816  &rsa2048TestKey : &rsa1024TestKey;
817  const BOOLEAN isDevice = ( cryptDevice != CRYPT_UNUSED ) ? TRUE : FALSE;
818  BOOLEAN loadLargeKey = useLargeKey;
819  int status;
820 
821  /* Allocate room for the public-key components */
822  if( ( rsaKey = ( CRYPT_PKCINFO_RSA * ) malloc( sizeof( CRYPT_PKCINFO_RSA ) ) ) == NULL )
823  return( FALSE );
824 
825  /* Some devices only support a single key size that isn't the same as
826  the built-in one so we adjust the key size being used if necessary */
827  if( isDevice )
828  {
829  CRYPT_QUERY_INFO cryptQueryInfo;
830 
831  status = cryptDeviceQueryCapability( cryptDevice, CRYPT_ALGO_RSA,
832  &cryptQueryInfo );
833  if( cryptStatusError( status ) )
834  {
835  free( rsaKey );
836  return( FALSE );
837  }
838  if( cryptQueryInfo.keySize != ( rsa1024TestKey.nLen >> 3 ) )
839  {
840  if( cryptQueryInfo.keySize != ( rsa2048TestKey.nLen >> 3 ) )
841  {
842  printf( "Device requires a %d-bit key, which doesn't "
843  "correspond to any built-in\ncryptlib key.\n",
844  cryptQueryInfo.keySize );
845  free( rsaKey );
846  return( FALSE );
847  }
848  rsaKeyTemplate = &rsa2048TestKey;
849  loadLargeKey = TRUE;
850  }
851  }
852 
853  /* Create the encryption context */
854  if( cryptContext != NULL )
855  {
856  status = loadRSAPublicKey( cryptDevice, cryptContext,
857  cryptContextLabel, rsaKey, isDevice,
858  loadLargeKey );
859  if( status == CRYPT_ERROR_NOTAVAIL && isDevice )
860  {
861  /* The device doesn't support public-key ops, use a native
862  context for the public key */
863  puts( " Warning: Device doesn't support public-key operations, "
864  "using a cryptlib\n native context instead." );
865  status = loadRSAPublicKey( CRYPT_UNUSED, cryptContext,
866  cryptContextLabel, rsaKey, FALSE,
867  loadLargeKey );
868  }
869  if( cryptStatusError( status ) )
870  {
871  free( rsaKey );
872  printf( "Public key load failed with error code %d, line %d.\n",
873  status, __LINE__ );
874  return( FALSE );
875  }
876  if( decryptContext == NULL )
877  {
878  /* We're only using a public-key context, return */
879  free( rsaKey );
880  return( TRUE );
881  }
882  }
883 
884  /* Create the decryption context */
885  if( isDevice )
886  status = cryptDeviceCreateContext( cryptDevice, decryptContext,
887  CRYPT_ALGO_RSA );
888  else
889  status = cryptCreateContext( decryptContext, CRYPT_UNUSED,
890  CRYPT_ALGO_RSA );
891  if( cryptStatusError( status ) )
892  {
893  free( rsaKey );
894  if( cryptContext != NULL )
895  {
896  cryptDestroyContext( *cryptContext );
897  if( isDevice )
898  cryptDeleteKey( cryptDevice, CRYPT_KEYID_NAME,
899  cryptContextLabel );
900  }
901  printf( "crypt%sCreateContext() failed with error code %d, "
902  "line %d.\n", isDevice ? "Device" : "", status, __LINE__ );
903  return( FALSE );
904  }
905  if( !setLabel( *decryptContext, decryptContextLabel ) )
906  {
907  free( rsaKey );
908  cryptDestroyContext( *decryptContext );
909  if( cryptContext != NULL )
910  {
911  cryptDestroyContext( *cryptContext );
912  if( isDevice )
913  cryptDeleteKey( cryptDevice, CRYPT_KEYID_NAME,
914  cryptContextLabel );
915  }
916  return( FALSE );
917  }
919  cryptSetComponent( rsaKey->n, rsaKeyTemplate->n, rsaKeyTemplate->nLen );
920  cryptSetComponent( rsaKey->e, rsaKeyTemplate->e, rsaKeyTemplate->eLen );
921  cryptSetComponent( rsaKey->d, rsaKeyTemplate->d, rsaKeyTemplate->dLen );
922  cryptSetComponent( rsaKey->p, rsaKeyTemplate->p, rsaKeyTemplate->pLen );
923  cryptSetComponent( rsaKey->q, rsaKeyTemplate->q, rsaKeyTemplate->qLen );
924  if( !useMinimalKey )
925  {
926  cryptSetComponent( rsaKey->u, rsaKeyTemplate->u, rsaKeyTemplate->uLen );
927  cryptSetComponent( rsaKey->e1, rsaKeyTemplate->e1, rsaKeyTemplate->e1Len );
928  cryptSetComponent( rsaKey->e2, rsaKeyTemplate->e2, rsaKeyTemplate->e2Len );
929  }
930  status = cryptSetAttributeString( *decryptContext,
932  sizeof( CRYPT_PKCINFO_RSA ) );
933  cryptDestroyComponents( rsaKey );
934  free( rsaKey );
935  if( cryptStatusError( status ) )
936  {
937  if( cryptContext != NULL )
938  {
939  cryptDestroyContext( *cryptContext );
940  if( isDevice )
941  cryptDeleteKey( cryptDevice, CRYPT_KEYID_NAME,
942  cryptContextLabel );
943  }
944  cryptDestroyContext( *decryptContext );
945  if( isDevice )
946  cryptDeleteKey( cryptDevice, CRYPT_KEYID_NAME,
947  decryptContextLabel );
948  printf( "Private key load failed with error code %d, line %d.\n",
949  status, __LINE__ );
950  return( FALSE );
951  }
952 
953  return( TRUE );
954  }
955 
957  CRYPT_CONTEXT *cryptContext,
958  CRYPT_CONTEXT *decryptContext )
959  {
960  return( loadRSAContextsEx( cryptDevice, cryptContext, decryptContext,
962  FALSE ) );
963  }
965  CRYPT_CONTEXT *cryptContext,
966  CRYPT_CONTEXT *decryptContext )
967  {
968  return( loadRSAContextsEx( cryptDevice, cryptContext, decryptContext,
970  FALSE ) );
971  }
972 
976  const C_STR signContextLabel,
977  const C_STR sigCheckContextLabel )
978  {
979  CRYPT_PKCINFO_DLP *dsaKey;
980  const BOOLEAN isDevice = ( cryptDevice != CRYPT_UNUSED ) ? TRUE : FALSE;
981  int status;
982 
983  /* Allocate room for the public-key components */
984  if( ( dsaKey = ( CRYPT_PKCINFO_DLP * ) malloc( sizeof( CRYPT_PKCINFO_DLP ) ) ) == NULL )
985  return( FALSE );
986 
987  /* Create the signature context */
988  if( signContext != NULL )
989  {
990  if( isDevice )
991  status = cryptDeviceCreateContext( cryptDevice, signContext,
992  CRYPT_ALGO_DSA );
993  else
994  status = cryptCreateContext( signContext, CRYPT_UNUSED,
995  CRYPT_ALGO_DSA );
996  if( cryptStatusError( status ) )
997  {
998  free( dsaKey );
999  printf( "cryptCreateContext() failed with error code %d, "
1000  "line %d.\n", status, __LINE__ );
1001  return( FALSE );
1002  }
1003  if( !setLabel( *signContext, signContextLabel ) )
1004  {
1005  free( dsaKey );
1006  cryptDestroyContext( *signContext );
1007  return( FALSE );
1008  }
1010  cryptSetComponent( dsaKey->p, dlp1024TestKey.p, dlp1024TestKey.pLen );
1011  cryptSetComponent( dsaKey->q, dlp1024TestKey.q, dlp1024TestKey.qLen );
1012  cryptSetComponent( dsaKey->g, dlp1024TestKey.g, dlp1024TestKey.gLen );
1013  cryptSetComponent( dsaKey->x, dlp1024TestKey.x, dlp1024TestKey.xLen );
1014  cryptSetComponent( dsaKey->y, dlp1024TestKey.y, dlp1024TestKey.yLen );
1015  status = cryptSetAttributeString( *signContext,
1017  sizeof( CRYPT_PKCINFO_DLP ) );
1018  cryptDestroyComponents( dsaKey );
1019  if( cryptStatusError( status ) )
1020  {
1021  free( dsaKey );
1022  cryptDestroyContext( *signContext );
1023  printf( "Private key load failed with error code %d, line %d.\n",
1024  status, __LINE__ );
1025  return( FALSE );
1026  }
1027  if( sigCheckContext == NULL )
1028  {
1029  free( dsaKey );
1030  return( TRUE );
1031  }
1032  }
1033 
1034  /* Create the sig.check context */
1035  if( isDevice )
1036  status = cryptDeviceCreateContext( cryptDevice, sigCheckContext,
1037  CRYPT_ALGO_DSA );
1038  else
1039  status = cryptCreateContext( sigCheckContext, CRYPT_UNUSED,
1040  CRYPT_ALGO_DSA );
1041  if( cryptStatusError( status ) )
1042  {
1043  free( dsaKey );
1044  if( signContext != NULL )
1045  {
1046  cryptDestroyContext( *signContext );
1047  if( isDevice )
1048  cryptDeleteKey( cryptDevice, CRYPT_KEYID_NAME,
1049  signContextLabel );
1050  }
1051  printf( "cryptCreateContext() failed with error code %d, line %d.\n",
1052  status, __LINE__ );
1053  return( FALSE );
1054  }
1055  if( !setLabel( *sigCheckContext, sigCheckContextLabel ) )
1056  {
1057  free( dsaKey );
1058  if( signContext != NULL )
1059  {
1060  cryptDestroyContext( *signContext );
1061  if( isDevice )
1062  cryptDeleteKey( cryptDevice, CRYPT_KEYID_NAME,
1063  signContextLabel );
1064  }
1065  cryptDestroyContext( *sigCheckContext );
1066  return( FALSE );
1067  }
1069  cryptSetComponent( dsaKey->p, dlp1024TestKey.p, dlp1024TestKey.pLen );
1070  cryptSetComponent( dsaKey->q, dlp1024TestKey.q, dlp1024TestKey.qLen );
1071  cryptSetComponent( dsaKey->g, dlp1024TestKey.g, dlp1024TestKey.gLen );
1072  cryptSetComponent( dsaKey->y, dlp1024TestKey.y, dlp1024TestKey.yLen );
1073  status = cryptSetAttributeString( *sigCheckContext,
1075  sizeof( CRYPT_PKCINFO_DLP ) );
1076  cryptDestroyComponents( dsaKey );
1077  free( dsaKey );
1078  if( cryptStatusError( status ) )
1079  {
1080  if( signContext != NULL )
1081  {
1082  cryptDestroyContext( *signContext );
1083  if( isDevice )
1084  cryptDeleteKey( cryptDevice, CRYPT_KEYID_NAME,
1085  signContextLabel );
1086  }
1087  cryptDestroyContext( *sigCheckContext );
1088  if( isDevice )
1089  cryptDeleteKey( cryptDevice, CRYPT_KEYID_NAME,
1090  sigCheckContextLabel );
1091  printf( "Public key load failed with error code %d, line %d.\n",
1092  status, __LINE__ );
1093  return( FALSE );
1094  }
1095 
1096  return( TRUE );
1097  }
1098 
1102  {
1103  return( loadDSAContextsEx( cryptDevice, signContext, sigCheckContext,
1105  }
1106 
1108  CRYPT_CONTEXT *decryptContext )
1109  {
1110  CRYPT_PKCINFO_DLP *elgamalKey;
1111  int status;
1112 
1113  /* Allocate room for the public-key components */
1114  if( ( elgamalKey = ( CRYPT_PKCINFO_DLP * ) malloc( sizeof( CRYPT_PKCINFO_DLP ) ) ) == NULL )
1115  return( FALSE );
1116 
1117  /* Create the encryption context */
1118  if( cryptContext != NULL )
1119  {
1120  status = cryptCreateContext( cryptContext, CRYPT_UNUSED,
1122  if( cryptStatusError( status ) )
1123  {
1124  free( elgamalKey );
1125  printf( "cryptCreateContext() failed with error code %d, "
1126  "line %d.\n", status, __LINE__ );
1127  return( FALSE );
1128  }
1129  if( !setLabel( *cryptContext, ELGAMAL_PUBKEY_LABEL ) )
1130  {
1131  free( elgamalKey );
1132  cryptDestroyContext( *cryptContext );
1133  return( FALSE );
1134  }
1136  cryptSetComponent( elgamalKey->p, dlp1024TestKey.p, dlp1024TestKey.pLen );
1137  cryptSetComponent( elgamalKey->g, dlp1024TestKey.g, dlp1024TestKey.gLen );
1138  cryptSetComponent( elgamalKey->q, dlp1024TestKey.q, dlp1024TestKey.qLen );
1139  cryptSetComponent( elgamalKey->y, dlp1024TestKey.y, dlp1024TestKey.yLen );
1140  status = cryptSetAttributeString( *cryptContext,
1141  CRYPT_CTXINFO_KEY_COMPONENTS, elgamalKey,
1142  sizeof( CRYPT_PKCINFO_DLP ) );
1143  cryptDestroyComponents( elgamalKey );
1144  if( cryptStatusError( status ) )
1145  {
1146  free( elgamalKey );
1147  cryptDestroyContext( *cryptContext );
1148  printf( "Public key load failed with error code %d, line %d.\n",
1149  status, __LINE__ );
1150  return( FALSE );
1151  }
1152  if( decryptContext == NULL )
1153  {
1154  free( elgamalKey );
1155  return( TRUE );
1156  }
1157  }
1158 
1159  /* Create the decryption context */
1160  status = cryptCreateContext( decryptContext, CRYPT_UNUSED,
1162  if( cryptStatusError( status ) )
1163  {
1164  free( elgamalKey );
1165  if( cryptContext != NULL )
1166  cryptDestroyContext( *cryptContext );
1167  printf( "cryptCreateContext() failed with error code %d, line %d.\n",
1168  status, __LINE__ );
1169  return( FALSE );
1170  }
1171  if( !setLabel( *decryptContext, ELGAMAL_PRIVKEY_LABEL ) )
1172  {
1173  free( elgamalKey );
1174  if( cryptContext != NULL )
1175  cryptDestroyContext( *cryptContext );
1176  cryptDestroyContext( *decryptContext );
1177  return( FALSE );
1178  }
1180  cryptSetComponent( elgamalKey->p, dlp1024TestKey.p, dlp1024TestKey.pLen );
1181  cryptSetComponent( elgamalKey->g, dlp1024TestKey.g, dlp1024TestKey.gLen );
1182  cryptSetComponent( elgamalKey->q, dlp1024TestKey.q, dlp1024TestKey.qLen );
1183  cryptSetComponent( elgamalKey->y, dlp1024TestKey.y, dlp1024TestKey.yLen );
1184  cryptSetComponent( elgamalKey->x, dlp1024TestKey.x, dlp1024TestKey.xLen );
1185  status = cryptSetAttributeString( *decryptContext,
1186  CRYPT_CTXINFO_KEY_COMPONENTS, elgamalKey,
1187  sizeof( CRYPT_PKCINFO_DLP ) );
1188  cryptDestroyComponents( elgamalKey );
1189  free( elgamalKey );
1190  if( cryptStatusError( status ) )
1191  {
1192  if( cryptContext != NULL )
1193  cryptDestroyContext( *cryptContext );
1194  if( decryptContext != NULL )
1195  cryptDestroyContext( *decryptContext );
1196  printf( "Private key load failed with error code %d, line %d.\n",
1197  status, __LINE__ );
1198  return( FALSE );
1199  }
1200 
1201  return( TRUE );
1202  }
1203 
1204 /* Load Diffie-Hellman encrytion contexts */
1205 
1207  CRYPT_CONTEXT *cryptContext2 )
1208  {
1209  CRYPT_PKCINFO_DLP *dhKey;
1210  int status;
1211 
1212  /* Allocate room for the public-key components */
1213  if( ( dhKey = ( CRYPT_PKCINFO_DLP * ) malloc( sizeof( CRYPT_PKCINFO_DLP ) ) ) == NULL )
1214  return( FALSE );
1215 
1216  /* Create the first encryption context */
1217  status = cryptCreateContext( cryptContext1, CRYPT_UNUSED, CRYPT_ALGO_DH );
1218  if( cryptStatusError( status ) )
1219  {
1220  free( dhKey );
1221  printf( "cryptCreateContext() failed with error code %d, line %d.\n",
1222  status, __LINE__ );
1223  return( FALSE );
1224  }
1225  if( !setLabel( *cryptContext1, DH_KEY1_LABEL ) )
1226  {
1227  free( dhKey );
1228  cryptDestroyContext( *cryptContext1 );
1229  return( FALSE );
1230  }
1232  cryptSetComponent( dhKey->p, dlp1024TestKey.p, dlp1024TestKey.pLen );
1233  cryptSetComponent( dhKey->q, dlp1024TestKey.q, dlp1024TestKey.qLen );
1234  cryptSetComponent( dhKey->g, dlp1024TestKey.g, dlp1024TestKey.gLen );
1235  status = cryptSetAttributeString( *cryptContext1,
1237  sizeof( CRYPT_PKCINFO_DLP ) );
1238  cryptDestroyComponents( dhKey );
1239  if( cryptStatusError( status ) )
1240  {
1241  free( dhKey );
1242  printf( "DH #1 key load failed with error code %d, line %d.\n",
1243  status, __LINE__ );
1244  return( FALSE );
1245  }
1246  if( cryptContext2 == NULL )
1247  {
1248  free( dhKey );
1249  return( TRUE );
1250  }
1251 
1252  /* Create the second encryption context */
1253  status = cryptCreateContext( cryptContext2, CRYPT_UNUSED, CRYPT_ALGO_DH );
1254  if( cryptStatusError( status ) )
1255  {
1256  free( dhKey );
1257  printf( "cryptCreateContext() failed with error code %d, line %d.\n",
1258  status, __LINE__ );
1259  return( FALSE );
1260  }
1261  if( !setLabel( *cryptContext2, DH_KEY2_LABEL ) )
1262  {
1263  free( dhKey );
1264  if( cryptContext1 != NULL )
1265  cryptDestroyContext( *cryptContext1 );
1266  cryptDestroyContext( *cryptContext2 );
1267  return( FALSE );
1268  }
1270  cryptSetComponent( dhKey->p, dlp1024TestKey.p, dlp1024TestKey.pLen );
1271  cryptSetComponent( dhKey->q, dlp1024TestKey.q, dlp1024TestKey.qLen );
1272  cryptSetComponent( dhKey->g, dlp1024TestKey.g, dlp1024TestKey.gLen );
1273  status = cryptSetAttributeString( *cryptContext2,
1275  sizeof( CRYPT_PKCINFO_DLP ) );
1276  cryptDestroyComponents( dhKey );
1277  free( dhKey );
1278  if( cryptStatusError( status ) )
1279  {
1280  printf( "DH #2 key load failed with error code %d, line %d.\n",
1281  status, __LINE__ );
1282  return( FALSE );
1283  }
1284 
1285  return( TRUE );
1286  }
1287 
1288 /* Load ECDSA encrytion contexts */
1289 
1292  {
1293  CRYPT_PKCINFO_ECC *eccKey;
1294  const ECC_KEY *eccKeyData = &eccP256TestKey;
1295  int status;
1296 
1297  /* Allocate room for the public-key components */
1298  if( ( eccKey = ( CRYPT_PKCINFO_ECC * ) malloc( sizeof( CRYPT_PKCINFO_ECC ) ) ) == NULL )
1299  return( FALSE );
1300 
1301  /* Create the signature context */
1302  if( signContext != NULL )
1303  {
1304  status = cryptCreateContext( signContext, CRYPT_UNUSED,
1305  CRYPT_ALGO_ECDSA );
1306  if( cryptStatusError( status ) )
1307  {
1308  free( eccKey );
1309  printf( "cryptCreateContext() failed with error code %d, "
1310  "line %d.\n", status, __LINE__ );
1311  return( FALSE );
1312  }
1313  if( !setLabel( *signContext, ECDSA_PRIVKEY_LABEL ) )
1314  {
1315  free( eccKey );
1316  cryptDestroyContext( *signContext );
1317  return( FALSE );
1318  }
1320  eccKey->curveType = CRYPT_ECCCURVE_P256;
1321  if( eccKeyData->pLen > 0 )
1322  {
1323  cryptSetComponent( eccKey->p, eccKeyData->p, eccKeyData->pLen );
1324  cryptSetComponent( eccKey->a, eccKeyData->a, eccKeyData->aLen );
1325  cryptSetComponent( eccKey->b, eccKeyData->b, eccKeyData->bLen );
1326  cryptSetComponent( eccKey->gx, eccKeyData->gx, eccKeyData->gxLen );
1327  cryptSetComponent( eccKey->gy, eccKeyData->gy, eccKeyData->gyLen );
1328  cryptSetComponent( eccKey->n, eccKeyData->n, eccKeyData->nLen );
1329  }
1330  cryptSetComponent( eccKey->qx, eccKeyData->qx, eccKeyData->qxLen );
1331  cryptSetComponent( eccKey->qy, eccKeyData->qy, eccKeyData->qyLen );
1332  cryptSetComponent( eccKey->d, eccKeyData->d, eccKeyData->dLen );
1333  status = cryptSetAttributeString( *signContext,
1335  sizeof( CRYPT_PKCINFO_ECC ) );
1336  cryptDestroyComponents( eccKey );
1337  if( cryptStatusError( status ) )
1338  {
1339  free( eccKey );
1340  cryptDestroyContext( *signContext );
1341  printf( "Private key load failed with error code %d, line %d.\n",
1342  status, __LINE__ );
1343  return( FALSE );
1344  }
1345  if( sigCheckContext == NULL )
1346  {
1347  free( eccKey );
1348  return( TRUE );
1349  }
1350  }
1351 
1352  /* Create the sig.check context */
1353  status = cryptCreateContext( sigCheckContext, CRYPT_UNUSED,
1354  CRYPT_ALGO_ECDSA );
1355  if( cryptStatusError( status ) )
1356  {
1357  free( eccKey );
1358  if( signContext != NULL )
1359  cryptDestroyContext( *signContext );
1360  printf( "cryptCreateContext() failed with error code %d, line %d.\n",
1361  status, __LINE__ );
1362  return( FALSE );
1363  }
1364  if( !setLabel( *sigCheckContext, ECDSA_PRIVKEY_LABEL ) )
1365  {
1366  free( eccKey );
1367  if( signContext != NULL )
1368  cryptDestroyContext( *signContext );
1369  cryptDestroyContext( *sigCheckContext );
1370  return( FALSE );
1371  }
1373  eccKey->curveType = CRYPT_ECCCURVE_P256;
1374  if( eccKeyData->pLen > 0 )
1375  {
1376  cryptSetComponent( eccKey->p, eccKeyData->p, eccKeyData->pLen );
1377  cryptSetComponent( eccKey->a, eccKeyData->a, eccKeyData->aLen );
1378  cryptSetComponent( eccKey->b, eccKeyData->b, eccKeyData->bLen );
1379  cryptSetComponent( eccKey->gx, eccKeyData->gx, eccKeyData->gxLen );
1380  cryptSetComponent( eccKey->gy, eccKeyData->gy, eccKeyData->gyLen );
1381  cryptSetComponent( eccKey->n, eccKeyData->n, eccKeyData->nLen );
1382  }
1383  cryptSetComponent( eccKey->qx, eccKeyData->qx, eccKeyData->qxLen );
1384  cryptSetComponent( eccKey->qy, eccKeyData->qy, eccKeyData->qyLen );
1385  status = cryptSetAttributeString( *sigCheckContext,
1387  sizeof( CRYPT_PKCINFO_ECC ) );
1388  cryptDestroyComponents( eccKey );
1389  free( eccKey );
1390  if( cryptStatusError( status ) )
1391  {
1392  if( signContext != NULL )
1393  cryptDestroyContext( *signContext );
1394  cryptDestroyContext( *sigCheckContext );
1395  printf( "Public key load failed with error code %d, line %d.\n",
1396  status, __LINE__ );
1397  return( FALSE );
1398  }
1399 
1400  return( TRUE );
1401  }
1402 
1403 /* Destroy the encryption contexts */
1404 
1405 void destroyContexts( const CRYPT_DEVICE cryptDevice,
1406  CRYPT_CONTEXT cryptContext,
1407  CRYPT_CONTEXT decryptContext )
1408  {
1409  int cryptAlgo, status;
1410 
1411  cryptGetAttribute( cryptContext, CRYPT_CTXINFO_ALGO, &cryptAlgo );
1412  status = cryptDestroyContext( cryptContext );
1413  if( cryptStatusError( status ) )
1414  printf( "cryptDestroyContext() failed with error code %d, "
1415  "line %d.\n", status, __LINE__ );
1416  status = cryptDestroyContext( decryptContext );
1417  if( cryptStatusError( status ) )
1418  printf( "cryptDestroyContext() failed with error code %d, "
1419  "line %d.\n", status, __LINE__ );
1420  if( cryptDevice == CRYPT_UNUSED )
1421  return;
1422 
1423  /* If the context is associated with a device then creating the object
1424  will generally also create a persistent object in the device, after
1425  performing the tests we have to explicitly delete the persistent
1426  object */
1427  if( cryptAlgo == CRYPT_ALGO_RSA )
1428  {
1431  }
1432  else
1433  if( cryptAlgo == CRYPT_ALGO_DSA )
1434  {
1437  }
1438  }