~ubuntu-branches/ubuntu/lucid/openssl/lucid-proposed

« back to all changes in this revision

Viewing changes to fips/fips_test_suite.c

  • Committer: Bazaar Package Importer
  • Author(s): Kurt Roeckx
  • Date: 2009-06-13 18:15:46 UTC
  • mto: (11.1.5 squeeze)
  • mto: This revision was merged to the branch mainline in revision 34.
  • Revision ID: james.westby@ubuntu.com-20090613181546-vbfntai3b009dl1u
Tags: upstream-0.9.8k
ImportĀ upstreamĀ versionĀ 0.9.8k

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ====================================================================
 
2
 * Copyright (c) 2003 The OpenSSL Project.  All rights reserved.
 
3
 *
 
4
 *
 
5
 * This command is intended as a test driver for the FIPS-140 testing
 
6
 * lab performing FIPS-140 validation.  It demonstrates the use of the
 
7
 * OpenSSL library ito perform a variety of common cryptographic
 
8
 * functions.  A power-up self test is demonstrated by deliberately
 
9
 * pointing to an invalid executable hash
 
10
 *
 
11
 * Contributed by Steve Marquess.
 
12
 *
 
13
 */
 
14
#include <stdio.h>
 
15
#include <assert.h>
 
16
#include <ctype.h>
 
17
#include <string.h>
 
18
#include <stdlib.h>
 
19
#include <openssl/aes.h>
 
20
#include <openssl/des.h>
 
21
#include <openssl/rsa.h>
 
22
#include <openssl/dsa.h>
 
23
#include <openssl/dh.h>
 
24
#include <openssl/hmac.h>
 
25
#include <openssl/err.h>
 
26
 
 
27
#include <openssl/bn.h>
 
28
#include <openssl/rand.h>
 
29
#include <openssl/sha.h>
 
30
 
 
31
 
 
32
#ifndef OPENSSL_FIPS
 
33
int main(int argc, char *argv[])
 
34
    {
 
35
    printf("No FIPS support\n");
 
36
    return(0);
 
37
    }
 
38
#else
 
39
 
 
40
#include <openssl/fips.h>
 
41
#include "fips_utl.h"
 
42
 
 
43
/* AES: encrypt and decrypt known plaintext, verify result matches original plaintext
 
44
*/
 
45
static int FIPS_aes_test(void)
 
46
        {
 
47
        int ret = 0;
 
48
        unsigned char pltmp[16];
 
49
        unsigned char citmp[16];
 
50
        unsigned char key[16] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
 
51
        unsigned char plaintext[16] = "etaonrishdlcu";
 
52
        EVP_CIPHER_CTX ctx;
 
53
        EVP_CIPHER_CTX_init(&ctx);
 
54
        if (EVP_CipherInit_ex(&ctx, EVP_aes_128_ecb(),NULL, key, NULL, 1) <= 0)
 
55
                goto err;
 
56
        EVP_Cipher(&ctx, citmp, plaintext, 16);
 
57
        if (EVP_CipherInit_ex(&ctx, EVP_aes_128_ecb(),NULL, key, NULL, 0) <= 0)
 
58
                goto err;
 
59
        EVP_Cipher(&ctx, pltmp, citmp, 16);
 
60
        if (memcmp(pltmp, plaintext, 16))
 
61
                goto err;
 
62
        ret = 1;
 
63
        err:
 
64
        EVP_CIPHER_CTX_cleanup(&ctx);
 
65
        return ret;
 
66
        }
 
67
 
 
68
static int FIPS_des3_test(void)
 
69
        {
 
70
        int ret = 0;
 
71
        unsigned char pltmp[8];
 
72
        unsigned char citmp[8];
 
73
        unsigned char key[] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,
 
74
                              19,20,21,22,23,24};
 
75
        unsigned char plaintext[] = { 'e', 't', 'a', 'o', 'n', 'r', 'i', 's' };
 
