1 /* 2 * Copyright (c) 2006-2023, RT-Thread Development Team 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Change Logs: 7 * Date Author Notes 8 * 2019-04-25 tyx the first version 9 */ 10 11 #ifndef __HW_BIGNUM_H__ 12 #define __HW_BIGNUM_H__ 13 14 #include <hwcrypto.h> 15 16 #ifdef __cplusplus 17 extern "C" { 18 #endif 19 20 struct hwcrypto_bignum; 21 22 /* bignum obj */ 23 struct hw_bignum_mpi 24 { 25 int sign; /**< integer sign. -1 or 1 */ 26 rt_size_t total; /**< total of limbs */ 27 rt_uint8_t *p; /**< pointer to limbs */ 28 }; 29 30 struct hwcrypto_bignum_ops 31 { 32 rt_err_t (*add)(struct hwcrypto_bignum *bignum_ctx, 33 struct hw_bignum_mpi *x, 34 const struct hw_bignum_mpi *a, 35 const struct hw_bignum_mpi *b); /**< x = a + b */ 36 rt_err_t (*sub)(struct hwcrypto_bignum *bignum_ctx, 37 struct hw_bignum_mpi *x, 38 const struct hw_bignum_mpi *a, 39 const struct hw_bignum_mpi *b); /**< x = a - b */ 40 rt_err_t (*mul)(struct hwcrypto_bignum *bignum_ctx, 41 struct hw_bignum_mpi *x, 42 const struct hw_bignum_mpi *a, 43 const struct hw_bignum_mpi *b); /**< x = a * b */ 44 rt_err_t (*mulmod)(struct hwcrypto_bignum *bignum_ctx, 45 struct hw_bignum_mpi *x, 46 const struct hw_bignum_mpi *a, 47 const struct hw_bignum_mpi *b, 48 const struct hw_bignum_mpi *c); /**< x = a * b (mod c) */ 49 rt_err_t (*exptmod)(struct hwcrypto_bignum *bignum_ctx, 50 struct hw_bignum_mpi *x, 51 const struct hw_bignum_mpi *a, 52 const struct hw_bignum_mpi *b, 53 const struct hw_bignum_mpi *c); /**< x = a ^ b (mod c) */ 54 }; 55 56 /** 57 * @brief bignum context. Hardware driver usage 58 */ 59 struct hwcrypto_bignum 60 { 61 struct rt_hwcrypto_ctx parent; /**< Inheritance from hardware crypto context */ 62 const struct hwcrypto_bignum_ops *ops; /**< !! Hardware initializes this value when creating context !! */ 63 }; 64 65 /** 66 * @brief Setting bignum default devices 67 * 68 * @return RT_EOK on success. 69 */ 70 rt_err_t rt_hwcrypto_bignum_default(struct rt_hwcrypto_device *device); 71 72 /** 73 * @brief Init bignum obj 74 */ 75 void rt_hwcrypto_bignum_init(struct hw_bignum_mpi *n); 76 77 /** 78 * @brief free a bignum obj 79 * 80 * @param Pointer to bignum obj 81 */ 82 void rt_hwcrypto_bignum_free(struct hw_bignum_mpi *n); 83 84 /** 85 * @brief Get length of bignum as an unsigned binary buffer 86 * 87 * @param n bignum obj 88 * 89 * @return binary buffer Length 90 */ 91 int rt_hwcrypto_bignum_get_len(const struct hw_bignum_mpi *n); 92 93 /** 94 * @brief Export n into unsigned binary data, big endian 95 * 96 * @param n bignum obj 97 * @param buf Buffer for the binary number 98 * @param len Length of the buffer 99 * 100 * @return export bin length 101 */ 102 int rt_hwcrypto_bignum_export_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len); 103 104 /** 105 * @brief Import n from unsigned binary data, big endian 106 * 107 * @param n bignum obj 108 * @param buf Buffer for the binary number 109 * @param len Length of the buffer 110 * 111 * @return import length. 112 */ 113 int rt_hwcrypto_bignum_import_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len); 114 115 /** 116 * @brief x = a + b 117 * 118 * @param a bignum obj 119 * @param b bignum obj 120 * @param c bignum obj 121 * 122 * @return RT_EOK on success. 123 */ 124 rt_err_t rt_hwcrypto_bignum_add(struct hw_bignum_mpi *x, 125 const struct hw_bignum_mpi *a, 126 const struct hw_bignum_mpi *b); 127 128 /** 129 * @brief x = a - b 130 * 131 * @param a bignum obj 132 * @param b bignum obj 133 * @param c bignum obj 134 * 135 * @return RT_EOK on success. 136 */ 137 rt_err_t rt_hwcrypto_bignum_sub(struct hw_bignum_mpi *x, 138 const struct hw_bignum_mpi *a, 139 const struct hw_bignum_mpi *b); 140 141 /** 142 * @brief x = a * b 143 * 144 * @param a bignum obj 145 * @param b bignum obj 146 * @param c bignum obj 147 * 148 * @return RT_EOK on success. 149 */ 150 rt_err_t rt_hwcrypto_bignum_mul(struct hw_bignum_mpi *x, 151 const struct hw_bignum_mpi *a, 152 const struct hw_bignum_mpi *b); 153 154 /** 155 * @brief x = a * b (mod c) 156 * 157 * @param a bignum obj 158 * @param b bignum obj 159 * @param c bignum obj 160 * 161 * @return RT_EOK on success. 162 */ 163 rt_err_t rt_hwcrypto_bignum_mulmod(struct hw_bignum_mpi *x, 164 const struct hw_bignum_mpi *a, 165 const struct hw_bignum_mpi *b, 166 const struct hw_bignum_mpi *c); 167 168 /** 169 * @brief x = a ^ b (mod c) 170 * 171 * @param a bignum obj 172 * @param b bignum obj 173 * @param c bignum obj 174 * 175 * @return RT_EOK on success. 176 */ 177 rt_err_t rt_hwcrypto_bignum_exptmod(struct hw_bignum_mpi *x, 178 const struct hw_bignum_mpi *a, 179 const struct hw_bignum_mpi *b, 180 const struct hw_bignum_mpi *c); 181 182 #ifdef __cplusplus 183 } 184 #endif 185 186 #endif 187