better error messages for generics in CTaoCrypt

This commit is contained in:
Todd A Ouska
2011-04-20 16:29:19 -07:00
parent adaef70aec
commit 92bc4fc00d
10 changed files with 85 additions and 59 deletions

View File

@@ -52,6 +52,7 @@ enum {
MP_MOD_E = -118, /* mp_mod error state, can't mod */ MP_MOD_E = -118, /* mp_mod error state, can't mod */
MP_INVMOD_E = -119, /* mp_invmod error state, can't inv mod */ MP_INVMOD_E = -119, /* mp_invmod error state, can't inv mod */
MP_CMP_E = -120, /* mp_cmp error state */ MP_CMP_E = -120, /* mp_cmp error state */
MP_ZERO_E = -121, /* got a mp zero result, not expected */
MEMORY_E = -125, /* out of memory error */ MEMORY_E = -125, /* out of memory error */
@@ -86,10 +87,12 @@ enum {
ASN_DH_KEY_E = -158, /* ASN key init error, invalid input */ ASN_DH_KEY_E = -158, /* ASN key init error, invalid input */
ASN_NTRU_KEY_E = -159, /* ASN ntru key decode error, invalid input */ ASN_NTRU_KEY_E = -159, /* ASN ntru key decode error, invalid input */
/* TODO: TAO add ECC error strings to ErrorString() */
ECC_BAD_ARG_E = -170, /* ECC input argument of wrong type */ ECC_BAD_ARG_E = -170, /* ECC input argument of wrong type */
ASN_ECC_KEY_E = -171, /* ASN ECC bad input */ ASN_ECC_KEY_E = -171, /* ASN ECC bad input */
ECC_CURVE_OID_E = -172, /* Unsupported ECC OID curve type */ ECC_CURVE_OID_E = -172, /* Unsupported ECC OID curve type */
BAD_FUNC_ARG = -173, /* Bad function argument provided */
NOT_COMPILED_IN = -174, /* Feature not compiled in */
UNICODE_SIZE_E = -175, /* Unicdoe password too big */
MIN_CODE_E = -200 /* errors -101 - -199 */ MIN_CODE_E = -200 /* errors -101 - -199 */
}; };

View File

