c7d4bb567cd22288475f1fd75c067edd12be569d
[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_aes()
178         && FIPS_selftest_aes_gcm()
179         && FIPS_selftest_des()
180         && FIPS_selftest_rsa()
181         && FIPS_selftest_ecdsa()
182         && FIPS_selftest_dsa();
183     }
184
185 extern const void         *FIPS_text_start(),  *FIPS_text_end();
186 extern const unsigned char FIPS_rodata_start[], FIPS_rodata_end[];
187 unsigned char              FIPS_signature [20] = { 0 };
188 static const char          FIPS_hmac_key[]="etaonrishdlcupfm";
189
190 unsigned int FIPS_incore_fingerprint(unsigned char *sig,unsigned int len)
191     {
192     const unsigned char *p1 = FIPS_text_start();
193     const unsigned char *p2 = FIPS_text_end();
194     const unsigned char *p3 = FIPS_rodata_start;
195     const unsigned char *p4 = FIPS_rodata_end;
196     HMAC_CTX c;
197
198     HMAC_CTX_init(&c);
199     HMAC_Init(&c,FIPS_hmac_key,strlen(FIPS_hmac_key),EVP_sha1());
200
201     /* detect overlapping regions */
202     if (p1<=p3 && p2>=p3)
203         p3=p1, p4=p2>p4?p2:p4, p1=NULL, p2=NULL;
204     else if (p3<=p1 && p4>=p1)
205         p3=p3, p4=p2>p4?p2:p4, p1=NULL, p2=NULL;
206
207     if (p1)
208         HMAC_Update(&c,p1,(size_t)p2-(size_t)p1);
209
210     if (FIPS_signature>=p3 && FIPS_signature<p4)
211         {
212         /* "punch" hole */
213         HMAC_Update(&c,p3,(size_t)FIPS_signature-(size_t)p3);
214         p3 = FIPS_signature+sizeof(FIPS_signature);
215         if (p3<p4)
216             HMAC_Update(&c,p3,(size_t)p4-(size_t)p3);
217         }
218     else
219         HMAC_Update(&c,p3,(size_t)p4-(size_t)p3);
220
221     HMAC_Final(&c,sig,&len);
222     HMAC_CTX_cleanup(&c);
223
224     return len;
225     }
226
227 int FIPS_check_incore_fingerprint(void)
228     {
229     unsigned char sig[EVP_MAX_MD_SIZE];
230     unsigned int len;
231 #if defined(__sgi) && (defined(__mips) || defined(mips))
232     extern int __dso_displacement[];
233 #else
234     extern int OPENSSL_NONPIC_relocated;
235 #endif
236
237     if (FIPS_text_start()==NULL)
238         {
239         FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_UNSUPPORTED_PLATFORM);
240         return 0;
241         }
242
243     len=FIPS_incore_fingerprint (sig,sizeof(sig));
244
245     if (len!=sizeof(FIPS_signature) ||
246         memcmp(FIPS_signature,sig,sizeof(FIPS_signature)))
247         {
248         if (FIPS_signature>=FIPS_rodata_start && FIPS_signature<FIPS_rodata_end)
249             FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH_SEGMENT_ALIASING);
250 #if defined(__sgi) && (defined(__mips) || defined(mips))
251         else if (__dso_displacement!=NULL)
252 #else
253         else if (OPENSSL_NONPIC_relocated)
254 #endif
255             FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH_NONPIC_RELOCATED);
256         else
257             FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH);
258 #ifdef OPENSSL_FIPS_DEBUGGER
259         return 1;
260 #else
261         return 0;
262 #endif
263         }
264     return 1;
265     }
266
267 int FIPS_mode_set(int onoff)
268     {
269     int fips_set_owning_thread();
270     int fips_clear_owning_thread();
271     int ret = 0;
272
273     fips_w_lock();
274     fips_started = 1;
275     fips_set_owning_thread();
276
277     if(onoff)
278         {
279         unsigned char buf[48];
280
281         fips_selftest_fail = 0;
282
283         /* Don't go into FIPS mode twice, just so we can do automagic
284            seeding */
285         if(FIPS_mode())
286             {
287             FIPSerr(FIPS_F_FIPS_MODE_SET,FIPS_R_FIPS_MODE_ALREADY_SET);
288             fips_selftest_fail = 1;
289             ret = 0;
290             goto end;
291             }
292
293 #ifdef OPENSSL_IA32_SSE2
294         if ((OPENSSL_ia32cap & (1<<25|1<<26)) != (1<<25|1<<26))
295             {
296             FIPSerr(FIPS_F_FIPS_MODE_SET,FIPS_R_UNSUPPORTED_PLATFORM);
297             fips_selftest_fail = 1;
298             ret = 0;
299             goto end;
300             }
301 #endif
302
303         if(fips_signature_witness() != FIPS_signature)
304             {
305             FIPSerr(FIPS_F_FIPS_MODE_SET,FIPS_R_CONTRADICTING_EVIDENCE);
306             fips_selftest_fail = 1;
307             ret = 0;
308             goto end;
309             }
310
311         if(!FIPS_check_incore_fingerprint())
312             {
313             fips_selftest_fail = 1;
314             ret = 0;
315             goto end;
316             }
317
318         if (!FIPS_selftest_drbg())
319             {
320             fips_selftest_fail = 1;
321             ret = 0;
322             goto end;
323             }
324
325         /* Perform RNG KAT before seeding */
326         if (!FIPS_selftest_rng())
327             {
328             fips_selftest_fail = 1;
329             ret = 0;
330             goto end;
331             }
332
333         /* automagically seed PRNG if not already seeded */
334         if(!FIPS_rand_status())
335             {
336             if(RAND_bytes(buf,sizeof buf) <= 0)
337                 {
338                 fips_selftest_fail = 1;
339                 ret = 0;
340                 goto end;
341                 }
342             FIPS_rand_set_key(buf,32);
343             FIPS_rand_seed(buf+32,16);
344             }
345
346         /* now switch into FIPS mode */
347         fips_set_rand_check(FIPS_rand_method());
348         RAND_set_rand_method(FIPS_rand_method());
349         if(FIPS_selftest())
350             fips_set_mode(1);
351         else
352             {
353             fips_selftest_fail = 1;
354             ret = 0;
355             goto end;
356             }
357         ret = 1;
358         goto end;
359         }
360     fips_set_mode(0);
361     fips_selftest_fail = 0;
362     ret = 1;
363 end:
364     fips_clear_owning_thread();
365     fips_w_unlock();
366     return ret;
367     }
368
369 static CRYPTO_THREADID fips_thread;
370 static int fips_thread_set = 0;
371
372 static int fips_is_owning_thread(void)
373         {
374         int ret = 0;
375
376         if (fips_started)
377                 {
378                 CRYPTO_r_lock(CRYPTO_LOCK_FIPS2);
379                 if (fips_thread_set)
380                         {
381                         CRYPTO_THREADID cur;
382                         CRYPTO_THREADID_current(&cur);
383                         if (!CRYPTO_THREADID_cmp(&cur, &fips_thread))
384                                 ret = 1;
385                         }
386                 CRYPTO_r_unlock(CRYPTO_LOCK_FIPS2);
387                 }
388         return ret;
389         }
390
391 int fips_set_owning_thread(void)
392         {
393         int ret = 0;
394
395         if (fips_started)
396                 {
397                 CRYPTO_w_lock(CRYPTO_LOCK_FIPS2);
398                 if (!fips_thread_set)
399                         {
400                         CRYPTO_THREADID_current(&fips_thread);
401                         ret = 1;
402                         }
403                 CRYPTO_w_unlock(CRYPTO_LOCK_FIPS2);
404                 }
405         return ret;
406         }
407
408 int fips_clear_owning_thread(void)
409         {
410         int ret = 0;
411
412         if (fips_started)
413                 {
414                 CRYPTO_w_lock(CRYPTO_LOCK_FIPS2);
415                 if (fips_thread_set)
416                         {
417                         CRYPTO_THREADID cur;
418                         CRYPTO_THREADID_current(&cur);
419                         if (!CRYPTO_THREADID_cmp(&cur, &fips_thread))
420                                 fips_thread_set = 0;
421                         }
422                 CRYPTO_w_unlock(CRYPTO_LOCK_FIPS2);
423                 }
424         return ret;
425         }
426
427 unsigned char *fips_signature_witness(void)
428         {
429         extern unsigned char FIPS_signature[];
430         return FIPS_signature;
431         }
432
433 /* Generalized public key test routine. Signs and verifies the data
434  * supplied in tbs using mesage digest md and setting RSA padding mode
435  * pad_mode. If the 'kat' parameter is not NULL it will
436  * additionally check the signature matches it: a known answer test
437  * The string "fail_str" is used for identification purposes in case
438  * of failure.
439  */
440
441 int fips_pkey_signature_test(EVP_PKEY *pkey,
442                         const unsigned char *tbs, int tbslen,
443                         const unsigned char *kat, unsigned int katlen,
444                         const EVP_MD *digest, int pad_mode,
445                         const char *fail_str)
446         {       
447         int ret = 0;
448         unsigned char sigtmp[256], *sig = sigtmp;
449         unsigned int siglen;
450         DSA_SIG *dsig = NULL;
451         ECDSA_SIG *esig = NULL;
452         EVP_MD_CTX mctx;
453         FIPS_md_ctx_init(&mctx);
454
455         if ((pkey->type == EVP_PKEY_RSA)
456                 && ((size_t)RSA_size(pkey->pkey.rsa) > sizeof(sigtmp)))
457                 {
458                 sig = OPENSSL_malloc(RSA_size(pkey->pkey.rsa));
459                 if (!sig)
460                         {
461                         FIPSerr(FIPS_F_FIPS_PKEY_SIGNATURE_TEST,ERR_R_MALLOC_FAILURE);
462                         return 0;
463                         }
464                 }
465
466         if (tbslen == -1)
467                 tbslen = strlen((char *)tbs);
468
469         if (digest == NULL)
470                 digest = EVP_sha256();
471
472         if (!FIPS_digestinit(&mctx, digest))
473                 goto error;
474         if (!FIPS_digestupdate(&mctx, tbs, tbslen))
475                 goto error;
476         if (pkey->type == EVP_PKEY_RSA)
477                 {
478                 if (!FIPS_rsa_sign_ctx(pkey->pkey.rsa, &mctx,
479                                         pad_mode, 0, NULL, sig, &siglen))
480                         goto error;
481                 }
482         else if (pkey->type == EVP_PKEY_DSA)
483                 {
484                 dsig = FIPS_dsa_sign_ctx(pkey->pkey.dsa, &mctx);
485                 if (!dsig)
486                         goto error;
487                 }
488         else if (pkey->type == EVP_PKEY_EC)
489                 {
490                 esig = FIPS_ecdsa_sign_ctx(pkey->pkey.ec, &mctx);
491                 if (!esig)
492                         goto error;
493                 }
494 #if 0
495         else if (!EVP_SignFinal(&mctx, sig, &siglen, pkey))
496                 goto error;
497 #endif
498
499         if (kat && ((siglen != katlen) || memcmp(kat, sig, katlen)))
500                 goto error;
501
502         if (!FIPS_digestinit(&mctx, digest))
503                 goto error;
504         if (!FIPS_digestupdate(&mctx, tbs, tbslen))
505                 goto error;
506         if (pkey->type == EVP_PKEY_RSA)
507                 {
508                 ret = FIPS_rsa_verify_ctx(pkey->pkey.rsa, &mctx,
509                                                 pad_mode, 0, NULL, sig, siglen);
510                 }
511         else if (pkey->type == EVP_PKEY_DSA)
512                 {
513                 ret = FIPS_dsa_verify_ctx(pkey->pkey.dsa, &mctx, dsig);
514                 }
515         else if (pkey->type == EVP_PKEY_EC)
516                 {
517                 ret = FIPS_ecdsa_verify_ctx(pkey->pkey.ec, &mctx, esig);
518                 }
519 #if 0
520         else
521                 ret = EVP_VerifyFinal(&mctx, sig, siglen, pkey);
522 #endif
523
524         error:
525         if (dsig != NULL)
526                 FIPS_dsa_sig_free(dsig);
527         if (esig != NULL)
528                 FIPS_ecdsa_sig_free(esig);
529         if (sig != sigtmp)
530                 OPENSSL_free(sig);
531         FIPS_md_ctx_cleanup(&mctx);
532         if (ret != 1)
533                 {
534                 FIPSerr(FIPS_F_FIPS_PKEY_SIGNATURE_TEST,FIPS_R_TEST_FAILURE);
535                 if (fail_str)
536                         FIPS_add_error_data(2, "Type=", fail_str);
537                 return 0;
538                 }
539         return 1;
540         }
541
542 /* Generalized symmetric cipher test routine. Encrypt data, verify result
543  * against known answer, decrypt and compare with original plaintext.
544  */
545
546 int fips_cipher_test(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
547                         const unsigned char *key,
548                         const unsigned char *iv,
549                         const unsigned char *plaintext,
550                         const unsigned char *ciphertext,
551                         int len)
552         {
553         unsigned char pltmp[FIPS_MAX_CIPHER_TEST_SIZE];
554         unsigned char citmp[FIPS_MAX_CIPHER_TEST_SIZE];
555         OPENSSL_assert(len <= FIPS_MAX_CIPHER_TEST_SIZE);
556         if (FIPS_cipherinit(ctx, cipher, key, iv, 1) <= 0)
557                 return 0;
558         FIPS_cipher(ctx, citmp, plaintext, len);
559         if (memcmp(citmp, ciphertext, len))
560                 return 0;
561         if (FIPS_cipherinit(ctx, cipher, key, iv, 0) <= 0)
562                 return 0;
563         FIPS_cipher(ctx, pltmp, citmp, len);
564         if (memcmp(pltmp, plaintext, len))
565                 return 0;
566         return 1;
567         }
568
569 #if 0
570 /* The purpose of this is to ensure the error code exists and the function
571  * name is to keep the error checking script quiet
572  */
573 void hash_final(void)
574         {
575         FIPSerr(FIPS_F_HASH_FINAL,FIPS_R_NON_FIPS_METHOD);
576         }
577 #endif
578
579
580 #endif