From 38cb4a2d692740109f30c97999f840399c07e961 Mon Sep 17 00:00:00 2001 From: Daniel Pouzzner Date: Mon, 21 Sep 2020 18:55:24 -0500 Subject: [PATCH] blake2{b,s}.c: return and propagate meaningful error codes. --- wolfcrypt/src/blake2b.c | 54 ++++++++++++++++++++++++++-------------- wolfcrypt/src/blake2s.c | 55 +++++++++++++++++++++++++++-------------- 2 files changed, 73 insertions(+), 36 deletions(-) diff --git a/wolfcrypt/src/blake2b.c b/wolfcrypt/src/blake2b.c index f6d92f472..9bd6dc49f 100644 --- a/wolfcrypt/src/blake2b.c +++ b/wolfcrypt/src/blake2b.c @@ -43,6 +43,7 @@ #include #include +#include static const word64 blake2b_IV[8] = @@ -124,7 +125,7 @@ int blake2b_init( blake2b_state *S, const byte outlen ) { blake2b_param P[1]; - if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1; + if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return BAD_FUNC_ARG; #ifdef WOLFSSL_BLAKE2B_INIT_EACH_FIELD P->digest_length = outlen; @@ -153,9 +154,9 @@ int blake2b_init_key( blake2b_state *S, const byte outlen, const void *key, { blake2b_param P[1]; - if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1; + if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return BAD_FUNC_ARG; - if ( !key || !keylen || keylen > BLAKE2B_KEYBYTES ) return -1; + if ( !key || !keylen || keylen > BLAKE2B_KEYBYTES ) return BAD_FUNC_ARG; #ifdef WOLFSSL_BLAKE2B_INIT_EACH_FIELD P->digest_length = outlen; @@ -177,7 +178,10 @@ int blake2b_init_key( blake2b_state *S, const byte outlen, const void *key, P->depth = 1; #endif - if( blake2b_init_param( S, P ) < 0 ) return -1; + { + int ret = blake2b_init_param( S, P ); + if ( ret < 0 ) return ret; + } { #ifdef WOLFSSL_SMALL_STACK @@ -185,7 +189,7 @@ int blake2b_init_key( blake2b_state *S, const byte outlen, const void *key, block = (byte*)XMALLOC(BLAKE2B_BLOCKBYTES, NULL, DYNAMIC_TYPE_TMP_BUFFER); - if ( block == NULL ) return -1; + if ( block == NULL ) return MEMORY_E; #else byte block[BLAKE2B_BLOCKBYTES]; #endif @@ -214,14 +218,14 @@ static int blake2b_compress( blake2b_state *S, m = (word64*)XMALLOC(sizeof(word64) * 16, NULL, DYNAMIC_TYPE_TMP_BUFFER); - if ( m == NULL ) return -1; + if ( m == NULL ) return MEMORY_E; v = (word64*)XMALLOC(sizeof(word64) * 16, NULL, DYNAMIC_TYPE_TMP_BUFFER); if ( v == NULL ) { XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER); - return -1; + return MEMORY_E; } #else word64 m[16]; @@ -305,7 +309,10 @@ int blake2b_update( blake2b_state *S, const byte *in, word64 inlen ) S->buflen += fill; blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES ); - if ( blake2b_compress( S, S->buf ) < 0 ) return -1; /* Compress */ + { + int ret = blake2b_compress( S, S->buf ); + if (ret < 0) return ret; + } XMEMCPY( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES ); /* Shift buffer left */ @@ -334,7 +341,10 @@ int blake2b_final( blake2b_state *S, byte *out, byte outlen ) { blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES ); - if ( blake2b_compress( S, S->buf ) < 0 ) return -1; + { + int ret = blake2b_compress( S, S->buf ); + if (ret < 0) return ret; + } S->buflen -= BLAKE2B_BLOCKBYTES; XMEMCPY( S->buf, S->buf + BLAKE2B_BLOCKBYTES, (wolfssl_word)S->buflen ); @@ -344,7 +354,10 @@ int blake2b_final( blake2b_state *S, byte *out, byte outlen ) blake2b_set_lastblock( S ); XMEMSET( S->buf + S->buflen, 0, (wolfssl_word)(2 * BLAKE2B_BLOCKBYTES - S->buflen) ); /* Padding */ - if ( blake2b_compress( S, S->buf ) < 0 ) return -1; + { + int ret = blake2b_compress( S, S->buf ); + if (ret < 0) return ret; + } for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */ store64( buffer + sizeof( S->h[i] ) * i, S->h[i] ); @@ -360,22 +373,27 @@ int blake2b( byte *out, const void *in, const void *key, const byte outlen, blake2b_state S[1]; /* Verify parameters */ - if ( NULL == in ) return -1; + if ( NULL == in ) return BAD_FUNC_ARG; - if ( NULL == out ) return -1; + if ( NULL == out ) return BAD_FUNC_ARG; if( NULL == key ) keylen = 0; if( keylen > 0 ) { - if( blake2b_init_key( S, outlen, key, keylen ) < 0 ) return -1; + int ret = blake2b_init_key( S, outlen, key, keylen ); + if (ret < 0) return ret; } else { - if( blake2b_init( S, outlen ) < 0 ) return -1; + int ret = blake2b_init( S, outlen ); + if (ret < 0) return ret; } - if ( blake2b_update( S, ( byte * )in, inlen ) < 0) return -1; + { + int ret = blake2b_update( S, ( byte * )in, inlen ); + if (ret < 0) return ret; + } return blake2b_final( S, out, outlen ); } @@ -422,7 +440,7 @@ int main( int argc, char **argv ) int wc_InitBlake2b(Blake2b* b2b, word32 digestSz) { if (b2b == NULL){ - return -1; + return BAD_FUNC_ARG; } b2b->digestSz = digestSz; @@ -433,12 +451,12 @@ int wc_InitBlake2b(Blake2b* b2b, word32 digestSz) int wc_InitBlake2b_WithKey(Blake2b* b2b, word32 digestSz, const byte *key, word32 keylen) { if (b2b == NULL){ - return -1; + return BAD_FUNC_ARG; } b2b->digestSz = digestSz; if (keylen >= 256) - return -1; + return BAD_FUNC_ARG; if (key) return blake2b_init_key(b2b->S, (byte)digestSz, key, (byte)keylen); diff --git a/wolfcrypt/src/blake2s.c b/wolfcrypt/src/blake2s.c index 53875a039..56a8d298e 100644 --- a/wolfcrypt/src/blake2s.c +++ b/wolfcrypt/src/blake2s.c @@ -43,6 +43,7 @@ #include #include +#include static const word32 blake2s_IV[8] = @@ -120,7 +121,7 @@ int blake2s_init( blake2s_state *S, const byte outlen ) { blake2s_param P[1]; - if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return -1; + if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return BAD_FUNC_ARG; #ifdef WOLFSSL_BLAKE2S_INIT_EACH_FIELD P->digest_length = outlen; @@ -149,9 +150,9 @@ int blake2s_init_key( blake2s_state *S, const byte outlen, const void *key, { blake2s_param P[1]; - if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return -1; + if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return BAD_FUNC_ARG; - if ( !key || !keylen || keylen > BLAKE2S_KEYBYTES ) return -1; + if ( !key || !keylen || keylen > BLAKE2S_KEYBYTES ) return BAD_FUNC_ARG; #ifdef WOLFSSL_BLAKE2S_INIT_EACH_FIELD P->digest_length = outlen; @@ -173,7 +174,11 @@ int blake2s_init_key( blake2s_state *S, const byte outlen, const void *key, P->depth = 1; #endif - if( blake2s_init_param( S, P ) < 0 ) return -1; + { + int ret = blake2s_init_param( S, P ); + if (ret < 0) + return ret; + } { #ifdef WOLFSSL_SMALL_STACK @@ -181,7 +186,7 @@ int blake2s_init_key( blake2s_state *S, const byte outlen, const void *key, block = (byte*)XMALLOC(BLAKE2S_BLOCKBYTES, NULL, DYNAMIC_TYPE_TMP_BUFFER); - if ( block == NULL ) return -1; + if ( block == NULL ) return MEMORY_E; #else byte block[BLAKE2S_BLOCKBYTES]; #endif @@ -210,14 +215,14 @@ static int blake2s_compress( blake2s_state *S, m = (word32*)XMALLOC(sizeof(word32) * 16, NULL, DYNAMIC_TYPE_TMP_BUFFER); - if ( m == NULL ) return -1; + if ( m == NULL ) return MEMORY_E; v = (word32*)XMALLOC(sizeof(word32) * 16, NULL, DYNAMIC_TYPE_TMP_BUFFER); if ( v == NULL ) { XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER); - return -1; + return MEMORY_E; } #else word32 m[16]; @@ -299,7 +304,10 @@ int blake2s_update( blake2s_state *S, const byte *in, word32 inlen ) S->buflen += fill; blake2s_increment_counter( S, BLAKE2S_BLOCKBYTES ); - if ( blake2s_compress( S, S->buf ) < 0 ) return -1; /* Compress */ + { + int ret= blake2s_compress( S, S->buf ); + if (ret < 0) return ret; + } XMEMCPY( S->buf, S->buf + BLAKE2S_BLOCKBYTES, BLAKE2S_BLOCKBYTES ); /* Shift buffer left */ @@ -328,7 +336,10 @@ int blake2s_final( blake2s_state *S, byte *out, byte outlen ) { blake2s_increment_counter( S, BLAKE2S_BLOCKBYTES ); - if ( blake2s_compress( S, S->buf ) < 0 ) return -1; + { + int ret = blake2s_compress( S, S->buf ); + if (ret < 0) return ret; + } S->buflen -= BLAKE2S_BLOCKBYTES; XMEMCPY( S->buf, S->buf + BLAKE2S_BLOCKBYTES, (wolfssl_word)S->buflen ); @@ -338,7 +349,10 @@ int blake2s_final( blake2s_state *S, byte *out, byte outlen ) blake2s_set_lastblock( S ); XMEMSET( S->buf + S->buflen, 0, (wolfssl_word)(2 * BLAKE2S_BLOCKBYTES - S->buflen) ); /* Padding */ - if ( blake2s_compress( S, S->buf ) < 0 ) return -1; + { + int ret = blake2s_compress( S, S->buf ); + if (ret < 0) return ret; + } for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */ store64( buffer + sizeof( S->h[i] ) * i, S->h[i] ); @@ -354,22 +368,27 @@ int blake2s( byte *out, const void *in, const void *key, const byte outlen, blake2s_state S[1]; /* Verify parameters */ - if ( NULL == in ) return -1; + if ( NULL == in ) return BAD_FUNC_ARG; - if ( NULL == out ) return -1; + if ( NULL == out ) return BAD_FUNC_ARG; if( NULL == key ) keylen = 0; if( keylen > 0 ) { - if( blake2s_init_key( S, outlen, key, keylen ) < 0 ) return -1; + int ret = blake2s_init_key( S, outlen, key, keylen ); + if (ret < 0) return ret; } else { - if( blake2s_init( S, outlen ) < 0 ) return -1; + int ret = blake2s_init( S, outlen ); + if (ret < 0) return ret; } - if ( blake2s_update( S, ( byte * )in, inlen ) < 0) return -1; + { + int ret = blake2s_update( S, ( byte * )in, inlen ); + if (ret < 0) return ret; + } return blake2s_final( S, out, outlen ); } @@ -416,7 +435,7 @@ int main( int argc, char **argv ) int wc_InitBlake2s(Blake2s* b2s, word32 digestSz) { if (b2s == NULL){ - return -1; + return BAD_FUNC_ARG; } b2s->digestSz = digestSz; @@ -428,12 +447,12 @@ int wc_InitBlake2s(Blake2s* b2s, word32 digestSz) int wc_InitBlake2s_WithKey(Blake2s* b2s, word32 digestSz, const byte *key, word32 keylen) { if (b2s == NULL){ - return -1; + return BAD_FUNC_ARG; } b2s->digestSz = digestSz; if (keylen >= 256) - return -1; + return BAD_FUNC_ARG; if (key) return blake2s_init_key(b2s->S, (byte)digestSz, key, (byte)keylen);