Skip to content

Commit d576c93

Browse files
authored
Merge pull request #2751 from enkiller/master
[components][drivers] 硬件大数适应性调整
2 parents 91c858e + ab7c153 commit d576c93

File tree

4 files changed

+121
-181
lines changed

4 files changed

+121
-181
lines changed

components/drivers/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ menu "Using Hardware Crypto drivers"
278278
int "IV max size"
279279
default "16"
280280

281-
config HWCRYPTO_KEYBIT_MAX_SIZE
281+
config RT_HWCRYPTO_KEYBIT_MAX_SIZE
282282
int "Key max bit length"
283283
default 256
284284

components/drivers/hwcrypto/hw_bignum.c

Lines changed: 73 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
static struct rt_hwcrypto_ctx *bignum_default;
1616

17-
rt_inline rt_err_t rt_hwcrypto_bignum_init(void)
17+
rt_inline rt_err_t hwcrypto_bignum_dev_is_init(void)
1818
{
1919
struct rt_hwcrypto_device *dev;
2020

@@ -55,20 +55,18 @@ rt_err_t rt_hwcrypto_bignum_default(struct rt_hwcrypto_device *device)
5555
}
5656

5757
/**
58-
* @brief Allocate memory for bignum
59-
*
60-
* @return Pointer to allocated bignum obj
58+
* @brief Init bignum obj
59+
*
60+
* @param n bignum obj
6161
*/
62-
struct hw_bignum_mpi *rt_hwcrypto_bignum_alloc(void)
62+
void rt_hwcrypto_bignum_init(struct hw_bignum_mpi *n)
6363
{
64-
struct hw_bignum_mpi *n;
64+
if(n == RT_NULL)
65+
return;
6566

66-
n = rt_malloc(sizeof(struct hw_bignum_mpi));
67-
if (n)
68-
{
69-
rt_memset(n, 0, sizeof(struct hw_bignum_mpi));
70-
}
71-
return n;
67+
n->sign = 1;
68+
n->total = 0;
69+
n->p = RT_NULL;
7270
}
7371

7472
/**
@@ -81,7 +79,9 @@ void rt_hwcrypto_bignum_free(struct hw_bignum_mpi *n)
8179
if (n)
8280
{
8381
rt_free(n->p);
84-
rt_free(n);
82+
n->sign = 0;
83+
n->total = 0;
84+
n->p = RT_NULL;
8585
}
8686
}
8787

@@ -90,7 +90,7 @@ void rt_hwcrypto_bignum_free(struct hw_bignum_mpi *n)
9090
*
9191
* @param n bignum obj
9292
*
93-
* @return binary buffer Length
93+
* @return binary buffer length
9494
*/
9595
int rt_hwcrypto_bignum_get_len(const struct hw_bignum_mpi *n)
9696
{
@@ -111,243 +111,205 @@ int rt_hwcrypto_bignum_get_len(const struct hw_bignum_mpi *n)
111111
}
112112

113113
/**
114-
* @brief Get length of bignum as an unsigned binary buffer
114+
* @brief Export n into unsigned binary data, big endian
115115
*
116116
* @param n bignum obj
117117
* @param buf Buffer for the binary number
118118
* @param len Length of the buffer
119119
*
120-
* @return binary buffer Length
120+
* @return export bin length
121121
*/
122-
int rt_hwcrypto_bignum_get_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len)
122+
int rt_hwcrypto_bignum_export_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len)
123123
{
124-
int cp_len;
124+
int cp_len, i, j;
125125

126-
if (n == RT_NULL || n->p == RT_NULL || buf == RT_NULL)
126+
if (n == RT_NULL || buf == RT_NULL)
127127
{
128128
return 0;
129129
}
130+
rt_memset(buf, 0, len);
130131
cp_len = n->total > len ? len : n->total;
131-
rt_memcpy(n->p, buf, cp_len);
132+
for(i = cp_len, j = 0; i > 0; i--, j++)
133+
{
134+
buf[i - 1] = n->p[j];
135+
}
136+
132137
return cp_len;
133138
}
134139

