1 /* 2 * Copyright (C) 2021 - This file is part of libecc project 3 * 4 * Authors: 5 * Ryad BENADJILA <ryadbenadjila@gmail.com> 6 * Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr> 7 * 8 * This software is licensed under a dual BSD and GPL v2 license. 9 * See LICENSE file at the root folder of the project. 10 */ 11 12 #include <libecc/lib_ecc_config.h> 13 #include <libecc/lib_ecc_types.h> 14 #ifdef WITH_SIG_SM2 15 16 #ifndef __SM2_H__ 17 #define __SM2_H__ 18 19 #include <libecc/words/words.h> 20 #include <libecc/sig/ec_key.h> 21 #include <libecc/hash/hash_algs.h> 22 #include <libecc/curves/curves.h> 23 #include <libecc/utils/utils.h> 24 25 #define SM2_R_LEN(q_bit_len) (BYTECEIL(q_bit_len)) 26 #define SM2_S_LEN(q_bit_len) (BYTECEIL(q_bit_len)) 27 #define SM2_SIGLEN(q_bit_len) (SM2_R_LEN(q_bit_len) + \ 28 SM2_S_LEN(q_bit_len)) 29 #define SM2_MAX_SIGLEN SM2_SIGLEN(CURVES_MAX_Q_BIT_LEN) 30 #define SM2_MAX_ID_LEN 8191 /* SM2 user ID max byte length */ 31 32 /* 33 * Compute max signature length for all the mechanisms enabled 34 * in the library (see lib_ecc_config.h). Having that done during 35 * preprocessing sadly requires some verbosity. 36 */ 37 #ifndef EC_MAX_SIGLEN 38 #define EC_MAX_SIGLEN 0 39 #endif 40 #if ((EC_MAX_SIGLEN) < (SM2_MAX_SIGLEN)) 41 #undef EC_MAX_SIGLEN 42 #define EC_MAX_SIGLEN SM2_MAX_SIGLEN 43 #endif 44 45 typedef struct { 46 hash_context h_ctx; 47 word_t magic; 48 } sm2_sign_data; 49 50 struct ec_sign_context; 51 52 ATTRIBUTE_WARN_UNUSED_RET int sm2_gen_priv_key(ec_priv_key *priv_key); 53 54 ATTRIBUTE_WARN_UNUSED_RET int sm2_init_pub_key(ec_pub_key *out_pub, const ec_priv_key *in_priv); 55 56 ATTRIBUTE_WARN_UNUSED_RET int sm2_siglen(u16 p_bit_len, u16 q_bit_len, u8 hsize, u8 blocksize, u8 *siglen); 57 58 ATTRIBUTE_WARN_UNUSED_RET int _sm2_sign_init(struct ec_sign_context *ctx); 59 60 ATTRIBUTE_WARN_UNUSED_RET int _sm2_sign_update(struct ec_sign_context *ctx, 61 const u8 *chunk, u32 chunklen); 62 63 ATTRIBUTE_WARN_UNUSED_RET int _sm2_sign_finalize(struct ec_sign_context *ctx, u8 *sig, u8 siglen); 64 65 typedef struct { 66 nn r; 67 nn s; 68 hash_context h_ctx; 69 word_t magic; 70 } sm2_verify_data; 71 72 struct ec_verify_context; 73 74 ATTRIBUTE_WARN_UNUSED_RET int _sm2_verify_init(struct ec_verify_context *ctx, 75 const u8 *sig, u8 siglen); 76 77 ATTRIBUTE_WARN_UNUSED_RET int _sm2_verify_update(struct ec_verify_context *ctx, 78 const u8 *chunk, u32 chunklen); 79 80 ATTRIBUTE_WARN_UNUSED_RET int _sm2_verify_finalize(struct ec_verify_context *ctx); 81 82 #endif /* __SM2_H__ */ 83 #endif /* WITH_SIG_SM2 */ 84