76
        EVP_CIPHER_CTX ctx;
 
77
        EVP_CIPHER_CTX_init(&ctx);
 
78
        if (EVP_CipherInit_ex(&ctx, EVP_des_ede3_ecb(),NULL, key, NULL, 1) <= 0)
 
79
                goto err;
 
80
        EVP_Cipher(&ctx, citmp, plaintext, 8);
 
81
        if (EVP_CipherInit_ex(&ctx, EVP_des_ede3_ecb(),NULL, key, NULL, 0) <= 0)
 
82
                goto err;
 
83
        EVP_Cipher(&ctx, pltmp, citmp, 8);
 
84
        if (memcmp(pltmp, plaintext, 8))
 
85
                goto err;
 
86
        ret = 1;
 
87
        err:
 
88
        EVP_CIPHER_CTX_cleanup(&ctx);
 
89
        return ret;
 
90
        }
 
91
 
 
92
/*
 
93
 * DSA: generate keys and sign, verify input plaintext.
 
94
 */
 
95
static int FIPS_dsa_test(int bad)
 
96
    {
 
97
    DSA *dsa = NULL;
 
98
    EVP_PKEY pk;
 
99
    unsigned char dgst[] = "etaonrishdlc";
 
100
    unsigned char buf[60];
 
101
    unsigned int slen;
 
102
    int r = 0;
 
103
    EVP_MD_CTX mctx;
 
104
 
 
105
    ERR_clear_error();
 
106
    EVP_MD_CTX_init(&mctx);
 
107
    dsa = FIPS_dsa_new();
 
108
    if (!dsa)
 
109
        goto end;
 
110
    if (!DSA_generate_parameters_ex(dsa, 1024,NULL,0,NULL,NULL,NULL))
 
111
        goto end;
 
112
    if (!DSA_generate_key(dsa))
 
113
        goto end;
 
114
    if (bad)
 
115
            BN_add_word(dsa->pub_key, 1);
 
116
 
 
117
    pk.type = EVP_PKEY_DSA;
 
118
    pk.pkey.dsa = dsa;
 
119
 
 
120
    if (!EVP_SignInit_ex(&mctx, EVP_dss1(), NULL))
 
121
        goto end;
 
122
    if (!EVP_SignUpdate(&mctx, dgst, sizeof(dgst) - 1))
 
123
        goto end;
 
124
    if (!EVP_SignFinal(&mctx, buf, &slen, &pk))
 
125
        goto end;
 
126
 
 
127
    if (!EVP_VerifyInit_ex(&mctx, EVP_dss1(), NULL))
 
128
        goto end;
 
129
    if (!EVP_VerifyUpdate(&mctx, dgst, sizeof(dgst) - 1))
 
130
        goto end;
 
131
    r = EVP_VerifyFinal(&mctx, buf, slen, &pk);
 
132
    end:
 
133
    EVP_MD_CTX_cleanup(&mctx);
 
134
    if (dsa)
 
135
          FIPS_dsa_free(dsa);
 
136
    if (r != 1)
 
137
        return 0;
 
138
    return 1;
 
139
    }
 
140
 
 
141
/*
 
142
 * RSA: generate keys and sign, verify input plaintext.
 
143
 */
 
144
static int FIPS_rsa_test(int bad)
 