@@ -22,6 +22,7 @@
#ifndef NO_AES #ifndef NO_AES
#include "ctc_aes.h" #include "ctc_aes.h"
#include "error.h"
#ifdef NO_INLINE #ifdef NO_INLINE
#include "misc.h" #include "misc.h"
#else #else
@@ -754,7 +755,7 @@ int AES_set_encrypt_key (const unsigned char *userKey, const int bits,
Aes* aes) Aes* aes)
{ {
if (!userKey || !aes) if (!userKey || !aes)
return -1; return BAD_FUNC_ARG;
if (bits == 128) { if (bits == 128) {
AES_128_Key_Expansion (userKey,(byte*)aes->key); aes->rounds = 10; AES_128_Key_Expansion (userKey,(byte*)aes->key); aes->rounds = 10;
@@ -768,7 +769,7 @@ int AES_set_encrypt_key (const unsigned char *userKey, const int bits,
AES_256_Key_Expansion (userKey,(byte*)aes->key); aes->rounds = 14; AES_256_Key_Expansion (userKey,(byte*)aes->key); aes->rounds = 14;
return 0; return 0;
} }
return -1; return BAD_FUNC_ARG;
} }
@@ -781,10 +782,10 @@ int AES_set_decrypt_key (const unsigned char* userKey, const int bits,
__m128i *Temp_Key_Schedule = (__m128i*)temp_key.key; __m128i *Temp_Key_Schedule = (__m128i*)temp_key.key;
if (!userKey || !aes) if (!userKey || !aes)
return -1; return BAD_FUNC_ARG;
if (AES_set_encrypt_key(userKey,bits,&temp_key) == -1) if (AES_set_encrypt_key(userKey,bits,&temp_key) == BAD_FUNC_ARG)
return -1; return BAD_FUNC_ARG;
nr = temp_key.rounds; nr = temp_key.rounds;
aes->rounds = nr; aes->rounds = nr;
@@ -827,7 +828,7 @@ int AesSetKey(Aes* aes, const byte* userKey, word32 keylen, const byte* iv,
unsigned int i = 0; unsigned int i = 0;
if (!((keylen == 16) || (keylen == 24) || (keylen == 32))) if (!((keylen == 16) || (keylen == 24) || (keylen == 32)))
return -1; return BAD_FUNC_ARG;
#ifdef CYASSL_AESNI #ifdef CYASSL_AESNI
if (checkAESNI == 0) { if (checkAESNI == 0) {

View File

@@ -521,7 +521,7 @@ int ToTraditional(byte* input, word32 sz)
< 0 on error */ < 0 on error */
static int CheckAlgo(int first, int second, int* id, int* version) static int CheckAlgo(int first, int second, int* id, int* version)
{ {
*id = -1; *id = ALGO_ID_E;
*version = PKCS5; /* default */ *version = PKCS5; /* default */
if (first == 1) { if (first == 1) {
@@ -535,7 +535,7 @@ static int CheckAlgo(int first, int second, int* id, int* version)
*version = PKCS12; *version = PKCS12;
return 0; return 0;
default: default:
return -1; return ALGO_ID_E;
} }
} }
@@ -555,7 +555,7 @@ static int CheckAlgo(int first, int second, int* id, int* version)
*id = PBE_SHA1_DES; *id = PBE_SHA1_DES;
return 0; return 0;
default: default:
return -1; return ALGO_ID_E;
} }
} }
@@ -573,7 +573,7 @@ static int CheckAlgoV2(int oid, int* id)
*id = PBE_SHA1_DES3; *id = PBE_SHA1_DES3;
return 0; return 0;
default: default:
return -1; return ALGO_ID_E;
} }
} }
@@ -616,7 +616,7 @@ static int DecryptKey(const char* password, int passwordSz, byte* salt,
break; break;
default: default:
return -1; /* unknown algo id */ return ALGO_ID_E;
} }
if (version == PKCS5v2) if (version == PKCS5v2)
@@ -630,7 +630,7 @@ static int DecryptKey(const char* password, int passwordSz, byte* salt,
byte unicodePasswd[MAX_UNICODE_SZ]; byte unicodePasswd[MAX_UNICODE_SZ];
if ( (passwordSz * 2 + 2) > sizeof(unicodePasswd)) if ( (passwordSz * 2 + 2) > sizeof(unicodePasswd))
return -1; /* unicode passwd too big */ return UNICODE_SIZE_E;
for (i = 0; i < passwordSz; i++) { for (i = 0; i < passwordSz; i++) {
unicodePasswd[idx++] = 0x00; unicodePasswd[idx++] = 0x00;
@@ -685,7 +685,7 @@ static int DecryptKey(const char* password, int passwordSz, byte* salt,
} }
default: default:
return -1; /* unknown algo id */ return ALGO_ID_E;
} }
return 0; return 0;
@@ -1071,7 +1071,7 @@ static int StoreRsaKey(DecodedCert* cert)
{ {
if (oid != ECC_256R1 && oid != ECC_384R1 && oid != ECC_521R1 && oid != if (oid != ECC_256R1 && oid != ECC_384R1 && oid != ECC_521R1 && oid !=
ECC_160R1 && oid != ECC_192R1 && oid != ECC_224R1) ECC_160R1 && oid != ECC_192R1 && oid != ECC_224R1)
return -1; return ALGO_ID_E;
return 0; return 0;
} }
@@ -2055,6 +2055,10 @@ void CTaoCryptErrorString(int error, char* buffer)
XSTRNCPY(buffer, "mp_cmp error state", max); XSTRNCPY(buffer, "mp_cmp error state", max);
break; break;
case MP_ZERO_E :
XSTRNCPY(buffer, "mp zero result, not expected", max);
break;
case MEMORY_E : case MEMORY_E :
XSTRNCPY(buffer, "out of memory error", max); XSTRNCPY(buffer, "out of memory error", max);
break; break;
@@ -2184,6 +2188,18 @@ void CTaoCryptErrorString(int error, char* buffer)
XSTRNCPY(buffer, "ECC curve sum OID unsupported, invalid input", max); XSTRNCPY(buffer, "ECC curve sum OID unsupported, invalid input", max);
break; break;
case BAD_FUNC_ARG :
XSTRNCPY(buffer, "Bad function argument", max);
break;
case NOT_COMPILED_IN :
XSTRNCPY(buffer, "Feature not compiled in", max);
break;
case UNICODE_SIZE_E :
XSTRNCPY(buffer, "Unicode password too big", max);
break;
default: default:
XSTRNCPY(buffer, "unknown error number", max); XSTRNCPY(buffer, "unknown error number", max);
@@ -2221,6 +2237,7 @@ int DerToPem(const byte* der, word32 derSz, byte* output, word32 outSz,
int headerLen; int headerLen;
int footerLen; int footerLen;
int i; int i;
int err;
int outLen; /* return length or error */ int outLen; /* return length or error */
if (type == CERT_TYPE) { if (type == CERT_TYPE) {
@@ -2235,11 +2252,11 @@ int DerToPem(const byte* der, word32 derSz, byte* output, word32 outSz,
footerLen = XSTRLEN(footer); footerLen = XSTRLEN(footer);
if (!der || !output) if (!der || !output)
return -1; return BAD_FUNC_ARG;
/* don't even try if outSz too short */ /* don't even try if outSz too short */
if (outSz < headerLen + footerLen + derSz) if (outSz < headerLen + footerLen + derSz)
return -1; return BAD_FUNC_ARG;
/* header */ /* header */
XMEMCPY(output, header, headerLen); XMEMCPY(output, header, headerLen);
@@ -2247,13 +2264,13 @@ int DerToPem(const byte* der, word32 derSz, byte* output, word32 outSz,
/* body */ /* body */
outLen = outSz; /* input to Base64Encode */ outLen = outSz; /* input to Base64Encode */
if (Base64Encode(der, derSz, output + i, (word32*)&outLen) < 0) if ( (err = Base64Encode(der, derSz, output + i, (word32*)&outLen)) < 0)
return -1; return ret;
i += outLen; i += outLen;
/* footer */ /* footer */
if ( (i + footerLen) > (int)outSz) if ( (i + footerLen) > (int)outSz)
return -1; return BAD_FUNC_ARG;
XMEMCPY(output + i, footer, footerLen); XMEMCPY(output + i, footer, footerLen);
return outLen + headerLen + footerLen; return outLen + headerLen + footerLen;
@@ -2302,10 +2319,10 @@ int RsaKeyToDer(RsaKey* key, byte* output, word32 inLen)
byte tmps[RSA_INTS][MAX_RSA_INT_SZ]; byte tmps[RSA_INTS][MAX_RSA_INT_SZ];
if (!key || !output) if (!key || !output)
return -1; return BAD_FUNC_ARG;
if (key->type != RSA_PRIVATE) if (key->type != RSA_PRIVATE)
return -1; return BAD_FUNC_ARG;
/* write all big ints from key to DER tmps */ /* write all big ints from key to DER tmps */
for (i = 0; i < RSA_INTS; i++) { for (i = 0; i < RSA_INTS; i++) {
@@ -2325,7 +2342,7 @@ int RsaKeyToDer(RsaKey* key, byte* output, word32 inLen)
return err; return err;
} }
else else
return -1; return ASN_INPUT_E;
} }
/* make headers */ /* make headers */
@@ -2334,7 +2351,7 @@ int RsaKeyToDer(RsaKey* key, byte* output, word32 inLen)
outLen = seqSz + verSz + intTotalLen; outLen = seqSz + verSz + intTotalLen;
if (outLen > (int)inLen) if (outLen > (int)inLen)
return -1; return BAD_FUNC_ARG;
/* write to output */ /* write to output */
XMEMCPY(output, seq, seqSz); XMEMCPY(output, seq, seqSz);
@@ -3108,7 +3125,7 @@ int StoreECC_DSA_Sig(byte* out, word32* outLen, mp_int* r, mp_int* s)
int err; int err;
if (*outLen < (rLen + sLen + headerSz + 2)) /* SEQ_TAG + LEN(ENUM) */ if (*outLen < (rLen + sLen + headerSz + 2)) /* SEQ_TAG + LEN(ENUM) */
return -1; return BAD_FUNC_ARG;
idx = SetSequence(rLen + sLen + headerSz, out); idx = SetSequence(rLen + sLen + headerSz, out);

View File

@@ -21,6 +21,7 @@
#include "coding.h" #include "coding.h"
#include "error.h"
enum { enum {
@@ -51,7 +52,7 @@ int Base64Decode(const byte* in, word32 inLen, byte* out, word32* outLen)
word32 plainSz = inLen - ((inLen + (PEM_LINE_SZ - 1)) / PEM_LINE_SZ ); word32 plainSz = inLen - ((inLen + (PEM_LINE_SZ - 1)) / PEM_LINE_SZ );
plainSz = (plainSz * 3 + 3) / 4; plainSz = (plainSz * 3 + 3) / 4;
if (plainSz > *outLen) return -1; if (plainSz > *outLen) return BAD_FUNC_ARG;
while (inLen > 3) { while (inLen > 3) {
byte b1, b2, b3; byte b1, b2, b3;
@@ -100,7 +101,7 @@ int Base64Decode(const byte* in, word32 inLen, byte* out, word32* outLen)
inLen--; inLen--;
} }
if (endLine != '\n') if (endLine != '\n')
return -1; return ASN_INPUT_E;
} }
} }
*outLen = i; *outLen = i;
@@ -133,7 +134,7 @@ int Base64Encode(const byte* in, word32 inLen, byte* out, word32* outLen)
word32 outSz = (inLen + 3 - 1) / 3 * 4; word32 outSz = (inLen + 3 - 1) / 3 * 4;
outSz += (outSz + PEM_LINE_SZ - 1) / PEM_LINE_SZ; /* new lines */ outSz += (outSz + PEM_LINE_SZ - 1) / PEM_LINE_SZ; /* new lines */
if (outSz > *outLen) return -1; if (outSz > *outLen) return BAD_FUNC_ARG;
while (inLen > 2) { while (inLen > 2) {
byte b1 = in[j++]; byte b1 = in[j++];
@@ -177,7 +178,7 @@ int Base64Encode(const byte* in, word32 inLen, byte* out, word32* outLen)
out[i++] = '\n'; out[i++] = '\n';
if (i != outSz) if (i != outSz)
return -1; return ASN_INPUT_E;
*outLen = outSz; *outLen = outSz;
return 0; return 0;
@@ -196,10 +197,10 @@ int Base16Decode(const byte* in, word32 inLen, byte* out, word32* outLen)
word32 outIdx = 0; word32 outIdx = 0;
if (inLen % 2) if (inLen % 2)
return -1; return BAD_FUNC_ARG;
if (*outLen < (inLen / 2)) if (*outLen < (inLen / 2))
return -1; return BAD_FUNC_ARG;
while (inLen) { while (inLen) {
byte b = in[inIdx++] - 0x30; /* 0 starts at 0x30 */ byte b = in[inIdx++] - 0x30; /* 0 starts at 0x30 */
@@ -207,15 +208,15 @@ int Base16Decode(const byte* in, word32 inLen, byte* out, word32* outLen)
/* sanity checks */ /* sanity checks */
if (b >= sizeof(hexDecode)/sizeof(hexDecode[0])) if (b >= sizeof(hexDecode)/sizeof(hexDecode[0]))
return -1; return ASN_INPUT_E;
if (b2 >= sizeof(hexDecode)/sizeof(hexDecode[0])) if (b2 >= sizeof(hexDecode)/sizeof(hexDecode[0]))
return -1; return ASN_INPUT_E;
b = hexDecode[b]; b = hexDecode[b];
b2 = hexDecode[b2]; b2 = hexDecode[b2];
if (b == BAD || b2 == BAD) if (b == BAD || b2 == BAD)
return -1; return ASN_INPUT_E;
out[outIdx++] = (b << 4) | b2; out[outIdx++] = (b << 4) | b2;
inLen -= 2; inLen -= 2;

View File

@@ -27,6 +27,7 @@
#ifdef USE_CYASSL_MEMORY #ifdef USE_CYASSL_MEMORY
#include "cyassl_memory.h" #include "cyassl_memory.h"
#include "error.h"
/* Set these to default values initially. */ /* Set these to default values initially. */
@@ -43,17 +44,17 @@ int CyaSSL_SetAllocators(CyaSSL_Malloc_cb mf,
if (mf) if (mf)
malloc_function = mf; malloc_function = mf;
else else
res = -1; res = BAD_FUNC_ARG;
if (ff) if (ff)
free_function = ff; free_function = ff;
else else
res = -1; res = BAD_FUNC_ARG;
if (rf) if (rf)
realloc_function = rf; realloc_function = rf;
else else
res = -1; res = BAD_FUNC_ARG;
return res; return res;
} }

View File

@@ -45,7 +45,7 @@ enum {
void InitDsaKey(DsaKey* key) void InitDsaKey(DsaKey* key)
{ {
key->type = -1; /* haven't decdied yet */ key->type = -1; /* haven't decided yet */
/* TomsFastMath doesn't use memory allocation */ /* TomsFastMath doesn't use memory allocation */
#ifndef USE_FAST_MATH #ifndef USE_FAST_MATH

View File

@@ -23,6 +23,7 @@
#ifndef NO_HMAC #ifndef NO_HMAC
#include "ctc_hmac.h" #include "ctc_hmac.h"
#include "error.h"
@@ -32,7 +33,7 @@ static int InitHmac(Hmac* hmac, int type)
hmac->macType = type; hmac->macType = type;
if (!(type == MD5 || type == SHA || type == SHA256)) if (!(type == MD5 || type == SHA || type == SHA256))
return -1; return BAD_FUNC_ARG;
if (type == MD5) if (type == MD5)
InitMd5(&hmac->hash.md5); InitMd5(&hmac->hash.md5);

View File

@@ -23,6 +23,7 @@
#include "os_settings.h" #include "os_settings.h"
#include "logging.h" #include "logging.h"
#include "error.h"
/* Set these to default values initially. */ /* Set these to default values initially. */
@@ -37,7 +38,7 @@ int CyaSSL_SetLoggingCb(CyaSSL_Logging_cb f)
if (f) if (f)
log_function = f; log_function = f;
else else
res = -1; res = BAD_FUNC_ARG;
return res; return res;
} }
@@ -49,7 +50,7 @@ int CyaSSL_Debugging_ON(void)
loggingEnabled = 1; loggingEnabled = 1;
return 0; return 0;
#else #else
return -1; /* not compiled in */ return NOT_COMPILED_IN;
#endif #endif
} }

View File

@@ -25,6 +25,7 @@
#include "pwdbased.h" #include "pwdbased.h"
#include "ctc_hmac.h" #include "ctc_hmac.h"
#include "integer.h" #include "integer.h"
#include "error.h"
#ifdef CYASSL_SHA512 #ifdef CYASSL_SHA512
#include "sha512.h" #include "sha512.h"
#endif #endif
@@ -56,13 +57,13 @@ int PBKDF1(byte* output, const byte* passwd, int pLen, const byte* salt,
byte buffer[SHA_DIGEST_SIZE]; /* max size */ byte buffer[SHA_DIGEST_SIZE]; /* max size */
if (hashType != MD5 && hashType != SHA) if (hashType != MD5 && hashType != SHA)
return -1; return BAD_FUNC_ARG;
if (kLen > hLen) if (kLen > hLen)
return -1; return BAD_FUNC_ARG;
if (iterations < 1) if (iterations < 1)
return -1; return BAD_FUNC_ARG;
if (hashType == MD5) { if (hashType == MD5) {
InitMd5(&md5); InitMd5(&md5);
@@ -117,7 +118,7 @@ int PBKDF2(byte* output, const byte* passwd, int pLen, const byte* salt,
} }
#endif #endif
else else
return -1; /* bad HMAC hashType */ return BAD_FUNC_ARG;
HmacSetKey(&hmac, hashType, passwd, pLen); HmacSetKey(&hmac, hashType, passwd, pLen);
@@ -191,7 +192,7 @@ int PKCS12_PBKDF(byte* output, const byte* passwd, int passLen,const byte* salt,
} }
#endif #endif
else else
return -1; /* bad hashType */ return BAD_FUNC_ARG;
dLen = v; dLen = v;
sLen = v * ((saltLen + v - 1) / v); sLen = v * ((saltLen + v - 1) / v);
@@ -205,7 +206,7 @@ int PKCS12_PBKDF(byte* output, const byte* passwd, int passLen,const byte* salt,
if (totalLen > sizeof(staticBuffer)) { if (totalLen > sizeof(staticBuffer)) {
buffer = (byte*)XMALLOC(totalLen, 0, DYNAMIC_TYPE_KEY); buffer = (byte*)XMALLOC(totalLen, 0, DYNAMIC_TYPE_KEY);
if (buffer == NULL) return -1; if (buffer == NULL) return MEMORY_E;
dynamic = 1; dynamic = 1;
} }
@@ -251,9 +252,9 @@ int PKCS12_PBKDF(byte* output, const byte* passwd, int passLen,const byte* salt,
mp_init(&B1); mp_init(&B1);
if (mp_read_unsigned_bin(&B1, B, v) != MP_OKAY) if (mp_read_unsigned_bin(&B1, B, v) != MP_OKAY)
ret = -1; ret = MP_READ_E;
else if (mp_add_d(&B1, (mp_digit)1, &B1) != MP_OKAY) { else if (mp_add_d(&B1, (mp_digit)1, &B1) != MP_OKAY) {
ret = -1; ret = MP_ADD_E;
mp_clear(&B1); mp_clear(&B1);
break; break;
} }
@@ -267,11 +268,11 @@ int PKCS12_PBKDF(byte* output, const byte* passwd, int passLen,const byte* salt,
mp_init(&res); mp_init(&res);
if (mp_read_unsigned_bin(&i1, I + i, v) != MP_OKAY) if (mp_read_unsigned_bin(&i1, I + i, v) != MP_OKAY)
ret = -1; ret = MP_READ_E;
else if (mp_add(&i1, &B1, &res) != MP_OKAY) else if (mp_add(&i1, &B1, &res) != MP_OKAY)
ret = -1; ret = MP_ADD_E;
else if ( (outSz = mp_unsigned_bin_size(&res)) < 0) else if ( (outSz = mp_unsigned_bin_size(&res)) < 0)
ret = -1; ret = MP_TO_E;
else { else {
if (outSz > v) { if (outSz > v) {
/* take off MSB */ /* take off MSB */

View File

@@ -48,7 +48,7 @@ enum {
void InitRsaKey(RsaKey* key, void* heap) void InitRsaKey(RsaKey* key, void* heap)
{ {
key->type = -1; /* haven't decdied yet */ key->type = -1; /* haven't decided yet */
key->heap = heap; key->heap = heap;
/* TomsFastMath doesn't use memory allocation */ /* TomsFastMath doesn't use memory allocation */
@@ -369,7 +369,7 @@ static int rand_prime(mp_int* N, int len, RNG* rng, void* heap)
byte* buf; byte* buf;
if (N == NULL || rng == NULL) if (N == NULL || rng == NULL)
return -1; return BAD_FUNC_ARG;
/* get type */ /* get type */
if (len < 0) { if (len < 0) {
@@ -381,13 +381,13 @@ static int rand_prime(mp_int* N, int len, RNG* rng, void* heap)
/* allow sizes between 2 and 512 bytes for a prime size */ /* allow sizes between 2 and 512 bytes for a prime size */
if (len < 2 || len > 512) { if (len < 2 || len > 512) {
return -1; return BAD_FUNC_ARG;
} }
/* allocate buffer to work with */ /* allocate buffer to work with */
buf = XMALLOC(len, heap, DYNAMIC_TYPE_RSA); buf = XMALLOC(len, heap, DYNAMIC_TYPE_RSA);
if (buf == NULL) { if (buf == NULL) {
return -1; return MEMORY_E;
} }
XMEMSET(buf, 0, len); XMEMSET(buf, 0, len);
@@ -432,13 +432,13 @@ int MakeRsaKey(RsaKey* key, int size, long e, RNG* rng)
int err; int err;
if (key == NULL || rng == NULL) if (key == NULL || rng == NULL)
return -1; return BAD_FUNC_ARG;
if (size < RSA_MIN_SIZE || size > RSA_MAX_SIZE) if (size < RSA_MIN_SIZE || size > RSA_MAX_SIZE)
return -1; return BAD_FUNC_ARG;
if (e < 3 || (e & 1) == 0) if (e < 3 || (e & 1) == 0)
return -1; return BAD_FUNC_ARG;
if ((err = mp_init_multi(&p, &q, &tmp1, &tmp2, &tmp3, NULL)) != MP_OKAY) if ((err = mp_init_multi(&p, &q, &tmp1, &tmp2, &tmp3, NULL)) != MP_OKAY)
return err; return err;