~ubuntu-branches/ubuntu/intrepid/gnunet/intrepid

« back to all changes in this revision

Viewing changes to src/util/crypto/hostkeytest.c

  • Committer: Bazaar Package Importer
  • Author(s): Stephan Hermann
  • Date: 2008-01-31 17:40:18 UTC
  • mfrom: (1.2.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20080131174018-sfnbb8wv7p4nmut1
Tags: 0.7.3-2ubuntu1
* Merge from debian unstable, remaining changes:
  - debian/rules:
    = Make use of code from cdbs' clean-la.mk file to clear the
      dependency_libs field in all .la files in the gnunet-dev package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
#include "platform.h"
30
30
 
31
31
#define TESTSTRING "Hello World\0"
32
 
#define MAX_TESTVAL sizeof(SESSIONKEY)
 
32
#define MAX_TESTVAL sizeof(GNUNET_AES_SessionKey)
33
33
#define ITER 10
34
34
 
35
 
#define PERF NO
 
35
#define PERF GNUNET_NO
36
36
 
37
37
static int
38
38
testEncryptDecrypt ()
39
39
{
40
 
  struct PrivateKey *hostkey;
41
 
  PublicKey pkey;
42
 
  RSAEncryptedData target;
 
40
  struct GNUNET_RSA_PrivateKey *hostkey;
 
41
  GNUNET_RSA_PublicKey pkey;
 
42
  GNUNET_RSA_EncryptedData target;
43
43
  char result[MAX_TESTVAL];
44
44
  int i;
45
 
  TIME_T start;
 
45
  GNUNET_Int32Time start;
46
46
  int ok;
47
47
 
48
48
  fprintf (stderr, "W");
49
 
  hostkey = makePrivateKey ();
50
 
  getPublicKey (hostkey, &pkey);
 
49
  hostkey = GNUNET_RSA_create_key ();
 
50
  GNUNET_RSA_get_public_key (hostkey, &pkey);
51
51
 
52
52
  ok = 0;
53
 
  TIME (&start);
 
53
  GNUNET_get_time_int32 (&start);
54
54
  for (i = 0; i < ITER; i++)
55
55
    {
56
56
      fprintf (stderr, ".");
57
 
      if (SYSERR == encryptPrivateKey (TESTSTRING,
58
 
                                       strlen (TESTSTRING) + 1,
59
 
                                       &pkey, &target))
 
57
      if (GNUNET_SYSERR == GNUNET_RSA_encrypt (TESTSTRING,
 
58
                                               strlen (TESTSTRING) + 1,
 
59
                                               &pkey, &target))
60
60
        {
61
61
          fprintf (stderr, "encryptPrivateKey returned SYSERR\n");
62
62
          ok++;
63
63
          continue;
64
64
        }
65
 
      if (-1 == decryptPrivateKey (hostkey,
66
 
                                   &target, result, strlen (TESTSTRING) + 1))
 
65
      if (-1 == GNUNET_RSA_decrypt (hostkey,
 
66
                                    &target, result, strlen (TESTSTRING) + 1))
67
67
        {
68
68
          fprintf (stderr, "decryptPrivateKey returned SYSERR\n");
69
69
          ok++;
79
79
        }
80
80
    }
81
81
  printf ("%d RSA encrypt/decrypt operations %ds (%d failures)\n",
82
 
          ITER, (int) (TIME (NULL) - start), ok);
83
 
  freePrivateKey (hostkey);
 
82
          ITER, (int) (GNUNET_get_time_int32 (NULL) - start), ok);
 
83
  GNUNET_RSA_free_key (hostkey);
84
84
  if (ok == 0)
85
 
    return OK;
 
85
    return GNUNET_OK;
86
86
  else
87
 
    return SYSERR;
 
87
    return GNUNET_SYSERR;
88
88
}
89
89
 
90
90
#if PERF
91
91
static int
92
92
testEncryptPerformance ()
93
93
{
94
 
  struct PrivateKey *hostkey;
95
 
  PublicKey pkey;
96
 
  RSAEncryptedData target;
 
94
  struct GNUNET_RSA_PrivateKey *hostkey;
 
95
  GNUNET_RSA_PublicKey pkey;
 
96
  GNUNET_RSA_EncryptedData target;
97
97
  int i;
98
 
  cron_t start;
 
98
  GNUNET_CronTime start;
99
99
  int ok;
100
100
 
101
101
  fprintf (stderr, "W");
102
 
  hostkey = makePrivateKey ();
103
 
  getPublicKey (hostkey, &pkey);
 
102
  hostkey = GNUNET_RSA_create_key ();
 
103
  GNUNET_RSA_get_public_key (hostkey, &pkey);
104
104
 
105
105
  ok = 0;
106
 
  start = get_time ();
 
106
  start = GNUNET_get_time ();
107
107
  for (i = 0; i < ITER; i++)
108
108
    {
109
109
      fprintf (stderr, ".");
110
 
      if (SYSERR == encryptPrivateKey (TESTSTRING,
111
 
                                       strlen (TESTSTRING) + 1,
112
 
                                       &pkey, &target))
 
110
      if (GNUNET_SYSERR == GNUNET_RSA_encrypt (TESTSTRING,
 
111
                                               strlen (TESTSTRING) + 1,
 
112
                                               &pkey, &target))
113
113
        {
114
114
          fprintf (stderr, "encryptPrivateKey returned SYSERR\n");
115
115
          ok++;
117
117
        }
118
118
    }
119
119
  printf ("%d RSA encrypt operations %llu ms (%d failures)\n",
120
 
          ITER, get_time () - start, ok);
121
 
  freePrivateKey (hostkey);
 
120
          ITER, GNUNET_get_time () - start, ok);
 