145
    {
 
146
    RSA *key;
 
147
    unsigned char input_ptext[] = "etaonrishdlc";
 
148
    unsigned char buf[256];
 
149
    unsigned int slen;
 
150
    BIGNUM *bn;
 
151
    EVP_MD_CTX mctx;
 
152
    EVP_PKEY pk;
 
153
    int r = 0;
 
154
 
 
155
    ERR_clear_error();
 
156
    EVP_MD_CTX_init(&mctx);
 
157
    key = FIPS_rsa_new();
 
158
    bn = BN_new();
 
159
    if (!key || !bn)
 
160
        return 0;
 
161
    BN_set_word(bn, 65537);
 
162
    if (!RSA_generate_key_ex(key, 1024,bn,NULL))
 
163
        return 0;
 
164
    BN_free(bn);
 
165
    if (bad)
 
166
            BN_add_word(key->n, 1);
 
167
 
 
168
    pk.type = EVP_PKEY_RSA;
 
169
    pk.pkey.rsa = key;
 
170
 
 
171
    if (!EVP_SignInit_ex(&mctx, EVP_sha1(), NULL))
 
172
        goto end;
 
173
    if (!EVP_SignUpdate(&mctx, input_ptext, sizeof(input_ptext) - 1))
 
174
        goto end;
 
175
    if (!EVP_SignFinal(&mctx, buf, &slen, &pk))
 
176
        goto end;
 
177
 
 
178
    if (!EVP_VerifyInit_ex(&mctx, EVP_sha1(), NULL))
 
179
        goto end;
 
180
    if (!EVP_VerifyUpdate(&mctx, input_ptext, sizeof(input_ptext) - 1))
 
181
        goto end;
 
182
    r = EVP_VerifyFinal(&mctx, buf, slen, &pk);
 
183
    end:
 
184
    EVP_MD_CTX_cleanup(&mctx);
 
185
    if (key)
 
186
          FIPS_rsa_free(key);
 
187
    if (r != 1)
 
188
        return 0;
 
189
    return 1;
 
190
    }
 
191
 
 
192
/* SHA1: generate hash of known digest value and compare to known
 
193
   precomputed correct hash
 
194
*/
 
195
static int FIPS_sha1_test()
 
196
    {
 
197
    unsigned char digest[SHA_DIGEST_LENGTH] =
 
198
        { 0x11, 0xf1, 0x9a, 0x3a, 0xec, 0x1a, 0x1e, 0x8e, 0x65, 0xd4, 0x9a, 0x38, 0x0c, 0x8b, 0x1e, 0x2c, 0xe8, 0xb3, 0xc5, 0x18 };
 
199
    unsigned char str[] = "etaonrishd";
 
200
 
 
201
    unsigned char md[SHA_DIGEST_LENGTH];
 
202
 
 
203
    ERR_clear_error();
 
204
    if (!EVP_Digest(str,sizeof(str) - 1,md, NULL, EVP_sha1(), NULL)) return 0;
 
205
    if (memcmp(md,digest,sizeof(md)))
 
206
        return 0;
 
207
    return 1;
 
208
    }
 
209
 
 
210
/* SHA256: generate hash of known digest value and compare to known
 
211
   precomputed correct hash
 
212
*/
 
213
static int FIPS_sha256_test()
 
214
    {
 
215
    unsigned char digest[SHA256_DIGEST_LENGTH] =
 
216
        {0xf5, 0x53, 0xcd, 0xb8, 0xcf, 0x1, 0xee, 0x17, 0x9b, 0x93, 0xc9, 0x68, 0xc0, 0xea, 0x40, 0x91,
 
217
         0x6, 0xec, 0x8e, 0x11, 0x96, 0xc8, 0x5d, 0x1c, 0xaf, 0x64, 0x22, 0xe6, 0x50, 0x4f, 0x47, 0x57};
 
218
    unsigned char str[] = "etaonrishd";
 
219
 
 
220
    unsigned char md[SHA256_DIGEST_LENGTH];
 
221
 
 
222
    ERR_clear_error();
 
223
    if (!EVP_Digest(str,sizeof(str) - 1,md, NULL, EVP_sha256(), NULL)) return 0;
 
224
    if (memcmp(md,digest,sizeof(md)))
 
225
        return 0;
 
226
    return 1;
 
227
    }
 
228
 
 
229
/* SHA512: generate hash of known digest value and compare to known
 
230
   precomputed correct hash
 
231
*/
 
232
static int FIPS_sha512_test()
 
