Skip to content

Commit eaadfb1

Browse files
authored
Merge pull request #10508 from JacobBarthelmeh/static_analysis_3
devcrypto fixes, forcezero on memory after use, RX64 GetHash port fix, blake2 stor64 alignment
2 parents 7bcc613 + 0073f3c commit eaadfb1

9 files changed

Lines changed: 54 additions & 18 deletions

File tree

linuxkm/lkcapi_aes_glue.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ static int km_AesInitCommon(
408408
if (! ctx->aes_decrypt_C) {
409409
pr_err("%s: allocation of %zu bytes for decryption key failed.\n",
410410
name, sizeof(*ctx->aes_decrypt_C));
411-
err = -MEMORY_E;
411+
err = -ENOMEM;
412412
goto out;
413413
}
414414

wolfcrypt/src/port/Renesas/renesas_rx64_hw_sha.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,23 @@ static int RX64_HashGet(wolfssl_RX64_HW_Hash* hash, byte* out)
318318
return BAD_FUNC_ARG;
319319
}
320320

321+
/* RX64 HW SHA rejects empty input; return the documented empty-message
322+
* digest instead. This matches the special case in RX64_HashFinal so
323+
* callers like wc_Sha256GetHash on a freshly-initialised state succeed. */
324+
if ((hash->msg == NULL) && (hash->len == 0) && (hash->used == 0))
325+
{
326+
if (hash->sha_type == RX64_SHA1) {
327+
XMEMCPY(out, DefaultShaHashData, sizeof(DefaultShaHashData));
328+
}
329+
else if (hash->sha_type == RX64_SHA224) {
330+
XMEMCPY(out, DefaultSha224HashData, sizeof(DefaultSha224HashData));
331+
}
332+
else if (hash->sha_type == RX64_SHA256) {
333+
XMEMCPY(out, DefaultSha256HashData, sizeof(DefaultSha256HashData));
334+
}
335+
return 0;
336+
}
337+
321338
ret = RX64_ShaCalc(hash->msg, hash->len, out, hash->sha_type);
322339
if (ret != R_PROCESS_COMPLETE) {
323340
return ret;

wolfcrypt/src/port/devcrypto/devcrypto_ecdsa.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ int wc_DevCryptoEccKeyGen(int curveId, int enc, byte* pri, word32 priSz,
6464
}
6565

6666
if (ret == 0) {
67+
XMEMSET(&kop, 0, sizeof(kop));
6768
kop.crk_op = CRK_ECC_KEYGEN;
6869
kop.ses = ctx.sess.ses;
6970
kop.crk_flags = ecdsel;

wolfcrypt/src/port/devcrypto/devcrypto_rsa.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ static void wc_SetupRsaPrivate(struct crypt_kop* kop, WC_CRYPTODEV* dev,
8989

9090
if (dpSz == 0 || dqSz == 0) {
9191
kop->crk_param[inIdx].crp_p = n;
92-
kop->crk_param[inIdx].crp_nbits = dSz * WOLFSSL_BIT_SIZE;
92+
kop->crk_param[inIdx].crp_nbits = nSz * WOLFSSL_BIT_SIZE;
9393
inIdx++;
9494

9595
kop->crk_param[inIdx].crp_p = d;
96-
kop->crk_param[inIdx].crp_nbits = nSz * WOLFSSL_BIT_SIZE;
96+
kop->crk_param[inIdx].crp_nbits = dSz * WOLFSSL_BIT_SIZE;
9797
inIdx++;
9898
}
9999
else {

wolfcrypt/src/port/devcrypto/wc_devcrypto.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ void wc_SetupCryptAead(struct crypt_auth_op* crt, WC_CRYPTODEV* dev,
234234
byte* src, word32 srcSz, byte* dst, byte* iv, word32 ivSz, int flag,
235235
byte* authIn, word32 authInSz, byte* authTag, word32 authTagSz)
236236
{
237-
XMEMSET(crt, 0, sizeof(struct crypt_op));
237+
XMEMSET(crt, 0, sizeof(struct crypt_auth_op));
238238
crt->ses = dev->sess.ses;
239239
crt->src = src;
240240
crt->len = srcSz;

wolfcrypt/src/port/nxp/dcp_port.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
#include <wolfssl/wolfcrypt/sha.h>
3232
#include <wolfssl/wolfcrypt/sha256.h>
3333
#include <wolfssl/wolfcrypt/error-crypt.h>
34+
#ifdef NO_INLINE
35+
#include <wolfssl/wolfcrypt/misc.h>
36+
#else
37+
#define WOLFSSL_MISC_INCLUDED
38+
#include <wolfcrypt/src/misc.c>
39+
#endif
3440

3541
#if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U) && defined(DCP_USE_DCACHE) && (DCP_USE_DCACHE == 1U)
3642
#error "DCACHE not supported by this driver. Please undefine DCP_USE_DCACHE."
@@ -205,14 +211,18 @@ int DCPAesInit(Aes *aes)
205211
return 0;
206212
}
207213

214+
static unsigned char aes_key_aligned[16] __attribute__((aligned(0x10)));
215+
208216
void DCPAesFree(Aes *aes)
209217
{
218+
dcp_lock();
219+
ForceZero(aes_key_aligned, sizeof(aes_key_aligned));
220+
dcp_unlock();
210221
dcp_free(aes->handle.channel);
211222
aes->handle.channel = 0;
212223
}
213224

214225

215-
static unsigned char aes_key_aligned[16] __attribute__((aligned(0x10)));
216226
int DCPAesSetKey(Aes* aes, const byte* key, word32 len, const byte* iv,
217227
int dir)
218228
{
@@ -231,8 +241,9 @@ int DCPAesSetKey(Aes* aes, const byte* key, word32 len, const byte* iv,
231241
return WC_HW_E;
232242
}
233243
dcp_lock();
234-
memcpy(aes_key_aligned, key, 16);
244+
XMEMCPY(aes_key_aligned, key, 16);
235245
status = DCP_AES_SetKey(DCP, &aes->handle, aes_key_aligned, 16);
246+
ForceZero(aes_key_aligned, sizeof(aes_key_aligned));
236247
if (status != kStatus_Success)
237248
status = WC_HW_E;
238249
else {

wolfcrypt/src/port/tropicsquare/tropic01.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,9 @@ int Tropic01_Deinit(void)
565565
WOLFSSL_MSG("TROPIC01: Crypto device deinitialized successfully");
566566
}
567567

568+
ForceZero(sh0priv, sizeof(sh0priv));
569+
ForceZero(sh0pub, sizeof(sh0pub));
570+
568571
return 0;
569572
}
570573

wolfcrypt/src/rc2.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,10 @@ int wc_Rc2EcbEncrypt(Rc2* rc2, byte* out, const byte* in, word32 sz)
171171
return BUFFER_E;
172172
}
173173

174-
r10 = (in[1] << 8) | in[0]; /* R[0] */
175-
r32 = (in[3] << 8) | in[2]; /* R[1] */
176-
r54 = (in[5] << 8) | in[4]; /* R[2] */
177-
r76 = (in[7] << 8) | in[6]; /* R[3] */
174+
r10 = (word16)((word16)in[1] << 8) | in[0]; /* R[0] */
175+
r32 = (word16)((word16)in[3] << 8) | in[2]; /* R[1] */
176+
r54 = (word16)((word16)in[5] << 8) | in[4]; /* R[2] */
177+
r76 = (word16)((word16)in[7] << 8) | in[6]; /* R[3] */
178178

179179
for (i = 0; i < 16; i++) {
180180
j = i * 4;
@@ -236,10 +236,10 @@ int wc_Rc2EcbDecrypt(Rc2* rc2, byte* out, const byte* in, word32 sz)
236236
return BUFFER_E;
237237
}
238238

239-
r0 = (in[1] << 8) | in[0];
240-
r1 = (in[3] << 8) | in[2];
241-
r2 = (in[5] << 8) | in[4];
242-
r3 = (in[7] << 8) | in[6];
239+
r0 = (word16)((word16)in[1] << 8) | in[0];
240+
r1 = (word16)((word16)in[3] << 8) | in[2];
241+
r2 = (word16)((word16)in[5] << 8) | in[4];
242+
r3 = (word16)((word16)in[7] << 8) | in[6];
243243

244244
for (i = 16; i > 0; i--) {
245245
j = 4*i - 1;

wolfssl/wolfcrypt/blake2-impl.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040

4141
static WC_INLINE word32 load32( const void *src )
4242
{
43-
#if defined(LITTLE_ENDIAN_ORDER)
43+
#if defined(LITTLE_ENDIAN_ORDER) && \
44+
(!defined(WOLFSSL_GENERAL_ALIGNMENT) || (WOLFSSL_GENERAL_ALIGNMENT == 0))
4445
return *( const word32 * )( src );
4546
#else
4647
const byte *p = ( const byte * )src;
@@ -54,7 +55,8 @@ static WC_INLINE word32 load32( const void *src )
5455

5556
static WC_INLINE word64 load64( const void *src )
5657
{
57-
#if defined(LITTLE_ENDIAN_ORDER)
58+
#if defined(LITTLE_ENDIAN_ORDER) && \
59+
(!defined(WOLFSSL_GENERAL_ALIGNMENT) || (WOLFSSL_GENERAL_ALIGNMENT == 0))
5860
return *( const word64 * )( src );
5961
#else
6062
const byte *p = ( const byte * )src;
@@ -72,7 +74,8 @@ static WC_INLINE word64 load64( const void *src )
7274

7375
static WC_INLINE void store32( void *dst, word32 w )
7476
{
75-
#if defined(LITTLE_ENDIAN_ORDER)
77+
#if defined(LITTLE_ENDIAN_ORDER) && \
78+
(!defined(WOLFSSL_GENERAL_ALIGNMENT) || (WOLFSSL_GENERAL_ALIGNMENT == 0))
7679
*( word32 * )( dst ) = w;
7780
#else
7881
byte *p = ( byte * )dst;
@@ -85,7 +88,8 @@ static WC_INLINE void store32( void *dst, word32 w )
8588

8689
static WC_INLINE void store64( void *dst, word64 w )
8790
{
88-
#if defined(LITTLE_ENDIAN_ORDER) && !defined(WOLFSSL_GENERAL_ALIGNMENT)
91+
#if defined(LITTLE_ENDIAN_ORDER) && \
92+
(!defined(WOLFSSL_GENERAL_ALIGNMENT) || (WOLFSSL_GENERAL_ALIGNMENT == 0))
8993
*( word64 * )( dst ) = w;
9094
#else
9195
byte *p = ( byte * )dst;

0 commit comments

Comments
 (0)