1 /* 2 * This file is part of the openHiTLS project. 3 * 4 * openHiTLS is licensed under the Mulan PSL v2. 5 * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 * You may obtain a copy of Mulan PSL v2 at: 7 * 8 * http://license.coscl.org.cn/MulanPSL2 9 * 10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13 * See the Mulan PSL v2 for more details. 14 */ 15 16 #ifndef CRYPT_ENCODE_INTERNAL_H 17 #define CRYPT_ENCODE_INTERNAL_H 18 19 #include "hitls_build.h" 20 #include "bsl_types.h" 21 #include "bsl_asn1.h" 22 #include "crypt_bn.h" 23 #include "crypt_eal_pkey.h" 24 25 #ifdef __cplusplus 26 extern "C" { 27 #endif /* __cpluscplus */ 28 29 30 #if defined(HITLS_CRYPTO_SM2_SIGN) || defined(HITLS_CRYPTO_DSA) || defined(HITLS_CRYPTO_ECDSA) 31 /** 32 * Get the maximum length of the signature data. 33 * 34 * @param rLen [in] The length of r. 35 * @param sLen [in] The length of s. 36 * @param maxLen [out] The maximum length of the signature data. 37 * @return: CRYPT_SUCCESS: Success, other: Error. 38 */ 39 int32_t CRYPT_EAL_GetSignEncodeLen(uint32_t rLen, uint32_t sLen, uint32_t *maxLen); 40 41 /** 42 * Encode the signature data by big number. 43 * 44 * @param r [in] The r value. 45 * @param s [in] The s value. 46 * @param encode [out] The encoded data. 47 * @param encodeLen [out] The length of the encoded data. 48 * @return: CRYPT_SUCCESS: Success, other: Error. 49 */ 50 int32_t CRYPT_EAL_EncodeSign(const BN_BigNum *r, const BN_BigNum *s, uint8_t *encode, uint32_t *encodeLen); 51 52 /** 53 * Decode the signature data to big number. 54 * 55 * @param encode [in] The encoded data. 56 * @param encodeLen [in] The length of the encoded data. 57 * @param r [out] The r value. 58 * @param s [out] The s value. 59 * @return: CRYPT_SUCCESS: Success, other: Error. 60 */ 61 int32_t CRYPT_EAL_DecodeSign(const uint8_t *encode, uint32_t encodeLen, BN_BigNum *r, BN_BigNum *s); 62 #endif 63 64 #ifdef HITLS_CRYPTO_SM2_CRYPT 65 typedef struct { 66 uint8_t *x; // XCoordinate 67 uint8_t *y; // YCoordinate 68 uint8_t *hash; // HASH 69 uint8_t *cipher; // CipherText 70 uint32_t xLen; 71 uint32_t yLen; 72 uint32_t hashLen; 73 uint32_t cipherLen; 74 } CRYPT_SM2_EncryptData; 75 76 /** 77 * Get the length of the SM2 encoded data. 78 * 79 * @param xLen [in] The length of the x coordinate. 80 * @param yLen [in] The length of the y coordinate. 81 * @param hashLen [in] The length of the hash. 82 * @param dataLen [in] The length of the data. 83 * @param maxLen [out] The length of the SM2 encoded data. 84 * @return: CRYPT_SUCCESS: Success, other: Error. 85 */ 86 int32_t CRYPT_EAL_GetSm2EncryptDataEncodeLen(uint32_t xLen, uint32_t yLen, uint32_t hashLen, uint32_t dataLen, 87 uint32_t *maxLen); 88 89 /** 90 * Encode the SM2 encrypt data. 91 * 92 * @param data [in] The SM2 encrypt data. 93 * @param encode [out] The encoded data. 94 * @param encodeLen [out] The length of the encoded data. 95 * @return: CRYPT_SUCCESS: Success, other: Error. 96 */ 97 int32_t CRYPT_EAL_EncodeSm2EncryptData(const CRYPT_SM2_EncryptData *data, uint8_t *encode, uint32_t *encodeLen); 98 99 /** 100 * Decode the SM2 encrypt data. 101 * 102 * @param encode [in] The encoded data. 103 * @param encodeLen [in] The length of the encoded data. 104 * @param data [out] The SM2 encrypt data. 105 * @return: CRYPT_SUCCESS: Success, other: Error. 106 */ 107 int32_t CRYPT_EAL_DecodeSm2EncryptData(const uint8_t *encode, uint32_t encodeLen, CRYPT_SM2_EncryptData *data); 108 #endif 109 110 #ifdef __cplusplus 111 } 112 #endif 113 114 #endif // CRYPT_ENCODE_INTERNAL_H 115