233
    {
 
234
    unsigned char digest[SHA512_DIGEST_LENGTH] =
 
235
        {0x99, 0xc9, 0xe9, 0x5b, 0x88, 0xd4, 0x78, 0x88, 0xdf, 0x88, 0x5f, 0x94, 0x71, 0x64, 0x28, 0xca,
 
236
         0x16, 0x1f, 0x3d, 0xf4, 0x1f, 0xf3, 0x0f, 0xc5, 0x03, 0x99, 0xb2, 0xd0, 0xe7, 0x0b, 0x94, 0x4a,
 
237
         0x45, 0xd2, 0x6c, 0x4f, 0x20, 0x06, 0xef, 0x71, 0xa9, 0x25, 0x7f, 0x24, 0xb1, 0xd9, 0x40, 0x22,
 
238
         0x49, 0x54, 0x10, 0xc2, 0x22, 0x9d, 0x27, 0xfe, 0xbd, 0xd6, 0xd6, 0xeb, 0x2d, 0x42, 0x1d, 0xa3};
 
239
    unsigned char str[] = "etaonrishd";
 
240
 
 
241
    unsigned char md[SHA512_DIGEST_LENGTH];
 
242
 
 
243
    ERR_clear_error();
 
244
    if (!EVP_Digest(str,sizeof(str) - 1,md, NULL, EVP_sha512(), NULL)) return 0;
 
245
    if (memcmp(md,digest,sizeof(md)))
 
246
        return 0;
 
247
    return 1;
 
248
    }
 
249
 
 
250
/* HMAC-SHA1: generate hash of known digest value and compare to known
 
251
   precomputed correct hash
 
252
*/
 
253
static int FIPS_hmac_sha1_test()
 
254
    {
 
255
    unsigned char key[] = "etaonrishd";
 
256
    unsigned char iv[] = "Sample text";
 
257
    unsigned char kaval[EVP_MAX_MD_SIZE] =
 
258
        {0x73, 0xf7, 0xa0, 0x48, 0xf8, 0x94, 0xed, 0xdd, 0x0a, 0xea, 0xea, 0x56, 0x1b, 0x61, 0x2e, 0x70,
 
259
         0xb2, 0xfb, 0xec, 0xc6};
 
260
 
 
261
    unsigned char out[EVP_MAX_MD_SIZE];
 
262
    unsigned int outlen;
 
263
 
 
264
    ERR_clear_error();
 
265
    if (!HMAC(EVP_sha1(),key,sizeof(key)-1,iv,sizeof(iv)-1,out,&outlen)) return 0;
 
266
    if (memcmp(out,kaval,outlen))
 
267
        return 0;
 
268
    return 1;
 
269
    }
 
270
 
 
271
/* HMAC-SHA224: generate hash of known digest value and compare to known
 
272
   precomputed correct hash
 
273
*/
 
274
static int FIPS_hmac_sha224_test()
 
275
    {
 
276
    unsigned char key[] = "etaonrishd";
 
277
    unsigned char iv[] = "Sample text";
 
278
    unsigned char kaval[EVP_MAX_MD_SIZE] =
 
279
        {0x75, 0x58, 0xd5, 0xbd, 0x55, 0x6d, 0x87, 0x0f, 0x75, 0xff, 0xbe, 0x1c, 0xb2, 0xf0, 0x20, 0x35,
 
280
         0xe5, 0x62, 0x49, 0xb6, 0x94, 0xb9, 0xfc, 0x65, 0x34, 0x33, 0x3a, 0x19};
 
281
 
 
282
    unsigned char out[EVP_MAX_MD_SIZE];
 
283
    unsigned int outlen;
 
284
 
 
285
    ERR_clear_error();
 
286
    if (!HMAC(EVP_sha224(),key,sizeof(key)-1,iv,sizeof(iv)-1,out,&outlen)) return 0;
 
287
    if (memcmp(out,kaval,outlen))
 
288
        return 0;
 
289
    return 1;
 
290
    }
 
