summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBernard Xiong <[email protected]>2019-06-06 06:59:45 +0800
committerGitHub <[email protected]>2019-06-06 06:59:45 +0800
commitd576c936444e08ba620ae1e744b05d40ae727f42 (patch)
tree04db5dafa3ced29075bd9ac9edbfd808531b005e
parent91c858ead231bdef64478f59a0fe3b43128be21b (diff)
parentab7c1530eaa4f5a60e67667a930eea2732867f2f (diff)
Merge pull request #2751 from enkiller/master
[components][drivers] 硬件大数适应性调整
-rwxr-xr-xcomponents/drivers/Kconfig2
-rw-r--r--components/drivers/hwcrypto/hw_bignum.c184
-rw-r--r--components/drivers/hwcrypto/hw_bignum.h114
-rw-r--r--components/drivers/hwcrypto/hw_hash.h2
4 files changed, 121 insertions, 181 deletions
diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig
index a100249620..45494c6a6e 100755
--- a/components/drivers/Kconfig
+++ b/components/drivers/Kconfig
@@ -278,7 +278,7 @@ menu "Using Hardware Crypto drivers"
int "IV max size"
default "16"
- config HWCRYPTO_KEYBIT_MAX_SIZE
+ config RT_HWCRYPTO_KEYBIT_MAX_SIZE
int "Key max bit length"
default 256
diff --git a/components/drivers/hwcrypto/hw_bignum.c b/components/drivers/hwcrypto/hw_bignum.c
index 173975c70a..cfc57f8273 100644
--- a/components/drivers/hwcrypto/hw_bignum.c
+++ b/components/drivers/hwcrypto/hw_bignum.c
@@ -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;
}
diff --git a/components/drivers/hwcrypto/hw_bignum.h b/components/drivers/hwcrypto/hw_bignum.h
index 92bd4dd3b6..a1f567e68f 100644
--- a/components/drivers/hwcrypto/hw_bignum.h
+++ b/components/drivers/hwcrypto/hw_bignum.h
@@ -22,34 +22,35 @@ struct hwcrypto_bignum;
/* bignum obj */
struct hw_bignum_mpi
{
- rt_size_t total; /**< Total length of data */
- rt_ubase_t *p; /**< pointer to data */
+ int sign; /**< integer sign */
+ rt_size_t total; /**< total of limbs */
+ rt_uint8_t *p; /**< pointer to limbs */
};
struct hwcrypto_bignum_ops
{
rt_err_t (*add)(struct hwcrypto_bignum *bignum_ctx,
- struct hw_bignum_mpi *a,
- const struct hw_bignum_mpi *b,
- const struct hw_bignum_mpi *c); /**< a = b + c */
+ struct hw_bignum_mpi *x,
+ const struct hw_bignum_mpi *a,
+ const struct hw_bignum_mpi *b); /**< x = a + b */
rt_err_t (*sub)(struct hwcrypto_bignum *bignum_ctx,
- struct hw_bignum_mpi *a,
- const struct hw_bignum_mpi *b,
- const struct hw_bignum_mpi *c); /**< a = b - c */
+ struct hw_bignum_mpi *x,
+ const struct hw_bignum_mpi *a,
+ const struct hw_bignum_mpi *b); /**< x = a - b */
rt_err_t (*mul)(struct hwcrypto_bignum *bignum_ctx,
- struct hw_bignum_mpi *a,
- const struct hw_bignum_mpi *b,
- const struct hw_bignum_mpi *c); /**< a = b * c */
+ struct hw_bignum_mpi *x,
+ const struct hw_bignum_mpi *a,
+ const struct hw_bignum_mpi *b); /**< x = a * b */
rt_err_t (*mulmod)(struct hwcrypto_bignum *bignum_ctx,
- struct hw_bignum_mpi *a,
+ 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); /**< a = b * c (mod d) */
+ const struct hw_bignum_mpi *c); /**< x = a * b (mod c) */
rt_err_t (*exptmod)(struct hwcrypto_bignum *bignum_ctx,
- struct hw_bignum_mpi *a,
+ 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); /**< a = b ^ c (mod d) */
+ const struct hw_bignum_mpi *c); /**< x = a ^ b (mod c) */
};
/**
@@ -69,11 +70,9 @@ struct hwcrypto_bignum
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
*/
-struct hw_bignum_mpi *rt_hwcrypto_bignum_alloc(void);
+void rt_hwcrypto_bignum_init(struct hw_bignum_mpi *n);
/**
* @brief free a bignum obj
@@ -92,18 +91,18 @@ void rt_hwcrypto_bignum_free(struct hw_bignum_mpi *n);
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);
/**
- * @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
@@ -111,31 +110,10 @@ 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);
/**
- * @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);
-
-/**
- * @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);
-
-/**
- * @brief a = b + c
+ * @brief x = a + b
*
* @param a bignum obj
* @param b bignum obj
@@ -143,12 +121,12 @@ 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);
/**
- * @brief a = b - c
+ * @brief x = a - b
*
* @param a bignum obj
* @param b bignum obj
@@ -156,12 +134,12 @@ 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);
/**
- * @brief a = b * c
+ * @brief x = a * b
*
* @param a bignum obj
* @param b bignum obj
@@ -169,12 +147,12 @@ 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);
/**
- * @brief a = b * c (mod d)
+ * @brief x = a * b (mod c)
*
* @param a bignum obj
* @param b bignum obj
@@ -182,13 +160,13 @@ 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);
/**
- * @brief a = b ^ c (mod d)
+ * @brief x = a ^ b (mod c)
*
* @param a bignum obj
* @param b bignum obj
@@ -196,10 +174,10 @@ 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);
#ifdef __cplusplus
}
diff --git a/components/drivers/hwcrypto/hw_hash.h b/components/drivers/hwcrypto/hw_hash.h
index b913f4ebd4..de0310e84c 100644
--- a/components/drivers/hwcrypto/hw_hash.h
+++ b/components/drivers/hwcrypto/hw_hash.h
@@ -101,7 +101,7 @@ void rt_hwcrypto_hash_reset(struct rt_hwcrypto_ctx *ctx);
*
* @return RT_EOK on success.
*/
-rt_err_t rt_hwcrypto_hash_type(struct rt_hwcrypto_ctx *ctx, hwcrypto_type type);
+rt_err_t rt_hwcrypto_hash_set_type(struct rt_hwcrypto_ctx *ctx, hwcrypto_type type);
#ifdef __cplusplus
}