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