xref: /freebsd/crypto/libecc/include/libecc/sig/ecsdsa.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 #ifdef WITH_SIG_ECSDSA
19 
20 #ifndef __ECSDSA_H__
21 #define __ECSDSA_H__
22 #include <libecc/words/words.h>
23 #include <libecc/sig/ec_key.h>
24 #include <libecc/utils/utils.h>
25 #include <libecc/hash/hash_algs.h>
26 #include <libecc/curves/curves.h>
27 
28 #define ECSDSA_R_LEN(hsize) (hsize)
29 #define ECSDSA_S_LEN(q_bit_len) (BYTECEIL(q_bit_len))
30 #define ECSDSA_SIGLEN(hsize, q_bit_len) (ECSDSA_R_LEN(hsize) + \
31 					 ECSDSA_S_LEN(q_bit_len))
32 #define ECSDSA_MAX_SIGLEN ECSDSA_SIGLEN(MAX_DIGEST_SIZE, CURVES_MAX_Q_BIT_LEN)
33 
34 /*
35  * Compute max signature length for all the mechanisms enabled
36  * in the library (see lib_ecc_config.h). Having that done during
37  * preprocessing sadly requires some verbosity.
38  */
39 #ifndef EC_MAX_SIGLEN
40 #define EC_MAX_SIGLEN 0
41 #endif
42 #if ((EC_MAX_SIGLEN) < (ECSDSA_MAX_SIGLEN))
43 #undef EC_MAX_SIGLEN
44 #define EC_MAX_SIGLEN ECSDSA_MAX_SIGLEN
45 #endif
46 
47 ATTRIBUTE_WARN_UNUSED_RET int ecsdsa_init_pub_key(ec_pub_key *out_pub, const ec_priv_key *in_priv);
48 
49 ATTRIBUTE_WARN_UNUSED_RET int ecsdsa_siglen(u16 p_bit_len, u16 q_bit_len, u8 hsize, u8 blocksize,
50 		  u8 *siglen);
51 
52 typedef struct {
53 	hash_context h_ctx;
54 	nn k;
55 	word_t magic;
56 } ecsdsa_sign_data;
57 
58 ATTRIBUTE_WARN_UNUSED_RET int _ecsdsa_sign_init(struct ec_sign_context *ctx);
59 
60 ATTRIBUTE_WARN_UNUSED_RET int _ecsdsa_sign_update(struct ec_sign_context *ctx,
61 			const u8 *chunk, u32 chunklen);
62 
63 ATTRIBUTE_WARN_UNUSED_RET int _ecsdsa_sign_finalize(struct ec_sign_context *ctx, u8 *sig, u8 siglen);
64 
65 typedef struct {
66 	hash_context h_ctx;
67 	u8 r[MAX_DIGEST_SIZE];
68 	nn s;
69 	word_t magic;
70 } ecsdsa_verify_data;
71 
72 ATTRIBUTE_WARN_UNUSED_RET int _ecsdsa_verify_init(struct ec_verify_context *ctx,
73 			const u8 *sig, u8 siglen);
74 
75 ATTRIBUTE_WARN_UNUSED_RET int _ecsdsa_verify_update(struct ec_verify_context *ctx,
76 			  const u8 *chunk, u32 chunklen);
77 
78 ATTRIBUTE_WARN_UNUSED_RET int _ecsdsa_verify_finalize(struct ec_verify_context *ctx);
79 
80 #endif /* __ECSDSA_H__ */
81 #endif /* WITH_SIG_ECSDSA */
82