xref: /freebsd/crypto/libecc/include/libecc/sig/ecdsa_common.h (revision f0865ec9906d5a18fa2a3b61381f22ce16e606ad)
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 #include <libecc/lib_ecc_config.h>
17 #include <libecc/lib_ecc_types.h>
18 #if defined(WITH_SIG_ECDSA) || defined(WITH_SIG_DECDSA)
19 
20 #ifndef __ECDSA_COMMON_H__
21 #define __ECDSA_COMMON_H__
22 
23 #include <libecc/words/words.h>
24 #include <libecc/sig/ec_key.h>
25 #include <libecc/hash/hash_algs.h>
26 #include <libecc/curves/curves.h>
27 #include <libecc/utils/utils.h>
28 
29 #define ECDSA_R_LEN(q_bit_len)  (BYTECEIL(q_bit_len))
30 #define ECDSA_S_LEN(q_bit_len)  (BYTECEIL(q_bit_len))
31 #define ECDSA_SIGLEN(q_bit_len) (ECDSA_R_LEN(q_bit_len) + \
32 				 ECDSA_S_LEN(q_bit_len))
33 #define ECDSA_MAX_SIGLEN ECDSA_SIGLEN(CURVES_MAX_Q_BIT_LEN)
34 
35 /*
36  * Compute max signature length for all the mechanisms enabled
37  * in the library (see lib_ecc_config.h). Having that done during
38  * preprocessing sadly requires some verbosity.
39  */
40 #ifndef EC_MAX_SIGLEN
41 #define EC_MAX_SIGLEN 0
42 #endif
43 #if ((EC_MAX_SIGLEN) < (ECDSA_MAX_SIGLEN))
44 #undef EC_MAX_SIGLEN
45 #define EC_MAX_SIGLEN ECDSA_MAX_SIGLEN
46 #endif
47 
48 typedef struct {
49 	hash_context h_ctx;
50 	word_t magic;
51 } ecdsa_sign_data;
52 
53 struct ec_sign_context;
54 
55 ATTRIBUTE_WARN_UNUSED_RET int __ecdsa_init_pub_key(ec_pub_key *out_pub, const ec_priv_key *in_priv, ec_alg_type key_type);
56 
57 ATTRIBUTE_WARN_UNUSED_RET int __ecdsa_siglen(u16 p_bit_len, u16 q_bit_len, u8 hsize, u8 blocksize, u8 *siglen);
58 
59 ATTRIBUTE_WARN_UNUSED_RET int __ecdsa_sign_init(struct ec_sign_context *ctx, ec_alg_type key_type);
60 
61 ATTRIBUTE_WARN_UNUSED_RET int __ecdsa_sign_update(struct ec_sign_context *ctx,
62 		       const u8 *chunk, u32 chunklen, ec_alg_type key_type);
63 
64 ATTRIBUTE_WARN_UNUSED_RET int __ecdsa_sign_finalize(struct ec_sign_context *ctx, u8 *sig, u8 siglen, ec_alg_type key_type);
65 
66 typedef struct {
67 	nn r;
68 	nn s;
69 	hash_context h_ctx;
70 	word_t magic;
71 } ecdsa_verify_data;
72 
73 struct ec_verify_context;
74 
75 ATTRIBUTE_WARN_UNUSED_RET int __ecdsa_verify_init(struct ec_verify_context *ctx,
76 		       const u8 *sig, u8 siglen, ec_alg_type key_type);
77 
78 ATTRIBUTE_WARN_UNUSED_RET int __ecdsa_verify_update(struct ec_verify_context *ctx,
79 			 const u8 *chunk, u32 chunklen, ec_alg_type key_type);
80 
81 ATTRIBUTE_WARN_UNUSED_RET int __ecdsa_verify_finalize(struct ec_verify_context *ctx, ec_alg_type key_type);
82 
83 ATTRIBUTE_WARN_UNUSED_RET int __ecdsa_public_key_from_sig(ec_pub_key *out_pub1, ec_pub_key *out_pub2, const ec_params *params,
84                                 const u8 *sig, u8 siglen, const u8 *hash, u8 hsize,
85                                 ec_alg_type key_type);
86 
87 #endif /* __ECDSA_COMMON_H__ */
88 #endif /* defined(WITH_SIG_ECDSA) || defined(WITH_SIG_DECDSA) */
89