291
 
 
292
/* HMAC-SHA256: generate hash of known digest value and compare to known
 
293
   precomputed correct hash
 
294
*/
 
295
static int FIPS_hmac_sha256_test()
 
296
    {
 
297
    unsigned char key[] = "etaonrishd";
 
298
    unsigned char iv[] = "Sample text";
 
299
    unsigned char kaval[EVP_MAX_MD_SIZE] =
 
300
        {0xe9, 0x17, 0xc1, 0x7b, 0x4c, 0x6b, 0x77, 0xda, 0xd2, 0x30, 0x36, 0x02, 0xf5, 0x72, 0x33, 0x87,
 
301
         0x9f, 0xc6, 0x6e, 0x7b, 0x7e, 0xa8, 0xea, 0xaa, 0x9f, 0xba, 0xee, 0x51, 0xff, 0xda, 0x24, 0xf4};
 
302
 
 
303
    unsigned char out[EVP_MAX_MD_SIZE];
 
304
    unsigned int outlen;
 
305
 
 
306
    ERR_clear_error();
 
307
    if (!HMAC(EVP_sha256(),key,sizeof(key)-1,iv,sizeof(iv)-1,out,&outlen)) return 0;
 
308
    if (memcmp(out,kaval,outlen))
 
309
        return 0;
 
310
    return 1;
 
311
    }
 
312
 
 
313
/* HMAC-SHA384: generate hash of known digest value and compare to known
 
314
   precomputed correct hash
 
315
*/
 
316
static int FIPS_hmac_sha384_test()
 
317
    {
 
318
    unsigned char key[] = "etaonrishd";
 
319
    unsigned char iv[] = "Sample text";
 
320
    unsigned char kaval[EVP_MAX_MD_SIZE] =
 
321
        {0xb2, 0x9d, 0x40, 0x58, 0x32, 0xc4, 0xe3, 0x31, 0xb6, 0x63, 0x08, 0x26, 0x99, 0xef, 0x3b, 0x10,
 
322
         0xe2, 0xdf, 0xf8, 0xff, 0xc6, 0xe1, 0x03, 0x29, 0x81, 0x2a, 0x1b, 0xac, 0xb0, 0x07, 0x39, 0x08,
 
323
         0xf3, 0x91, 0x35, 0x11, 0x76, 0xd6, 0x4c, 0x20, 0xfb, 0x4d, 0xc3, 0xf3, 0xb8, 0x9b, 0x88, 0x1c};
 
324
 
 
325
    unsigned char out[EVP_MAX_MD_SIZE];
 
326
    unsigned int outlen;
 
327
 
 
328
    ERR_clear_error();
 
329
    if (!HMAC(EVP_sha384(),key,sizeof(key)-1,iv,sizeof(iv)-1,out,&outlen)) return 0;
 
330
    if (memcmp(out,kaval,outlen))
 
331
        return 0;
 
332
    return 1;
 
333
    }
 
334
 
 
335
/* HMAC-SHA512: generate hash of known digest value and compare to known
 
336
   precomputed correct hash
 
337
*/
 
338
static int FIPS_hmac_sha512_test()
 
