Skip to content

Commit 86b3227

Browse files
authored
Merge pull request #133 from ColtonWilley/wp_cipher_null_zero_fix
Fix AES handling to accept NULL/0 input
2 parents 6daa186 + 076dbb4 commit 86b3227

File tree

5 files changed

+95
-2
lines changed

5 files changed

+95
-2
lines changed

src/wp_aes_block.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,9 @@ static int wp_aes_block_cipher(wp_AesBlockCtx* ctx, unsigned char* out,
693693
if (ok && (outSize < inLen)) {
694694
ok = 0;
695695
}
696-
if (ok && !wp_aes_block_doit(ctx, out, in, inLen)) {
696+
/* NULL in, NULL out, 0 len is OK */
697+
if (ok && (out != NULL && in != NULL && inLen != 0) &&
698+
!wp_aes_block_doit(ctx, out, in, inLen)) {
697699
ok = 0;
698700
}
699701
if (ok) {

src/wp_aes_stream.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,9 @@ static int wp_aes_stream_cipher(wp_AesStreamCtx* ctx, unsigned char* out,
477477
if (ok && (outSize < inLen)) {
478478
ok = 0;
479479
}
480-
if (ok && (!wp_aes_stream_doit(ctx, out, in, inLen))) {
480+
/* NULL in, NULL out, 0 len is OK */
481+
if (ok && (in != NULL && out != NULL && inLen != 0) &&
482+
(!wp_aes_stream_doit(ctx, out, in, inLen))) {
481483
ok = 0;
482484
}
483485

test/test_cipher.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,92 @@ static int test_stream_enc_dec(void *data, const char *cipher, int keyLen,
344344
return err;
345345
}
346346

347+
static int test_cipher_null_zero_ex(void *data, const char *cipher, int keyLen,
348+
int ivLen)
349+
{
350+
int err = 0;
351+
unsigned char msg[16] = "Test pattern";
352+
unsigned char key[32];
353+
unsigned char iv[16];
354+
unsigned char enc[sizeof(msg) + 16];
355+
EVP_CIPHER *ocipher;
356+
EVP_CIPHER *wcipher;
357+
EVP_CIPHER_CTX *ctx;
358+
359+
(void)data;
360+
361+
ocipher = EVP_CIPHER_fetch(osslLibCtx, cipher, "");
362+
wcipher = EVP_CIPHER_fetch(wpLibCtx, cipher, "");
363+
364+
if (RAND_bytes(key, keyLen) != 1) {
365+
err = 1;
366+
}
367+
if (err == 0) {
368+
if (RAND_bytes(iv, ivLen) != 1) {
369+
err = 1;
370+
}
371+
}
372+
373+
/* Test that a final call with NULL/NULL/0 yields the correct return
374+
* value, flow mimics that of libssh2 */
375+
err = (ctx = EVP_CIPHER_CTX_new()) == NULL;
376+
if (err == 0) {
377+
err = EVP_CipherInit(ctx, ocipher, key, iv, 1) != 1;
378+
}
379+
if (err == 0) {
380+
err = EVP_Cipher(ctx, enc, msg, sizeof(msg)) <= 0;
381+
}
382+
/* Return is 0, not negative value for NULL/NULL/0 input */
383+
if (err == 0) {
384+
err = EVP_Cipher(ctx, NULL, NULL, 0) != 0;
385+
}
386+
EVP_CIPHER_CTX_free(ctx);
387+
388+
err = (ctx = EVP_CIPHER_CTX_new()) == NULL;
389+
if (err == 0) {
390+
err = EVP_CipherInit(ctx, wcipher, key, iv, 1) != 1;
391+
}
392+
if (err == 0) {
393+
err = EVP_Cipher(ctx, enc, msg, sizeof(msg)) <= 0;
394+
}
395+
/* Return is 0, not negative value for NULL/NULL/0 input */
396+
if (err == 0) {
397+
err = EVP_Cipher(ctx, NULL, NULL, 0) != 0;
398+
}
399+
EVP_CIPHER_CTX_free(ctx);
400+
401+
EVP_CIPHER_free(wcipher);
402+
EVP_CIPHER_free(ocipher);
403+
404+
return err;
405+
}
406+
407+
int test_cipher_null_zero(void *data)
408+
{
409+
int err = 0;
410+
411+
#ifdef WP_HAVE_AESECB
412+
err = test_cipher_null_zero_ex(data, "AES-256-ECB", 32, 16);
413+
#endif
414+
#ifdef WP_HAVE_AESCBC
415+
if (err == 0) {
416+
err = test_cipher_null_zero_ex(data, "AES-256-CBC", 32, 16);
417+
}
418+
#endif
419+
#ifdef WP_HAVE_AESCTR
420+
if (err == 0) {
421+
err = test_cipher_null_zero_ex(data, "AES-256-CTR", 32, 16);
422+
}
423+
#endif
424+
#ifdef WP_HAVE_AESCFB
425+
if (err == 0) {
426+
err = test_cipher_null_zero_ex(data, "AES-256-CFB", 32, 16);
427+
}
428+
#endif
429+
430+
return err;
431+
}
432+
347433
#endif /* WP_HAVE_DES3CBC || WP_HAVE_AESCBC */
348434

349435
/******************************************************************************/

test/unit.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ TEST_CASE test_case[] = {
129129
TEST_DECL(test_aes192_cfb_stream, NULL),
130130
TEST_DECL(test_aes256_cfb_stream, NULL),
131131
#endif
132+
TEST_DECL(test_cipher_null_zero, NULL),
132133
#ifdef WP_HAVE_AESGCM
133134
TEST_DECL(test_aes128_gcm, NULL),
134135
TEST_DECL(test_aes192_gcm, NULL),

test/unit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ int test_aes256_cfb_stream(void *data);
174174

175175
#endif
176176

177+
int test_cipher_null_zero(void *data);
178+
177179
#ifdef WP_HAVE_AESGCM
178180

179181
int test_aes128_gcm(void *data);

0 commit comments

Comments
 (0)