2019-05-24 20:04:27 +08:00
|
|
|
/*
|
2021-03-08 18:19:04 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2019-05-24 20:04:27 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2019-04-25 tyx the first version
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __HW_BIGNUM_H__
|
|
|
|
#define __HW_BIGNUM_H__
|
|
|
|
|
|
|
|
#include <hwcrypto.h>
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct hwcrypto_bignum;
|
|
|
|
|
|
|
|
/* bignum obj */
|
|
|
|
struct hw_bignum_mpi
|
|
|
|
{
|
2019-06-05 14:49:19 +08:00
|
|
|
int sign; /**< integer sign */
|
|
|
|
rt_size_t total; /**< total of limbs */
|
|
|
|
rt_uint8_t *p; /**< pointer to limbs */
|
2019-05-24 20:04:27 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct hwcrypto_bignum_ops
|
|
|
|
{
|
|
|
|
rt_err_t (*add)(struct hwcrypto_bignum *bignum_ctx,
|
2019-06-05 14:49:19 +08:00
|
|
|
struct hw_bignum_mpi *x,
|
|
|
|
const struct hw_bignum_mpi *a,
|
|
|
|
const struct hw_bignum_mpi *b); /**< x = a + b */
|
2019-05-24 20:04:27 +08:00
|
|
|
rt_err_t (*sub)(struct hwcrypto_bignum *bignum_ctx,
|
2019-06-05 14:49:19 +08:00
|
|
|
struct hw_bignum_mpi *x,
|
|
|
|
const struct hw_bignum_mpi *a,
|
|
|
|
const struct hw_bignum_mpi *b); /**< x = a - b */
|
2019-05-24 20:04:27 +08:00
|
|
|
rt_err_t (*mul)(struct hwcrypto_bignum *bignum_ctx,
|
2019-06-05 14:49:19 +08:00
|
|
|
struct hw_bignum_mpi *x,
|
|
|
|
const struct hw_bignum_mpi *a,
|
|
|
|
const struct hw_bignum_mpi *b); /**< x = a * b */
|
2019-05-24 20:04:27 +08:00
|
|
|
rt_err_t (*mulmod)(struct hwcrypto_bignum *bignum_ctx,
|
2019-06-05 14:49:19 +08:00
|
|
|
struct hw_bignum_mpi *x,
|
|
|
|
const struct hw_bignum_mpi *a,
|
2019-05-24 20:04:27 +08:00
|
|
|
const struct hw_bignum_mpi *b,
|
2019-06-05 14:49:19 +08:00
|
|
|
const struct hw_bignum_mpi *c); /**< x = a * b (mod c) */
|
2019-05-24 20:04:27 +08:00
|
|
|
rt_err_t (*exptmod)(struct hwcrypto_bignum *bignum_ctx,
|
2019-06-05 14:49:19 +08:00
|
|
|
struct hw_bignum_mpi *x,
|
|
|
|
const struct hw_bignum_mpi *a,
|
2019-05-24 20:04:27 +08:00
|
|
|
const struct hw_bignum_mpi *b,
|
2019-06-05 14:49:19 +08:00
|
|
|
const struct hw_bignum_mpi *c); /**< x = a ^ b (mod c) */
|
2019-05-24 20:04:27 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief bignum context. Hardware driver usage
|
|
|
|
*/
|
|
|
|
struct hwcrypto_bignum
|
|
|
|
{
|
|
|
|
struct rt_hwcrypto_ctx parent; /**< Inheritance from hardware crypto context */
|
|
|
|
const struct hwcrypto_bignum_ops *ops; /**< !! Hardware initializes this value when creating context !! */
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Setting bignum default devices
|
|
|
|
*
|
|
|
|
* @return RT_EOK on success.
|
|
|
|
*/
|
|
|
|
rt_err_t rt_hwcrypto_bignum_default(struct rt_hwcrypto_device *device);
|
|
|
|
|
|
|
|
/**
|
2019-06-05 14:49:19 +08:00
|
|
|
* @brief Init bignum obj
|
2019-05-24 20:04:27 +08:00
|
|
|
*/
|
2019-06-05 14:49:19 +08:00
|
|
|
void rt_hwcrypto_bignum_init(struct hw_bignum_mpi *n);
|
2019-05-24 20:04:27 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief free a bignum obj
|
|
|
|
*
|
|
|
|
* @param Pointer to bignum obj
|
|
|
|
*/
|
|
|
|
void rt_hwcrypto_bignum_free(struct hw_bignum_mpi *n);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get length of bignum as an unsigned binary buffer
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2019-05-24 20:04:27 +08:00
|
|
|
* @param n bignum obj
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2019-05-24 20:04:27 +08:00
|
|
|
* @return binary buffer Length
|
|
|
|
*/
|
|
|
|
int rt_hwcrypto_bignum_get_len(const struct hw_bignum_mpi *n);
|
|
|
|
|
|
|
|
/**
|
2019-06-05 14:49:19 +08:00
|
|
|
* @brief Export n into unsigned binary data, big endian
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2019-05-24 20:04:27 +08:00
|
|
|
* @param n bignum obj
|
|
|
|
* @param buf Buffer for the binary number
|
|
|
|
* @param len Length of the buffer
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2019-06-05 14:49:19 +08:00
|
|
|
* @return export bin length
|
2019-05-24 20:04:27 +08:00
|
|
|
*/
|
2019-06-05 14:49:19 +08:00
|
|
|
int rt_hwcrypto_bignum_export_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len);
|
2019-05-24 20:04:27 +08:00
|
|
|
|
|
|
|
/**
|
2019-06-05 14:49:19 +08:00
|
|
|
* @brief Import n from unsigned binary data, big endian
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2019-05-24 20:04:27 +08:00
|
|
|
* @param n bignum obj
|
|
|
|
* @param buf Buffer for the binary number
|
|
|
|
* @param len Length of the buffer
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2019-05-24 20:04:27 +08:00
|
|
|
* @return RT_EOK on success.
|
|
|
|
*/
|
2019-06-05 14:49:19 +08:00
|
|
|
rt_err_t rt_hwcrypto_bignum_import_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len);
|
2019-05-24 20:04:27 +08:00
|
|
|
|
|
|
|
/**
|
2019-06-05 14:49:19 +08:00
|
|
|
* @brief x = a + b
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2019-05-24 20:04:27 +08:00
|
|
|
* @param a bignum obj
|
|
|
|
* @param b bignum obj
|
|
|
|
* @param c bignum obj
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2019-05-24 20:04:27 +08:00
|
|
|
* @return RT_EOK on success.
|
|
|
|
*/
|
2019-06-05 14:49:19 +08:00
|
|
|
rt_err_t rt_hwcrypto_bignum_add(struct hw_bignum_mpi *x,
|
|
|
|
const struct hw_bignum_mpi *a,
|
|
|
|
const struct hw_bignum_mpi *b);
|
2019-05-24 20:04:27 +08:00
|
|
|
|
|
|
|
/**
|
2019-06-05 14:49:19 +08:00
|
|
|
* @brief x = a - b
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2019-05-24 20:04:27 +08:00
|
|
|
* @param a bignum obj
|
|
|
|
* @param b bignum obj
|
|
|
|
* @param c bignum obj
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2019-05-24 20:04:27 +08:00
|
|
|
* @return RT_EOK on success.
|
|
|
|
*/
|
2019-06-05 14:49:19 +08:00
|
|
|
rt_err_t rt_hwcrypto_bignum_sub(struct hw_bignum_mpi *x,
|
|
|
|
const struct hw_bignum_mpi *a,
|
|
|
|
const struct hw_bignum_mpi *b);
|
2019-05-24 20:04:27 +08:00
|
|
|
|
|
|
|
/**
|
2019-06-05 14:49:19 +08:00
|
|
|
* @brief x = a * b
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2019-05-24 20:04:27 +08:00
|
|
|
* @param a bignum obj
|
|
|
|
* @param b bignum obj
|
|
|
|
* @param c bignum obj
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2019-05-24 20:04:27 +08:00
|
|
|
* @return RT_EOK on success.
|
|
|
|
*/
|
2019-06-05 14:49:19 +08:00
|
|
|
rt_err_t rt_hwcrypto_bignum_mul(struct hw_bignum_mpi *x,
|
|
|
|
const struct hw_bignum_mpi *a,
|
|
|
|
const struct hw_bignum_mpi *b);
|
2019-05-24 20:04:27 +08:00
|
|
|
|
|
|
|
/**
|
2019-06-05 14:49:19 +08:00
|
|
|
* @brief x = a * b (mod c)
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2019-05-24 20:04:27 +08:00
|
|
|
* @param a bignum obj
|
|
|
|
* @param b bignum obj
|
|
|
|
* @param c bignum obj
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2019-05-24 20:04:27 +08:00
|
|
|
* @return RT_EOK on success.
|
|
|
|
*/
|
2019-06-05 14:49:19 +08:00
|
|
|
rt_err_t rt_hwcrypto_bignum_mulmod(struct hw_bignum_mpi *x,
|
|
|
|
const struct hw_bignum_mpi *a,
|
2019-05-24 20:04:27 +08:00
|
|
|
const struct hw_bignum_mpi *b,
|
2019-06-05 14:49:19 +08:00
|
|
|
const struct hw_bignum_mpi *c);
|
2019-05-24 20:04:27 +08:00
|
|
|
|
|
|
|
/**
|
2019-06-05 14:49:19 +08:00
|
|
|
* @brief x = a ^ b (mod c)
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2019-05-24 20:04:27 +08:00
|
|
|
* @param a bignum obj
|
|
|
|
* @param b bignum obj
|
|
|
|
* @param c bignum obj
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2019-05-24 20:04:27 +08:00
|
|
|
* @return RT_EOK on success.
|
|
|
|
*/
|
2019-06-05 14:49:19 +08:00
|
|
|
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);
|
2019-05-24 20:04:27 +08:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|