Skip to content
Closed
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
45 changes: 34 additions & 11 deletions src/we_rsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,7 @@ static int we_rsa_priv_enc_int(size_t fromLen, const unsigned char *from,
#ifdef WE_HAVE_RSA_X931
unsigned char* padded = NULL;
int paddedSz;
int padSz;
mp_int toMp;
mp_int nMinusTo;
int rc;
Expand Down Expand Up @@ -1198,7 +1199,7 @@ static int we_rsa_priv_enc_int(size_t fromLen, const unsigned char *from,
ret = -1;
}
else {
rc = mp_read_unsigned_bin(&toMp, to, (int)toLen);
rc = mp_read_unsigned_bin(&toMp, to, (int)tLen);
if (rc != MP_OKAY) {
WOLFENGINE_ERROR_FUNC(WE_LOG_PK, "mp_read_unsigned_bin",
rc);
Expand All @@ -1215,12 +1216,22 @@ static int we_rsa_priv_enc_int(size_t fromLen, const unsigned char *from,
ret = -1;
}
else if (mp_cmp(&toMp, &nMinusTo) == MP_GT) {
rc = mp_to_unsigned_bin(&nMinusTo, to);
if (rc != MP_OKAY) {
WOLFENGINE_ERROR_FUNC(WE_LOG_PK,
"mp_to_unsigned_bin", rc);
/* Complement can be shorter than n: left-pad. */
padSz = (int)tLen - mp_unsigned_bin_size(&nMinusTo);
if (padSz < 0) {
WOLFENGINE_ERROR_MSG(WE_LOG_PK,
"X9.31 complement larger than signature");
ret = -1;
}
else {
XMEMSET(to, 0, (size_t)padSz);
rc = mp_to_unsigned_bin(&nMinusTo, to + padSz);
if (rc != MP_OKAY) {
WOLFENGINE_ERROR_FUNC(WE_LOG_PK,
"mp_to_unsigned_bin", rc);
ret = -1;
}
}
}
}

Expand Down Expand Up @@ -1358,6 +1369,7 @@ static int we_rsa_pub_dec_int(size_t fromLen, const unsigned char *from,
mp_int toMp;
mp_int nMinusTo;
int rc;
int padSz;
#endif

WOLFENGINE_ENTER(WE_LOG_PK, "we_rsa_pub_dec_int");
Expand Down Expand Up @@ -1455,7 +1467,7 @@ static int we_rsa_pub_dec_int(size_t fromLen, const unsigned char *from,
* step. Taking "to" mod 16 is the same as just checking the
* lower 4 bits of "to."
*/
if ((to[toLen-1] & 0x0F) != 12) {
if ((to[tLen-1] & 0x0F) != 12) {
rc = mp_init_multi(&toMp, &nMinusTo, NULL, NULL, NULL,
NULL);
if (rc != MP_OKAY) {
Expand All @@ -1464,7 +1476,7 @@ static int we_rsa_pub_dec_int(size_t fromLen, const unsigned char *from,
ret = -1;
}
else {
rc = mp_read_unsigned_bin(&toMp, to, (int)toLen);
rc = mp_read_unsigned_bin(&toMp, to, (int)tLen);
if (rc != MP_OKAY) {
WOLFENGINE_ERROR_FUNC(WE_LOG_PK,
"mp_read_unsigned_bin", ret);
Expand All @@ -1477,12 +1489,23 @@ static int we_rsa_pub_dec_int(size_t fromLen, const unsigned char *from,
ret = -1;
}
else {
rc = mp_to_unsigned_bin(&nMinusTo, to);
if (rc != MP_OKAY) {
WOLFENGINE_ERROR_FUNC(WE_LOG_PK,
"mp_to_unsigned_bin", rc);
padSz = (int)tLen -
mp_unsigned_bin_size(&nMinusTo);
if (padSz < 0) {
WOLFENGINE_ERROR_MSG(WE_LOG_PK,
"X9.31 complement too large");
ret = -1;
}
else {
XMEMSET(to, 0, (size_t)padSz);
rc = mp_to_unsigned_bin(&nMinusTo,
to + padSz);
if (rc != MP_OKAY) {
WOLFENGINE_ERROR_FUNC(WE_LOG_PK,
"mp_to_unsigned_bin", rc);
ret = -1;
}
}
}
}

Expand Down
57 changes: 56 additions & 1 deletion test/test_rsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -1067,11 +1067,66 @@ int test_rsa_sign_verify_pss(ENGINE *e, void *data)
}

#ifdef WE_HAVE_RSA_X931
static int test_rsa_x931_sig_leading_zero(ENGINE *e)
{
int err;
EVP_PKEY *pkey = NULL;
unsigned char sslSig[256];
unsigned char weSig[256];
size_t sslSigLen = sizeof(sslSig);
size_t weSigLen = sizeof(weSig);
/* SHA-256 X9.31 signature of this message under rsa_key_der_2048 is
* n - s with a leading zero byte. */
unsigned char msg[20] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x6c
};
const unsigned char *p = rsa_key_der_2048;

PRINT_MSG("Sign message whose X9.31 signature has a leading zero byte");
pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, sizeof(rsa_key_der_2048));
err = pkey == NULL;
if (err == 0) {
err = test_digest_sign(pkey, NULL, msg, sizeof(msg), EVP_sha256(),
sslSig, &sslSigLen, RSA_X931_PADDING);
}
if (err == 0) {
err = sslSigLen != sizeof(sslSig) || sslSig[0] != 0x00;
}
if (err == 0) {
err = test_digest_sign(pkey, e, msg, sizeof(msg), EVP_sha256(),
weSig, &weSigLen, RSA_X931_PADDING);
}
if (err == 0) {
err = weSigLen != sslSigLen ||
memcmp(weSig, sslSig, sslSigLen) != 0;
}
if (err == 0) {
err = test_digest_verify(pkey, NULL, msg, sizeof(msg), EVP_sha256(),
weSig, weSigLen, RSA_X931_PADDING);
}
if (err == 0) {
err = test_digest_verify(pkey, e, msg, sizeof(msg), EVP_sha256(),
sslSig, sslSigLen, RSA_X931_PADDING);
}

EVP_PKEY_free(pkey);

return err;
}

int test_rsa_sign_verify_x931(ENGINE *e, void *data)
{
int err;

(void)data;

return test_rsa_sign_verify_pad(e, RSA_X931_PADDING, NULL, NULL);
err = test_rsa_sign_verify_pad(e, RSA_X931_PADDING, NULL, NULL);
if (err == 0) {
err = test_rsa_x931_sig_leading_zero(e);
}

return err;
}
#endif /* WE_HAVE_RSA_X931 */

Expand Down