1 /* 2 * Copyright (c) 2021 Chipsea Technologies (Shenzhen) Corp., Ltd. All rights reserved. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 /** 16 **************************************************************************************** 17 * 18 * @file ce_api.h 19 * 20 * @brief CE utility functions 21 * 22 **************************************************************************************** 23 */ 24 25 #ifndef _CE_API_H_ 26 #define _CE_API_H_ 27 /*chipsea_ohos proguard begin*/ 28 #include "cs_proguard.h" 29 /*chipsea_ohos proguard end*/ 30 31 /* 32 * INCLUDE FILES 33 **************************************************************************************** 34 */ 35 #include "arch.h" 36 #include "ce_common.h" 37 38 #define ECC_LENGTH_MAX 112 //16word * 7 39 #define ECC_KEY_LEN_MAX 16 40 #define RSA_LENGTH_MAX 384 //64word *6 41 #define RSA_KEY_LEN_MAX 64 42 43 #define HASH_PAD_LENGTH 64 //64byte 44 45 int isGreaterThanOrEqual256(uint32_t *bigHexA, uint32_t *bigHexB); 46 47 void dma_cypt_init(dma_cypt_t *obj); 48 49 void aes_init(dma_cypt_t *obj); 50 51 void hash_init(dma_cypt_t *obj); 52 53 void rsa_init(dma_cypt_t *obj); 54 55 void ecc_init(dma_cypt_t *obj); 56 57 void aes_key_update(dma_cypt_t *obj); 58 59 void sha1_calc(uint8_t *input, uint32_t ilen, uint8_t output[20]); 60 61 void hash_proc_512bit(dma_cypt_t *ce_obj, hash_state_t *hash_obj, uint32_t *hash_result); 62 63 void aes_calc(dma_cypt_t *ce_obj, aes_state_t *aes_obj, uint32_t *aes_result); 64 65 void aes_gcm_auth_data_init(aes_gcm_state_t *aes_gcm_obj); 66 67 void aes_gcm_calc(dma_cypt_t *ce_obj, aes_gcm_state_t *aes_gcm_obj, uint32_t *aes_result, uint32_t *aes_gcm_tag); 68 69 void rsa_coef_init(uint32_t *index, uint32_t *Mod_P); 70 71 void rsa_calc(dma_cypt_t *ce_obj, uint32_t *base, uint32_t *comb_rsa_data); 72 73 void curve_coef_init(dma_cypt_t *ce_obj, uint32_t *coef_A, uint32_t *Mod_P, uint32_t *R2ModP); 74 75 void ecc_generate_key(dma_cypt_t *ce_obj, uint32_t *secret_key, uint32_t *public_key_x, uint32_t *public_key_y, uint32_t *EccResult); 76 77 uint32_t pka_len_get(dma_cypt_t *obj); 78 79 /** 80 **************************************************************************************** 81 * @brief Get current key len depend on current mode 82 **************************************************************************************** 83 */ 84 void pka_key_len_get(dma_cypt_t *obj); 85 86 /** 87 **************************************************************************************** 88 * @brief Calculate R - Mod_P 89 * @param[in] Mod_P :modulus P 90 **************************************************************************************** 91 */ 92 void Mod_P_inv_calc(uint32_t *Mod_P, uint32_t *Mod_P_inv); 93 94 /** 95 **************************************************************************************** 96 * @brief Calculate a big num subtract another big num 97 * @param[in] pBigNum :big number A, must bigger than big number B 98 * @param[in] pSmallNum :big number B 99 **************************************************************************************** 100 */ 101 void SubtractFromSelfBigHex256(uint32_t *pBigNum, uint32_t *pSmallNum); 102 103 /** 104 **************************************************************************************** 105 * @brief Calculate AmodP (A length equal B length plus 32bit), the result store in tmpHexA 106 * @param[in] tmpHexA :big number A, length of A equal length of Mod_P plus 32bit 107 * @param[in] Mod_P :modulus P 108 **************************************************************************************** 109 */ 110 void specialModP256(uint32_t *tmpHexA, uint32_t *Mod_P); 111 112 /** 113 **************************************************************************************** 114 * @brief Calculate R2modP = R^2 mod P 115 * @param[in] Mod_P :modulus P 116 **************************************************************************************** 117 */ 118 void R2_calc(uint32_t *Mod_P, uint32_t *R2modP); 119 120 /** 121 **************************************************************************************** 122 * @brief Combine data for RSA calculate, result = base ^ index mod Mod_P 123 * @param[in] base :base of power operation 124 * @param[in] index :index of power operation 125 * @param[in] Mod_P :modulus P 126 * @param[in] R2modP :R^2 mod P 127 **************************************************************************************** 128 */ 129 void rsa_data_comb(uint32_t *base, uint32_t *index, uint32_t *Mod_P, uint32_t *R2modP, uint32_t *comb_rsa_data); 130 131 /** 132 **************************************************************************************** 133 * @brief Combine data for ECC calculate, result = secret_key *(public_key_x,public_key_y) 134 * 135 * @param[in] secret_key :secret_key to generator public key / privkey to generator ECDH 136 * @param[in] public_key_x :ellipse curve base ponit x /public key x 137 * @param[in] public_key_y :ellipse curve base ponit y /public key y 138 * @param[in] coef_A :ellipse curve y^2 = x^3 + coef_A * x + coef_B 139 * @param[in] Mod_P :modulus P 140 * @param[in] R2modP :R^2 mod P 141 **************************************************************************************** 142 */ 143 void ecc_data_comb(uint32_t *secret_key, uint32_t *public_key_x, uint32_t *public_key_y, uint32_t *coef_A, uint32_t *Mod_P, uint32_t *R2modP, uint32_t *comb_ecc_data); 144 145 int cs_get_ecc_generate_key_ready(void); 146 147 void chksum_calc_trans(uint32_t *src_addr, uint32_t len, uint32_t endian, uint32_t *dest_addr); 148 149 void chksum_calc(uint32_t *src_addr, uint32_t len, uint32_t endian); 150 151 uint16_t chksum_result_get(void); 152 153 #endif // _CE_API_H_ 154