|
@@ -14,7 +14,7 @@
|
|
|
|
|
|
static struct rt_hwcrypto_ctx *bignum_default;
|
|
|
|
|
|
-rt_inline rt_err_t rt_hwcrypto_bignum_init(void)
|
|
|
+rt_inline rt_err_t hwcrypto_bignum_dev_is_init(void)
|
|
|
{
|
|
|
struct rt_hwcrypto_device *dev;
|
|
|
|
|
@@ -55,20 +55,18 @@ rt_err_t rt_hwcrypto_bignum_default(struct rt_hwcrypto_device *device)
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @brief Allocate memory for bignum
|
|
|
- *
|
|
|
- * @return Pointer to allocated bignum obj
|
|
|
+ * @brief Init bignum obj
|
|
|
+ *
|
|
|
+ * @param n bignum obj
|
|
|
*/
|
|
|
-struct hw_bignum_mpi *rt_hwcrypto_bignum_alloc(void)
|
|
|
+void rt_hwcrypto_bignum_init(struct hw_bignum_mpi *n)
|
|
|
{
|
|
|
- struct hw_bignum_mpi *n;
|
|
|
+ if(n == RT_NULL)
|
|
|
+ return;
|
|
|
|
|
|
- n = rt_malloc(sizeof(struct hw_bignum_mpi));
|
|
|
- if (n)
|
|
|
- {
|
|
|
- rt_memset(n, 0, sizeof(struct hw_bignum_mpi));
|
|
|
- }
|
|
|
- return n;
|
|
|
+ n->sign = 1;
|
|
|
+ n->total = 0;
|
|
|
+ n->p = RT_NULL;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -81,7 +79,9 @@ void rt_hwcrypto_bignum_free(struct hw_bignum_mpi *n)
|
|
|
if (n)
|
|
|
{
|
|
|
rt_free(n->p);
|
|
|
- rt_free(n);
|
|
|
+ n->sign = 0;
|
|
|
+ n->total = 0;
|
|
|
+ n->p = RT_NULL;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -90,7 +90,7 @@ void rt_hwcrypto_bignum_free(struct hw_bignum_mpi *n)
|
|
|
*
|
|
|
* @param n bignum obj
|
|
|
*
|
|
|
- * @return binary buffer Length
|
|
|
+ * @return binary buffer length
|
|
|
*/
|
|
|
int rt_hwcrypto_bignum_get_len(const struct hw_bignum_mpi *n)
|
|
|
{
|
|
@@ -111,29 +111,34 @@ int rt_hwcrypto_bignum_get_len(const struct hw_bignum_mpi *n)
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @brief Get length of bignum as an unsigned binary buffer
|
|
|
+ * @brief Export n into unsigned binary data, big endian
|
|
|
*
|
|
|
* @param n bignum obj
|
|
|
* @param buf Buffer for the binary number
|
|
|
* @param len Length of the buffer
|
|
|
*
|
|
|
- * @return binary buffer Length
|
|
|
+ * @return export bin length
|
|
|
*/
|
|
|
-int rt_hwcrypto_bignum_get_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len)
|
|
|
+int rt_hwcrypto_bignum_export_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len)
|
|
|
{
|
|
|
- int cp_len;
|
|
|
+ int cp_len, i, j;
|
|
|
|
|
|
- if (n == RT_NULL || n->p == RT_NULL || buf == RT_NULL)
|
|
|
+ if (n == RT_NULL || buf == RT_NULL)
|
|
|
{
|
|
|
return 0;
|
|
|
}
|
|
|
+ rt_memset(buf, 0, len);
|
|
|
cp_len = n->total > len ? len : n->total;
|
|
|
- rt_memcpy(n->p, buf, cp_len);
|
|
|
+ for(i = cp_len, j = 0; i > 0; i--, j++)
|
|
|
+ {
|
|
|
+ buf[i - 1] = n->p[j];
|
|
|
+ }
|
|
|
+
|
|
|
return cp_len;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @brief Set binary buffer to unsigned bignum
|
|
|
+ * @brief Import n from unsigned binary data, big endian
|
|
|
*
|
|
|
* @param n bignum obj
|
|
|
* @param buf Buffer for the binary number
|
|
@@ -141,82 +146,39 @@ int rt_hwcrypto_bignum_get_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len
|
|
|
*
|
|
|
* @return RT_EOK on success.
|
|
|
*/
|
|
|
-rt_err_t rt_hwcrypto_bignum_set_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len)
|
|
|
+rt_err_t rt_hwcrypto_bignum_import_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len)
|
|
|
{
|
|
|
+ int cp_len, i, j;
|
|
|
void *temp_p;
|
|
|
|
|
|
- if (n == RT_NULL)
|
|
|
- {
|
|
|
- return -RT_EINVAL;
|
|
|
- }
|
|
|
- if (n->p && n->total >= len)
|
|
|
- {
|
|
|
- rt_memcpy(n->p, buf, len);
|
|
|
- return RT_EOK;
|
|
|
- }
|
|
|
- temp_p = rt_malloc(len);
|
|
|
- if (temp_p == RT_NULL)
|
|
|
+ if (n == RT_NULL || buf == RT_NULL)
|
|
|
{
|
|
|
- return -RT_ENOMEM;
|
|
|
+ return 0;
|
|
|
}
|
|
|
- if (n->p)
|
|
|
+ if (n->total < len)
|
|
|
{
|
|
|
+ temp_p = rt_malloc(len);
|
|
|
+ if (temp_p == RT_NULL)
|
|
|
+ {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ rt_memset(temp_p, 0, len);
|
|
|
rt_free(n->p);
|
|
|
n->p = temp_p;
|
|
|
- n->total = 0;
|
|
|
+ n->total = len;
|
|
|
}
|
|
|
- rt_memcpy(n->p, buf, len);
|
|
|
- n->total = len;
|
|
|
- return RT_EOK;
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * @brief Unsigned comparison
|
|
|
- *
|
|
|
- * @param a bignum obj
|
|
|
- * @param b bignum obj
|
|
|
- *
|
|
|
- * @return 0 is equal
|
|
|
- */
|
|
|
-int rt_hwcrypto_bignum_cmp(const struct hw_bignum_mpi *a,
|
|
|
- const struct hw_bignum_mpi *b)
|
|
|
-{
|
|
|
- int a_len, b_len;
|
|
|
+ cp_len = n->total > len ? len : n->total;
|
|
|
|
|
|
- if (a == RT_NULL || a->p == RT_NULL
|
|
|
- || b == RT_NULL || b->p == RT_NULL)
|
|
|
- {
|
|
|
- return -1;
|
|
|
- }
|
|
|
- a_len = rt_hwcrypto_bignum_get_len(a);
|
|
|
- b_len = rt_hwcrypto_bignum_get_len(b);
|
|
|
- if (a_len != b_len)
|
|
|
+ for(i = cp_len, j = 0; i > 0; i--, j++)
|
|
|
{
|
|
|
- return a_len - b_len;
|
|
|
+ n->p[j] = buf[i - 1];
|
|
|
}
|
|
|
- return rt_memcmp(a->p, b->p, a_len);
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * @brief Compare bignum to standard Unsigned integer
|
|
|
- *
|
|
|
- * @param a bignum obj
|
|
|
- * @param b Unsigned integer
|
|
|
- *
|
|
|
- * @return 0 is equal
|
|
|
- */
|
|
|
-int rt_hwcrypto_bignum_cmp_d(const struct hw_bignum_mpi *a, unsigned long b)
|
|
|
-{
|
|
|
- struct hw_bignum_mpi tmp_b;
|
|
|
|
|
|
- b = b <= 0 ? -b : b;
|
|
|
- tmp_b.total = sizeof(unsigned long);
|
|
|
- tmp_b.p = &b;
|
|
|
- return rt_hwcrypto_bignum_cmp(a, &tmp_b);
|
|
|
+ return cp_len;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @brief a = b + c
|
|
|
+ * @brief x = a + b
|
|
|
*
|
|
|
* @param a bignum obj
|
|
|
* @param b bignum obj
|
|
@@ -224,26 +186,26 @@ int rt_hwcrypto_bignum_cmp_d(const struct hw_bignum_mpi *a, unsigned long b)
|
|
|
*
|
|
|
* @return RT_EOK on success.
|
|
|
*/
|
|
|
-rt_err_t rt_hwcrypto_bignum_add(struct hw_bignum_mpi *a,
|
|
|
- const struct hw_bignum_mpi *b,
|
|
|
- const struct hw_bignum_mpi *c)
|
|
|
+rt_err_t rt_hwcrypto_bignum_add(struct hw_bignum_mpi *x,
|
|
|
+ const struct hw_bignum_mpi *a,
|
|
|
+ const struct hw_bignum_mpi *b)
|
|
|
{
|
|
|
struct hwcrypto_bignum *bignum_ctx;
|
|
|
|
|
|
- if (rt_hwcrypto_bignum_init() != RT_EOK)
|
|
|
+ if (hwcrypto_bignum_dev_is_init() != RT_EOK)
|
|
|
{
|
|
|
return -RT_ERROR;
|
|
|
}
|
|
|
bignum_ctx = (struct hwcrypto_bignum *)bignum_default;
|
|
|
if (bignum_ctx->ops->add)
|
|
|
{
|
|
|
- return bignum_ctx->ops->add(bignum_ctx, a, b, c);
|
|
|
+ return bignum_ctx->ops->add(bignum_ctx, x, a, b);
|
|
|
}
|
|
|
return -RT_ERROR;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @brief a = b - c
|
|
|
+ * @brief x = a - b
|
|
|
*
|
|
|
* @param a bignum obj
|
|
|
* @param b bignum obj
|
|
@@ -251,26 +213,26 @@ rt_err_t rt_hwcrypto_bignum_add(struct hw_bignum_mpi *a,
|
|
|
*
|
|
|
* @return RT_EOK on success.
|
|
|
*/
|
|
|
-rt_err_t rt_hwcrypto_bignum_sub(struct hw_bignum_mpi *a,
|
|
|
- const struct hw_bignum_mpi *b,
|
|
|
- const struct hw_bignum_mpi *c)
|
|
|
+rt_err_t rt_hwcrypto_bignum_sub(struct hw_bignum_mpi *x,
|
|
|
+ const struct hw_bignum_mpi *a,
|
|
|
+ const struct hw_bignum_mpi *b)
|
|
|
{
|
|
|
struct hwcrypto_bignum *bignum_ctx;
|
|
|
|
|
|
- if (rt_hwcrypto_bignum_init() != RT_EOK)
|
|
|
+ if (hwcrypto_bignum_dev_is_init() != RT_EOK)
|
|
|
{
|
|
|
return -RT_ERROR;
|
|
|
}
|
|
|
bignum_ctx = (struct hwcrypto_bignum *)bignum_default;
|
|
|
if (bignum_ctx->ops->sub)
|
|
|
{
|
|
|
- return bignum_ctx->ops->sub(bignum_ctx, a, b, c);
|
|
|
+ return bignum_ctx->ops->sub(bignum_ctx, x, a, b);
|
|
|
}
|
|
|
return -RT_ERROR;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @brief a = b * c
|
|
|
+ * @brief x = a * b
|
|
|
*
|
|
|
* @param a bignum obj
|
|
|
* @param b bignum obj
|
|
@@ -278,26 +240,26 @@ rt_err_t rt_hwcrypto_bignum_sub(struct hw_bignum_mpi *a,
|
|
|
*
|
|
|
* @return RT_EOK on success.
|
|
|
*/
|
|
|
-rt_err_t rt_hwcrypto_bignum_mul(struct hw_bignum_mpi *a,
|
|
|
- const struct hw_bignum_mpi *b,
|
|
|
- const struct hw_bignum_mpi *c)
|
|
|
+rt_err_t rt_hwcrypto_bignum_mul(struct hw_bignum_mpi *x,
|
|
|
+ const struct hw_bignum_mpi *a,
|
|
|
+ const struct hw_bignum_mpi *b)
|
|
|
{
|
|
|
struct hwcrypto_bignum *bignum_ctx;
|
|
|
|
|
|
- if (rt_hwcrypto_bignum_init() != RT_EOK)
|
|
|
+ if (hwcrypto_bignum_dev_is_init() != RT_EOK)
|
|
|
{
|
|
|
return -RT_ERROR;
|
|
|
}
|
|
|
bignum_ctx = (struct hwcrypto_bignum *)bignum_default;
|
|
|
if (bignum_ctx->ops->mul)
|
|
|
{
|
|
|
- return bignum_ctx->ops->mul(bignum_ctx, a, b, c);
|
|
|
+ return bignum_ctx->ops->mul(bignum_ctx, x, a, b);
|
|
|
}
|
|
|
return -RT_ERROR;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @brief a = b * c (mod d)
|
|
|
+ * @brief x = a * b (mod c)
|
|
|
*
|
|
|
* @param a bignum obj
|
|
|
* @param b bignum obj
|
|
@@ -305,27 +267,27 @@ rt_err_t rt_hwcrypto_bignum_mul(struct hw_bignum_mpi *a,
|
|
|
*
|
|
|
* @return RT_EOK on success.
|
|
|
*/
|
|
|
-rt_err_t rt_hwcrypto_bignum_mulmod(struct hw_bignum_mpi *a,
|
|
|
+rt_err_t rt_hwcrypto_bignum_mulmod(struct hw_bignum_mpi *x,
|
|
|
+ const struct hw_bignum_mpi *a,
|
|
|
const struct hw_bignum_mpi *b,
|
|
|
- const struct hw_bignum_mpi *c,
|
|
|
- const struct hw_bignum_mpi *d)
|
|
|
+ const struct hw_bignum_mpi *c)
|
|
|
{
|
|
|
struct hwcrypto_bignum *bignum_ctx;
|
|
|
|
|
|
- if (rt_hwcrypto_bignum_init() != RT_EOK)
|
|
|
+ if (hwcrypto_bignum_dev_is_init() != RT_EOK)
|
|
|
{
|
|
|
return -RT_ERROR;
|
|
|
}
|
|
|
bignum_ctx = (struct hwcrypto_bignum *)bignum_default;
|
|
|
if (bignum_ctx->ops->mulmod)
|
|
|
{
|
|
|
- return bignum_ctx->ops->mulmod(bignum_ctx, a, b, c, d);
|
|
|
+ return bignum_ctx->ops->mulmod(bignum_ctx, x, a, b, c);
|
|
|
}
|
|
|
return -RT_ERROR;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @brief a = b ^ c (mod d)
|
|
|
+ * @brief x = a ^ b (mod c)
|
|
|
*
|
|
|
* @param a bignum obj
|
|
|
* @param b bignum obj
|
|
@@ -333,21 +295,21 @@ rt_err_t rt_hwcrypto_bignum_mulmod(struct hw_bignum_mpi *a,
|
|
|
*
|
|
|
* @return RT_EOK on success.
|
|
|
*/
|
|
|
-rt_err_t bignum_exptmod(struct hw_bignum_mpi *a,
|
|
|
- const struct hw_bignum_mpi *b,
|
|
|
- const struct hw_bignum_mpi *c,
|
|
|
- const struct hw_bignum_mpi *d)
|
|
|
+rt_err_t rt_hwcrypto_bignum_exptmod(struct hw_bignum_mpi *x,
|
|
|
+ const struct hw_bignum_mpi *a,
|
|
|
+ const struct hw_bignum_mpi *b,
|
|
|
+ const struct hw_bignum_mpi *c)
|
|
|
{
|
|
|
struct hwcrypto_bignum *bignum_ctx;
|
|
|
|
|
|
- if (rt_hwcrypto_bignum_init() != RT_EOK)
|
|
|
+ if (hwcrypto_bignum_dev_is_init() != RT_EOK)
|
|
|
{
|
|
|
return -RT_ERROR;
|
|
|
}
|
|
|
bignum_ctx = (struct hwcrypto_bignum *)bignum_default;
|
|
|
if (bignum_ctx->ops->exptmod)
|
|
|
{
|
|
|
- return bignum_ctx->ops->exptmod(bignum_ctx, a, b, c, d);
|
|
|
+ return bignum_ctx->ops->exptmod(bignum_ctx, x, a, b, c);
|
|
|
}
|
|
|
return -RT_ERROR;
|
|
|
}
|