1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2024 Rockchip Electronics Co., Ltd 4 */ 5 6 #ifndef _ROCKCHIP_CRYPTO_ECC_H_ 7 #define _ROCKCHIP_CRYPTO_ECC_H_ 8 9 #include <asm/io.h> 10 #include <common.h> 11 #include <rockchip/crypto_v2.h> 12 #include <rockchip/crypto_v2_util.h> 13 14 #define SM2_RAM_BASE ((crypto_base) + 0x1000) 15 16 #define _SBF(s, v) ((v) << (s)) 17 #define _BIT(b) _SBF(b, 1) 18 19 #define RK_ECP_MAX_BITS 256 20 #define RK_ECP_MAX_BYTES ((RK_ECP_MAX_BITS) / 8) 21 #define RK_ECP_MAX_WORDS ((RK_ECP_MAX_BITS) / 32) 22 #define RK_ECP_MAX_WORDS_ALL (512 / 32) 23 24 #define RK_ECC_CTL 0x03F0 25 #define RK_ECC_CTL_RAND_K_SRC_SOFT _SBF(12, 0) 26 #define RK_ECC_CTL_RAND_K_SRC_HARD _SBF(12, 1) 27 #define RK_ECC_CTL_FUNC_SM2_CURVER _SBF(8, 0x0) 28 #define RK_ECC_CTL_FUNC_ECC_CURVER _SBF(8, 0x1) 29 30 #define RK_ECC_CTL_FUNC_SEL_MUL _SBF(4, 0x0) 31 #define RK_ECC_CTL_FUNC_SEL_KG _SBF(4, 0x1) 32 #define RK_ECC_CTL_FUNC_SEL_SIGN _SBF(4, 0x2) 33 #define RK_ECC_CTL_FUNC_SEL_VERIFY _SBF(4, 0x3) 34 #define RK_ECC_CTL_FUNC_SEL_MUL_MOD _SBF(4, 0x4) 35 #define RK_ECC_CTL_FUNC_SEL_ADD_MOD _SBF(4, 0x5) 36 #define RK_ECC_CTL_FUNC_SEL_DOUBLE_POINT _SBF(4, 0x6) 37 #define RK_ECC_CTL_FUNC_SEL_ADD_POINT _SBF(4, 0x7) 38 #define RK_ECC_CTL_FUNC_SEL_KP _SBF(4, 0x8) 39 #define RK_ECC_CTL_FUNC_SEL_KP_KG _SBF(4, 0xa) 40 #define RK_ECC_CTL_REQ_ECC _SBF(0, 1) 41 42 #define RK_ECC_INT_EN 0x03F4 43 #define RK_ECC_INT_EN_DONE _BIT(0) 44 45 #define RK_ECC_INT_ST 0x03F8 46 #define RK_ECC_INT_ST_DONE _BIT(0) 47 48 #define RK_ECC_ABN_ST 0x03FC 49 #define RK_ECC_ABN_ST_BAD_INV_OUT _BIT(8) 50 #define RK_ECC_ABN_ST_BAD_K_IN _BIT(7) 51 #define RK_ECC_ABN_ST_BAD_R_IN _BIT(6) 52 #define RK_ECC_ABN_ST_BAD_S_IN _BIT(5) 53 #define RK_ECC_ABN_ST_BAD_R_K_MID _BIT(4) 54 #define RK_ECC_ABN_ST_BAD_R_OUT _BIT(3) 55 #define RK_ECC_ABN_ST_BAD_S_OUT _BIT(2) 56 #define RK_ECC_ABN_ST_BAD_T_OUT _BIT(1) 57 #define RK_ECC_ABN_ST_BAD_POINT_OUT _BIT(0) 58 59 #define RK_ECC_CURVE_WIDE 0x0400 60 #define RK_ECC_CURVE_WIDE_192 192 61 #define RK_ECC_CURVE_WIDE_224 224 62 #define RK_ECC_CURVE_WIDE_256 256 63 64 #define RK_ECC_MAX_CURVE_WIDE 0x0404 65 66 #define RK_ECC_DATA_ENDIAN 0x0408 67 #define RK_ECC_DATA_ENDIAN_LITTLE 0x0 68 #define RK_ECC_DATA_ENDIAN_BIG 0x1 69 70 #define RK_ECC_RAM_CTL 0x0480 71 #define RK_ECC_RAM_CTL_SEL_MASK _SBF(16, 3) 72 #define RK_ECC_RAM_CTL_CPU _SBF(0, 0) 73 #define RK_ECC_RAM_CTL_PKA _SBF(0, 1) 74 #define RK_ECC_RAM_CTL_ECC _SBF(0, 2) 75 76 77 struct rk_ecp_point { 78 struct mpa_num *x; /*!< the point's X coordinate */ 79 struct mpa_num *y; /*!< the point's Y coordinate */ 80 }; 81 82 enum rk_ecp_group_id { 83 RK_ECP_DP_NONE = 0, 84 RK_ECP_DP_SECP192R1, /*!< 192-bits NIST curve */ 85 RK_ECP_DP_SECP224R1, /*!< 224-bits NIST curve */ 86 RK_ECP_DP_SECP256R1, /*!< 256-bits NIST curve */ 87 RK_ECP_DP_SM2P256V1, /*!< */ 88 }; 89 90 struct rk_ecp_group { 91 enum rk_ecp_group_id id; /*!< internal group identifier */ 92 const char *curve_name; 93 uint32_t wide; 94 const uint8_t *p; /*!< prime modulus of the base field */ 95 const uint8_t *a; /*!< 1. A in the equation, or 2. (A + 2) / 4 */ 96 const uint8_t *n; 97 const uint8_t *gx; 98 const uint8_t *gy; 99 size_t p_len; 100 size_t a_len; 101 size_t n_len; 102 size_t gx_len; 103 size_t gy_len; 104 }; 105 106 struct rk_ecc_verify { 107 uint32_t e[RK_ECP_MAX_WORDS_ALL]; // 0x00 108 uint32_t r_[RK_ECP_MAX_WORDS_ALL]; // 0x40 109 uint32_t s_[RK_ECP_MAX_WORDS_ALL]; // 0x80 110 uint32_t p_x[RK_ECP_MAX_WORDS_ALL]; // 0xC0 111 uint32_t p_y[RK_ECP_MAX_WORDS_ALL]; // 0x100 112 uint32_t A[RK_ECP_MAX_WORDS_ALL]; // 0x140 113 uint32_t P[RK_ECP_MAX_WORDS_ALL]; // 0x180 114 uint32_t N[RK_ECP_MAX_WORDS_ALL]; // 0x1C0 115 uint32_t G_x[RK_ECP_MAX_WORDS_ALL]; // 0x200 116 uint32_t G_y[RK_ECP_MAX_WORDS_ALL]; // 0x240 117 uint32_t r[RK_ECP_MAX_WORDS_ALL]; // 0x280 118 uint32_t v[RK_ECP_MAX_WORDS_ALL]; // 0x2C0 119 }; 120 121 int rockchip_ecc_verify(uint32_t crypto_algo, uint8_t *hash, uint32_t hash_len, 122 struct rk_ecp_point *point_P, struct rk_ecp_point *point_sign); 123 #endif 124