Skip to content
Open
Show file tree
Hide file tree
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
80 changes: 80 additions & 0 deletions tests/api/test_ssl_pk.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@
#include <wolfssl/ssl.h>
#include <wolfssl/internal.h>
#include <wolfssl/openssl/ec.h>
#ifdef WOLFSSL_HAVE_PSA
#include <wolfssl/wolfcrypt/port/psa/psa.h>
#endif

/* Must precede certs_test.h: defines USE_CERT_BUFFERS_256. */
#include <tests/api/api.h>
#include <wolfssl/certs_test.h>
#include <tests/utils.h>
#include <tests/api/test_ssl_pk.h>

Expand Down Expand Up @@ -606,3 +612,77 @@ int test_wolfSSL_CTX_set_dh_auto(void)
#endif
return EXPECT_RESULT();
}

/* The PSA verify callback with r and s of different sizes */
int test_wolfSSL_CTX_psa_enable_ecc_verify(void)
{
EXPECT_DECLS;
#if defined(WOLFSSL_HAVE_PSA) && defined(HAVE_PK_CALLBACKS) && \
!defined(WOLFSSL_PSA_NO_PKCBS) && defined(HAVE_ECC) && \
(!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
ECC_MIN_KEY_SZ <= 256 && \
!defined(NO_WOLFSSL_CLIENT) && !defined(NO_TLS)
static const byte digest[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
};
static const byte otherDigest[sizeof(digest)] = { 0 };
/* Signatures of digest by certs/ecc-key.pem: r has 31 bytes */
static const byte sigShortR[] = {
0x30, 0x43, 0x02, 0x1f, 0x0e, 0xd9, 0xbd, 0x52, 0x69, 0xec, 0x1d, 0x0e,
0x8c, 0xae, 0xf8, 0xff, 0x50, 0xa1, 0x46, 0xa3, 0xe0, 0x86, 0xa4, 0xb5,
0x63, 0xc5, 0x87, 0xe5, 0xce, 0x90, 0x1f, 0x7d, 0xc5, 0xfe, 0x4a, 0x02,
0x20, 0x53, 0xf5, 0x88, 0x60, 0x0c, 0x2a, 0xdf, 0xdf, 0xd9, 0x20, 0x91,
0xbc, 0xe6, 0xd9, 0x8d, 0xdd, 0x23, 0xc1, 0xf9, 0x1c, 0x4d, 0x35, 0xd3,
0x4c, 0x99, 0x03, 0xff, 0xef, 0x51, 0x9d, 0xb4, 0x8f
};
/* s has 31 bytes */
static const byte sigShortS[] = {
0x30, 0x44, 0x02, 0x21, 0x00, 0xa1, 0x0c, 0x76, 0xde, 0x09, 0xcd, 0x63,
0xf3, 0x7d, 0x0a, 0xfb, 0xc8, 0xb4, 0x46, 0x58, 0xc9, 0x6a, 0xba, 0x3f,
0x20, 0xf8, 0x6f, 0x16, 0x98, 0x9b, 0xa5, 0x9b, 0x6c, 0x0a, 0x82, 0xd8,
0xb6, 0x02, 0x1f, 0x5f, 0x03, 0x6e, 0xc8, 0x28, 0xf0, 0x80, 0xe3, 0x9c,
0x39, 0x39, 0x3f, 0x2a, 0x1a, 0xd4, 0x47, 0x32, 0xef, 0xba, 0x55, 0x5e,
0x01, 0x81, 0x94, 0xb9, 0xdb, 0x2c, 0xfd, 0x8e, 0x7f, 0xce
};
/* r has 33 bytes, more than the key size */
static const byte sigLongR[] = {
0x30, 0x45, 0x02, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x02, 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
static const struct {
const byte* sig;
word32 sz;
const byte* hash;
int ret;
int result;
} sigs[] = {
{ sigShortR, sizeof(sigShortR), digest, 0, 1 },
{ sigShortS, sizeof(sigShortS), digest, 0, 1 },
{ sigShortS, sizeof(sigShortS), otherDigest, 0, 0 },
{ sigLongR, sizeof(sigLongR), digest, WC_NO_ERR_TRACE(BUFFER_E), 0 }
};
WOLFSSL_CTX* ctx = NULL;
size_t i;
int result;

ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
ExpectIntEQ(wolfSSL_CTX_psa_enable(ctx), WOLFSSL_SUCCESS);

for (i = 0; EXPECT_SUCCESS() && i < sizeof(sigs) / sizeof(*sigs); i++) {
result = -1;
ExpectIntEQ(ctx->EccVerifyCb(NULL, sigs[i].sig, sigs[i].sz,
sigs[i].hash, sizeof(digest), ecc_key_pub_der_256,
sizeof_ecc_key_pub_der_256, &result, NULL), sigs[i].ret);
ExpectIntEQ(result, sigs[i].result);
}

wolfSSL_CTX_free(ctx);
#endif
return EXPECT_RESULT();
}
4 changes: 3 additions & 1 deletion tests/api/test_ssl_pk.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ int test_wolfSSL_get_peer_signature_nid(void);
int test_wolfSSL_get_peer_signature_type_nid(void);
int test_wolfSSL_SSL_CTX_set_tmp_ecdh(void);
int test_wolfSSL_CTX_set_dh_auto(void);
int test_wolfSSL_CTX_psa_enable_ecc_verify(void);

#define TEST_SSL_PK_DECLS \
TEST_DECL_GROUP("ssl_pk", test_wolfSSL_CTX_SetMinEccKey_Sz), \
Expand All @@ -57,6 +58,7 @@ int test_wolfSSL_CTX_set_dh_auto(void);
TEST_DECL_GROUP("ssl_pk", test_wolfSSL_get_peer_signature_nid), \
TEST_DECL_GROUP("ssl_pk", test_wolfSSL_get_peer_signature_type_nid), \
TEST_DECL_GROUP("ssl_pk", test_wolfSSL_SSL_CTX_set_tmp_ecdh), \
TEST_DECL_GROUP("ssl_pk", test_wolfSSL_CTX_set_dh_auto)
TEST_DECL_GROUP("ssl_pk", test_wolfSSL_CTX_set_dh_auto), \
TEST_DECL_GROUP("ssl_pk", test_wolfSSL_CTX_psa_enable_ecc_verify)

#endif /* TESTS_API_SSL_PK_H */
28 changes: 19 additions & 9 deletions wolfcrypt/src/port/psa/psa_pkcbs.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ static int psa_ecc_sign_cb(WOLFSSL* ssl, const unsigned char* input,
}

static int psa_ecc_decode_public_key(const uint8_t *key, word32 key_length,
psa_key_id_t *key_id,
psa_key_id_t *key_id, size_t *key_size,
psa_algorithm_t hash_algo)
{
uint8_t raw_key[(MAX_ECC_BYTES * 2) + 1];
Expand Down Expand Up @@ -377,20 +377,29 @@ static int psa_ecc_decode_public_key(const uint8_t *key, word32 key_length,
goto exit;
}

*key_size = ecc_curve_size;
ret = 0;

exit:
wc_ecc_free(&tmp_key);
return ret;
}

/* Moves the len bytes at the start of buf to its end, zero filling the rest */
static void psa_ecc_zero_extend(uint8_t *buf, word32 len, size_t size)
{
XMEMMOVE(buf + size - len, buf, len);
XMEMSET(buf, 0, size - len);
}

static int psa_ecc_verify_cb(WOLFSSL* ssl, const byte* sig, word32 sig_length,
const byte* hash, word32 hash_length,
const byte* key, word32 key_length,
int* result, void* ctx)
{
uint8_t raw_signature[MAX_ECC_BYTES * 2], *s;
uint8_t raw_signature[MAX_ECC_BYTES * 2];
psa_key_id_t tmp_key;
size_t key_size;
word32 r_len, s_len;
psa_status_t status;
psa_algorithm_t hash_algo;
Expand All @@ -407,23 +416,24 @@ static int psa_ecc_verify_cb(WOLFSSL* ssl, const byte* sig, word32 sig_length,
if (hash_algo == PSA_ALG_NONE)
return BAD_FUNC_ARG;

ret = psa_ecc_decode_public_key(key, key_length, &tmp_key, hash_algo);
ret = psa_ecc_decode_public_key(key, key_length, &tmp_key, &key_size,
hash_algo);
if (ret != 0)
return ret;

r_len = s_len = MAX_ECC_BYTES;
s = raw_signature + r_len;
/* PSA takes r and s each zero extended to the key size */
r_len = s_len = (word32)key_size;
ret = wc_ecc_sig_to_rs(sig, sig_length, raw_signature, &r_len,
s, &s_len);
raw_signature + key_size, &s_len);
if (ret != 0)
goto exit;

/* coalescence of r and s in the buffer */
XMEMMOVE(raw_signature + r_len, s, s_len);
psa_ecc_zero_extend(raw_signature, r_len, key_size);
psa_ecc_zero_extend(raw_signature + key_size, s_len, key_size);

PSA_LOCK();
status = psa_verify_hash(tmp_key, PSA_ALG_ECDSA(hash_algo), hash,
hash_length, raw_signature, r_len + s_len);
hash_length, raw_signature, key_size * 2);
PSA_UNLOCK();

if (status == PSA_SUCCESS) {
Expand Down
Loading