Skip to content

Commit 3878271

Browse files
committed
nxp casper changes. turn off ecc and rsa hw accel
1 parent b6106ff commit 3878271

5 files changed

Lines changed: 200 additions & 21 deletions

File tree

wolfcrypt/src/port/nxp/casper_port.c

Lines changed: 170 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ static uint8_t sig_buf[CASPER_MAX_BUF_SZ];
5050
static uint8_t out_buf[CASPER_MAX_BUF_SZ];
5151

5252
int casper_rsa_public_exptmod(
53-
const byte* in, word32 inLen, byte* out, word32 outLen, RsaKey* key
53+
const byte* in, word32 inLen, byte* out, word32* outLen, RsaKey* key
5454
)
5555
{
5656
int res;
5757
int sig_sz = inLen;
5858
int key_sz = mp_unsigned_bin_size(&key->n);
5959
word32 exp;
6060

61-
if (inLen > CASPER_MAX_BUF_SZ || outLen > CASPER_MAX_BUF_SZ)
61+
if (inLen > CASPER_MAX_BUF_SZ || *outLen > CASPER_MAX_BUF_SZ)
6262
return BAD_FUNC_ARG;
6363

6464
/* casper requires little endian format for inputs/outputs */
@@ -78,8 +78,176 @@ int casper_rsa_public_exptmod(
7878
mp_reverse(out_buf, sig_sz);
7979
XMEMCPY(out, out_buf, sig_sz);
8080

81+
*outLen = inLen;
82+
8183
return 0;
8284
}
8385
#endif
8486

87+
88+
/* 32 for 256 bits, 48 for 384 bits and 72 for 521 bits... */
89+
#define CASPER_MAX_ECC_SIZE_BYTES (72)
90+
91+
#if defined(HAVE_ECC) && defined(WOLFSSL_NXP_CASPER_ECC_MULMOD)
92+
/* calculates R = m*P[X, Y] */
93+
int casper_ecc_mulmod(
94+
const mp_int *m, ecc_point *P, ecc_point *R, int curve_id
95+
)
96+
{
97+
uint32_t M[CASPER_MAX_ECC_SIZE_BYTES / sizeof(uint32_t)] = { 0 };
98+
uint32_t X[CASPER_MAX_ECC_SIZE_BYTES / sizeof(uint32_t)] = { 0 };
99+
uint32_t Y[CASPER_MAX_ECC_SIZE_BYTES / sizeof(uint32_t)] = { 0 };
100+
int size;
101+
102+
if (!m || !P || !R)
103+
return BAD_FUNC_ARG;
104+
105+
if (curve_id == ECC_SECP256R1)
106+
{
107+
size = 32;
108+
CASPER_ecc_init(kCASPER_ECC_P256);
109+
}
110+
else if (curve_id == ECC_SECP384R1)
111+
{
112+
size = 48;
113+
CASPER_ecc_init(kCASPER_ECC_P384);
114+
}
115+
else if (curve_id == ECC_SECP521R1)
116+
{
117+
size = 66;
118+
CASPER_ecc_init(kCASPER_ECC_P521);
119+
}
120+
else
121+
return BAD_FUNC_ARG;
122+
123+
/* scalar */
124+
if (mp_to_unsigned_bin(m, (unsigned char *)&M[0]) != MP_OKAY)
125+
return MP_TO_E;
126+
mp_reverse((unsigned char *)&M[0], size);
127+
128+
/* point */
129+
if (mp_to_unsigned_bin(P->x, (unsigned char *)&X[0]) != MP_OKAY)
130+
return MP_TO_E;
131+
mp_reverse((unsigned char *)&X[0], size);
132+
if (mp_to_unsigned_bin(P->y, (unsigned char *)&Y[0]) != MP_OKAY)
133+
return MP_TO_E;
134+
mp_reverse((unsigned char *)&Y[0], size);
135+
136+
if (curve_id == ECC_SECP256R1)
137+
{
138+
CASPER_ECC_SECP256R1_Mul(CASPER, X, Y, X, Y, (void *)M);
139+
}
140+
else if (curve_id == ECC_SECP384R1)
141+
{
142+
CASPER_ECC_SECP384R1_Mul(CASPER, X, Y, X, Y, (void *)M);
143+
}
144+
else if (curve_id == ECC_SECP521R1)
145+
{
146+
CASPER_ECC_SECP521R1_Mul(CASPER, X, Y, X, Y, (void *)M);
147+
}
148+
149+
/* result */
150+
mp_reverse((unsigned char *)&X[0], size);
151+
if (mp_read_unsigned_bin(R->x, (unsigned char *)&X[0], size) != MP_OKAY)
152+
return MP_READ_E;
153+
mp_reverse((unsigned char *)&Y[0], size);
154+
if (mp_read_unsigned_bin(R->y, (unsigned char *)&Y[0], size) != MP_OKAY)
155+
return MP_READ_E;
156+
mp_set(R->z, 1);
157+
158+
return 0;
159+
}
160+
#endif
161+
162+
#if defined(HAVE_ECC) && defined(WOLFSSL_NXP_CASPER_ECC_MUL2ADD)
163+
/* calculates R = m*P[X, Y] + n*Q[X, Y] */
164+
int casper_ecc_mul2add(
165+
const mp_int *m, ecc_point *P, const mp_int *n, ecc_point *Q,
166+
ecc_point *R, int curve_id
167+
)
168+
{
169+
uint32_t M[CASPER_MAX_ECC_SIZE_BYTES / sizeof(uint32_t)] = { 0 };
170+
uint32_t X1[CASPER_MAX_ECC_SIZE_BYTES / sizeof(uint32_t)] = { 0 };
171+
uint32_t Y1[CASPER_MAX_ECC_SIZE_BYTES / sizeof(uint32_t)] = { 0 };
172+
uint32_t N[CASPER_MAX_ECC_SIZE_BYTES / sizeof(uint32_t)] = { 0 };
173+
uint32_t X2[CASPER_MAX_ECC_SIZE_BYTES / sizeof(uint32_t)] = { 0 };
174+
uint32_t Y2[CASPER_MAX_ECC_SIZE_BYTES / sizeof(uint32_t)] = { 0 };
175+
int size;
176+
177+
if (!m || !P || !n || !Q || !R)
178+
return BAD_FUNC_ARG;
179+
180+
if (curve_id == ECC_SECP256R1)
181+
{
182+
size = 32;
183+
CASPER_ecc_init(kCASPER_ECC_P256);
184+
}
185+
else if (curve_id == ECC_SECP384R1)
186+
{
187+
size = 48;
188+
CASPER_ecc_init(kCASPER_ECC_P384);
189+
}
190+
else if (curve_id == ECC_SECP521R1)
191+
{
192+
size = 66;
193+
CASPER_ecc_init(kCASPER_ECC_P521);
194+
}
195+
else
196+
return BAD_FUNC_ARG;
197+
198+
/* first scalar */
199+
if (mp_to_unsigned_bin(m, (unsigned char *)&M[0]) != MP_OKAY)
200+
return MP_TO_E;
201+
mp_reverse((unsigned char *)&M[0], size);
202+
203+
/* first point */
204+
if (mp_to_unsigned_bin(P->x, (unsigned char *)&X1[0]) != MP_OKAY)
205+
return MP_TO_E;
206+
mp_reverse((unsigned char *)&X1[0], size);
207+
if (mp_to_unsigned_bin(P->y, (unsigned char *)&Y1[0]) != MP_OKAY)
208+
return MP_TO_E;
209+
mp_reverse((unsigned char *)&Y1[0], size);
210+
211+
/* second scalar */
212+
if (mp_to_unsigned_bin(n, (unsigned char *)&N[0]) != MP_OKAY)
213+
return MP_TO_E;
214+
mp_reverse((unsigned char *)&N[0], size);
215+
216+
/* second point */
217+
if (mp_to_unsigned_bin(Q->x, (unsigned char *)&X2[0]) != MP_OKAY)
218+
return MP_TO_E;
219+
mp_reverse((unsigned char *)&X2[0], size);
220+
if (mp_to_unsigned_bin(Q->y, (unsigned char *)&Y2[0]) != MP_OKAY)
221+
return MP_TO_E;
222+
mp_reverse((unsigned char *)&Y2[0], size);
223+
224+
if (curve_id == ECC_SECP256R1)
225+
{
226+
CASPER_ECC_SECP256R1_MulAdd(CASPER, &X1[0], &Y1[0], &X1[0], &Y1[0],
227+
(void *)M, &X2[0], &Y2[0], (void *)N);
228+
}
229+
else if (curve_id == ECC_SECP384R1)
230+
{
231+
CASPER_ECC_SECP384R1_MulAdd(CASPER, &X1[0], &Y1[0], &X1[0], &Y1[0],
232+
(void *)M, &X2[0], &Y2[0], (void *)N);
233+
}
234+
else if (curve_id == ECC_SECP521R1)
235+
{
236+
CASPER_ECC_SECP521R1_MulAdd(CASPER, &X1[0], &Y1[0], &X1[0], &Y1[0],
237+
(void *)M, &X2[0], &Y2[0], (void *)N);
238+
}
239+
240+
/* result */
241+
mp_reverse((unsigned char *)&X1[0], size);
242+
if (mp_read_unsigned_bin(R->x, (unsigned char *)&X1[0], size) != MP_OKAY)
243+
return MP_READ_E;
244+
mp_reverse((unsigned char *)&Y1[0], size);
245+
if (mp_read_unsigned_bin(R->y, (unsigned char *)&Y1[0], size) != MP_OKAY)
246+
return MP_READ_E;
247+
mp_set(R->z, 1);
248+
249+
return 0;
250+
}
251+
#endif
252+
85253
#endif /* WOLFSSL_NXP_CASPER */

wolfcrypt/src/rsa.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2415,20 +2415,6 @@ static int wc_RsaFunctionSync(const byte* in, word32 inLen, byte* out,
24152415
return ret;
24162416
}
24172417

2418-
#elif defined(WOLFSSL_NXP_CASPER_RSA_PUB_EXPTMOD)
2419-
static int wc_RsaFunctionSync(const byte* in, word32 inLen, byte* out,
2420-
word32* outLen, int type, RsaKey* key,
2421-
WC_RNG* rng)
2422-
{
2423-
(void)rng;
2424-
2425-
if (type == RSA_PUBLIC_DECRYPT || type == RSA_PUBLIC_ENCRYPT) {
2426-
return casper_rsa_public_exptmod(in, inLen, out, *outLen, key);
2427-
}
2428-
2429-
return RSA_WRONG_TYPE_E;
2430-
}
2431-
24322418
#else
24332419
#ifndef WOLF_CRYPTO_CB_ONLY_RSA
24342420
#ifdef WOLFSSL_HAVE_SP_RSA
@@ -2845,6 +2831,15 @@ static int wc_RsaFunctionSync(const byte* in, word32 inLen, byte* out,
28452831
return MP_VAL;
28462832
}
28472833

2834+
#if defined(WOLFSSL_NXP_CASPER_RSA_PUB_EXPTMOD)
2835+
if (type == RSA_PUBLIC_DECRYPT || type == RSA_PUBLIC_ENCRYPT) {
2836+
ret = casper_rsa_public_exptmod(in, inLen, out, outLen, key);
2837+
if (ret == 0)
2838+
return MP_OKAY;
2839+
/* else fall through for software fallback */
2840+
}
2841+
#endif
2842+
28482843
#ifdef WOLFSSL_HAVE_SP_RSA
28492844
ret = RsaFunction_SP(in, inLen, out, outLen, type, key, rng);
28502845
if (ret != WC_NO_ERR_TRACE(WC_KEY_SIZE_E))

wolfcrypt/test/test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2522,7 +2522,7 @@ options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
25222522
if ( (ret = aesofb_test()) != 0)
25232523
TEST_FAIL("AES-OFB test failed!\n", ret);
25242524
else
2525-
TEST_PASS("AES-OFB test passed!\n");
2525+
TEST_PASS("AES-OFB test passed!\n");
25262526
#endif
25272527

25282528
#ifdef HAVE_AESGCM

wolfssl/wolfcrypt/port/nxp/casper_port.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,26 @@ int wc_casper_init(void);
3030
#include <wolfssl/wolfcrypt/rsa.h>
3131

3232
int casper_rsa_public_exptmod(
33-
const byte* in, word32 inLen, byte* out, word32 outLen, RsaKey* key
33+
const byte* in, word32 inLen, byte* out, word32* outLen, RsaKey* key
3434
);
3535
#endif
3636

37+
38+
#if defined(HAVE_ECC)
39+
#include <wolfssl/wolfcrypt/ecc.h>
40+
41+
#ifdef WOLFSSL_NXP_CASPER_ECC_MULMOD
42+
int casper_ecc_mulmod(
43+
const mp_int *m, ecc_point *P, ecc_point *R, int curve_id
44+
);
45+
#endif
46+
47+
#ifdef WOLFSSL_NXP_CASPER_ECC_MUL2ADD
48+
int casper_ecc_mul2add(
49+
const mp_int *m, ecc_point *P, const mp_int *n, ecc_point *Q,
50+
ecc_point *R, int curve_id
51+
);
52+
#endif
53+
#endif
54+
3755
#endif

wolfssl/wolfcrypt/settings.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,9 +2106,7 @@ extern void uITRON4_free(void *p) ;
21062106
#define WOLFSSL_NXP_HASHCRYPT_SHA
21072107
#define WOLFSSL_NXP_HASHCRYPT_SHA256
21082108
#define WOLFSSL_NXP_CASPER
2109-
#define WOLFSSL_NXP_CASPER_RSA_PUB_EXPTMOD
2110-
// #define WOLFSSL_NXP_CASPER_ECC_MUL2ADD
2111-
// #define WOLFSSL_SP_MULMOD
2109+
// #define WOLFSSL_NXP_CASPER_RSA_PUB_EXPTMOD
21122110
#endif
21132111
#endif /* WOLFSSL_NXP_LPC55S69 */
21142112

0 commit comments

Comments
 (0)