And so it begins... again.
[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_FIPSEVP
51
52 #include <openssl/rand.h>
53 #include <openssl/fips_rand.h>
54 #include <openssl/err.h>
55 #include <openssl/bio.h>
56 #include <openssl/hmac.h>
57 #include <openssl/rsa.h>
58 #include <openssl/dsa.h>
59 #include <string.h>
60 #include <limits.h>
61 #include "fips_locl.h"
62
63 #ifdef OPENSSL_FIPS
64
65 #include <openssl/fips.h>
66
67 #ifndef PATH_MAX
68 #define PATH_MAX 1024
69 #endif
70
71 static int fips_selftest_fail;
72 static int fips_mode;
73 static const void *fips_rand_check;
74
75 static void fips_set_mode(int onoff)
76         {
77         int owning_thread = fips_is_owning_thread();
78
79         if (fips_is_started())
80                 {
81                 if (!owning_thread) fips_w_lock();
82                 fips_mode = onoff;
83                 if (!owning_thread) fips_w_unlock();
84                 }
85         }
86
87 static void fips_set_rand_check(const void *rand_check)
88         {
89         int owning_thread = fips_is_owning_thread();
90
91         if (fips_is_started())
92                 {
93                 if (!owning_thread) fips_w_lock();
94                 fips_rand_check = rand_check;
95                 if (!owning_thread) fips_w_unlock();
96                 }
97         }
98
99 int FIPS_mode(void)
100         {
101         int ret = 0;
102         int owning_thread = fips_is_owning_thread();
103
104         if (fips_is_started())
105                 {
106                 if (!owning_thread) fips_r_lock();
107                 ret = fips_mode;
108                 if (!owning_thread) fips_r_unlock();
109                 }
110         return ret;
111         }
112
113 const void *FIPS_rand_check(void)
114         {
115         const void *ret = 0;
116         int owning_thread = fips_is_owning_thread();
117
118         if (fips_is_started())
119                 {
120                 if (!owning_thread) fips_r_lock();
121                 ret = fips_rand_check;
122                 if (!owning_thread) fips_r_unlock();
123                 }
124         return ret;
125         }
126
127 int FIPS_selftest_failed(void)
128     {
129     int ret = 0;
130     if (fips_is_started())
131         {
132         int owning_thread = fips_is_owning_thread();
133
134         if (!owning_thread) fips_r_lock();
135         ret = fips_selftest_fail;
136         if (!owning_thread) fips_r_unlock();
137         }
138     return ret;
139     }
140
141 /* Selftest failure fatal exit routine. This will be called
142  * during *any* cryptographic operation. It has the minimum
143  * overhead possible to avoid too big a performance hit.
144  */
145
146 void FIPS_selftest_check(void)
147     {
148     if (fips_selftest_fail)
149         {
150         OpenSSLDie(__FILE__,__LINE__, "FATAL FIPS SELFTEST FAILURE");
151         }
152     }
153
154 void fips_set_selftest_fail(void)
155     {
156     fips_selftest_fail = 1;
157     }
158
159 int FIPS_selftest(void)
160     {
161
162     return FIPS_selftest_sha1()
163         && FIPS_selftest_hmac()
164         && FIPS_selftest_aes()
165         && FIPS_selftest_des()
166         && FIPS_selftest_rsa()
167         && FIPS_selftest_dsa();
168     }
169
170 extern const void         *FIPS_text_start(),  *FIPS_text_end();
171 extern const unsigned char FIPS_rodata_start[], FIPS_rodata_end[];
172 unsigned char              FIPS_signature [20] = { 0 };
173 static const char          FIPS_hmac_key[]="etaonrishdlcupfm";
174
175 unsigned int FIPS_incore_fingerprint(unsigned char *sig,unsigned int len)
176     {
177     const unsigned char *p1 = FIPS_text_start();
178     const unsigned char *p2 = FIPS_text_end();
179     const unsigned char *p3 = FIPS_rodata_start;
180     const unsigned char *p4 = FIPS_rodata_end;
181     HMAC_CTX c;
182
183     HMAC_CTX_init(&c);
184     HMAC_Init(&c,FIPS_hmac_key,strlen(FIPS_hmac_key),EVP_sha1());
185
186     /* detect overlapping regions */
187     if (p1<=p3 && p2>=p3)
188         p3=p1, p4=p2>p4?p2:p4, p1=NULL, p2=NULL;
189     else if (p3<=p1 && p4>=p1)
190         p3=p3, p4=p2>p4?p2:p4, p1=NULL, p2=NULL;
191
192     if (p1)
193         HMAC_Update(&c,p1,(size_t)p2-(size_t)p1);
194
195     if (FIPS_signature>=p3 && FIPS_signature<p4)
196         {
197         /* "punch" hole */
198         HMAC_Update(&c,p3,(size_t)FIPS_signature-(size_t)p3);
199         p3 = FIPS_signature+sizeof(FIPS_signature);
200         if (p3<p4)
201             HMAC_Update(&c,p3,(size_t)p4-(size_t)p3);
202         }
203     else
204         HMAC_Update(&c,p3,(size_t)p4-(size_t)p3);
205
206     HMAC_Final(&c,sig,&len);
207     HMAC_CTX_cleanup(&c);
208
209     return len;
210     }
211
212 int FIPS_check_incore_fingerprint(void)
213     {
214     unsigned char sig[EVP_MAX_MD_SIZE];
215     unsigned int len;
216 #if defined(__sgi) && (defined(__mips) || defined(mips))
217     extern int __dso_displacement[];
218 #else
219     extern int OPENSSL_NONPIC_relocated;
220 #endif
221
222     if (FIPS_text_start()==NULL)
223         {
224         FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_UNSUPPORTED_PLATFORM);
225         return 0;
226         }
227
228     len=FIPS_incore_fingerprint (sig,sizeof(sig));
229
230     if (len!=sizeof(FIPS_signature) ||
231         memcmp(FIPS_signature,sig,sizeof(FIPS_signature)))
232         {
233         if (FIPS_signature>=FIPS_rodata_start && FIPS_signature<FIPS_rodata_end)
234             FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH_SEGMENT_ALIASING);
235 #if defined(__sgi) && (defined(__mips) || defined(mips))
236         else if (__dso_displacement!=NULL)
237 #else
238         else if (OPENSSL_NONPIC_relocated)
239 #endif
240             FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH_NONPIC_RELOCATED);
241         else
242             FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH);
243 #ifdef OPENSSL_FIPS_DEBUGGER
244         return 1;
245 #else
246         return 0;
247 #endif
248         }
249     return 1;
250     }
251
252 int FIPS_mode_set(int onoff)
253     {
254     int fips_set_owning_thread();
255     int fips_clear_owning_thread();
256     int ret = 0;
257
258     fips_w_lock();
259     fips_set_started();
260     fips_set_owning_thread();
261
262     if(onoff)
263         {
264         unsigned char buf[48];
265
266         fips_selftest_fail = 0;
267
268         /* Don't go into FIPS mode twice, just so we can do automagic
269            seeding */
270         if(FIPS_mode())
271             {
272             FIPSerr(FIPS_F_FIPS_MODE_SET,FIPS_R_FIPS_MODE_ALREADY_SET);
273             fips_selftest_fail = 1;
274             ret = 0;
275             goto end;
276             }
277
278 #ifdef OPENSSL_IA32_SSE2
279         if ((OPENSSL_ia32cap & (1<<25|1<<26)) != (1<<25|1<<26))
280             {
281             FIPSerr(FIPS_F_FIPS_MODE_SET,FIPS_R_UNSUPPORTED_PLATFORM);
282             fips_selftest_fail = 1;
283             ret = 0;
284             goto end;
285             }
286 #endif
287
288         if(fips_signature_witness() != FIPS_signature)
289             {
290             FIPSerr(FIPS_F_FIPS_MODE_SET,FIPS_R_CONTRADICTING_EVIDENCE);
291             fips_selftest_fail = 1;
292             ret = 0;
293             goto end;
294             }
295
296         if(!FIPS_check_incore_fingerprint())
297             {
298             fips_selftest_fail = 1;
299             ret = 0;
300             goto end;
301             }
302
303         /* Perform RNG KAT before seeding */
304         if (!FIPS_selftest_rng())
305             {
306             fips_selftest_fail = 1;
307             ret = 0;
308             goto end;
309             }
310
311         /* automagically seed PRNG if not already seeded */
312         if(!FIPS_rand_status())
313             {
314             if(RAND_bytes(buf,sizeof buf) <= 0)
315                 {
316                 fips_selftest_fail = 1;
317                 ret = 0;
318                 goto end;
319                 }
320             FIPS_rand_set_key(buf,32);
321             FIPS_rand_seed(buf+32,16);
322             }
323
324         /* now switch into FIPS mode */
325         fips_set_rand_check(FIPS_rand_method());
326         RAND_set_rand_method(FIPS_rand_method());
327         if(FIPS_selftest())
328             fips_set_mode(1);
329         else
330             {
331             fips_selftest_fail = 1;
332             ret = 0;
333             goto end;
334             }
335         ret = 1;
336         goto end;
337         }
338     fips_set_mode(0);
339     fips_selftest_fail = 0;
340     ret = 1;
341 end:
342     fips_clear_owning_thread();
343     fips_w_unlock();
344     return ret;
345     }
346
347 void fips_w_lock(void)          { CRYPTO_w_lock(CRYPTO_LOCK_FIPS); }
348 void fips_w_unlock(void)        { CRYPTO_w_unlock(CRYPTO_LOCK_FIPS); }
349 void fips_r_lock(void)          { CRYPTO_r_lock(CRYPTO_LOCK_FIPS); }
350 void fips_r_unlock(void)        { CRYPTO_r_unlock(CRYPTO_LOCK_FIPS); }
351
352 static int fips_started = 0;
353 static CRYPTO_THREADID fips_thread;
354 static int fips_thread_set = 0;
355
356 void fips_set_started(void)
357         {
358         fips_started = 1;
359         }
360
361 int fips_is_started(void)
362         {
363         return fips_started;
364         }
365
366 int fips_is_owning_thread(void)
367         {
368         int ret = 0;
369
370         if (fips_is_started())
371                 {
372                 CRYPTO_r_lock(CRYPTO_LOCK_FIPS2);
373                 if (fips_thread_set)
374                         {
375                         CRYPTO_THREADID cur;
376                         CRYPTO_THREADID_current(&cur);
377                         if (!CRYPTO_THREADID_cmp(&cur, &fips_thread))
378                                 ret = 1;
379                         }
380                 CRYPTO_r_unlock(CRYPTO_LOCK_FIPS2);
381                 }
382         return ret;
383         }
384
385 int fips_set_owning_thread(void)
386         {
387         int ret = 0;
388
389         if (fips_is_started())
390                 {
391                 CRYPTO_w_lock(CRYPTO_LOCK_FIPS2);
392                 if (!fips_thread_set)
393                         {
394                         CRYPTO_THREADID_current(&fips_thread);
395                         ret = 1;
396                         }
397                 CRYPTO_w_unlock(CRYPTO_LOCK_FIPS2);
398                 }
399         return ret;
400         }
401
402 int fips_clear_owning_thread(void)
403         {
404         int ret = 0;
405
406         if (fips_is_started())
407                 {
408                 CRYPTO_w_lock(CRYPTO_LOCK_FIPS2);
409                 if (fips_thread_set)
410                         {
411                         CRYPTO_THREADID cur;
412                         CRYPTO_THREADID_current(&cur);
413                         if (!CRYPTO_THREADID_cmp(&cur, &fips_thread))
414                                 fips_thread_set = 0;
415                         }
416                 CRYPTO_w_unlock(CRYPTO_LOCK_FIPS2);
417                 }
418         return ret;
419         }
420
421 unsigned char *fips_signature_witness(void)
422         {
423         extern unsigned char FIPS_signature[];
424         return FIPS_signature;
425         }
426
427 /* Generalized public key test routine. Signs and verifies the data
428  * supplied in tbs using mesage digest md and setting RSA padding mode
429  * pad_mode. If the 'kat' parameter is not NULL it will
430  * additionally check the signature matches it: a known answer test
431  * The string "fail_str" is used for identification purposes in case
432  * of failure.
433  */
434
435 int fips_pkey_signature_test(EVP_PKEY *pkey,
436                         const unsigned char *tbs, int tbslen,
437                         const unsigned char *kat, unsigned int katlen,
438                         const EVP_MD *digest, int pad_mode,
439                         const char *fail_str)
440         {       
441         int ret = 0;
442         unsigned char sigtmp[256], *sig = sigtmp;
443         unsigned int siglen;
444         DSA_SIG *dsig = NULL;
445         EVP_MD_CTX mctx;
446         EVP_MD_CTX_init(&mctx);
447
448         if ((pkey->type == EVP_PKEY_RSA)
449                 && ((size_t)RSA_size(pkey->pkey.rsa) > sizeof(sigtmp)))
450                 {
451                 sig = OPENSSL_malloc(RSA_size(pkey->pkey.rsa));
452                 if (!sig)
453                         {
454                         FIPSerr(FIPS_F_FIPS_PKEY_SIGNATURE_TEST,ERR_R_MALLOC_FAILURE);
455                         return 0;
456                         }
457                 }
458
459         if (tbslen == -1)
460                 tbslen = strlen((char *)tbs);
461
462         if (!EVP_DigestInit_ex(&mctx, digest, NULL))
463                 goto error;
464         if (!EVP_DigestUpdate(&mctx, tbs, tbslen))
465                 goto error;
466         if (pkey->type == EVP_PKEY_RSA)
467                 {
468                 if (!FIPS_rsa_sign_ctx(pkey->pkey.rsa, &mctx,
469                                         pad_mode, 0, NULL, sig, &siglen))
470                         goto error;
471                 }
472         else if (pkey->type == EVP_PKEY_DSA)
473                 {
474                 dsig = FIPS_dsa_sign_ctx(pkey->pkey.dsa, &mctx);
475                 if (!dsig)
476                         goto error;
477                 }
478 #if 0
479         else if (!EVP_SignFinal(&mctx, sig, &siglen, pkey))
480                 goto error;
481 #endif
482
483         if (kat && ((siglen != katlen) || memcmp(kat, sig, katlen)))
484                 goto error;
485
486         if (!EVP_DigestInit_ex(&mctx, digest, NULL))
487                 goto error;
488         if (!EVP_DigestUpdate(&mctx, tbs, tbslen))
489                 goto error;
490         if (pkey->type == EVP_PKEY_RSA)
491                 {
492                 ret = FIPS_rsa_verify_ctx(pkey->pkey.rsa, &mctx,
493                                                 pad_mode, 0, NULL, sig, siglen);
494                 }
495         else if (pkey->type == EVP_PKEY_DSA)
496                 {
497                 ret = FIPS_dsa_verify_ctx(pkey->pkey.dsa, &mctx, dsig);
498                 }
499 #if 0
500         else
501                 ret = EVP_VerifyFinal(&mctx, sig, siglen, pkey);
502 #endif
503
504         error:
505         if (dsig != NULL)
506                 DSA_SIG_free(dsig);
507         if (sig != sigtmp)
508                 OPENSSL_free(sig);
509         EVP_MD_CTX_cleanup(&mctx);
510         if (ret != 1)
511                 {
512                 FIPSerr(FIPS_F_FIPS_PKEY_SIGNATURE_TEST,FIPS_R_TEST_FAILURE);
513                 if (fail_str)
514                         ERR_add_error_data(2, "Type=", fail_str);
515                 return 0;
516                 }
517         return 1;
518         }
519
520 /* Generalized symmetric cipher test routine. Encrypt data, verify result
521  * against known answer, decrypt and compare with original plaintext.
522  */
523
524 int fips_cipher_test(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
525                         const unsigned char *key,
526                         const unsigned char *iv,
527                         const unsigned char *plaintext,
528                         const unsigned char *ciphertext,
529                         int len)
530         {
531         unsigned char pltmp[FIPS_MAX_CIPHER_TEST_SIZE];
532         unsigned char citmp[FIPS_MAX_CIPHER_TEST_SIZE];
533         OPENSSL_assert(len <= FIPS_MAX_CIPHER_TEST_SIZE);
534         if (EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, 1) <= 0)
535                 return 0;
536         EVP_Cipher(ctx, citmp, plaintext, len);
537         if (memcmp(citmp, ciphertext, len))
538                 return 0;
539         if (EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, 0) <= 0)
540                 return 0;
541         EVP_Cipher(ctx, pltmp, citmp, len);
542         if (memcmp(pltmp, plaintext, len))
543                 return 0;
544         return 1;
545         }
546
547 #if 0
548 /* The purpose of this is to ensure the error code exists and the function
549  * name is to keep the error checking script quiet
550  */
551 void hash_final(void)
552         {
553         FIPSerr(FIPS_F_HASH_FINAL,FIPS_R_NON_FIPS_METHOD);
554         }
555 #endif
556
557
558 #endif