135140
/**
136-
* @brief Set binary buffer to unsigned bignum
141+
* @brief Import n from unsigned binary data, big endian
137142
*
138143
* @param n bignum obj
139144
* @param buf Buffer for the binary number
140145
* @param len Length of the buffer
141146
*
142147
* @return RT_EOK on success.
143148
*/
144-
rt_err_t rt_hwcrypto_bignum_set_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len)
149+
rt_err_t rt_hwcrypto_bignum_import_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len)
145150
{
151+
int cp_len, i, j;
146152
void *temp_p;
147153

148-
if (n == RT_NULL)
149-
{
150-
return -RT_EINVAL;
151-
}
152-
if (n->p && n->total >= len)
153-
{
154-
rt_memcpy(n->p, buf, len);
155-
return RT_EOK;
156-
}
157-
temp_p = rt_malloc(len);
158-
if (temp_p == RT_NULL)
154+
if (n == RT_NULL || buf == RT_NULL)
159155
{
160-
return -RT_ENOMEM;
156+
return 0;
161157
}
162-
if (n->p)
158+
if (n->total < len)
163159
{
160+
temp_p = rt_malloc(len);
161+
if (temp_p == RT_NULL)
162+
{
163+
return 0;
164+
}
165+
rt_memset(temp_p, 0, len);
164166
rt_free(n->p);
165167
n->p = temp_p;
166-
n->total = 0;
168+
n->total = len;
167169
}
168-
rt_memcpy(n->p, buf, len);
169-
n->total = len;
170-
return RT_EOK;
171-
}
172-
173-
/**
174-
* @brief Unsigned comparison
175-
*
176-
* @param a bignum obj
177-
* @param b bignum obj
178-
*
179-
* @return 0 is equal
180-
*/
181-
int rt_hwcrypto_bignum_cmp(const struct hw_bignum_mpi *a,
182-
const struct hw_bignum_mpi *b)
183-
{
184-
int a_len, b_len;
170+
cp_len = n->total > len ? len : n->total;
185171

186-
if (a == RT_NULL || a->p == RT_NULL
187-
|| b == RT_NULL || b->p == RT_NULL)
188-
{
189-
return -1;
190-
}
191-
a_len = rt_hwcrypto_bignum_get_len(a);
192-
b_len = rt_hwcrypto_bignum_get_len(b);
193-
if (a_len != b_len)
172+
for(i = cp_len, j = 0; i > 0; i--, j++)
194173
{
195-
return a_len - b_len;
174+
n->p[j] = buf[i - 1];
196175
}
197-
return rt_memcmp(a->p, b->p, a_len);
198-
}
199-
200-
/**
201-
* @brief Compare bignum to standard Unsigned integer
202-
*
203-
* @param a bignum obj
204-
* @param b Unsigned integer
205-
*
206-
* @return 0 is equal
207-
*/
208-
int rt_hwcrypto_bignum_cmp_d(const struct hw_bignum_mpi *a, unsigned long b)
209-
{
210-
struct hw_bignum_mpi tmp_b;
211176

212-
b = b <= 0 ? -b : b;
213-
tmp_b.total = sizeof(unsigned long);
214-
tmp_b.p = &b;
215-
return rt_hwcrypto_bignum_cmp(a, &tmp_b);
177+
return cp_len;
216178
}
217179

218180
/**
219-
* @brief a = b + c
181+
* @brief x = a + b
220182
*
221183
* @param a bignum obj
222184
* @param b bignum obj
223185
* @param c bignum obj
224186
*
225187
* @return RT_EOK on success.
226188
*/
227-
rt_err_t rt_hwcrypto_bignum_add(struct hw_bignum_mpi *a,
228-
const struct hw_bignum_mpi *b,
229-
const struct hw_bignum_mpi *c)
189+
rt_err_t rt_hwcrypto_bignum_add(struct hw_bignum_mpi *x,
190+
const struct hw_bignum_mpi *a,
191+
const struct hw_bignum_mpi *b)
230192
{
231193
struct hwcrypto_bignum *bignum_ctx;
232194

233-
if (rt_hwcrypto_bignum_init() != RT_EOK)
195+
if (hwcrypto_bignum_dev_is_init() != RT_EOK)
234196
{
235197
return -RT_ERROR;
236198
}
237199
bignum_ctx = (struct hwcrypto_bignum *)bignum_default;
238200
if (bignum_ctx->ops->add)
239201
{
240-
return bignum_ctx->ops->add(bignum_ctx, a, b, c);
202+
return bignum_ctx->ops->add(bignum_ctx, x, a, b);
241203
}
242204
return -RT_ERROR;
243205
}
244206

245207
/**
246-
* @brief a = b - c
208+
* @brief x = a - b
247209
*
248210
* @param a bignum obj
249211
* @param b bignum obj
250212
* @param c bignum obj
251213
*
252214
* @return RT_EOK on success.
253215
*/
254-
rt_err_t rt_hwcrypto_bignum_sub(struct hw_bignum_mpi *a,
255-
const struct hw_bignum_mpi *b,
256-
const struct hw_bignum_mpi *c)
216+
rt_err_t rt_hwcrypto_bignum_sub(struct hw_bignum_mpi *x,
217+
const struct hw_bignum_mpi *a,
218+
const struct hw_bignum_mpi *b)
257219
{
258220
struct hwcrypto_bignum *bignum_ctx;
259221

260-
if (rt_hwcrypto_bignum_init() != RT_EOK)
222+
if (hwcrypto_bignum_dev_is_init() != RT_EOK)
261223
{
262224
return -RT_ERROR;
263225
}
264226
bignum_ctx = (struct hwcrypto_bignum *)bignum_default;
265227
if (bignum_ctx->ops->sub)
266228
{
267-
return bignum_ctx->ops->sub(bignum_ctx, a, b, c);
229+
return bignum_ctx->ops->sub(bignum_ctx, x, a, b);
268230
}
269231
return -RT_ERROR;
270232
}
271233

