xref: /freebsd/crypto/libecc/src/sig/decdsa.c (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 #ifdef WITH_SIG_DECDSA
18 
19 #if !defined(WITH_HMAC)
20 #error "DECDSA signature needs HMAC, please activate it!"
21 #endif
22 #include <libecc/hash/hmac.h>
23 
24 #include <libecc/nn/nn_rand.h>
25 #include <libecc/nn/nn_mul_public.h>
26 #include <libecc/nn/nn_logical.h>
27 
28 #include <libecc/sig/sig_algs_internal.h>
29 #include <libecc/sig/ec_key.h>
30 #include <libecc/utils/utils.h>
31 #ifdef VERBOSE_INNER_VALUES
32 #define EC_SIG_ALG "DECDSA"
33 #endif
34 #include <libecc/utils/dbg_sig.h>
35 
decdsa_init_pub_key(ec_pub_key * out_pub,const ec_priv_key * in_priv)36 int decdsa_init_pub_key(ec_pub_key *out_pub, const ec_priv_key *in_priv)
37 {
38 	return __ecdsa_init_pub_key(out_pub, in_priv, DECDSA);
39 }
40 
decdsa_siglen(u16 p_bit_len,u16 q_bit_len,u8 hsize,u8 blocksize,u8 * siglen)41 int decdsa_siglen(u16 p_bit_len, u16 q_bit_len, u8 hsize, u8 blocksize, u8 *siglen)
42 {
43 	return __ecdsa_siglen(p_bit_len, q_bit_len, hsize, blocksize, siglen);
44 }
45 
_decdsa_sign_init(struct ec_sign_context * ctx)46 int _decdsa_sign_init(struct ec_sign_context *ctx)
47 {
48 	int ret;
49 
50 	/* Override our random source with NULL since we want a deterministic
51 	 * generation.
52 	 */
53 	MUST_HAVE((ctx != NULL), ret, err);
54 
55 	ctx->rand = NULL;
56 	ret =  __ecdsa_sign_init(ctx, DECDSA);
57 
58 err:
59 	return ret;
60 }
61 
_decdsa_sign_update(struct ec_sign_context * ctx,const u8 * chunk,u32 chunklen)62 int _decdsa_sign_update(struct ec_sign_context *ctx,
63 		       const u8 *chunk, u32 chunklen)
64 {
65 	int ret;
66 
67 	/* NOTE: for deterministic ECDSA, the random source MUST be NULL, hence
68 	 * the following check.
69 	 */
70 	MUST_HAVE((ctx != NULL) && (ctx->rand == NULL), ret, err);
71 
72 	ret = __ecdsa_sign_update(ctx, chunk, chunklen, DECDSA);
73 
74 err:
75 	return ret;
76 }
77 
_decdsa_sign_finalize(struct ec_sign_context * ctx,u8 * sig,u8 siglen)78 int _decdsa_sign_finalize(struct ec_sign_context *ctx, u8 *sig, u8 siglen)
79 {
80 	int ret;
81 
82 	/* NOTE: for deterministic ECDSA, the random source MUST be NULL, hence
83 	 * the following check.
84 	 */
85 	MUST_HAVE((ctx != NULL) && (ctx->rand == NULL), ret, err);
86 
87 	ret =  __ecdsa_sign_finalize(ctx, sig, siglen, DECDSA);
88 
89 err:
90 	return ret;
91 }
92 
_decdsa_verify_init(struct ec_verify_context * ctx,const u8 * sig,u8 siglen)93 int _decdsa_verify_init(struct ec_verify_context *ctx, const u8 *sig, u8 siglen)
94 {
95 	return __ecdsa_verify_init(ctx, sig, siglen, DECDSA);
96 }
97 
_decdsa_verify_update(struct ec_verify_context * ctx,const u8 * chunk,u32 chunklen)98 int _decdsa_verify_update(struct ec_verify_context *ctx,
99 			 const u8 *chunk, u32 chunklen)
100 {
101 	return __ecdsa_verify_update(ctx, chunk, chunklen, DECDSA);
102 }
103 
_decdsa_verify_finalize(struct ec_verify_context * ctx)104 int _decdsa_verify_finalize(struct ec_verify_context *ctx)
105 {
106 	return __ecdsa_verify_finalize(ctx, DECDSA);
107 }
108 
decdsa_public_key_from_sig(ec_pub_key * out_pub1,ec_pub_key * out_pub2,const ec_params * params,const u8 * sig,u8 siglen,const u8 * hash,u8 hsize)109 int decdsa_public_key_from_sig(ec_pub_key *out_pub1, ec_pub_key *out_pub2, const ec_params *params,
110                               const u8 *sig, u8 siglen, const u8 *hash, u8 hsize)
111 {
112 	return __ecdsa_public_key_from_sig(out_pub1, out_pub2, params, sig, siglen, hash, hsize, DECDSA);
113 }
114 
115 #else /* WITH_SIG_DECDSA */
116 
117 /*
118  * Dummy definition to avoid the empty translation unit ISO C warning
119  */
120 typedef int dummy;
121 #endif /* WITH_SIG_DECDSA */
122