Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ECC Enc test: fix ecc_ctx_kdf_salt_test #6083

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 45 additions & 33 deletions wolfcrypt/test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -26606,80 +26606,90 @@ static int ecc_ctx_kdf_salt_test(WC_RNG* rng, ecc_key* a, ecc_key* b)
ecEncCtx* bCtx = NULL;
const byte salt[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15};
int ret = 0, aRet = -1, bRet = -1;
const char* message = "Hello wolfSSL!";
word32 plaintextLen = sizeof(message), encryptLen = 128, decryptLen = 128;
int ret = 0;
const char message[] = "Hello wolfSSL!";
word32 plaintextLen;
word32 encryptLen = 128;
word32 decryptLen = 128;

#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
plaintext = XMALLOC(128, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
encrypted = XMALLOC(128, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
decrypted = XMALLOC(128, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
#endif

ret = aRet = wc_ecc_init(a);

if (ret == 0)
ret = bRet = wc_ecc_init(b);
wc_ecc_free(a);
wc_ecc_free(b);

if (ret == 0)
ret = wc_ecc_make_key(rng, 32, a);
ret = wc_ecc_init(a);
if (ret != 0)
ret = -10480;
if (ret == 0) {
ret = wc_ecc_init(b);
if (ret != 0)
ret = -10481;
}

ret = wc_ecc_make_key(rng, 32, a);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why isn't this conditional on (ret == 0)?

if (ret == 0)
ret = wc_ecc_make_key(rng, 32, b);

/* create context */
if (ret == 0) {
aCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);

if (aCtx == NULL)
ret = -1;
ret = -10470;
}

if (ret == 0) {
bCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);

if (bCtx == NULL)
ret = -1;
ret = -10471;
}

/* set salt */
if (ret == 0)
if (ret == 0) {
ret = wc_ecc_ctx_set_kdf_salt(aCtx, salt, sizeof(salt));
if (ret != 0)
ret = -10472;
}

if (ret == 0)
if (ret == 0) {
ret = wc_ecc_ctx_set_kdf_salt(bCtx, salt, sizeof(salt));
if (ret != 0)
ret = 10473;
}

XMEMCPY(plaintext, message, XSTRLEN(message));

while (plaintextLen % AES_BLOCK_SIZE != 0) {
plaintextLen++;
}
plaintextLen = (((word32)XSTRLEN(message) + AES_BLOCK_SIZE - 1) /
AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
XMEMSET(plaintext + XSTRLEN(message), 0, plaintextLen - XSTRLEN(message));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sanitizer is not happy with this construction. probably just do what I did in #6081 -- pre-zero plaintext in its entirety, XMEMSET(plaintext, 0, 128); I retested with that change and it was fine.

In file included from /usr/include/string.h:535,
                 from ./wolfssl/wolfcrypt/types.h:623,
                 from wolfcrypt/test/test.c:34:
In function ‘memset’,
    inlined from ‘ecc_ctx_kdf_salt_test’ at wolfcrypt/test/test.c:26666:5,
338d8db274 (<106998124+jpbland1@users.noreply.github.com> 2023-02-10 13:05:52 -0500 27333)         ret = ecc_ctx_kdf_salt_test(&rng, userA, userB);
    inlined from ‘ecc_encrypt_test’ at wolfcrypt/test/test.c:27333:15:
/usr/include/bits/string_fortified.h:59:10: error: ‘__builtin_memset’ offset [0, 1] is out of the bounds [0, 0] [-Werror=array-bounds]
   59 |   return __builtin___memset_chk (__dest, __ch, __len,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   60 |                                  __glibc_objsize0 (__dest));
      |                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/bits/string_fortified.h:59:10: error: ‘__builtin_memset’ offset [0, 1] is out of the bounds [0, 0] [-Werror=array-bounds]

found by the sanitizer-all-asm-smallstack-async test, i.e. '--enable-all' '--enable-asynccrypt' '--enable-intelasm' '--enable-sp-math-all' '--enable-smallstack' '--enable-smallstackcache' 'CPPFLAGS=-pedantic' 'CC=gcc' 'CFLAGS=-g -fno-omit-frame-pointer -fsanitize=address,pointer-subtract,leak,undefined,float-cast-overflow,float-divide-by-zero,bounds-strict -fsanitize-recover=all --param=max-vartrack-size=128000000 -march=native' 'LDFLAGS=-g -fno-omit-frame-pointer -fsanitize-recover=all -fsanitize=address,pointer-subtract,leak,undefined,float-cast-overflow,float-divide-by-zero,bounds-strict -fno-sanitize-recover=all '

the async part has nothing to do with it AFAIK -- all about the smallstack.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


/* encrypt */
if (ret == 0)
if (ret == 0) {
ret = wc_ecc_encrypt(a, b, plaintext, plaintextLen, encrypted,
&encryptLen, aCtx);
if (ret != 0)
ret = -10474;
}

/* decrypt */
if (ret == 0)
if (ret == 0) {
ret = wc_ecc_decrypt(b, a, encrypted, encryptLen, decrypted,
&decryptLen, bCtx);
if (ret != 0)
ret = -10475;
}

/* compare */
if (ret == 0 && XMEMCMP(decrypted, (byte*)message, sizeof(message)) != 0)
ret = -1;
if (ret == 0 && XMEMCMP(decrypted, (byte*)message, XSTRLEN(message)) != 0)
ret = -10476;

if (aRet == 0)
wc_ecc_free(a);
wc_ecc_free(a);
wc_ecc_free(b);

if (bRet == 0)
wc_ecc_free(b);

if (aCtx != NULL)
wc_ecc_ctx_free(aCtx);

if (bCtx != NULL)
wc_ecc_ctx_free(bCtx);
wc_ecc_ctx_free(aCtx);
wc_ecc_ctx_free(bCtx);

#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
XFREE(plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
Expand Down Expand Up @@ -27317,6 +27327,8 @@ WOLFSSL_TEST_SUBROUTINE int ecc_encrypt_test(void)
}
}
#endif
#endif
#if !defined(NO_AES) && defined(HAVE_AES_CBC)
if (ret == 0) {
ret = ecc_ctx_kdf_salt_test(&rng, userA, userB);
}
Expand Down