339
    {
 
340
    unsigned char key[] = "etaonrishd";
 
341
    unsigned char iv[] = "Sample text";
 
342
    unsigned char kaval[EVP_MAX_MD_SIZE] =
 
343
        {0xcd, 0x3e, 0xb9, 0x51, 0xb8, 0xbc, 0x7f, 0x9a, 0x23, 0xaf, 0xf3, 0x77, 0x59, 0x85, 0xa9, 0xe6,
 
344
         0xf7, 0xd1, 0x51, 0x96, 0x17, 0xe0, 0x92, 0xd8, 0xa6, 0x3b, 0xc1, 0xad, 0x7e, 0x24, 0xca, 0xb1,
 
345
         0xd7, 0x79, 0x0a, 0xa5, 0xea, 0x2c, 0x02, 0x58, 0x0b, 0xa6, 0x52, 0x6b, 0x61, 0x7f, 0xeb, 0x9c,
 
346
         0x47, 0x86, 0x5d, 0x74, 0x2b, 0x88, 0xdf, 0xee, 0x46, 0x69, 0x96, 0x3d, 0xa6, 0xd9, 0x2a, 0x53};
 
347
 
 
348
    unsigned char out[EVP_MAX_MD_SIZE];
 
349
    unsigned int outlen;
 
350
 
 
351
    ERR_clear_error();
 
352
    if (!HMAC(EVP_sha512(),key,sizeof(key)-1,iv,sizeof(iv)-1,out,&outlen)) return 0;
 
353
    if (memcmp(out,kaval,outlen))
 
354
        return 0;
 
355
    return 1;
 
356
    }
 
357
 
 
358
 
 
359
/* DH: generate shared parameters
 
360
*/
 
361
static int dh_test()
 
362
    {
 
363
    DH *dh;
 
364
    ERR_clear_error();
 
365
    dh = FIPS_dh_new();
 
366
    if (!dh)
 
367
        return 0;
 
368
    if (!DH_generate_parameters_ex(dh, 1024, 2, NULL))
 
369
        return 0;
 
370
    FIPS_dh_free(dh);
 
371
    return 1;
 
372
    }
 
373
 
 
374
/* Zeroize
 
375
*/
 
376
static int Zeroize()
 
377
    {
 
378
    RSA *key;
 
379
    BIGNUM *bn;
 
380
    unsigned char userkey[16] = 
 
381
        { 0x48, 0x50, 0xf0, 0xa3, 0x3a, 0xed, 0xd3, 0xaf, 0x6e, 0x47, 0x7f, 0x83, 0x02, 0xb1, 0x09, 0x68 };
 
382
    int i, n;
 
383
 
 
384
    key = FIPS_rsa_new();
 
385
    bn = BN_new();
 
386
    if (!key || !bn)
 
387
        return 0;
 
388
    BN_set_word(bn, 65537);
 
389
    if (!RSA_generate_key_ex(key, 1024,bn,NULL))
 
390
        return 0;
 
391
    BN_free(bn);
 
392
    
 
393
    n = BN_num_bytes(key->d);
 
394
    printf(" Generated %d byte RSA private key\n", n);
 
395
    printf("\tBN key before overwriting:\n");
 
396
    do_bn_print(stdout, key->d);
 
397
    BN_rand(key->d,n*8,-1,0);
 
398
    printf("\tBN key after overwriting:\n");
 
399
    do_bn_print(stdout, key->d);
 
400
 
 
401
    printf("\tchar buffer key before overwriting: \n\t\t");
 
402
    for(i = 0; i < sizeof(userkey); i++) printf("%02x", userkey[i]);
 
403
        printf("\n");
 
404
    RAND_bytes(userkey, sizeof userkey);
 
405
    printf("\tchar buffer key after overwriting: \n\t\t");
 
406
    for(i = 0; i < sizeof(userkey); i++) printf("%02x", userkey[i]);
 
407
        printf("\n");
 
408
 
 
409
    return 1;
 
410
    }
 
411
 
 
412
static int Error;
 
413
const char * Fail(const char *msg)
 
414
    {
 
415
    do_print_errors();
 
416
    Error++;
 
417
    return msg; 
 
418
    }
 
419
 
 
420
int main(int argc,char **argv)
 
