xref: /freebsd/crypto/libecc/include/libecc/sig/sig_algs.h (revision dd21556857e8d40f66bf5ad54754d9d52669ebf7)
1 /*
2  *  Copyright (C) 2017 - 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  *      Jean-Pierre FLORI <jean-pierre.flori@ssi.gouv.fr>
8  *
9  *  Contributors:
10  *      Nicolas VIVET <nicolas.vivet@ssi.gouv.fr>
11  *      Karim KHALFALLAH <karim.khalfallah@ssi.gouv.fr>
12  *
13  *  This software is licensed under a dual BSD and GPL v2 license.
14  *  See LICENSE file at the root folder of the project.
15  */
16 #ifndef __SIG_ALGS_H__
17 #define __SIG_ALGS_H__
18 
19 #include <libecc/sig/sig_algs_internal.h>
20 
21 /* Private key generation function specific to each scheme */
22 ATTRIBUTE_WARN_UNUSED_RET int gen_priv_key(ec_priv_key *priv_key);
23 
24 /*
25  * Generic function to init a uninitialized public key from an initialized
26  * private key. The function uses the expected logic to derive the key
27  * (e.g. Y=xG, Y=(x^-1)G, etc). It returns -1 on error (i.e. if the signature
28  * alg is unknown) in which case the public key has not been initialized.
29  */
30 ATTRIBUTE_WARN_UNUSED_RET int init_pubkey_from_privkey(ec_pub_key *pub_key, ec_priv_key *priv_key);
31 
32 ATTRIBUTE_WARN_UNUSED_RET int get_sig_by_name(const char *ec_sig_name, const ec_sig_mapping **sig_mapping);
33 ATTRIBUTE_WARN_UNUSED_RET int get_sig_by_type(ec_alg_type sig_type, const ec_sig_mapping **sig_mapping);
34 
35 /* Sanity checks for calbacks */
36 ATTRIBUTE_WARN_UNUSED_RET int ec_sig_mapping_callbacks_sanity_check(const ec_sig_mapping *sig);
37 ATTRIBUTE_WARN_UNUSED_RET int ec_sig_ctx_callbacks_sanity_check(const struct ec_sign_context *sig_ctx);
38 ATTRIBUTE_WARN_UNUSED_RET int ec_verify_ctx_callbacks_sanity_check(const struct ec_verify_context *verify_ctx);
39 
40 /*
41  * Compute generic effective signature length depending on the curve parameters,
42  * the signature algorithm and the hash function
43  */
44 ATTRIBUTE_WARN_UNUSED_RET int ec_get_sig_len(const ec_params *params, ec_alg_type sig_type,
45 		   hash_alg_type hash_type, u8 *siglen);
46 
47 /* Generic signature init/update/finalize */
48 
49 ATTRIBUTE_WARN_UNUSED_RET int _ec_sign_init(struct ec_sign_context *ctx,
50 			 const ec_key_pair *key_pair,
51 			 int (*rand) (nn_t out, nn_src_t q),
52 			 ec_alg_type sig_type, hash_alg_type hash_type,
53 			 const u8 *adata, u16 adata_len);
54 
55 ATTRIBUTE_WARN_UNUSED_RET int ec_sign_init(struct ec_sign_context *ctx, const ec_key_pair *key_pair,
56 		 ec_alg_type sig_type, hash_alg_type hash_type,
57 		 const u8 *adata, u16 adata_len);
58 
59 ATTRIBUTE_WARN_UNUSED_RET int ec_sign_update(struct ec_sign_context *ctx, const u8 *chunk, u32 chunklen);
60 
61 ATTRIBUTE_WARN_UNUSED_RET int ec_sign_finalize(struct ec_sign_context *ctx, u8 *sig, u8 siglen);
62 
63 ATTRIBUTE_WARN_UNUSED_RET int _ec_sign(u8 *sig, u8 siglen, const ec_key_pair *key_pair,
64 	     const u8 *m, u32 mlen,
65 	     int (*rand) (nn_t out, nn_src_t q),
66 	     ec_alg_type sig_type, hash_alg_type hash_type,
67 	     const u8 *adata, u16 adata_len);
68 
69 ATTRIBUTE_WARN_UNUSED_RET int ec_sign(u8 *sig, u8 siglen, const ec_key_pair *key_pair,
70 	    const u8 *m, u32 mlen,
71 	    ec_alg_type sig_type, hash_alg_type hash_type,
72 	    const u8 *adata, u16 adata_len);
73 
74 /* Generic signature verification init/update/finalize */
75 
76 ATTRIBUTE_WARN_UNUSED_RET int ec_verify_init(struct ec_verify_context *ctx, const ec_pub_key *pub_key,
77 		   const u8 *sig, u8 siglen, ec_alg_type sig_type,
78 		   hash_alg_type hash_type, const u8 *adata, u16 adata_len);
79 
80 ATTRIBUTE_WARN_UNUSED_RET int ec_verify_update(struct ec_verify_context *ctx,
81 		     const u8 *chunk, u32 chunklen);
82 
83 ATTRIBUTE_WARN_UNUSED_RET int ec_verify_finalize(struct ec_verify_context *ctx);
84 
85 ATTRIBUTE_WARN_UNUSED_RET int ec_verify(const u8 *sig, u8 siglen, const ec_pub_key *pub_key,
86 	      const u8 *m, u32 mlen,
87 	      ec_alg_type sig_type, hash_alg_type hash_type,
88 	      const u8 *adata, u16 adata_len);
89 
90 int ec_verify_batch(const u8 **s, const u8 *s_len, const ec_pub_key **pub_keys,
91               const u8 **m, const u32 *m_len, u32 num, ec_alg_type sig_type,
92               hash_alg_type hash_type, const u8 **adata, const u16 *adata_len,
93 	      verify_batch_scratch_pad *scratch_pad_area, u32 *scratch_pad_area_len);
94 
95 /* Generic signature import and export functions */
96 
97 ATTRIBUTE_WARN_UNUSED_RET int ec_structured_sig_import_from_buf(u8 *sig, u32 siglen,
98 				      const u8 *out_buf, u32 outlen,
99 				      ec_alg_type * sig_type,
100 				      hash_alg_type * hash_type,
101 				      u8 curve_name[MAX_CURVE_NAME_LEN]);
102 
103 ATTRIBUTE_WARN_UNUSED_RET int ec_structured_sig_export_to_buf(const u8 *sig, u32 siglen,
104 				    u8 *out_buf, u32 outlen,
105 				    ec_alg_type sig_type,
106 				    hash_alg_type hash_type,
107 				    const u8
108 				    curve_name[MAX_CURVE_NAME_LEN]);
109 
110 ATTRIBUTE_WARN_UNUSED_RET int ec_verify_bos_coster(verify_batch_scratch_pad *elements,
111 				                   u32 num, bitcnt_t bits);
112 
113 #endif /* __SIG_ALGS_H__ */
114