Merge pull request #5070 from miyazakh/crypto_only_flwup

This commit is contained in:
Chris Conlon
2022-05-20 17:08:29 -06:00
committed by GitHub
3 changed files with 222 additions and 7 deletions

View File

@ -8398,7 +8398,7 @@ WOLFSSL_EC_POINT *wolfSSL_EC_POINT_new(const WOLFSSL_EC_GROUP *group)
return p;
}
#ifndef WOLFSSL_SP_MATH
#if !defined(WOLFSSL_SP_MATH) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
/* return code compliant with OpenSSL :
* 1 if success, 0 if error
*/
@ -8535,7 +8535,8 @@ int wolfSSL_EC_POINT_set_affine_coordinates_GFp(const WOLFSSL_EC_GROUP *group,
}
#if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
!defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH)
!defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
!defined(WOLF_CRYPTO_CB_ONLY_ECC)
int wolfSSL_EC_POINT_add(const WOLFSSL_EC_GROUP *group, WOLFSSL_EC_POINT *r,
const WOLFSSL_EC_POINT *p1,
const WOLFSSL_EC_POINT *p2, WOLFSSL_BN_CTX *ctx)
@ -9027,13 +9028,16 @@ int wolfSSL_EC_POINT_is_at_infinity(const WOLFSSL_EC_GROUP *group,
if (setupPoint(point) != WOLFSSL_SUCCESS) {
return WOLFSSL_FAILURE;
}
#ifndef WOLF_CRYPTO_CB_ONLY_ECC
ret = wc_ecc_point_is_at_infinity((ecc_point*)point->internal);
if (ret < 0) {
WOLFSSL_MSG("ecc_point_is_at_infinity failure");
return WOLFSSL_FAILURE;
}
#else
WOLFSSL_MSG("ecc_point_is_at_infinitiy compiled out");
return WOLFSSL_FAILURE;
#endif
return ret;
}
@ -9260,6 +9264,13 @@ int wolfSSL_ECDSA_do_verify(const unsigned char *d, int dlen,
const WOLFSSL_ECDSA_SIG *sig, WOLFSSL_EC_KEY *key)
{
int check_sign = 0;
#ifdef WOLF_CRYPTO_CB_ONLY_ECC
byte signature[ECC_MAX_SIG_SIZE];
word32 signaturelen = (word32)sizeof(signature);
char* r;
char* s;
int ret = 0;
#endif
WOLFSSL_ENTER("wolfSSL_ECDSA_do_verify");
@ -9279,6 +9290,7 @@ int wolfSSL_ECDSA_do_verify(const unsigned char *d, int dlen,
}
}
#ifndef WOLF_CRYPTO_CB_ONLY_ECC
if (wc_ecc_verify_hash_ex((mp_int*)sig->r->internal,
(mp_int*)sig->s->internal, d, dlen, &check_sign,
(ecc_key *)key->internal) != MP_OKAY) {
@ -9289,6 +9301,36 @@ int wolfSSL_ECDSA_do_verify(const unsigned char *d, int dlen,
WOLFSSL_MSG("wc_ecc_verify_hash incorrect signature detected");
return WOLFSSL_FAILURE;
}
#else
/* convert big number to hex */
r = wolfSSL_BN_bn2hex(sig->r);
s = wolfSSL_BN_bn2hex(sig->s);
/* get DER-encoded ECDSA signature */
ret = wc_ecc_rs_to_sig((const char*)r, (const char*)s,
signature, &signaturelen);
/* free r and s */
if (r)
XFREE(r, NULL, DYNAMIC_TYPE_OPENSSL);
if (s)
XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
if (ret != MP_OKAY) {
WOLFSSL_MSG("wc_ecc_verify_hash failed");
return WOLFSSL_FATAL_ERROR;
}
/* verify hash. expects to call wc_CryptoCb_EccVerify internally */
ret = wc_ecc_verify_hash(signature, signaturelen, d, dlen, &check_sign,
(ecc_key*)key->internal);
if (ret != MP_OKAY) {
WOLFSSL_MSG("wc_ecc_verify_hash failed");
return WOLFSSL_FATAL_ERROR;
}
else if (check_sign == 0) {
WOLFSSL_MSG("wc_ecc_verify_hash incorrect signature detected");
return WOLFSSL_FAILURE;
}
#endif
return WOLFSSL_SUCCESS;
}
@ -9357,6 +9399,7 @@ int wolfSSL_i2d_ECDSA_SIG(const WOLFSSL_ECDSA_SIG *sig, unsigned char **pp)
}
/* End ECDSA_SIG */
#ifndef WOLF_CRYPTO_CB_ONLY_ECC
/* Start ECDH */
/* return code compliant with OpenSSL :
* length of computed key if success, -1 if error
@ -9427,6 +9470,7 @@ int wolfSSL_ECDH_compute_key(void *out, size_t outlen,
return len;
}
#endif /* WOLF_CRYPTO_CB_ONLY_ECC */
/* End ECDH */
#if !defined(NO_FILESYSTEM)

