Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
lcx_ecdsa.h
Go to the documentation of this file.
1
2/*******************************************************************************
3 * Ledger Nano S - Secure firmware
4 * (c) 2022 Ledger
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 ********************************************************************************/
18
28#ifndef LCX_ECDSA_H
29#define LCX_ECDSA_H
30
31#ifdef HAVE_ECDSA
32
33#include "lcx_wrappers.h"
34#include "lcx_ecfp.h"
35
36// DER SIGNATURE FORMAT
37// TAG(0x30) | TOT_LEN | TAG(0x02) | R_LEN | R | TAG(0x02) | S_LEN | S
38// Min sig length (no padding): 1 + 1 + 2 * (1 + 1 + 32)
39#define CX_ECDSA_SHA256_SIG_MIN_ASN1_LENGTH (70U)
40
41// Max sig length (padding) : 1 + 1 + 2 * (1 + 1 + 33)
42#define CX_ECDSA_SHA256_SIG_MAX_ASN1_LENGTH (72U)
43
45#define cx_ecdsa_init_public_key cx_ecfp_init_public_key_no_throw
47#define cx_ecdsa_init_private_key cx_ecfp_init_private_key_no_throw
48
91WARN_UNUSED_RESULT cx_err_t cx_ecdsa_sign_no_throw(const cx_ecfp_private_key_t *pvkey,
92 uint32_t mode,
93 cx_md_t hashID,
94 const uint8_t *hash,
95 size_t hash_len,
96 uint8_t *sig,
97 size_t *sig_len,
98 uint32_t *info);
99
104DEPRECATED static inline size_t cx_ecdsa_sign(const cx_ecfp_private_key_t *pvkey,
105 uint32_t mode,
106 cx_md_t hashID,
107 const unsigned char *hash,
108 unsigned int hash_len,
109 unsigned char *sig,
110 unsigned int sig_len,
111 unsigned int *info)
112{
113 size_t sig_len_ = sig_len;
114 uint32_t info_;
115 CX_THROW(cx_ecdsa_sign_no_throw(pvkey, mode, hashID, hash, hash_len, sig, &sig_len_, &info_));
116 if (info) {
117 *info = (uint32_t) info_;
118 }
119 return sig_len_;
120}
121
163WARN_UNUSED_RESULT cx_err_t cx_ecdsa_sign_rs_no_throw(const cx_ecfp_private_key_t *key,
164 uint32_t mode,
165 cx_md_t hashID,
166 const uint8_t *hash,
167 size_t hash_len,
168 size_t rs_len,
169 uint8_t *sig_r,
170 uint8_t *sig_s,
171 uint32_t *info);
172
191WARN_UNUSED_RESULT bool cx_ecdsa_verify_no_throw(const cx_ecfp_public_key_t *pukey,
192 const uint8_t *hash,
193 size_t hash_len,
194 const uint8_t *sig,
195 size_t sig_len);
196
201DEPRECATED static inline bool cx_ecdsa_verify(const cx_ecfp_public_key_t *pukey,
202 int mode,
203 cx_md_t hashID,
204 const unsigned char *hash,
205 unsigned int hash_len,
206 const unsigned char *sig,
207 unsigned int sig_len)
208{
209 UNUSED(mode);
210 UNUSED(hashID);
211 return cx_ecdsa_verify_no_throw(pukey, hash, hash_len, sig, sig_len);
212}
213
214#endif // HAVE_ECDSA
215
216#endif // LCX_ECDSA_H
Key pair generation based on elliptic curves.
#define CX_THROW(call)