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