Skip to content

Commit fe7987f

Browse files
night1riderZackLabPC
authored andcommitted
Adding SHA-384/512 support, Null Checks, RNG Health Test for HW, and MAA call update for MAX3266X Port.
1 parent d714e55 commit fe7987f

7 files changed

Lines changed: 267 additions & 73 deletions

File tree

wolfcrypt/src/aes.c

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5412,16 +5412,23 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
54125412
int status;
54135413
byte *iv;
54145414

5415-
#ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
5415+
if ((in == NULL) || (out == NULL) || (aes == NULL)) {
5416+
return BAD_FUNC_ARG;
5417+
}
5418+
5419+
/* Always enforce a length check */
54165420
if (sz % AES_BLOCK_SIZE) {
5421+
#ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
54175422
return BAD_LENGTH_E;
5423+
#else
5424+
return BAD_FUNC_ARG;
54185425
}
5419-
#endif
5420-
if (sz == 0)
5426+
#endif
5427+
if (sz == 0) {
54215428
return 0;
5429+
}
54225430

54235431
iv = (byte*)aes->reg;
5424-
54255432
status = wc_AesGetKeySize(aes, &keySize);
54265433
if (status != 0) {
54275434
return status;
@@ -5430,12 +5437,10 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
54305437
status = wc_MXC_TPU_AesEncrypt(in, iv, (byte*)aes->key,
54315438
MXC_TPU_MODE_CBC, sz, out,
54325439
(unsigned int)keySize);
5433-
54345440
/* store iv for next call */
54355441
if (status == 0) {
54365442
XMEMCPY(iv, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
54375443
}
5438-
54395444
return (status == 0) ? 0 : -1;
54405445
}
54415446

@@ -5447,34 +5452,38 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
54475452
byte *iv;
54485453
byte temp_block[AES_BLOCK_SIZE];
54495454

5450-
#ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
5455+
if ((in == NULL) || (out == NULL) || (aes == NULL)) {
5456+
return BAD_FUNC_ARG;
5457+
}
5458+
5459+
/* Always enforce a length check */
54515460
if (sz % AES_BLOCK_SIZE) {
5461+
#ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
54525462
return BAD_LENGTH_E;
5463+
#else
5464+
return BAD_FUNC_ARG;
54535465
}
5454-
#endif
5455-
if (sz == 0)
5466+
#endif
5467+
if (sz == 0) {
54565468
return 0;
5469+
}
54575470

54585471
iv = (byte*)aes->reg;
5459-
54605472
status = wc_AesGetKeySize(aes, &keySize);
54615473
if (status != 0) {
54625474
return status;
54635475
}
54645476

54655477
/* get IV for next call */
54665478
XMEMCPY(temp_block, in + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
5467-
54685479
status = wc_MXC_TPU_AesDecrypt(in, iv, (byte*)aes->key,
54695480
MXC_TPU_MODE_CBC, sz, out,
54705481
keySize);
54715482

5472-
54735483
/* store iv for next call */
54745484
if (status == 0) {
54755485
XMEMCPY(iv, temp_block, AES_BLOCK_SIZE);
54765486
}
5477-
54785487
return (status == 0) ? 0 : -1;
54795488
}
54805489
#endif /* HAVE_AES_DECRYPT */

wolfcrypt/src/port/maxim/README.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,30 +41,26 @@ all other operations will use the default software implementations.
4141
The other prerequisite is that a change needs to be made to the Maxim SDK. This
4242
is to use the MAA Math Accelerator, this change only needs to be made if you are
4343
using `#define WOLFSSL_MAX3266X` or `define WOLFSSL_MAX3266X_OLD` by themselves
44-
or you are specifying `#define MAX3266X_MATH`.
44+
or you are specifying `#define MAX3266X_MATH`. This is only needed if you are
45+
not using the latest Maxim SDK.
4546

4647
In the SDK you will need to find the underlying function that
4748
`MXC_TPU_MAA_Compute()` from `tpu.h` compute calls in the newer SDK. In the
4849
older SDK this function is called `MAA_Compute()` in `maa.h`. In the underlying
49-
function you will need to change this error check:
50+
function you will need to this:
5051

5152
```
52-
// Check that we're performing a valid operation
53-
if (clc >= 0x6) {
54-
return E_INVALID;
55-
}
53+
MXC_SETFIELD(tpu->maa_ctrl, MXC_F_TPU_REVA_MAA_CTRL_CLC, clc);
5654
```
5755
to
5856
```
59-
// Check that we're performing a valid operation
60-
if (clc >= 0b1111) {
61-
return E_INVALID;
62-
}
57+
MXC_SETFIELD(tpu->maa_ctrl, MXC_F_TPU_REVA_MAA_CTRL_CLC,
58+
clc << MXC_F_TPU_REVA_MAA_CTRL_CLC_POS);
6359
```
6460

65-
This bug has been reported to Analog Devices
66-
[here](https://github.com/analogdevicesinc/msdk/issues/1089)
67-
if you want to know more details on the issue.
61+
This bug has been reported to Analog Devices and a PR has been made
62+
[here](https://github.com/analogdevicesinc/msdk/pull/1104)
63+
if you want to know more details on the issue, or use a patch.
6864

6965

7066
## Supported Algos
@@ -81,17 +77,21 @@ hardware.
8177

8278
`#define MAX3266X_SHA`:
8379

80+
- SHA-1
81+
- SHA-224
8482
- SHA-256
83+
- SHA-384
84+
- SHA-512
8585

8686
`#define MAX3266X_MATH` (Replaces math operation calls for algos
8787
like RSA and ECC key generation):
8888

89-
- mod - `a mod m = r`
90-
- addmod - `(a+b)mod m = r`
91-
- submod - `(a-b)mod m = r`
92-
- mulmod - `(a*b)mod m = r`
93-
- sqrmod - `(b^2)mod m = r`
94-
- exptmod - `(b^e)mod m = r`
89+
- mod: `a mod m = r`
90+
- addmod: `(a+b)mod m = r`
91+
- submod: `(a-b)mod m = r`
92+
- mulmod: `(a*b)mod m = r`
93+
- sqrmod: `(b^2)mod m = r`
94+
- exptmod: `(b^e)mod m = r`
9595

9696
## Extra Information
9797
For more Verbose info you can use `#define DEBUG_WOLFSSL` in combination with

wolfcrypt/src/port/maxim/max3266x.c

Lines changed: 126 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -360,16 +360,26 @@ int wc_MXC_TPU_SHA_GetDigest(wc_MXC_Sha *hash, unsigned char* digest,
360360
XMEMCPY(digest, MXC_EMPTY_DIGEST_SHA1, WC_SHA_DIGEST_SIZE);
361361
break;
362362
#endif /* NO_SHA */
363+
#ifdef WOLFSSL_SHA224
364+
case MXC_TPU_HASH_SHA224:
365+
XMEMCPY(digest, MXC_EMPTY_DIGEST_SHA224, WC_SHA224_DIGEST_SIZE);
366+
break;
367+
#endif /* WOLFSSL_SHA224 */
363368
#ifndef NO_SHA256
364369
case MXC_TPU_HASH_SHA256:
365370
XMEMCPY(digest, MXC_EMPTY_DIGEST_SHA256, WC_SHA256_DIGEST_SIZE);
366371
break;
367372
#endif /* NO_SHA256 */
368-
#ifdef WOLFSSL_SHA224
369-
case MXC_TPU_HASH_SHA224:
370-
XMEMCPY(digest, MXC_EMPTY_DIGEST_SHA224, WC_SHA224_DIGEST_SIZE);
373+
#ifdef WOLFSSL_SHA384
374+
case MXC_TPU_HASH_SHA384:
375+
XMEMCPY(digest, MXC_EMPTY_DIGEST_SHA384, WC_SHA384_DIGEST_SIZE);
371376
break;
372-
#endif /* WOLFSSL_SHA224 */
377+
#endif /* WOLFSSL_SHA384 */
378+
#ifdef WOLFSSL_SHA512
379+
case MXC_TPU_HASH_SHA512:
380+
XMEMCPY(digest, MXC_EMPTY_DIGEST_SHA512, WC_SHA512_DIGEST_SIZE);
381+
break;
382+
#endif /* WOLFSSL_SHA512 */
373383
default:
374384
return BAD_FUNC_ARG;
375385
}
@@ -517,6 +527,102 @@ WOLFSSL_API void wc_Sha256Free(wc_Sha256* sha256)
517527

518528
#endif /* NO_SHA256 */
519529

530+
#if defined(WOLFSSL_SHA384)
531+
532+
WOLFSSL_API int wc_InitSha384_ex(wc_Sha384* sha384, void* heap, int devId)
533+
{
534+
if (sha384 == NULL) {
535+
return BAD_FUNC_ARG;
536+
}
537+
(void)heap;
538+
(void)devId;
539+
return wc_MXC_TPU_SHA_Init((wc_MXC_Sha *)sha384);
540+
}
541+
542+
WOLFSSL_API int wc_InitSha384(wc_Sha384* sha384)
543+
{
544+
return wc_InitSha384_ex(sha384, NULL, INVALID_DEVID);
545+
}
546+
547+
WOLFSSL_API int wc_Sha384Update(wc_Sha384* sha384, const unsigned char* data,
548+
unsigned int len)
549+
{
550+
return wc_MXC_TPU_SHA_Update(sha384, data, len);
551+
}
552+
553+
WOLFSSL_API int wc_Sha384Final(wc_Sha384* sha384, unsigned char* hash)
554+
{
555+
return wc_MXC_TPU_SHA_Final((wc_MXC_Sha *)sha384, hash,
556+
MXC_TPU_HASH_SHA384);
557+
}
558+
559+
WOLFSSL_API int wc_Sha384GetHash(wc_Sha384* sha384, unsigned char* hash)
560+
{
561+
return wc_MXC_TPU_SHA_GetHash((wc_MXC_Sha *)sha384, hash,
562+
MXC_TPU_HASH_SHA384);
563+
}
564+
565+
WOLFSSL_API int wc_Sha384Copy(wc_Sha384* src, wc_Sha384* dst)
566+
{
567+
return wc_MXC_TPU_SHA_Copy((wc_MXC_Sha *)src, (wc_MXC_Sha *)dst);
568+
}
569+
570+
WOLFSSL_API void wc_Sha384Free(wc_Sha384* sha384)
571+
{
572+
wc_MXC_TPU_SHA_Free((wc_MXC_Sha *)sha384);
573+
return;
574+
}
575+
576+
#endif /* WOLFSSL_SHA384 */
577+
578+
#if defined(WOLFSSL_SHA512)
579+
580+
WOLFSSL_API int wc_InitSha512_ex(wc_Sha512* sha512, void* heap, int devId)
581+
{
582+
if (sha512 == NULL) {
583+
return BAD_FUNC_ARG;
584+
}
585+
(void)heap;
586+
(void)devId;
587+
return wc_MXC_TPU_SHA_Init((wc_MXC_Sha *)sha512);
588+
}
589+
590+
WOLFSSL_API int wc_InitSha512(wc_Sha512* sha512)
591+
{
592+
return wc_InitSha512_ex(sha512, NULL, INVALID_DEVID);
593+
}
594+
595+
WOLFSSL_API int wc_Sha512Update(wc_Sha512* sha512, const unsigned char* data,
596+
unsigned int len)
597+
{
598+
return wc_MXC_TPU_SHA_Update(sha512, data, len);
599+
}
600+
601+
WOLFSSL_API int wc_Sha512Final(wc_Sha512* sha512, unsigned char* hash)
602+
{
603+
return wc_MXC_TPU_SHA_Final((wc_MXC_Sha *)sha512, hash,
604+
MXC_TPU_HASH_SHA512);
605+
}
606+
607+
WOLFSSL_API int wc_Sha512GetHash(wc_Sha512* sha512, unsigned char* hash)
608+
{
609+
return wc_MXC_TPU_SHA_GetHash((wc_MXC_Sha *)sha512, hash,
610+
MXC_TPU_HASH_SHA512);
611+
}
612+
613+
WOLFSSL_API int wc_Sha512Copy(wc_Sha512* src, wc_Sha512* dst)
614+
{
615+
return wc_MXC_TPU_SHA_Copy((wc_MXC_Sha *)src, (wc_MXC_Sha *)dst);
616+
}
617+
618+
WOLFSSL_API void wc_Sha512Free(wc_Sha512* sha512)
619+
{
620+
wc_MXC_TPU_SHA_Free((wc_MXC_Sha *)sha512);
621+
return;
622+
}
623+
624+
#endif /* WOLFSSL_SHA512 */
625+
520626
#endif /* MAX3266X_SHA */
521627

522628
#if defined(MAX3266X_MATH)
@@ -615,7 +721,7 @@ int wc_MXC_MAA_zeroPad(mp_int* multiplier, mp_int* multiplicand,
615721
return BAD_FUNC_ARG;
616722
}
617723
if ((result == NULL) || (multiplier == NULL) || (multiplicand == NULL) ||
618-
((exp == NULL) && (clc == WC_MXC_TPU_MAA_EXP)) || (mod == NULL)) {
724+
((exp == NULL) && (clc == MXC_TPU_MAA_EXP)) || (mod == NULL)) {
619725
return BAD_FUNC_ARG;
620726
}
621727

@@ -630,17 +736,17 @@ int wc_MXC_MAA_zeroPad(mp_int* multiplier, mp_int* multiplicand,
630736

631737
/* Check for invalid arguments befor padding */
632738
switch ((char)clc) {
633-
case WC_MXC_TPU_MAA_EXP:
739+
case MXC_TPU_MAA_EXP:
634740
/* Cannot be 0 for a^e mod m operation */
635741
if (XMEMCMP(zero_tmp, exp, (exp->used*sizeof(mp_digit))) == 0) {
636742
XFREE(zero_tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
637743
MAX3266X_MSG("Cannot use Value 0 for Exp");
638744
return BAD_FUNC_ARG;
639745
}
640746

641-
/* Padd out rest of data if used != length to ensure no */
747+
/* Pad out rest of data if used != length to ensure no */
642748
/* garbage is used in calculation */
643-
if ((exp != NULL) && (clc == WC_MXC_TPU_MAA_EXP)) {
749+
if ((exp != NULL) && (clc == MXC_TPU_MAA_EXP)) {
644750
if ((exp->dp != NULL) && (exp->used < length)) {
645751
MAX3266X_MSG("Zero Padding Exp Buffer");
646752
XMEMSET(exp->dp + exp->used, 0x00,
@@ -649,11 +755,11 @@ int wc_MXC_MAA_zeroPad(mp_int* multiplier, mp_int* multiplicand,
649755
}
650756

651757
/* Fall through to check mod is not 0 */
652-
case WC_MXC_TPU_MAA_SQ:
653-
case WC_MXC_TPU_MAA_MUL:
654-
case WC_MXC_TPU_MAA_SQMUL:
655-
case WC_MXC_TPU_MAA_ADD:
656-
case WC_MXC_TPU_MAA_SUB:
758+
case MXC_TPU_MAA_SQ:
759+
case MXC_TPU_MAA_MUL:
760+
case MXC_TPU_MAA_SQMUL:
761+
case MXC_TPU_MAA_ADD:
762+
case MXC_TPU_MAA_SUB:
657763
/* Cannot be 0 for mod m value */
658764
if (XMEMCMP(zero_tmp, mod, (exp->used*sizeof(mp_digit))) == 0) {
659765
XFREE(zero_tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
@@ -723,7 +829,7 @@ int wc_MXC_MAA_math(mp_int* multiplier, mp_int* multiplicand, mp_int* exp,
723829
return MP_VAL;
724830
}
725831

726-
if (clc == WC_MXC_TPU_MAA_EXP) {
832+
if (clc == MXC_TPU_MAA_EXP) {
727833
length = wc_MXC_MAA_Largest(5, multiplier->used, multiplicand->used,
728834
exp->used, mod->used, result->used);
729835
}
@@ -791,7 +897,7 @@ int wc_MXC_MAA_expmod(mp_int* base, mp_int* exp, mp_int* mod,
791897
multiplicand.used = mod->used;
792898
MAX3266X_MSG("Preparing exptmod MAA HW Call");
793899
return wc_MXC_MAA_math(base, &multiplicand, exp, mod, result,
794-
WC_MXC_TPU_MAA_EXP);
900+
MXC_TPU_MAA_EXP);
795901
}
796902

797903
int wc_MXC_MAA_sqrmod(mp_int* multiplier, mp_int* mod, mp_int* result)
@@ -802,31 +908,31 @@ int wc_MXC_MAA_sqrmod(mp_int* multiplier, mp_int* mod, mp_int* result)
802908
multiplicand.used = mod->used;
803909
MAX3266X_MSG("Preparing sqrmod MAA HW Call");
804910
return wc_MXC_MAA_math(multiplier, &multiplicand, NULL, mod, result,
805-
WC_MXC_TPU_MAA_SQ);
911+
MXC_TPU_MAA_SQ);
806912
}
807913

808914
int wc_MXC_MAA_mulmod(mp_int* multiplier, mp_int* multiplicand, mp_int* mod,
809915
mp_int* result)
810916
{
811917
MAX3266X_MSG("Preparing mulmod MAA HW Call");
812918
return wc_MXC_MAA_math(multiplier, multiplicand, NULL, mod, result,
813-
WC_MXC_TPU_MAA_MUL);
919+
MXC_TPU_MAA_MUL);
814920
}
815921

816922
int wc_MXC_MAA_sqrmulmod(mp_int* multiplier, mp_int* multiplicand,
817923
mp_int* exp, mp_int* mod, mp_int* result)
818924
{
819925
MAX3266X_MSG("Preparing sqrmulmod MAA HW Call");
820926
return wc_MXC_MAA_math(multiplier, multiplicand, NULL, mod, result,
821-
WC_MXC_TPU_MAA_SQMUL);
927+
MXC_TPU_MAA_SQMUL);
822928
}
823929

824930
int wc_MXC_MAA_addmod(mp_int* multiplier, mp_int* multiplicand, mp_int* mod,
825931
mp_int* result)
826932
{
827933
MAX3266X_MSG("Preparing addmod MAA HW Call");
828934
return wc_MXC_MAA_math(multiplier, multiplicand, NULL, mod, result,
829-
WC_MXC_TPU_MAA_ADD);
935+
MXC_TPU_MAA_ADD);
830936
}
831937

832938
int wc_MXC_MAA_submod(mp_int* multiplier, mp_int* multiplicand, mp_int* mod,
@@ -839,7 +945,7 @@ int wc_MXC_MAA_submod(mp_int* multiplier, mp_int* multiplicand, mp_int* mod,
839945
}
840946
else {
841947
return wc_MXC_MAA_math(multiplier, multiplicand, NULL, mod, result,
842-
WC_MXC_TPU_MAA_SUB);
948+
MXC_TPU_MAA_SUB);
843949
}
844950
}
845951

0 commit comments

Comments
 (0)