121
  GNUNET_RSA_free_key (hostkey);
122
122
  if (ok != 0)
123
 
    return SYSERR;
124
 
  return OK;
 
123
    return GNUNET_SYSERR;
 
124
  return GNUNET_OK;
125
125
}
126
126
#endif
127
127
 
128
128
static int
129
129
testEncryptDecryptSK ()
130
130
{
131
 
  struct PrivateKey *hostkey;
132
 
  PublicKey pkey;
133
 
  RSAEncryptedData target;
134
 
  SESSIONKEY insk;
135
 
  SESSIONKEY outsk;
 
131
  struct GNUNET_RSA_PrivateKey *hostkey;
 
132
  GNUNET_RSA_PublicKey pkey;
 
133
  GNUNET_RSA_EncryptedData target;
 
134
  GNUNET_AES_SessionKey insk;
 
135
  GNUNET_AES_SessionKey outsk;
136
136
  int i;
137
 
  TIME_T start;
 
137
  GNUNET_Int32Time start;
138
138
  int ok;
139
139
 
140
140
  fprintf (stderr, "W");
141
 
  hostkey = makePrivateKey ();
142
 
  getPublicKey (hostkey, &pkey);
 
141
  hostkey = GNUNET_RSA_create_key ();
 
142
  GNUNET_RSA_get_public_key (hostkey, &pkey);
143
143
 
144
144
  ok = 0;
145
 
  TIME (&start);
 
145
  GNUNET_get_time_int32 (&start);
146
146
  for (i = 0; i < ITER; i++)
147
147
    {
148
148
      fprintf (stderr, ".");
149
 
      makeSessionkey (&insk);
150
 
      if (SYSERR == encryptPrivateKey (&insk,
151
 
                                       sizeof (SESSIONKEY), &pkey, &target))
 
149
      GNUNET_AES_create_session_key (&insk);
 
150
      if (GNUNET_SYSERR == GNUNET_RSA_encrypt (&insk,
 
151
                                               sizeof (GNUNET_AES_SessionKey),
 
152
                                               &pkey, &target))
152
153
        {
153
154
          fprintf (stderr, "encryptPrivateKey returned SYSERR\n");
154
155
          ok++;
155
156
          continue;
156
157
        }
157
 
      if (-1 == decryptPrivateKey (hostkey,
158
 
                                   &target, &outsk, sizeof (SESSIONKEY)))
 
158
      if (-1 == GNUNET_RSA_decrypt (hostkey,
 
159
                                    &target, &outsk,
 
160
                                    sizeof (GNUNET_AES_SessionKey)))
159
161
        {
160
162
          fprintf (stderr, "decryptPrivateKey returned SYSERR\n");
161
163
          ok++;
162
164
          continue;
163
165
        }
164
 
      if (0 != memcmp (&insk, &outsk, sizeof (SESSIONKEY)))
 
166
      if (0 != memcmp (&insk, &outsk, sizeof (GNUNET_AES_SessionKey)))
165
167
        {
166
168
          printf ("testEncryptDecryptSK failed!\n");
167
169
          ok++;
169
171
        }
170
172
    }
171
173
  printf ("%d RSA encrypt/decrypt SK operations %ds (%d failures)\n",
172
 
          ITER, (int) (TIME (NULL) - start), ok);
173
 
  freePrivateKey (hostkey);
 
174
          ITER, (int) (GNUNET_get_time_int32 (NULL) - start), ok);
 
175
  GNUNET_RSA_free_key (hostkey);
174
176
  if (ok != 0)
175
 
    return SYSERR;
176
 
  return OK;
 
177
    return GNUNET_SYSERR;
 
178
  return GNUNET_OK;
177
179
}
178
180
 