View File

@ -1612,7 +1612,7 @@ int wolfSSL_EVP_PKEY_derive(WOLFSSL_EVP_PKEY_CTX *ctx, unsigned char *key, size_
*keylen = (size_t)len;
break;
#endif
#ifdef HAVE_ECC
#if defined(HAVE_ECC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
case EVP_PKEY_EC:
/* Use ECDH */
if (!ctx->pkey->ecc || !ctx->peerKey->ecc) {

View File

@ -1238,11 +1238,12 @@ options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
else
TEST_PASS("OPENSSL (PKEY1) passed!\n");
#if !defined(WOLF_CRYPTO_CB_ONLY_RSA) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
if ( (ret = openssl_evpSig_test()) != 0)
return err_sys("OPENSSL (EVP Sign/Verify) test failed!\n", ret);
else
TEST_PASS("OPENSSL (EVP Sign/Verify) passed!\n");
#endif
#endif
#if defined(HAVE_ECC)
@ -39420,12 +39421,59 @@ static int ecc_onlycb_test(myCryptoDevCtx *ctx)
HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
byte* out = (byte*)XMALLOC(sizeof(byte),
HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
#ifdef OPENSSL_EXTRA
byte* check = (byte*)XMALLOC(sizeof(byte)*(256), HEAP_HINT,
DYNAMIC_TYPE_TMP_BUFFER);
#endif
#else
ecc_key key[1];
ecc_key pub[1];
byte out[256];
#ifdef OPENSSL_EXTRA
unsigned char check[256];
#endif
#endif
#ifdef OPENSSL_EXTRA
EVP_PKEY* privKey = NULL;
EVP_PKEY* pubKey = NULL;
ecc_key* pkey;
EVP_MD_CTX mdCtx;
const char testData[] = "Hi There";
size_t checkSz = -1;
const unsigned char* cp;
const unsigned char* p;
const unsigned char check_v[256] = {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f,0x3b,
0xcf,0xbb,0x60,0x81,0x33,0x94,0xfa,0x0d,0x35,0xd2,
0x3d,0xb9,0x99,0xe3,0x12,0xf8,0xf4,0xa3,0x74,0xf4,
0x94,0x1d,0x7a,0x66,0xf8,0xd1,0x1d,0xcf,0xb0,0x48,
0xef,0x8c,0x94,0x6f,0xdd,0x62,
};
#endif
WC_RNG rng;
EncryptedInfo encInfo;
int keyFormat = 0;
@ -39523,16 +39571,128 @@ static int ecc_onlycb_test(myCryptoDevCtx *ctx)
ret = wc_ecc_shared_secret(key, pub, out, &outLen);
}
if (ret != NO_VALID_DEVID) {
ERROR_OUT(-8016, exit_onlycb);
ERROR_OUT(-8018, exit_onlycb);
}
else
ret = 0;
#ifdef OPENSSL_EXTRA
(void)pkey;
cp = ecc_clikey_der_256;
privKey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
sizeof_ecc_clikey_der_256);
if (privKey == NULL) {
ERROR_OUT(-8019, exit_onlycb);
}
pkey = (ecc_key*)privKey->ecc->internal;
pkey->devId = devId;
p = ecc_clikeypub_der_256;
pubKey = d2i_PUBKEY(NULL, &p, sizeof_ecc_clikeypub_der_256);
if (pubKey == NULL) {
ERROR_OUT(-8020, exit_onlycb);
}
pkey = (ecc_key*)pubKey->ecc->internal;
pkey->devId = devId;
/* sign */
EVP_MD_CTX_init(&mdCtx);
ret = EVP_DigestSignInit(&mdCtx, NULL, EVP_sha256(), NULL, privKey);
if (ret != WOLFSSL_SUCCESS) {
ERROR_OUT(-8021, exit_onlycb);
}
ret = EVP_DigestSignUpdate(&mdCtx, testData,
(unsigned int)XSTRLEN(testData));
if (ret != WOLFSSL_SUCCESS) {
ERROR_OUT(-8022, exit_onlycb);
}
ret = EVP_DigestSignFinal(&mdCtx, NULL, &checkSz);
if (ret != WOLFSSL_SUCCESS) {
ERROR_OUT(-8023, exit_onlycb);
}
ctx->exampleVar = 99;
ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
/* just called crypt callback as dummy
* EVP_DigestSignFinal returns 0 internally.
*/
if (ret != 0) {
ERROR_OUT(-8024, exit_onlycb);
}
ctx->exampleVar = 1;
ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
/* just called crypt callback as dummy
* EVP_DigestSignFinal returns 0 internally.
*/
if (ret != 0) {
ERROR_OUT(-8025, exit_onlycb);
}
/* restore checkSz for verify */
checkSz = 71;
ret = EVP_MD_CTX_cleanup(&mdCtx);
if (ret != SSL_SUCCESS) {
ERROR_OUT(-8026, exit_onlycb);
}
/* verify */
EVP_MD_CTX_init(&mdCtx);
if (ret == SSL_SUCCESS) {
ret = EVP_DigestVerifyInit(&mdCtx, NULL, EVP_sha256(), NULL, pubKey);
}
if (ret != WOLFSSL_SUCCESS) {
ERROR_OUT(-8027, exit_onlycb);
}
if (ret == WOLFSSL_SUCCESS) {
ret = EVP_DigestVerifyUpdate(&mdCtx, testData,
(unsigned int)XSTRLEN(testData));
}
if (ret != WOLFSSL_SUCCESS) {
ERROR_OUT(-8028, exit_onlycb);
}
ctx->exampleVar = 99;
ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
/* just called crypt callback as dummy
* EVP_DigestSignFinal returns 0 internally.
*/
if (ret != 0) {
ERROR_OUT(-8029, exit_onlycb);
}
ctx->exampleVar = 1;
ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
/* just called crypt callback as dummy
* EVP_DigestVerifyFinal returns -1 internally rather than NO_VALID_DEVID.
*/
if (ret != -1) {
ERROR_OUT(-8030, exit_onlycb);
}
ret = EVP_MD_CTX_cleanup(&mdCtx);
if (ret != SSL_SUCCESS) {
ERROR_OUT(-8031, exit_onlycb);
} else
ret = 0;
#endif
#else
(void)verify;
(void)outLen;
(void)inLen;
(void)out;
(void)pub;
#ifdef OPENSSL_EXTRA
(void)privKey;
(void)pubKey;
(void)mdCtx;
(void)check;
(void)checkSz;
(void)p;
#endif
#endif
(void)keyFormat;
(void)encInfo;
@ -39549,8 +39709,19 @@ exit_onlycb:
if (out != NULL) {
XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
}
#ifdef OPENSSL_EXTRA
if (check) {
FREE(check, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
}
#endif
#else
wc_ecc_free(key);
#ifdef OPENSSL_EXTRA
if (privKey)
EVP_PKEY_free(privKey);
if (pubKey)
EVP_PKEY_free(pubKey);
#endif
#endif
#endif /* HAVE_ECC */