~ubuntu-branches/ubuntu/hardy/opencryptoki/hardy

« back to all changes in this revision

Viewing changes to testcases/rsa_test/rsa_test.c

  • Committer: Bazaar Package Importer
  • Author(s): Stephan Hermann
  • Date: 2008-01-24 14:22:58 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080124142258-i0nmjvqoy6wxe8x1
Tags: 2.2.5+dfsg-1ubuntu1
* Merge from debian unstable
* debian/libopencryptoki0.install, debian/libopencryptoki-dev.install: 
  - removed /usr/lib/opencryptoki/stdll/*, it's empty now
 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// File: driver.c
2
 
//
3
 
//
4
 
// Test driver.  In-depth regression test for PKCS #11
5
 
//
6
 
 
7
 
 
8
 
#include <windows.h>
9
 
 
10
 
#include <stdio.h>
11
 
#include <stdlib.h>
12
 
#include <string.h>
13
 
#include <memory.h>
14
 
#include <dlfcn.h>
15
 
#include <sys/timeb.h>
16
 
 
17
 
#include "pkcs11types.h"
18
 
#include "regress.h"
19
 
 
20
 
int skip_token_obj;
21
 
int do_GetFunctionList(void);
22
 
 
23
 
CK_FUNCTION_LIST  *funcs;
24
 
CK_SLOT_ID  SLOT_ID;
25
 
 
26
 
// these values are required when generating a PKCS DSA value.  they were
27
 
// obtained by generating a DSA key pair on the 4758 with the default (random)
28
 
// values.  these values are in big-endian format
29
 
//
30
 
CK_BYTE DSA_PUBL_PRIME[128] =
31
 
{
32
 
   0xba, 0xa2, 0x5b, 0xd9, 0x77, 0xb3, 0xf0, 0x2d, 0xa1, 0x65, 0xf1, 0x83, 0xa7, 0xc9, 0xf0,
33
 
   0x8a, 0x51, 0x3f, 0x74, 0xe8, 0xeb, 0x1f, 0xd7, 0x0a, 0xd5, 0x41, 0xfa, 0x52, 0x3c, 0x1f,
34
 
   0x79, 0x15, 0x55, 0x18, 0x45, 0x41, 0x29, 0x27, 0x12, 0x4a, 0xb4, 0x32, 0xa6, 0xd2, 0xec,
35
 
   0xe2, 0x82, 0x73, 0xf4, 0x30, 0x66, 0x1a, 0x31, 0x06, 0x37, 0xd2, 0xb0, 0xe4, 0x26, 0x39,
36
 
   0x2a, 0x0e, 0x48, 0xf6, 0x77, 0x94, 0x47, 0xea, 0x7d, 0x99, 0x22, 0xce, 0x65, 0x61, 0x82,
37
 
   0xd5, 0xe3, 0xfc, 0x15, 0x3f, 0xff, 0xff, 0xc8, 0xb9, 0x4f, 0x37, 0xbf, 0x7a, 0xa6, 0x6a,
38
 
   0xbe, 0xff, 0xa9, 0xdf, 0xfd, 0xed, 0x4a, 0xb6, 0x83, 0xd6, 0x0f, 0xea, 0xf6, 0x90, 0x4f,
39
 
   0x12, 0x8e, 0x09, 0x6e, 0x3c, 0x0a, 0x6d, 0x2e, 0xfb, 0xb3, 0x79, 0x90, 0x8e, 0x39, 0xc0,
40
 
   0x86, 0x0e, 0x5d, 0xf0, 0x56, 0xcd, 0x26, 0x45
41
 
};
42
 
 
43
 
CK_BYTE DSA_PUBL_SUBPRIME[20] =
44
 
{
45
 
   0x9f, 0x3d, 0x47, 0x13, 0xa3, 0xff, 0x93, 0xbb, 0x4a, 0xa6, 0xb0, 0xf1, 0x7e, 0x54, 0x1e,
46
 
   0xba, 0xf0, 0x66, 0x03, 0x61
47
 
};
48
 
 
49
 
 
50
 
CK_BYTE DSA_PUBL_BASE[128] =
51
 
{
52
 
   0x1a, 0x5b, 0xfe, 0x12, 0xba, 0x85, 0x8e, 0x9b, 0x08, 0x86, 0xd1, 0x43, 0x9b, 0x4a, 0xaf,
53
 
   0x44, 0x31, 0xdf, 0xa1, 0x57, 0xd8, 0xe0, 0xec, 0x34, 0x07, 0x4b, 0x78, 0x8e, 0x3c, 0x62,
54
 
   0x47, 0x4c, 0x2f, 0x5d, 0xd3, 0x31, 0x2c, 0xe9, 0xdd, 0x59, 0xc5, 0xe7, 0x2e, 0x06, 0x40,
55
 
   0x6c, 0x72, 0x9c, 0x95, 0xc6, 0xa4, 0x2a, 0x1c, 0x1c, 0x45, 0xb9, 0xf3, 0xdc, 0x83, 0xb6,
56
 
   0xc6, 0xdd, 0x94, 0x45, 0x4f, 0x74, 0xc6, 0x55, 0x36, 0x54, 0xba, 0x20, 0xad, 0x9a, 0xb6,
57
 
   0xe3, 0x20, 0xf2, 0xdd, 0xd3, 0x66, 0x19, 0xeb, 0x53, 0xf5, 0x88, 0x35, 0xe1, 0xea, 0xe8,
58
 
   0xd4, 0x57, 0xe1, 0x3d, 0xea, 0xd5, 0x00, 0xc2, 0xa4, 0xf5, 0xff, 0xfb, 0x0b, 0xfb, 0xa2,
59
 
   0xb9, 0xf1, 0x49, 0x46, 0x9d, 0x11, 0xa5, 0xb1, 0x94, 0x52, 0x47, 0x6e, 0x2e, 0x79, 0x4b,
60
 
   0xc5, 0x18, 0xe9, 0xbc, 0xff, 0xae, 0x34, 0x7f
61
 
};
62
 
 
63
 
#if 0
64
 
void hex_dump_to_file(char *str, unsigned char *data, int bytes)
65
 
{
66
 
   FILE *fp;
67
 
   fp = fopen("rsadump.h", "a");
68
 
   fprintf(fp, "\nunsigned char %s[] =\n                  {", str);
69
 
   while (bytes--)
70
 
   {
71
 
      fprintf(fp, " 0x%x", *data++);
72
 
      if (bytes)
73
 
      {
74
 
         fprintf(fp, ",");
75
 
         if (!(bytes % 8))
76
 
            fprintf(fp, "\n                   ");
77
 
      }
78
 
   }
79
 
   fprintf(fp, " };\n");
80
 
   fclose(fp);
81
 
 
82
 
}
83
 
#endif
84
 
 
85
 
unsigned char cka_modulus[] =
86
 
                  { 0xec, 0x51, 0xab, 0xa1, 0xf8, 0x40, 0x2c, 0x8,
87
 
                    0x2e, 0x24, 0x52, 0x2e, 0x3c, 0x51, 0x6d, 0x98,
88
 
                    0xad, 0xee, 0xc7, 0x7d, 0x0, 0xaf, 0xe1, 0xa8,
89
 
                    0x61, 0xda, 0x32, 0x97, 0xb4, 0x32, 0x97, 0xe3,
90
 
                    0x52, 0xda, 0x28, 0x45, 0x55, 0xc6, 0xb2, 0x46,
91
 
                    0x65, 0x1b, 0x2, 0xcb, 0xbe, 0xf4, 0x2c, 0x6b,
92
 
                    0x2a, 0x5f, 0xe1, 0xdf, 0xe9, 0xe3, 0xbc, 0x47,
93
 
                    0xb7, 0x38, 0xb5, 0xa2, 0x78, 0x9d, 0x15, 0xe2,
94
 
                    0x59, 0x81, 0x77, 0x6b, 0x6b, 0x2e, 0xa9, 0xdb,
95
 
                    0x13, 0x26, 0x9c, 0xca, 0x5e, 0xa, 0x1f, 0x3c,
96
 
                    0x50, 0x9d, 0xd6, 0x79, 0x59, 0x99, 0x50, 0xe5,
97
 
                    0x68, 0x1a, 0x98, 0xca, 0x11, 0xce, 0x37, 0x63,
98
 
                    0x58, 0x22, 0x40, 0x19, 0x29, 0x72, 0x4c, 0x41,
99
 
                    0x89, 0xb, 0x56, 0x9e, 0x3e, 0xd5, 0x6d, 0x75,
100
 
                    0x9e, 0x3f, 0x8a, 0x50, 0xf1, 0xa, 0x59, 0x4a,
101
 
                    0xc3, 0x59, 0x4b, 0xf6, 0xbb, 0xc9, 0xa5, 0x93 };
102
 
 
103
 
unsigned char cka_public_exponent[] =
104
 
                  { 0x3 };
105
 
 
106
 
unsigned char cka_prime_1[] =
107
 
                  { 0xfb, 0xb7, 0x73, 0x24, 0x42, 0xfe, 0x8f, 0x16,
108
 
                    0xf0, 0x6e, 0x2d, 0x86, 0x22, 0x46, 0x79, 0xd1,
109
 
                    0x58, 0x6f, 0x26, 0x24, 0x17, 0x12, 0xa3, 0x1a,
110
 
                    0xfd, 0xf7, 0x75, 0xd4, 0xcd, 0xf9, 0xde, 0x4b,
111
 
                    0x8c, 0xb7, 0x4, 0x5d, 0xd9, 0x18, 0xc8, 0x26,
112
 
                    0x61, 0x54, 0xe0, 0x92, 0x2f, 0x47, 0xf7, 0x33,
113
 
                    0xc2, 0x17, 0xd8, 0xda, 0xe0, 0x6d, 0xb6, 0x30,
114
 
                    0xd6, 0xdc, 0xf9, 0x6a, 0x4c, 0xa1, 0xa2, 0x4b };
115
 
 
116
 
unsigned char cka_prime_2[] =
117
 
                  { 0xf0, 0x57, 0x24, 0xf6, 0x2a, 0x5a, 0x6d, 0x8e,
118
 
                    0xb8, 0xc6, 0x6f, 0xd2, 0xbb, 0x36, 0x4f, 0x6d,
119
 
                    0xd8, 0xbc, 0xa7, 0x2f, 0xbd, 0x43, 0xdc, 0x9a,
120
 
                    0xe, 0x2a, 0x36, 0xb9, 0x21, 0x5, 0xfa, 0x22,
121
 
                    0x6c, 0xe8, 0x22, 0x68, 0x2f, 0x1c, 0xe8, 0x27,
122
 
                    0xc1, 0xed, 0x8, 0x7a, 0x43, 0x70, 0x7b, 0xe3,
123
 
                    0x46, 0x74, 0x2, 0x6e, 0xb2, 0xb1, 0xeb, 0x44,
124
 
                    0x72, 0x86, 0xd, 0x55, 0x3b, 0xc8, 0xbc, 0xd9 };
125
 
 
126
 
unsigned char cka_exponent_1[] =
127
 
                  { 0xa7, 0xcf, 0xa2, 0x18, 0x2c, 0xa9, 0xb4, 0xb9,
128
 
                    0xf5, 0x9e, 0xc9, 0x4, 0x16, 0xd9, 0xa6, 0x8b,
129
 
                    0x90, 0x4a, 0x19, 0x6d, 0x64, 0xb7, 0x17, 0x67,
130
 
                    0x53, 0xfa, 0x4e, 0x8d, 0xde, 0xa6, 0x94, 0x32,
131
 
                    0x5d, 0xcf, 0x58, 0x3e, 0x90, 0xbb, 0x30, 0x19,
132
 
                    0x96, 0x38, 0x95, 0xb6, 0xca, 0x2f, 0xfa, 0x22,
133
 
                    0x81, 0x65, 0x3b, 0x3c, 0x95, 0x9e, 0x79, 0x75,
134
 
                    0xe4, 0x93, 0x50, 0xf1, 0x88, 0x6b, 0xc1, 0x87 };
135
 
 
136
 
unsigned char cka_exponent_2[] =
137
 
                  { 0xa0, 0x3a, 0x18, 0xa4, 0x1c, 0x3c, 0x49, 0x9,
138
 
                    0xd0, 0x84, 0x4a, 0x8c, 0x7c, 0xce, 0xdf, 0x9e,
139
 
                    0x90, 0x7d, 0xc4, 0xca, 0x7e, 0x2d, 0x3d, 0xbc,
140
 
                    0x9, 0x71, 0x79, 0xd0, 0xc0, 0xae, 0xa6, 0xc1,
141
 
                    0x9d, 0xf0, 0x16, 0xf0, 0x1f, 0x68, 0x9a, 0xc5,
142
 
                    0x2b, 0xf3, 0x5a, 0xfc, 0x2c, 0xf5, 0xa7, 0xec,
143
 
                    0xd9, 0xa2, 0xac, 0x49, 0xcc, 0x76, 0x9c, 0xd8,
144
 
                    0x4c, 0x59, 0x5e, 0x38, 0xd2, 0x85, 0xd3, 0x3b };
145
 
 
146
 
unsigned char cka_coefficient[] =
147
 
                  { 0x83, 0xf1, 0xca, 0x6, 0x58, 0x4a, 0x4, 0x5e,
148
 
                    0x96, 0xb5, 0x30, 0x32, 0x40, 0x36, 0x48, 0xb9,
149
 
                    0x2, 0xc, 0xe3, 0x37, 0xb7, 0x51, 0xbc, 0x22,
150
 
                    0x26, 0x5d, 0x74, 0x3, 0x47, 0xd3, 0x33, 0x20,
151
 
                    0x8e, 0x75, 0x62, 0xf2, 0x9d, 0x4e, 0xc8, 0x7d,
152
 
                    0x5d, 0x8e, 0xb6, 0xd9, 0x69, 0x4a, 0x9a, 0xe1,
153
 
                    0x36, 0x6e, 0x1c, 0xbe, 0x8a, 0x14, 0xb1, 0x85,
154
 
                    0x39, 0x74, 0x7c, 0x25, 0xd8, 0xa4, 0x4f, 0xde };
155
 
 
156
 
int do_EncryptRSA_PKCS( void )
157
 
{
158
 
   CK_BYTE             data1[100];
159
 
   CK_BYTE             data2[256];
160
 
   CK_BYTE             cipher[256];
161
 
   CK_SLOT_ID          slot_id;
162
 
   CK_SESSION_HANDLE   session;
163
 
   CK_MECHANISM        mech;
164
 
   CK_OBJECT_HANDLE    publ_key, priv_key;
165
 
   CK_FLAGS            flags;
166
 
   CK_BYTE             user_pin[128];
167
 
   CK_ULONG            user_pin_len;
168
 
   CK_ULONG            i;
169
 
   CK_ULONG            len1, len2, cipherlen;
170
 
   CK_RV               rv;
171
 
   CK_OBJECT_CLASS  class = CKO_PUBLIC_KEY; 
172
 
   CK_KEY_TYPE      type= CKK_RSA;
173
 
   CK_OBJECT_CLASS  privclass = CKO_PRIVATE_KEY; 
174
 
   CK_BBOOL             true = TRUE;
175
 
   CK_BBOOL             false = FALSE;
176
 
 
177
 
 
178
 
   CK_ULONG  bits = 1024;
179
 
   CK_BYTE   pub_exp[] = { 0x3 };
180
 
 
181
 
   CK_ATTRIBUTE pub_tmpl[] = {
182
 
      {CKA_MODULUS_BITS,    &bits,    sizeof(bits)    },
183
 
      {CKA_PUBLIC_EXPONENT, &pub_exp, sizeof(pub_exp) },
184
 
   };
185
 
 
186
 
   printf("do_EncryptRSA_PKCS...\n");
187
 
 
188
 
   slot_id = SLOT_ID;
189
 
   flags = CKF_SERIAL_SESSION | CKF_RW_SESSION;
190
 
   rv = funcs->C_OpenSession( slot_id, flags, NULL, NULL, &session );
191
 
   if (rv != CKR_OK) {
192
 
      show_error("   C_OpenSession #1", rv );
193
 
      return FALSE;
194
 
   }
195
 
 
196
 
 
197
 
   if (get_user_pin(user_pin))
198
 
           return CKR_FUNCTION_FAILED;
199
 
   user_pin_len = (CK_ULONG)strlen((char *)user_pin);
200
 
 
201
 
   rv = funcs->C_Login( session, CKU_USER, user_pin, user_pin_len );
202
 
   if (rv != CKR_OK) {
203
 
      show_error("   C_Login #1", rv );
204
 
      return FALSE;
205
 
   }
206
 
 
207
 
 
208
 
   printf("GENERATING KEY \n");
209
 
   mech.mechanism      = CKM_RSA_PKCS_KEY_PAIR_GEN;
210
 
   mech.ulParameterLen = 0;
211
 
   mech.pParameter     = NULL;
212
 
 
213
 
   rv = funcs->C_GenerateKeyPair( session,   &mech,
214
 
                                  pub_tmpl,   2,
215
 
                                  NULL,       0,
216
 
                                  &publ_key, &priv_key );
217
 
   if (rv != CKR_OK) {
218
 
      show_error("   C_GenerateKeyPair #1", rv );
219
 
      return FALSE;
220
 
   }
221
 
 
222
 
   // now, encrypt some data
223
 
   //
224
 
   len1      = sizeof(data1);
225
 
   len2      = sizeof(data2);
226
 
   cipherlen = sizeof(cipher);
227
 
 
228
 
   for (i=0; i < len1; i++)
229
 
      data1[i] = i % 255;
230
 
 
231
 
   mech.mechanism      = CKM_RSA_PKCS;
232
 
   mech.ulParameterLen = 0;
233
 
   mech.pParameter     = NULL;
234
 
 
235
 
   rv = funcs->C_EncryptInit( session, &mech, publ_key );
236
 
   if (rv != CKR_OK) {
237
 
      show_error("   C_EncryptInit #1", rv );
238
 
      return FALSE;
239
 
   }
240
 
 
241
 
   rv = funcs->C_Encrypt( session, data1, len1, cipher, &cipherlen );
242
 
   if (rv != CKR_OK) {
243
 
      show_error("   C_Encrypt #1", rv );
244
 
      return FALSE;
245
 
   }
246
 
 
247
 
   printf("Cipher len %d \n", (int)cipherlen);
248
 
   //hex_dump_to_file("Ciphertext",cipher,cipherlen);
249
 
 
250
 
   // now, decrypt the data
251
 
   //
252
 
   rv = funcs->C_DecryptInit( session, &mech, priv_key );
253
 
   if (rv != CKR_OK) {
254
 
      show_error("   C_DecryptInit #1", rv );
255
 
      return FALSE;
256
 
   }
257
 
 
258
 
   rv = funcs->C_Decrypt( session, cipher, cipherlen, data2, &len2 );
259
 
   if (rv != CKR_OK) {
260
 
      show_error("   C_Decrypt #1", rv );
261
 
      return FALSE;
262
 
   }
263
 
 
264
 
   printf("Len from encrypt %d  from decrypt %d \n",(int)len1, (int)len2);
265
 
   //if (len1 != len2) {
266
 
   //   printf("   ERROR:  lengths don't match\n");
267
 
   //   return FALSE;
268
 
  // }
269
 
 
270
 
   //hex_dump_to_file("decrypted",data2,len2);
271
 
   for (i=0; i <len1; i++) {
272
 
      if (data1[i] != data2[i]) {
273
 
         printf("   ERROR:  mismatch at byte %d\n", (int)i );
274
 
      return FALSE;
275
 
   }
276
 
   }
277
 
 
278
 
   rv = funcs->C_CloseAllSessions( slot_id );
279
 
   if (rv != CKR_OK) {
280
 
      show_error("   C_CloseAllSessions #1", rv );
281
 
      return FALSE;
282
 
   }
283
 
 
284
 
 
285
 
   printf("Success.\n");
286
 
   return TRUE;
287
 
}
288
 
 
289
 
 
290
 
//
291
 
//
292
 
int
293
 
main( int argc, char **argv )
294
 
{
295
 
   CK_C_INITIALIZE_ARGS  cinit_args;
296
 
   int        rv, i;
297
 
 
298
 
   CK_BBOOL      no_init;
299
 
 
300
 
   SLOT_ID = 0;
301
 
   skip_token_obj = TRUE;
302
 
   no_init = FALSE;
303
 
 
304
 
 
305
 
   for (i=1; i < argc; i++) {
306
 
      if (strcmp(argv[i], "-noskip") == 0)
307
 
         skip_token_obj = FALSE;
308
 
 
309
 
      if (strcmp(argv[i], "-slot") == 0) {
310
 
         SLOT_ID = atoi(argv[i+1]);
311
 
         i++;
312
 
      }
313
 
      if (strcmp(argv[i], "-noinit") == 0)
314
 
         no_init = TRUE;
315
 
 
316
 
      if (strcmp(argv[i], "-h") == 0) {
317
 
         printf("usage:  %s [-noskip] [-slot <num>] [-h]\n\n", argv[0] );
318
 
         printf("By default, Slot #1 is used\n\n");
319
 
         printf("By default we skip anything that creates or modifies\n");
320
 
         printf("token objects to preserve flash lifetime.\n");
321
 
         return 0;
322
 
      }
323
 
   }
324
 
 
325
 
   printf("Using slot #%d...\n\n", (int)SLOT_ID );
326
 
 
327
 
   rv = do_GetFunctionList();
328
 
   if (rv != TRUE) {
329
 
           show_error("do_GetFunctionList", rv);
330
 
           return rv;
331
 
   }
332
 
 
333
 
   memset( &cinit_args, 0x0, sizeof(cinit_args) );
334
 
   cinit_args.flags = CKF_OS_LOCKING_OK;
335
 
 
336
 
   // SAB Add calls to ALL functions before the C_Initialize gets hit
337
 
 
338
 
   rv = funcs->C_Initialize( &cinit_args );
339
 
   if (rv != CKR_OK) {
340
 
           show_error("C_Initialize", rv);
341
 
           return rv;
342
 
   }
343
 
 
344
 
   rv = do_EncryptRSA_PKCS();
345
 
   if (rv != TRUE) {
346
 
           show_error("do_EncryptRSA_PKCS", rv);
347
 
           return rv;
348
 
   }
349
 
 
350
 
   rv = funcs->C_Finalize( NULL );
351
 
   if (rv != CKR_OK) {
352
 
           show_error("C_Finalize", rv);
353
 
           return rv;
354
 
   }
355
 
 
356
 
   return 0;
357
 
}