179
181
static int
180
182
testSignVerify ()
181
183
{
182
 
  struct PrivateKey *hostkey;
183
 
  Signature sig;
184
 
  PublicKey pkey;
 
184
  struct GNUNET_RSA_PrivateKey *hostkey;
 
185
  GNUNET_RSA_Signature sig;
 
186
  GNUNET_RSA_PublicKey pkey;
185
187
  int i;
186
 
  TIME_T start;
187
 
  int ok = OK;
 
188
  GNUNET_Int32Time start;
 
189
  int ok = GNUNET_OK;
188
190
 
189
191
  fprintf (stderr, "W");
190
 
  hostkey = makePrivateKey ();
191
 
  getPublicKey (hostkey, &pkey);
192
 
  TIME (&start);
 
192
  hostkey = GNUNET_RSA_create_key ();
 
193
  GNUNET_RSA_get_public_key (hostkey, &pkey);
 
194
  GNUNET_get_time_int32 (&start);
193
195
  for (i = 0; i < ITER; i++)
194
196
    {
195
197
      fprintf (stderr, ".");
196
 
      if (SYSERR == sign (hostkey, strlen (TESTSTRING), TESTSTRING, &sig))
 
198
      if (GNUNET_SYSERR ==
 
199
          GNUNET_RSA_sign (hostkey, strlen (TESTSTRING), TESTSTRING, &sig))
197
200
        {
198
201
          fprintf (stderr, "sign returned SYSERR\n");
199
 
          ok = SYSERR;
 
202
          ok = GNUNET_SYSERR;
200
203
          continue;
201
204
        }
202
 
      if (SYSERR == verifySig (TESTSTRING, strlen (TESTSTRING), &sig, &pkey))
 
205
      if (GNUNET_SYSERR ==
 
206
          GNUNET_RSA_verify (TESTSTRING, strlen (TESTSTRING), &sig, &pkey))
203
207
        {
204
208
          printf ("testSignVerify failed!\n");
205
 
          ok = SYSERR;
 
209
          ok = GNUNET_SYSERR;
206
210
          continue;
207
211
        }
208
212
    }
209
213
  printf ("%d RSA sign/verify operations %ds\n",
210
 
          ITER, (int) (TIME (NULL) - start));
211
 
  freePrivateKey (hostkey);
 
214
          ITER, (int) (GNUNET_get_time_int32 (NULL) - start));
 
215
  GNUNET_RSA_free_key (hostkey);
212
216
  return ok;
213
217
}
214
218
 