272234
/**
273-
* @brief a = b * c
235+
* @brief x = a * b
274236
*
275237
* @param a bignum obj
276238
* @param b bignum obj
277239
* @param c bignum obj
278240
*
279241
* @return RT_EOK on success.
280242
*/
281-
rt_err_t rt_hwcrypto_bignum_mul(struct hw_bignum_mpi *a,
282-
const struct hw_bignum_mpi *b,
283-
const struct hw_bignum_mpi *c)
243+
rt_err_t rt_hwcrypto_bignum_mul(struct hw_bignum_mpi *x,
244+
const struct hw_bignum_mpi *a,
245+
const struct hw_bignum_mpi *b)
284246
{
285247
struct hwcrypto_bignum *bignum_ctx;
286248

287-
if (rt_hwcrypto_bignum_init() != RT_EOK)
249+
if (hwcrypto_bignum_dev_is_init() != RT_EOK)
288250
{
289251
return -RT_ERROR;
290252
}
291253
bignum_ctx = (struct hwcrypto_bignum *)bignum_default;
292254
if (bignum_ctx->ops->mul)
293255
{
294-
return bignum_ctx->ops->mul(bignum_ctx, a, b, c);
256+
return bignum_ctx->ops->mul(bignum_ctx, x, a, b);
295257
}
296258
return -RT_ERROR;
297259
}
298260

299261
/**
300-
* @brief a = b * c (mod d)
262+
* @brief x = a * b (mod c)
301263
*
302264
* @param a bignum obj
303265
* @param b bignum obj
304266
* @param c bignum obj
305267
*
306268
* @return RT_EOK on success.
307269
*/
308-
rt_err_t rt_hwcrypto_bignum_mulmod(struct hw_bignum_mpi *a,
270+
rt_err_t rt_hwcrypto_bignum_mulmod(struct hw_bignum_mpi *x,
271+
const struct hw_bignum_mpi *a,
309272
const struct hw_bignum_mpi *b,
310-
const struct hw_bignum_mpi *c,
311-
const struct hw_bignum_mpi *d)
273+
const struct hw_bignum_mpi *c)
312274
{
313275
struct hwcrypto_bignum *bignum_ctx;
314276

315-
if (rt_hwcrypto_bignum_init() != RT_EOK)
277+
if (hwcrypto_bignum_dev_is_init() != RT_EOK)
316278
{
317279
return -RT_ERROR;
318280
}
319281
bignum_ctx = (struct hwcrypto_bignum *)bignum_default;
320282
if (bignum_ctx->ops->mulmod)
321283
{
322-
return bignum_ctx->ops->mulmod(bignum_ctx, a, b, c, d);
284+
return bignum_ctx->ops->mulmod(bignum_ctx, x, a, b, c);
323285
}
324286
return -RT_ERROR;
325287
}
326288

327289
/**
328-
* @brief a = b ^ c (mod d)
290+
* @brief x = a ^ b (mod c)
329291
*
330292
* @param a bignum obj
331293
* @param b bignum obj
332294
* @param c bignum obj
333295
*
334296
* @return RT_EOK on success.
335297
*/
336-
rt_err_t bignum_exptmod(struct hw_bignum_mpi *a,
337-
const struct hw_bignum_mpi *b,
338-
const struct hw_bignum_mpi *c,
339-
const struct hw_bignum_mpi *d)
298+
rt_err_t rt_hwcrypto_bignum_exptmod(struct hw_bignum_mpi *x,
299+
const struct hw_bignum_mpi *a,
300+
const struct hw_bignum_mpi *b,
301+
const struct hw_bignum_mpi *c)
340302
{
341303
struct hwcrypto_bignum *bignum_ctx;
342304

343-
if (rt_hwcrypto_bignum_init() != RT_EOK)
305+
if (hwcrypto_bignum_dev_is_init() != RT_EOK)
344306
{
345307
return -RT_ERROR;
346308
}
347309
bignum_ctx = (struct hwcrypto_bignum *)bignum_default;
348310
if (bignum_ctx->ops->exptmod)
349311
{
350-
return bignum_ctx->ops->exptmod(bignum_ctx, a, b, c, d);
312+
return bignum_ctx->ops->exptmod(bignum_ctx, x, a, b, c);
351313
}
352314
return -RT_ERROR;
353315
}

0 commit comments

Comments
 (0)