421
    {
 
422
 
 
423
    int do_corrupt_rsa_keygen = 0, do_corrupt_dsa_keygen = 0;
 
424
    int bad_rsa = 0, bad_dsa = 0;
 
425
    int do_rng_stick = 0;
 
426
    int no_exit = 0;
 
427
 
 
428
    printf("\tFIPS-mode test application\n\n");
 
429
 
 
430
    /* Load entropy from external file, if any */
 
431
    RAND_load_file(".rnd", 1024);
 
432
 
 
433
    if (argv[1]) {
 
434
        /* Corrupted KAT tests */
 
435
        if (!strcmp(argv[1], "aes")) {
 
436
            FIPS_corrupt_aes();
 
437
            printf("AES encryption/decryption with corrupted KAT...\n");
 
438
        } else if (!strcmp(argv[1], "des")) {
 
439
            FIPS_corrupt_des();
 
440
            printf("DES3-ECB encryption/decryption with corrupted KAT...\n");
 
441
        } else if (!strcmp(argv[1], "dsa")) {
 
442
            FIPS_corrupt_dsa();
 
443
            printf("DSA key generation and signature validation with corrupted KAT...\n");
 
444
        } else if (!strcmp(argv[1], "rsa")) {
 
445
            FIPS_corrupt_rsa();
 
446
            printf("RSA key generation and signature validation with corrupted KAT...\n");
 
447
        } else if (!strcmp(argv[1], "rsakey")) {
 
448
            printf("RSA key generation and signature validation with corrupted key...\n");
 
449
            bad_rsa = 1;
 
450
            no_exit = 1;
 
451
        } else if (!strcmp(argv[1], "rsakeygen")) {
 
452
            do_corrupt_rsa_keygen = 1;
 
453
            no_exit = 1;
 
454
            printf("RSA key generation and signature validation with corrupted keygen...\n");
 
455
        } else if (!strcmp(argv[1], "dsakey")) {
 
456
            printf("DSA key generation and signature validation with corrupted key...\n");
 
457
            bad_dsa = 1;
 
458
            no_exit = 1;
 
459
        } else if (!strcmp(argv[1], "dsakeygen")) {
 
460
            do_corrupt_dsa_keygen = 1;
 
461
            no_exit = 1;
 
462
            printf("DSA key generation and signature validation with corrupted keygen...\n");
 
463
        } else if (!strcmp(argv[1], "sha1")) {
 
464
            FIPS_corrupt_sha1();
 
465
            printf("SHA-1 hash with corrupted KAT...\n");
 
466
        } else if (!strcmp(argv[1], "rng")) {
 
467
            FIPS_corrupt_rng();
 
468
        } else if (!strcmp(argv[1], "rngstick")) {
 
469
            do_rng_stick = 1;
 
470
            no_exit = 1;
 
471
            printf("RNG test with stuck continuous test...\n");
 
472
        } else {
 
473
            printf("Bad argument \"%s\"\n", argv[1]);
 
474
            exit(1);
 
475
        }
 
476
        if (!no_exit) {
 
477
                if (!FIPS_mode_set(1)) {
 
478
                    do_print_errors();
 
479
                    printf("Power-up self test failed\n");
 
480
                    exit(1);
 
481
                }
 
482
                printf("Power-up self test successful\n");
 
483
                exit(0);
 
484
        }
 
485
    }
 
486
 
 
487
    /* Non-Approved cryptographic operation
 
488
    */
 
489
    printf("1. Non-Approved cryptographic operation test...\n");
 
490
    printf("\ta. Included algorithm (D-H)...");
 
491
    printf( dh_test() ? "successful\n" :  Fail("FAILED!\n") );
 
492
 
 
493
    /* Power-up self test
 
494
    */
 
495
    ERR_clear_error();
 
496
    printf("2. Automatic power-up self test...");
 
497
    if (!FIPS_mode_set(1))
 
498
        {
 
499
        do_print_errors();
 
500
        printf(Fail("FAILED!\n"));
 
501
        exit(1);
 
502
        }
 
503
    printf("successful\n");
 
504
    if (do_corrupt_dsa_keygen)
 
505
            FIPS_corrupt_dsa_keygen();
 
506
    if (do_corrupt_rsa_keygen)
 
507
            FIPS_corrupt_rsa_keygen();
 
508
    if (do_rng_stick)
 
509
            FIPS_rng_stick();
 
510
 
 
511
    /* AES encryption/decryption
 
512
    */
 
513
    printf("3. AES encryption/decryption...");
 
514
    printf( FIPS_aes_test() ? "successful\n" :  Fail("FAILED!\n") );
 
515
 
 
516
    /* RSA key generation and encryption/decryption
 
517
    */
 
518
    printf("4. RSA key generation and encryption/decryption...");
 
519
    printf( FIPS_rsa_test(bad_rsa) ? "successful\n" :  Fail("FAILED!\n") );
 
520
 
 
521
    /* DES-CBC encryption/decryption
 
522
    */
 
523
    printf("5. DES-ECB encryption/decryption...");
 
524
    printf( FIPS_des3_test() ? "successful\n" :  Fail("FAILED!\n") );
 
525
 
 
526
    /* DSA key generation and signature validation
 
527
    */
 
528
    printf("6. DSA key generation and signature validation...");
 
529
    printf( FIPS_dsa_test(bad_dsa) ? "successful\n" :  Fail("FAILED!\n") );
 
530
 
 
531
    /* SHA-1 hash
 
532
    */
 
533
    printf("7a. SHA-1 hash...");
 
534
    printf( FIPS_sha1_test() ? "successful\n" :  Fail("FAILED!\n") );
 
535
 
 
536
    /* SHA-256 hash
 
537
    */
 
538
    printf("7b. SHA-256 hash...");
 
539
    printf( FIPS_sha256_test() ? "successful\n" :  Fail("FAILED!\n") );
 
540
 
 
541
    /* SHA-512 hash
 
542
    */
 
543
    printf("7c. SHA-512 hash...");
 
544
    printf( FIPS_sha512_test() ? "successful\n" :  Fail("FAILED!\n") );
 
545
 
 
546
    /* HMAC-SHA-1 hash
 
547
    */
 
548
    printf("7d. HMAC-SHA-1 hash...");
 
549
    printf( FIPS_hmac_sha1_test() ? "successful\n" :  Fail("FAILED!\n") );
 
550
 
 
551
    /* HMAC-SHA-224 hash
 
552
    */
 
553
    printf("7e. HMAC-SHA-224 hash...");
 
554
    printf( FIPS_hmac_sha224_test() ? "successful\n" :  Fail("FAILED!\n") );
 
555
 
 
556
    /* HMAC-SHA-256 hash
 
557
    */
 
558
    printf("7f. HMAC-SHA-256 hash...");
 
559
    printf( FIPS_hmac_sha256_test() ? "successful\n" :  Fail("FAILED!\n") );
 
560
 
 
561
    /* HMAC-SHA-384 hash
 
562
    */
 
563
    printf("7g. HMAC-SHA-384 hash...");
 
564
    printf( FIPS_hmac_sha384_test() ? "successful\n" :  Fail("FAILED!\n") );
 
565
 
 
566
    /* HMAC-SHA-512 hash
 
567
    */
 
568
    printf("7h. HMAC-SHA-512 hash...");
 
569
    printf( FIPS_hmac_sha512_test() ? "successful\n" :  Fail("FAILED!\n") );
 
570
 
 
571
    /* Non-Approved cryptographic operation
 
572
    */
 
573
    printf("8. Non-Approved cryptographic operation test...\n");
 
574
    printf("\ta. Included algorithm (D-H)...");
 
575
    printf( dh_test() ? "successful as expected\n"
 
576
            : Fail("failed INCORRECTLY!\n") );
 
577
 
 
578
    /* Zeroization
 
579
    */
 
580
    printf("9. Zero-ization...\n");
 
581
    printf( Zeroize() ? "\tsuccessful as expected\n"
 
582
            : Fail("\tfailed INCORRECTLY!\n") );
 
583
 
 
584
    printf("\nAll tests completed with %d errors\n", Error);
 
585
    return Error ? 1 : 0;
 
586
    }
 
587
 
 
588
#endif