216
220
static int
217
221
testSignPerformance ()
218
222
{
219
 
  struct PrivateKey *hostkey;
220
 
  Signature sig;
221
 
  PublicKey pkey;
 
223
  struct GNUNET_RSA_PrivateKey *hostkey;
 
224
  GNUNET_RSA_Signature sig;
 
225
  GNUNET_RSA_PublicKey pkey;
222
226
  int i;
223
 
  cron_t start;
224
 
  int ok = OK;
 
227
  GNUNET_CronTime start;
 
228
  int ok = GNUNET_OK;
225
229
 
226
230
  fprintf (stderr, "W");
227
 
  hostkey = makePrivateKey ();
228
 
  getPublicKey (hostkey, &pkey);
229
 
  start = get_time ();
 
231
  hostkey = GNUNET_RSA_create_key ();
 
232
  GNUNET_RSA_get_public_key (hostkey, &pkey);
 
233
  start = GNUNET_get_time ();
230
234
  for (i = 0; i < ITER; i++)
231
235
    {
232
236
      fprintf (stderr, ".");
233
 
      if (SYSERR == sign (hostkey, strlen (TESTSTRING), TESTSTRING, &sig))
 
237
      if (GNUNET_SYSERR ==
 
238
          GNUNET_RSA_sign (hostkey, strlen (TESTSTRING), TESTSTRING, &sig))
234
239
        {
235
240
          fprintf (stderr, "sign returned SYSERR\n");
236
 
          ok = SYSERR;
 
241
          ok = GNUNET_SYSERR;
237
242
          continue;
238
243
        }
239
244
    }
240
 
  printf ("%d RSA sign operations %llu ms\n", ITER, get_time () - start);
241
 
  freePrivateKey (hostkey);
 
245
  printf ("%d RSA sign operations %llu ms\n", ITER,
 
246
          GNUNET_get_time () - start);
 
247
  GNUNET_RSA_free_key (hostkey);
242
248
  return ok;
243
249
}
244
250
#endif
246
252
static int
247
253
testPrivateKeyEncoding ()
248
254
{
249
 
  struct PrivateKey *hostkey;
250
 
  PrivateKeyEncoded *encoding;
251
 
  PublicKey pkey;
252
 
  RSAEncryptedData target;
 
255
  struct GNUNET_RSA_PrivateKey *hostkey;
 
256
  GNUNET_RSA_PrivateKeyEncoded *encoding;
 
257
  GNUNET_RSA_PublicKey pkey;
 
258
  GNUNET_RSA_EncryptedData target;
253
259
  char result[MAX_TESTVAL];
254
260
  int i;
255
 
  TIME_T start;
256
 
  int ok = OK;
 
261
  GNUNET_Int32Time start;
 
262
  int ok = GNUNET_OK;
257
263
 
258
264
  fprintf (stderr, "W");
259
 
  hostkey = makePrivateKey ();
 
265
  hostkey = GNUNET_RSA_create_key ();
260
266
 
261
 
  TIME (&start);
 
267
  GNUNET_get_time_int32 (&start);
262
268
  for (i = 0; i < ITER; i++)
263
269
    {
264
270
      fprintf (stderr, ".");
265
 
      getPublicKey (hostkey, &pkey);
266
 
      if (SYSERR == encryptPrivateKey (TESTSTRING,
267
 
                                       strlen (TESTSTRING) + 1,
268
 
                                       &pkey, &target))
 
271
      GNUNET_RSA_get_public_key (hostkey, &pkey);
 
272
      if (GNUNET_SYSERR == GNUNET_RSA_encrypt (TESTSTRING,
 
273
                                               strlen (TESTSTRING) + 1,
 
274
                                               &pkey, &target))
269
275
        {
270
276
          fprintf (stderr, "encryptPrivateKey returned SYSERR\n");
271
 
          ok = SYSERR;
 
277
          ok = GNUNET_SYSERR;
272
278
          continue;
273
279
        }
274
 
      encoding = encodePrivateKey (hostkey);
275
 
      freePrivateKey (hostkey);
 
280
      encoding = GNUNET_RSA_encode_key (hostkey);
 
281
      GNUNET_RSA_free_key (hostkey);
276
282
      if (encoding == NULL)
277
283
        {
278
284
          fprintf (stderr, "encodePrivateKey returned NULL\n");
279
 
          ok = SYSERR;
 
285
          ok = GNUNET_SYSERR;
280
286
          continue;
281
287
        }
282
 
      hostkey = decodePrivateKey (encoding);
283
 
      FREE (encoding);
284
 
      if (SYSERR == decryptPrivateKey (hostkey,
285
 
                                       &target,
286
 
                                       result, strlen (TESTSTRING) + 1))
 
288
      hostkey = GNUNET_RSA_decode_key (encoding);
 
289
      GNUNET_free (encoding);
 
290
      if (GNUNET_SYSERR == GNUNET_RSA_decrypt (hostkey,
 
291
                                               &target,
 
292
                                               result,
 
293
                                               strlen (TESTSTRING) + 1))
287
294
        {
288
295
          fprintf (stderr, "decryptPrivateKey returned SYSERR\n");
289
 
          ok = SYSERR;
 
296
          ok = GNUNET_SYSERR;
290
297
          continue;
291
298
        }
292
299
      if (strncmp (TESTSTRING, result, strlen (TESTSTRING)) != 0)
293
300
        {
294
301
          printf ("%s != %.*s - testEncryptDecrypt failed!\n",
295
302
                  TESTSTRING, (int) strlen (TESTSTRING), result);
296
 
          ok = SYSERR;
 
303
          ok = GNUNET_SYSERR;
297
304
          continue;
298
305
        }
299
306
    }
300
 
  freePrivateKey (hostkey);
 
307
  GNUNET_RSA_free_key (hostkey);
301
308
  printf ("%d RSA encrypt/encode/decode/decrypt operations %ds\n",
302
 
          ITER, (int) (TIME (NULL) - start));
 
309
          ITER, (int) (GNUNET_get_time_int32 (NULL) - start));
303
310
  return ok;
304
311
}
305
312
 
309
316
  int failureCount = 0;
310
317
 
311
318
#if PERF
312
 
  if (OK != testEncryptPerformance ())
 
319
  if (GNUNET_OK != testEncryptPerformance ())
313
320
    failureCount++;
314
 
  if (OK != testSignPerformance ())
 
321
  if (GNUNET_OK != testSignPerformance ())
315
322
    failureCount++;
316
323
#endif
317
 
  if (OK != testEncryptDecryptSK ())
318
 
    failureCount++;
319
 
  if (OK != testEncryptDecrypt ())
320
 
    failureCount++;
321
 
  if (OK != testSignVerify ())
322
 
    failureCount++;
323
 
  if (OK != testPrivateKeyEncoding ())
 
324
  if (GNUNET_OK != testEncryptDecryptSK ())
 
325
    failureCount++;
 
326
  if (GNUNET_OK != testEncryptDecrypt ())
 
327
    failureCount++;
 
328
  if (GNUNET_OK != testSignVerify ())
 
329
    failureCount++;
 
330
  if (GNUNET_OK != testPrivateKeyEncoding ())
324
331
    failureCount++;
325
332
 
326
333
  if (failureCount != 0)