diff options
author | Dr. Stephen Henson <steve@openssl.org> | 2011-03-08 14:16:30 +0000 |
---|---|---|
committer | Dr. Stephen Henson <steve@openssl.org> | 2011-03-08 14:16:30 +0000 |
commit | a1e7883edb9041aff44700fc9817891c800ac703 (patch) | |
tree | 7755d01e0e8ebb2f55f389a731cf1913ed61583f /fips | |
parent | dd0d2df562d8f43f2b6edbf62ae47a4a861e3e67 (diff) |
Add meaningful error codes to DRBG.
Diffstat (limited to 'fips')
-rw-r--r-- | fips/fips.h | 18 | ||||
-rw-r--r-- | fips/rand/fips_drbg_lib.c | 105 |
2 files changed, 108 insertions, 15 deletions
diff --git a/fips/fips.h b/fips/fips.h index 4cfb985efa..3fc7a5e40f 100644 --- a/fips/fips.h +++ b/fips/fips.h @@ -190,6 +190,10 @@ void ERR_load_FIPS_strings(void); #define FIPS_F_FIPS_CHECK_RSA 106 #define FIPS_F_FIPS_CIPHERINIT 128 #define FIPS_F_FIPS_DIGESTINIT 127 +#define FIPS_F_FIPS_DRBG_GENERATE 132 +#define FIPS_F_FIPS_DRBG_INSTANTIATE 133 +#define FIPS_F_FIPS_DRBG_NEW 134 +#define FIPS_F_FIPS_DRBG_RESEED 135 #define FIPS_F_FIPS_DSA_CHECK 107 #define FIPS_F_FIPS_MODE_SET 108 #define FIPS_F_FIPS_PKEY_SIGNATURE_TEST 109 @@ -211,23 +215,37 @@ void ERR_load_FIPS_strings(void); #define FIPS_F_SSLEAY_RAND_BYTES 122 /* Reason codes. */ +#define FIPS_R_ADDITIONAL_INPUT_TOO_LONG 118 +#define FIPS_R_ALREADY_INSTANTIATED 119 #define FIPS_R_CANNOT_READ_EXE 103 #define FIPS_R_CANNOT_READ_EXE_DIGEST 104 #define FIPS_R_CONTRADICTING_EVIDENCE 114 +#define FIPS_R_ERROR_INITIALISING_DRBG 120 +#define FIPS_R_ERROR_INSTANTIATING_DRBG 121 +#define FIPS_R_ERROR_RETRIEVING_ENTROPY 122 +#define FIPS_R_ERROR_RETRIEVING_NONCE 123 #define FIPS_R_EXE_DIGEST_DOES_NOT_MATCH 105 #define FIPS_R_FINGERPRINT_DOES_NOT_MATCH 110 #define FIPS_R_FINGERPRINT_DOES_NOT_MATCH_NONPIC_RELOCATED 111 #define FIPS_R_FINGERPRINT_DOES_NOT_MATCH_SEGMENT_ALIASING 112 #define FIPS_R_FIPS_MODE_ALREADY_SET 102 #define FIPS_R_FIPS_SELFTEST_FAILED 106 +#define FIPS_R_GENERATE_ERROR 124 +#define FIPS_R_INSTANTIATE_ERROR 125 #define FIPS_R_INVALID_KEY_LENGTH 109 +#define FIPS_R_IN_ERROR_STATE 126 #define FIPS_R_KEY_TOO_SHORT 108 #define FIPS_R_NON_FIPS_METHOD 100 +#define FIPS_R_NOT_INSTANTIATED 127 #define FIPS_R_PAIRWISE_TEST_FAILED 107 +#define FIPS_R_PERSONALISATION_STRING_TOO_LONG 128 +#define FIPS_R_REQUEST_TOO_LARGE_FOR_DRBG 129 +#define FIPS_R_RESEED_ERROR 130 #define FIPS_R_RSA_DECRYPT_ERROR 115 #define FIPS_R_RSA_ENCRYPT_ERROR 116 #define FIPS_R_SELFTEST_FAILED 101 #define FIPS_R_TEST_FAILURE 117 +#define FIPS_R_UNSUPPORTED_DRBG_TYPE 131 #define FIPS_R_UNSUPPORTED_PLATFORM 113 #ifdef __cplusplus diff --git a/fips/rand/fips_drbg_lib.c b/fips/rand/fips_drbg_lib.c index 0bf30f0314..fd154c0346 100644 --- a/fips/rand/fips_drbg_lib.c +++ b/fips/rand/fips_drbg_lib.c @@ -57,6 +57,7 @@ #include <openssl/crypto.h> #include <openssl/evp.h> #include <openssl/aes.h> +#include <openssl/err.h> #include <openssl/fips_rand.h> #include "fips_rand_lcl.h" @@ -80,12 +81,23 @@ static int fips_drbg_init(DRBG_CTX *dctx, int type, unsigned int flags) DRBG_CTX *FIPS_drbg_new(int type, unsigned int flags) { + int rv; DRBG_CTX *dctx; dctx = OPENSSL_malloc(sizeof(DRBG_CTX)); if (!dctx) + { + FIPSerr(FIPS_F_FIPS_DRBG_NEW, ERR_R_MALLOC_FAILURE); return NULL; - if (fips_drbg_init(dctx, type, flags) <= 0) + } + rv = fips_drbg_init(dctx, type, flags); + + if (rv <= 0) { + if (rv == -2) + FIPSerr(FIPS_F_FIPS_DRBG_NEW, FIPS_R_UNSUPPORTED_DRBG_TYPE); + else + FIPSerr(FIPS_F_FIPS_DRBG_NEW, FIPS_R_ERROR_INITIALISING_DRBG); + OPENSSL_free(dctx); return NULL; } @@ -105,13 +117,32 @@ int FIPS_drbg_instantiate(DRBG_CTX *dctx, { size_t entlen, noncelen; +#if 0 + /* Put here so error script picks them up */ + FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, + FIPS_R_PERSONALISATION_STRING_TOO_LONG); + FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_IN_ERROR_STATE); + FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_ALREADY_INSTANTIATED); + FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_ERROR_RETRIEVING_ENTROPY); + FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_ERROR_RETRIEVING_NONCE); + FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_INSTANTIATE_ERROR); +#endif + + int r = 0; + if (perslen > dctx->max_pers) - return 0; + { + r = FIPS_R_PERSONALISATION_STRING_TOO_LONG; + goto end; + } if (dctx->status != DRBG_STATUS_UNINITIALISED) { - /* error */ - return 0; + if (dctx->status == DRBG_STATUS_ERROR) + r = FIPS_R_IN_ERROR_STATE; + else + r = FIPS_R_ALREADY_INSTANTIATED; + goto end; } dctx->status = DRBG_STATUS_ERROR; @@ -120,7 +151,10 @@ int FIPS_drbg_instantiate(DRBG_CTX *dctx, dctx->min_entropy, dctx->max_entropy); if (entlen < dctx->min_entropy || entlen > dctx->max_entropy) + { + r = FIPS_R_ERROR_RETRIEVING_ENTROPY; goto end; + } if (dctx->max_nonce > 0) { @@ -130,7 +164,10 @@ int FIPS_drbg_instantiate(DRBG_CTX *dctx, dctx->min_nonce, dctx->max_nonce); if (noncelen < dctx->min_nonce || noncelen > dctx->max_nonce) + { + r = FIPS_R_ERROR_RETRIEVING_NONCE; goto end; + } } else @@ -140,7 +177,10 @@ int FIPS_drbg_instantiate(DRBG_CTX *dctx, dctx->entropy, entlen, dctx->nonce, noncelen, pers, perslen)) + { + r = FIPS_R_ERROR_INSTANTIATING_DRBG; goto end; + } dctx->status = DRBG_STATUS_READY; @@ -156,6 +196,9 @@ int FIPS_drbg_instantiate(DRBG_CTX *dctx, if (dctx->status == DRBG_STATUS_READY) return 1; + if (r && !(dctx->flags & DRBG_FLAG_TEST)) + FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, r); + return 0; } @@ -164,19 +207,28 @@ int FIPS_drbg_reseed(DRBG_CTX *dctx, const unsigned char *adin, size_t adinlen) { size_t entlen; + int r = 0; + +#if 0 + FIPSerr(FIPS_F_FIPS_DRBG_RESEED, FIPS_R_NOT_INSTANTIATED); + FIPSerr(FIPS_F_FIPS_DRBG_RESEED, FIPS_R_ADDITIONAL_INPUT_TOO_LONG); +#endif if (dctx->status != DRBG_STATUS_READY && dctx->status != DRBG_STATUS_RESEED) { - /* error */ - return 0; + if (dctx->status == DRBG_STATUS_ERROR) + r = FIPS_R_IN_ERROR_STATE; + else if(dctx->status == DRBG_STATUS_UNINITIALISED) + r = FIPS_R_NOT_INSTANTIATED; + goto end; } if (!adin) adinlen = 0; else if (adinlen > dctx->max_adin) { - /* error */ - return 0; + r = FIPS_R_ADDITIONAL_INPUT_TOO_LONG; + goto end; } dctx->status = DRBG_STATUS_ERROR; @@ -185,7 +237,10 @@ int FIPS_drbg_reseed(DRBG_CTX *dctx, dctx->min_entropy, dctx->max_entropy); if (entlen < dctx->min_entropy || entlen > dctx->max_entropy) + { + r = FIPS_R_ERROR_RETRIEVING_ENTROPY; goto end; + } if (!dctx->reseed(dctx, dctx->entropy, entlen, adin, adinlen)) goto end; @@ -194,44 +249,64 @@ int FIPS_drbg_reseed(DRBG_CTX *dctx, dctx->reseed_counter = 1; end: OPENSSL_cleanse(dctx->entropy, sizeof(dctx->entropy)); + if (dctx->status == DRBG_STATUS_READY) return 1; + + if (r && !(dctx->flags & DRBG_FLAG_TEST)) + FIPSerr(FIPS_F_FIPS_DRBG_RESEED, r); + return 0; } - + int FIPS_drbg_generate(DRBG_CTX *dctx, unsigned char *out, size_t outlen, int prediction_resistance, const unsigned char *adin, size_t adinlen) { + int r = 0; if (outlen > dctx->max_request) { - /* Too large */ + r = FIPS_R_REQUEST_TOO_LARGE_FOR_DRBG; return 0; } if (dctx->status == DRBG_STATUS_RESEED || prediction_resistance) { if (!FIPS_drbg_reseed(dctx, adin, adinlen)) - return 0; + { + r = FIPS_R_RESEED_ERROR; + goto end; + } adin = NULL; adinlen = 0; } if (dctx->status != DRBG_STATUS_READY) { - /* Bad error */ - return 0; + if (dctx->status == DRBG_STATUS_ERROR) + r = FIPS_R_IN_ERROR_STATE; + else if(dctx->status == DRBG_STATUS_UNINITIALISED) + r = FIPS_R_NOT_INSTANTIATED; + goto end; } if (!dctx->generate(dctx, out, outlen, adin, adinlen)) { - /* Bad error */ + r = FIPS_R_GENERATE_ERROR; dctx->status = DRBG_STATUS_ERROR; - return 0; + goto end; } if (dctx->reseed_counter > dctx->reseed_interval) dctx->status = DRBG_STATUS_RESEED; else dctx->reseed_counter++; + end: + if (r) + { + if (!(dctx->flags & DRBG_FLAG_TEST)) + FIPSerr(FIPS_F_FIPS_DRBG_GENERATE, r); + return 0; + } + return 1; } |