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
66 changes: 66 additions & 0 deletions sm2.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#if defined(WOLFSSL_SM2) && defined(HAVE_ECC)

#include <wolfssl/wolfcrypt/sm2.h>
#ifdef WOLF_CRYPTO_CB
#include <wolfssl/wolfcrypt/cryptocb.h>
#endif
#include <wolfssl/wolfcrypt/sp.h>
#include <wolfssl/wolfcrypt/hash.h>
#include <wolfssl/wolfcrypt/coding.h>
Expand Down Expand Up @@ -293,6 +296,23 @@ int wc_ecc_sm2_create_digest(const byte *id, word16 idSz,
err = BUFFER_E;
}

#ifdef WOLF_CRYPTO_CB
if (err == 0) {
#ifndef WOLF_CRYPTO_CB_FIND
if (key->devId != INVALID_DEVID)
#endif
{
err = wc_CryptoCb_Sm2CreateDigest(id, idSz, msg, msgSz, hashType,
out, outSz, key);
if (err != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
return err;
}
/* fall-through when unavailable */
err = 0;
}
}
#endif

#ifdef WOLFSSL_SMALL_STACK
if (err == 0) {
hash = (wc_HashAlg*)XMALLOC(sizeof(wc_HashAlg), key->heap,
Expand Down Expand Up @@ -356,6 +376,20 @@ int wc_ecc_sm2_make_key(WC_RNG* rng, ecc_key* key, int flags)
int wc_ecc_sm2_shared_secret(ecc_key* priv, ecc_key* pub, byte* out,
word32* outLen)
{
#ifdef WOLF_CRYPTO_CB
/* Check for NULL pointers to mirror the software path. */
if ((priv != NULL) && (pub != NULL) && (out != NULL) && (outLen != NULL)) {
#ifndef WOLF_CRYPTO_CB_FIND
if (priv->devId != INVALID_DEVID)
#endif
{
int ret = wc_CryptoCb_Sm2SharedSecret(priv, pub, out, outLen);
Comment thread
padelsbach marked this conversation as resolved.
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
return ret;
}
}
}
#endif
return wc_ecc_shared_secret(priv, pub, out, outLen);
}

Expand Down Expand Up @@ -630,6 +664,22 @@ int wc_ecc_sm2_sign_hash(const byte* hash, word32 hashSz, byte* sig,
err = BAD_FUNC_ARG;
}

#ifdef WOLF_CRYPTO_CB
if (err == MP_OKAY) {
#ifndef WOLF_CRYPTO_CB_FIND
if (key->devId != INVALID_DEVID)
#endif
{
err = wc_CryptoCb_Sm2Sign(hash, hashSz, sig,
sigSz, rng, key);
if (err != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
return err;
}
err = MP_OKAY;
}
}
#endif

#ifdef WOLFSSL_SMALL_STACK
if (err == MP_OKAY) {
/* Allocate MP integers. */
Expand Down Expand Up @@ -1019,6 +1069,22 @@ int wc_ecc_sm2_verify_hash(const byte* sig, word32 sigSz, const byte* hash,
err = BAD_FUNC_ARG;
}

#ifdef WOLF_CRYPTO_CB
if (err == 0) {
#ifndef WOLF_CRYPTO_CB_FIND
if (key->devId != INVALID_DEVID)
#endif
{
err = wc_CryptoCb_Sm2Verify(sig, sigSz, hash,
hashSz, res, key);
if (err != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
return err;
}
err = 0;
}
}
#endif

#ifdef WOLFSSL_SMALL_STACK
if (err == 0) {
/* Allocate MP integers. */
Expand Down
91 changes: 85 additions & 6 deletions sm3.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@

#include <wolfssl/wolfcrypt/sm3.h>
#include <wolfssl/wolfcrypt/cpuid.h>
#ifdef WOLF_CRYPTO_CB
#include <wolfssl/wolfcrypt/cryptocb.h>
#endif
#include <wolfssl/wolfcrypt/hash.h>

#ifdef NO_INLINE
Expand Down Expand Up @@ -839,8 +842,9 @@ int wc_InitSm3(wc_Sm3* sm3, void* heap, int devId)
{
int ret = 0;

/* No device support yet. */
#ifndef WOLF_CRYPTO_CB
(void)devId;
#endif

/* Validate parameters. */
if (sm3 == NULL) {
Expand All @@ -852,6 +856,11 @@ int wc_InitSm3(wc_Sm3* sm3, void* heap, int devId)
sm3_init(sm3);

sm3->heap = heap;
#ifdef WOLF_CRYPTO_CB
/* Cache the device to offer hashing to. */
sm3->devId = devId;
sm3->devCtx = NULL;
#endif
#ifdef WOLFSSL_HASH_FLAGS
sm3->flags = 0;
#endif
Expand Down Expand Up @@ -934,6 +943,21 @@ int wc_Sm3Update(wc_Sm3* sm3, const byte* data, word32 len)
}
#endif

#ifdef WOLF_CRYPTO_CB
if (ret == 0) {
#ifndef WOLF_CRYPTO_CB_FIND
if (sm3->devId != INVALID_DEVID)
#endif
{
ret = wc_CryptoCb_Sm3Hash(sm3, data, len, NULL);
Comment thread
padelsbach marked this conversation as resolved.
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
return ret;
}
ret = 0;
}
}
#endif

if ((ret == 0) && (len > 0)) {
/* Always add to length. */
sm3_add_to_len(sm3, len);
Expand Down Expand Up @@ -1056,6 +1080,21 @@ int wc_Sm3Final(wc_Sm3* sm3, byte* hash)
ret = BAD_FUNC_ARG;
}

#ifdef WOLF_CRYPTO_CB
if (ret == 0) {
#ifndef WOLF_CRYPTO_CB_FIND
if (sm3->devId != INVALID_DEVID)
#endif
{
ret = wc_CryptoCb_Sm3Hash(sm3, NULL, 0, hash);
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
return ret;
}
ret = 0;
}
}
#endif

if (ret == 0) {
byte* buffer8 = (byte*)sm3->buffer;

Expand Down Expand Up @@ -1091,6 +1130,20 @@ int wc_Sm3Final(wc_Sm3* sm3, byte* hash)
*/
void wc_Sm3Free(wc_Sm3* sm3)
{
#if defined(WOLF_CRYPTO_CB) && \
defined(WOLF_CRYPTO_CB_FREE)
/* Check we have something to work with. */
if (sm3 != NULL) {
#ifndef WOLF_CRYPTO_CB_FIND
if (sm3->devId != INVALID_DEVID)
#endif
{
/* Let the device release any state it holds for this context. */
(void)wc_CryptoCb_Free(sm3->devId, WC_ALGO_TYPE_HASH,
WC_HASH_TYPE_SM3, 0, sm3);
}
}
#endif
(void)sm3;
}

Expand All @@ -1101,6 +1154,9 @@ void wc_Sm3Free(wc_Sm3* sm3)
* @param [in] src SM3 hash object to copy.
* @param [in, out] dst SM3 hash object to copy into.
*/
/* Defined below; used by wc_Sm3GetHash() for a device aware copy. */
int wc_Sm3Copy(const wc_Sm3* src, wc_Sm3* dst);

static void sm3_copy(const wc_Sm3* src, wc_Sm3* dst)
{
XMEMCPY(dst, src, sizeof(wc_Sm3));
Expand All @@ -1123,7 +1179,7 @@ int wc_Sm3GetHash(wc_Sm3* sm3, byte* hash)
{
int ret = 0;
#ifdef WOLFSSL_SMALL_STACK
wc_Sm3* sm3Copy;
wc_Sm3* sm3Copy = NULL;
#else
wc_Sm3 sm3Copy[1];
#endif
Expand All @@ -1144,18 +1200,21 @@ int wc_Sm3GetHash(wc_Sm3* sm3, byte* hash)
}
#endif
if (ret == 0) {
/* Get a copy of the hash object. */
sm3_copy(sm3, sm3Copy);
ret = wc_Sm3Copy(sm3, sm3Copy);
}
if (ret == 0) {
/* Calculate final hash value. */
ret = wc_Sm3Final(sm3Copy, hash);
/* Dispose of hash object. */
wc_Sm3Free(sm3Copy);
}

#ifdef WOLFSSL_SMALL_STACK
#ifdef WOLFSSL_SMALL_STACK
if (sm3Copy != NULL) {
Comment thread
padelsbach marked this conversation as resolved.
/* Free the SM3 hash object that was the copy. */
XFREE(sm3Copy, sm3->heap, DYNAMIC_TYPE_TMP_BUFFER);
#endif
}
#endif

return ret;
}
Expand All @@ -1176,6 +1235,26 @@ int wc_Sm3Copy(const wc_Sm3* src, wc_Sm3* dst)
ret = BAD_FUNC_ARG;
}

#if defined(WOLF_CRYPTO_CB) && \
defined(WOLF_CRYPTO_CB_COPY)
if (ret == 0) {
#ifndef WOLF_CRYPTO_CB_FIND
if (src->devId != INVALID_DEVID)
#endif
{
/* The struct copy below would alias the device context, leaving
* two objects sharing one handle. Let the device duplicate it. */
ret = wc_CryptoCb_Copy(src->devId, WC_ALGO_TYPE_HASH,
WC_HASH_TYPE_SM3, (void*)src, (void*)dst);
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
return ret;
}
/* Fall through to software when the device declines. */
ret = 0;
}
}
#endif

if (ret == 0) {
sm3_copy(src, dst);
Comment thread
padelsbach marked this conversation as resolved.
}
Expand Down
5 changes: 5 additions & 0 deletions sm3.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ struct wc_Sm3 {
word32 hiLen;
/* Dynamic allocation hint. */
void* heap;
#ifdef WOLF_CRYPTO_CB
/* Device to offer hashing to, and its context. */
int devId;
void* devCtx;
#endif
#ifdef WOLFSSL_HASH_FLAGS
/* Flags of hash object - see enum wc_HashFlags. */
word32 flags;
Expand Down
Loading
Loading