5ea4be1e08b3c3a9d47a6e848de6ea8aec52aada
[openssl.git] / fips / fips.c
1 /* ====================================================================
2  * Copyright (c) 2003 The OpenSSL Project.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer. 
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *    the documentation and/or other materials provided with the
14  *    distribution.
15  *
16  * 3. All advertising materials mentioning features or use of this
17  *    software must display the following acknowledgment:
18  *    "This product includes software developed by the OpenSSL Project
19  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
20  *
21  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22  *    endorse or promote products derived from this software without
23  *    prior written permission. For written permission, please contact
24  *    openssl-core@openssl.org.
25  *
26  * 5. Products derived from this software may not be called "OpenSSL"
27  *    nor may "OpenSSL" appear in their names without prior written
28  *    permission of the OpenSSL Project.
29  *
30  * 6. Redistributions of any form whatsoever must retain the following
31  *    acknowledgment:
32  *    "This product includes software developed by the OpenSSL Project
33  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46  * OF THE POSSIBILITY OF SUCH DAMAGE.
47  *
48  */
49
50 #define OPENSSL_FIPSAPI
51
52 #include <openssl/crypto.h>
53 #include <openssl/rand.h>
54 #include <openssl/fips_rand.h>
55 #include <openssl/err.h>
56 #include <openssl/bio.h>
57 #include <openssl/hmac.h>
58 #include <openssl/rsa.h>
59 #include <openssl/dsa.h>
60 #include <openssl/ecdsa.h>
61 #include <string.h>
62 #include <limits.h>
63 #include "fips_locl.h"
64
65 #ifdef OPENSSL_FIPS
66
67 #include <openssl/fips.h>
68
69 #ifndef PATH_MAX
70 #define PATH_MAX 1024
71 #endif
72
73 static int fips_selftest_fail;
74 static int fips_mode;
75 static int fips_started = 0;
76 static const void *fips_rand_check;
77
78 static int fips_is_owning_thread(void);
79 static int fips_set_owning_thread(void);
80 static int fips_clear_owning_thread(void);
81 static unsigned char *fips_signature_witness(void);
82
83 static void fips_w_lock(void)   { CRYPTO_w_lock(CRYPTO_LOCK_FIPS); }
84 static void fips_w_unlock(void) { CRYPTO_w_unlock(CRYPTO_LOCK_FIPS); }
85 static void fips_r_lock(void)   { CRYPTO_r_lock(CRYPTO_LOCK_FIPS); }
86 static void fips_r_unlock(void) { CRYPTO_r_unlock(CRYPTO_LOCK_FIPS); }
87
88 static void fips_set_mode(int onoff)
89         {
90         int owning_thread = fips_is_owning_thread();
91
92         if (fips_started)
93                 {
94                 if (!owning_thread) fips_w_lock();
95                 fips_mode = onoff;
96                 if (!owning_thread) fips_w_unlock();
97                 }
98         }
99
100 static void fips_set_rand_check(const void *rand_check)
101         {
102         int owning_thread = fips_is_owning_thread();
103
104         if (fips_started)
105                 {
106                 if (!owning_thread) fips_w_lock();
107                 fips_rand_check = rand_check;
108                 if (!owning_thread) fips_w_unlock();
109                 }
110         }
111
112 int FIPS_mode(void)
113         {
114         int ret = 0;
115         int owning_thread = fips_is_owning_thread();
116
117         if (fips_started)
118                 {
119                 if (!owning_thread) fips_r_lock();
120                 ret = fips_mode;
121                 if (!owning_thread) fips_r_unlock();
122                 }
123         return ret;
124         }
125
126 const void *FIPS_rand_check(void)
127         {
128         const void *ret = 0;
129         int owning_thread = fips_is_owning_thread();
130
131         if (fips_started)
132                 {
133                 if (!owning_thread) fips_r_lock();
134                 ret = fips_rand_check;
135                 if (!owning_thread) fips_r_unlock();
136                 }
137         return ret;
138         }
139
140 int FIPS_selftest_failed(void)
141     {
142     int ret = 0;
143     if (fips_started)
144         {
145         int owning_thread = fips_is_owning_thread();
146
147         if (!owning_thread) fips_r_lock();
148         ret = fips_selftest_fail;
149         if (!owning_thread) fips_r_unlock();
150         }
151     return ret;
152     }
153
154 /* Selftest failure fatal exit routine. This will be called
155  * during *any* cryptographic operation. It has the minimum
156  * overhead possible to avoid too big a performance hit.
157  */
158
159 void FIPS_selftest_check(void)
160     {
161     if (fips_selftest_fail)
162         {
163         OpenSSLDie(__FILE__,__LINE__, "FATAL FIPS SELFTEST FAILURE");
164         }
165     }
166
167 void fips_set_selftest_fail(void)
168     {
169     fips_selftest_fail = 1;
170     }
171
172 int FIPS_selftest(void)
173     {
174
175     return FIPS_selftest_sha1()
176         && FIPS_selftest_hmac()
177         && FIPS_selftest_cmac()
178         && FIPS_selftest_aes()
179         && FIPS_selftest_aes_gcm()
180         && FIPS_selftest_des()
181         && FIPS_selftest_rsa()
182         && FIPS_selftest_ecdsa()
183         && FIPS_selftest_dsa();
184     }
185
186 extern const void         *FIPS_text_start(),  *FIPS_text_end();
187 extern const unsigned char FIPS_rodata_start[], FIPS_rodata_end[];
188 unsigned char              FIPS_signature [20] = { 0 };
189 static const char          FIPS_hmac_key[]="etaonrishdlcupfm";
190
191 unsigned int FIPS_incore_fingerprint(unsigned char *sig,unsigned int len)
192     {
193     const unsigned char *p1 = FIPS_text_start();
194     const unsigned char *p2 = FIPS_text_end();
195     const unsigned char *p3 = FIPS_rodata_start;
196     const unsigned char *p4 = FIPS_rodata_end;
197     HMAC_CTX c;
198
199     HMAC_CTX_init(&c);
200     HMAC_Init(&c,FIPS_hmac_key,strlen(FIPS_hmac_key),EVP_sha1());
201
202     /* detect overlapping regions */
203     if (p1<=p3 && p2>=p3)
204         p3=p1, p4=p2>p4?p2:p4, p1=NULL, p2=NULL;
205     else if (p3<=p1 && p4>=p1)
206         p3=p3, p4=p2>p4?p2:p4, p1=NULL, p2=NULL;
207
208     if (p1)
209         HMAC_Update(&c,p1,(size_t)p2-(size_t)p1);
210
211     if (FIPS_signature>=p3 && FIPS_signature<p4)
212         {
213         /* "punch" hole */
214         HMAC_Update(&c,p3,(size_t)FIPS_signature-(size_t)p3);
215         p3 = FIPS_signature+sizeof(FIPS_signature);
216         if (p3<p4)
217             HMAC_Update(&c,p3,(size_t)p4-(size_t)p3);
218         }
219     else
220         HMAC_Update(&c,p3,(size_t)p4-(size_t)p3);
221
222     HMAC_Final(&c,sig,&len);
223     HMAC_CTX_cleanup(&c);
224
225     return len;
226     }
227
228 int FIPS_check_incore_fingerprint(void)
229     {
230     unsigned char sig[EVP_MAX_MD_SIZE];
231     unsigned int len;
232 #if defined(__sgi) && (defined(__mips) || defined(mips))
233     extern int __dso_displacement[];
234 #else
235     extern int OPENSSL_NONPIC_relocated;
236 #endif
237
238     if (FIPS_text_start()==NULL)
239         {
240         FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_UNSUPPORTED_PLATFORM);
241         return 0;
242         }
243
244     len=FIPS_incore_fingerprint (sig,sizeof(sig));
245
246     if (len!=sizeof(FIPS_signature) ||
247         memcmp(FIPS_signature,sig,sizeof(FIPS_signature)))
248         {
249         if (FIPS_signature>=FIPS_rodata_start && FIPS_signature<FIPS_rodata_end)
250             FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH_SEGMENT_ALIASING);
251 #if defined(__sgi) && (defined(__mips) || defined(mips))
252         else if (__dso_displacement!=NULL)
253 #else
254         else if (OPENSSL_NONPIC_relocated)
255 #endif
256             FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH_NONPIC_RELOCATED);
257         else
258             FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH);
259 #ifdef OPENSSL_FIPS_DEBUGGER
260         return 1;
261 #else
262         return 0;
263 #endif
264         }
265     return 1;
266     }
267
268 int FIPS_mode_set(int onoff)
269     {
270     int fips_set_owning_thread();
271     int fips_clear_owning_thread();
272     int ret = 0;
273
274     fips_w_lock();
275     fips_started = 1;
276     fips_set_owning_thread();
277
278     if(onoff)
279         {
280         unsigned char buf[48];
281
282         fips_selftest_fail = 0;
283
284         /* Don't go into FIPS mode twice, just so we can do automagic
285            seeding */
286         if(FIPS_mode())
287             {
288             FIPSerr(FIPS_F_FIPS_MODE_SET,FIPS_R_FIPS_MODE_ALREADY_SET);
289             fips_selftest_fail = 1;
290             ret = 0;
291             goto end;
292             }
293
294 #ifdef OPENSSL_IA32_SSE2
295         if ((OPENSSL_ia32cap & (1<<25|1<<26)) != (1<<25|1<<26))
296             {
297             FIPSerr(FIPS_F_FIPS_MODE_SET,FIPS_R_UNSUPPORTED_PLATFORM);
298             fips_selftest_fail = 1;
299             ret = 0;
300             goto end;
301             }
302 #endif
303
304         if(fips_signature_witness() != FIPS_signature)
305             {
306             FIPSerr(FIPS_F_FIPS_MODE_SET,FIPS_R_CONTRADICTING_EVIDENCE);
307             fips_selftest_fail = 1;
308             ret = 0;
309             goto end;
310             }
311
312         if(!FIPS_check_incore_fingerprint())
313             {
314             fips_selftest_fail = 1;
315             ret = 0;
316             goto end;
317             }
318
319         if (!FIPS_selftest_drbg())
320             {
321             fips_selftest_fail = 1;
322             ret = 0;
323             goto end;
324             }
325
326         /* Perform RNG KAT before seeding */
327         if (!FIPS_selftest_rng())
328             {
329             fips_selftest_fail = 1;
330             ret = 0;
331             goto end;
332             }
333
334         /* automagically seed PRNG if not already seeded */
335         if(!FIPS_rand_status())
336             {
337             if(RAND_bytes(buf,sizeof buf) <= 0)
338                 {
339                 fips_selftest_fail = 1;
340                 ret = 0;
341                 goto end;
342                 }
343             FIPS_rand_set_key(buf,32);
344             FIPS_rand_seed(buf+32,16);
345             }
346
347         /* now switch into FIPS mode */
348         fips_set_rand_check(FIPS_rand_method());
349         RAND_set_rand_method(FIPS_rand_method());
350         if(FIPS_selftest())
351             fips_set_mode(1);
352         else
353             {
354             fips_selftest_fail = 1;
355             ret = 0;
356             goto end;
357             }
358         ret = 1;
359         goto end;
360         }
361     fips_set_mode(0);
362     fips_selftest_fail = 0;
363     ret = 1;
364 end:
365     fips_clear_owning_thread();
366     fips_w_unlock();
367     return ret;
368     }
369
370 static CRYPTO_THREADID fips_thread;
371 static int fips_thread_set = 0;
372
373 static int fips_is_owning_thread(void)
374         {
375         int ret = 0;
376
377         if (fips_started)
378                 {
379                 CRYPTO_r_lock(CRYPTO_LOCK_FIPS2);
380                 if (fips_thread_set)
381                         {
382                         CRYPTO_THREADID cur;
383                         CRYPTO_THREADID_current(&cur);
384                         if (!CRYPTO_THREADID_cmp(&cur, &fips_thread))
385                                 ret = 1;
386                         }
387                 CRYPTO_r_unlock(CRYPTO_LOCK_FIPS2);
388                 }
389         return ret;
390         }
391
392 int fips_set_owning_thread(void)
393         {
394         int ret = 0;
395
396         if (fips_started)
397                 {
398                 CRYPTO_w_lock(CRYPTO_LOCK_FIPS2);
399                 if (!fips_thread_set)
400                         {
401                         CRYPTO_THREADID_current(&fips_thread);
402                         ret = 1;
403                         }
404                 CRYPTO_w_unlock(CRYPTO_LOCK_FIPS2);
405                 }
406         return ret;
407         }
408
409 int fips_clear_owning_thread(void)
410         {
411         int ret = 0;
412
413         if (fips_started)
414                 {
415                 CRYPTO_w_lock(CRYPTO_LOCK_FIPS2);
416                 if (fips_thread_set)
417                         {
418                         CRYPTO_THREADID cur;
419                         CRYPTO_THREADID_current(&cur);
420                         if (!CRYPTO_THREADID_cmp(&cur, &fips_thread))
421                                 fips_thread_set = 0;
422                         }
423                 CRYPTO_w_unlock(CRYPTO_LOCK_FIPS2);
424                 }
425         return ret;
426         }
427
428 unsigned char *fips_signature_witness(void)
429         {
430         extern unsigned char FIPS_signature[];
431         return FIPS_signature;
432         }
433
434 /* Generalized public key test routine. Signs and verifies the data
435  * supplied in tbs using mesage digest md and setting RSA padding mode
436  * pad_mode. If the 'kat' parameter is not NULL it will
437  * additionally check the signature matches it: a known answer test
438  * The string "fail_str" is used for identification purposes in case
439  * of failure.
440  */
441
442 int fips_pkey_signature_test(EVP_PKEY *pkey,
443                         const unsigned char *tbs, int tbslen,
444                         const unsigned char *kat, unsigned int katlen,
445                         const EVP_MD *digest, int pad_mode,
446                         const char *fail_str)
447         {       
448         int ret = 0;
449         unsigned char sigtmp[256], *sig = sigtmp;
450         unsigned int siglen;
451         DSA_SIG *dsig = NULL;
452         ECDSA_SIG *esig = NULL;
453         EVP_MD_CTX mctx;
454         FIPS_md_ctx_init(&mctx);
455
456         if ((pkey->type == EVP_PKEY_RSA)
457                 && ((size_t)RSA_size(pkey->pkey.rsa) > sizeof(sigtmp)))
458                 {
459                 sig = OPENSSL_malloc(RSA_size(pkey->pkey.rsa));
460                 if (!sig)
461                         {
462                         FIPSerr(FIPS_F_FIPS_PKEY_SIGNATURE_TEST,ERR_R_MALLOC_FAILURE);
463                         return 0;
464                         }
465                 }
466
467         if (tbslen == -1)
468                 tbslen = strlen((char *)tbs);
469
470         if (digest == NULL)
471                 digest = EVP_sha256();
472
473         if (!FIPS_digestinit(&mctx, digest))
474                 goto error;
475         if (!FIPS_digestupdate(&mctx, tbs, tbslen))
476                 goto error;
477         if (pkey->type == EVP_PKEY_RSA)
478                 {
479                 if (!FIPS_rsa_sign_ctx(pkey->pkey.rsa, &mctx,
480                                         pad_mode, 0, NULL, sig, &siglen))
481                         goto error;
482                 }
483         else if (pkey->type == EVP_PKEY_DSA)
484                 {
485                 dsig = FIPS_dsa_sign_ctx(pkey->pkey.dsa, &mctx);
486                 if (!dsig)
487                         goto error;
488                 }
489         else if (pkey->type == EVP_PKEY_EC)
490                 {
491                 esig = FIPS_ecdsa_sign_ctx(pkey->pkey.ec, &mctx);
492                 if (!esig)
493                         goto error;
494                 }
495 #if 0
496         else if (!EVP_SignFinal(&mctx, sig, &siglen, pkey))
497                 goto error;
498 #endif
499
500         if (kat && ((siglen != katlen) || memcmp(kat, sig, katlen)))
501                 goto error;
502
503         if (!FIPS_digestinit(&mctx, digest))
504                 goto error;
505         if (!FIPS_digestupdate(&mctx, tbs, tbslen))
506                 goto error;
507         if (pkey->type == EVP_PKEY_RSA)
508                 {
509                 ret = FIPS_rsa_verify_ctx(pkey->pkey.rsa, &mctx,
510                                                 pad_mode, 0, NULL, sig, siglen);
511                 }
512         else if (pkey->type == EVP_PKEY_DSA)
513                 {
514                 ret = FIPS_dsa_verify_ctx(pkey->pkey.dsa, &mctx, dsig);
515                 }
516         else if (pkey->type == EVP_PKEY_EC)
517                 {
518                 ret = FIPS_ecdsa_verify_ctx(pkey->pkey.ec, &mctx, esig);
519                 }
520 #if 0
521         else
522                 ret = EVP_VerifyFinal(&mctx, sig, siglen, pkey);
523 #endif
524
525         error:
526         if (dsig != NULL)
527                 FIPS_dsa_sig_free(dsig);
528         if (esig != NULL)
529                 FIPS_ecdsa_sig_free(esig);
530         if (sig != sigtmp)
531                 OPENSSL_free(sig);
532         FIPS_md_ctx_cleanup(&mctx);
533         if (ret != 1)
534                 {
535                 FIPSerr(FIPS_F_FIPS_PKEY_SIGNATURE_TEST,FIPS_R_TEST_FAILURE);
536                 if (fail_str)
537                         FIPS_add_error_data(2, "Type=", fail_str);
538                 return 0;
539                 }
540         return 1;
541         }
542
543 /* Generalized symmetric cipher test routine. Encrypt data, verify result
544  * against known answer, decrypt and compare with original plaintext.
545  */
546
547 int fips_cipher_test(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
548                         const unsigned char *key,
549                         const unsigned char *iv,
550                         const unsigned char *plaintext,
551                         const unsigned char *ciphertext,
552                         int len)
553         {
554         unsigned char pltmp[FIPS_MAX_CIPHER_TEST_SIZE];
555         unsigned char citmp[FIPS_MAX_CIPHER_TEST_SIZE];
556         OPENSSL_assert(len <= FIPS_MAX_CIPHER_TEST_SIZE);
557         if (FIPS_cipherinit(ctx, cipher, key, iv, 1) <= 0)
558                 return 0;
559         FIPS_cipher(ctx, citmp, plaintext, len);
560         if (memcmp(citmp, ciphertext, len))
561                 return 0;
562         if (FIPS_cipherinit(ctx, cipher, key, iv, 0) <= 0)
563                 return 0;
564         FIPS_cipher(ctx, pltmp, citmp, len);
565         if (memcmp(pltmp, plaintext, len))
566                 return 0;
567         return 1;
568         }
569
570 #if 0
571 /* The purpose of this is to ensure the error code exists and the function
572  * name is to keep the error checking script quiet
573  */
574 void hash_final(void)
575         {
576         FIPSerr(FIPS_F_HASH_FINAL,FIPS_R_NON_FIPS_METHOD);
577         }
578 #endif
579
580
581 #endif