Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
lcx_ecfp.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
26#ifndef LCX_ECFP_H
27#define LCX_ECFP_H
28
29#ifdef HAVE_ECC
30
31#include "lcx_wrappers.h"
32#include "lcx_hash.h"
33#include "ox_ec.h"
34#include <stdbool.h>
35
37struct cx_ecfp_public_key_s {
38 cx_curve_t curve;
39 size_t W_len;
40 uint8_t W[1];
41};
42
44struct cx_ecfp_private_key_s {
45 cx_curve_t curve;
46 size_t d_len;
47 uint8_t d[1];
48};
49
51struct cx_ecfp_256_public_key_s {
52 cx_curve_t curve;
53 size_t W_len;
54 uint8_t W[65];
55};
57struct cx_ecfp_256_private_key_s {
58 cx_curve_t curve;
59 size_t d_len;
60 uint8_t d[32];
61};
63struct cx_ecfp_256_extended_private_key_s {
64 cx_curve_t curve;
65 size_t d_len;
66 uint8_t d[64];
67};
69typedef struct cx_ecfp_256_public_key_s cx_ecfp_256_public_key_t;
71typedef struct cx_ecfp_256_private_key_s cx_ecfp_256_private_key_t;
73typedef struct cx_ecfp_256_extended_private_key_s cx_ecfp_256_extended_private_key_t;
74
75/* Do not use those types anymore for declaration, they will become abstract */
76typedef struct cx_ecfp_256_public_key_s cx_ecfp_public_key_t;
77typedef struct cx_ecfp_256_private_key_s cx_ecfp_private_key_t;
78
80struct cx_ecfp_384_public_key_s {
81 cx_curve_t curve;
82 size_t W_len;
83 uint8_t W[97];
84};
86struct cx_ecfp_384_private_key_s {
87 cx_curve_t curve;
88 size_t d_len;
89 uint8_t d[48];
90};
92typedef struct cx_ecfp_384_private_key_s cx_ecfp_384_private_key_t;
94typedef struct cx_ecfp_384_public_key_s cx_ecfp_384_public_key_t;
95
97struct cx_ecfp_512_public_key_s {
98 cx_curve_t curve;
99 size_t W_len;
100 uint8_t W[129];
101};
103struct cx_ecfp_512_private_key_s {
104 cx_curve_t curve;
105 size_t d_len;
106 uint8_t d[64];
107};
109struct cx_ecfp_512_extented_private_key_s {
110 cx_curve_t curve;
111 size_t d_len;
112 uint8_t d[128];
113};
115typedef struct cx_ecfp_512_public_key_s cx_ecfp_512_public_key_t;
117typedef struct cx_ecfp_512_private_key_s cx_ecfp_512_private_key_t;
119typedef struct cx_ecfp_512_extented_private_key_s cx_ecfp_512_extented_private_key_t;
120
122struct cx_ecfp_640_public_key_s {
123 cx_curve_t curve;
124 size_t W_len;
125 uint8_t W[161];
126};
128struct cx_ecfp_640_private_key_s {
129 cx_curve_t curve;
130 size_t d_len;
131 uint8_t d[80];
132};
134typedef struct cx_ecfp_640_public_key_s cx_ecfp_640_public_key_t;
136typedef struct cx_ecfp_640_private_key_s cx_ecfp_640_private_key_t;
137
163WARN_UNUSED_RESULT cx_err_t cx_ecfp_add_point_no_throw(cx_curve_t curve,
164 uint8_t *R,
165 const uint8_t *P,
166 const uint8_t *Q);
167
172DEPRECATED static inline size_t cx_ecfp_add_point(cx_curve_t curve,
173 unsigned char *R,
174 const unsigned char *P,
175 const unsigned char *Q,
176 unsigned int X_len)
177{
178 UNUSED(X_len);
179
180 CX_THROW(cx_ecfp_add_point_no_throw(curve, R, P, Q));
181
182 size_t size;
183 CX_THROW(cx_ecdomain_parameters_length(curve, &size));
184
185 return 1 + 2 * size;
186}
187
213WARN_UNUSED_RESULT cx_err_t cx_ecfp_scalar_mult_no_throw(cx_curve_t curve,
214 uint8_t *P,
215 const uint8_t *k,
216 size_t k_len);
217
222DEPRECATED static inline size_t cx_ecfp_scalar_mult(cx_curve_t curve,
223 unsigned char *P,
224 unsigned int P_len,
225 const unsigned char *k,
226 unsigned int k_len)
227{
228 UNUSED(P_len);
229
230 CX_THROW(cx_ecfp_scalar_mult_no_throw(curve, P, k, k_len));
231
232 size_t size;
233 CX_THROW(cx_ecdomain_parameters_length(curve, &size));
234
235 return 1 + 2 * size;
236}
237
261WARN_UNUSED_RESULT cx_err_t cx_ecfp_init_public_key_no_throw(cx_curve_t curve,
262 const uint8_t *rawkey,
263 size_t key_len,
264 cx_ecfp_public_key_t *key);
265
270DEPRECATED static inline size_t cx_ecfp_init_public_key(cx_curve_t curve,
271 const unsigned char *rawkey,
272 unsigned int key_len,
273 cx_ecfp_public_key_t *key)
274{
275 CX_THROW(cx_ecfp_init_public_key_no_throw(curve, rawkey, key_len, key));
276 return key_len;
277}
278
299WARN_UNUSED_RESULT cx_err_t cx_ecfp_init_private_key_no_throw(cx_curve_t curve,
300 const uint8_t *rawkey,
301 size_t key_len,
302 cx_ecfp_private_key_t *pvkey);
303
308DEPRECATED static inline size_t cx_ecfp_init_private_key(cx_curve_t curve,
309 const unsigned char *rawkey,
310 size_t key_len,
311 cx_ecfp_private_key_t *pvkey)
312{
313 CX_THROW(cx_ecfp_init_private_key_no_throw(curve, rawkey, key_len, pvkey));
314 return key_len;
315}
316
342WARN_UNUSED_RESULT cx_err_t cx_ecfp_generate_pair_no_throw(cx_curve_t curve,
343 cx_ecfp_public_key_t *pubkey,
344 cx_ecfp_private_key_t *privkey,
345 bool keepprivate);
346
351DEPRECATED static inline int cx_ecfp_generate_pair(cx_curve_t curve,
352 cx_ecfp_public_key_t *pubkey,
353 cx_ecfp_private_key_t *privkey,
354 int keepprivate)
355{
356 CX_THROW(cx_ecfp_generate_pair_no_throw(curve, pubkey, privkey, keepprivate));
357 return 0;
358}
359
387WARN_UNUSED_RESULT cx_err_t cx_ecfp_generate_pair2_no_throw(cx_curve_t curve,
388 cx_ecfp_public_key_t *pubkey,
389 cx_ecfp_private_key_t *privkey,
390 bool keepprivate,
391 cx_md_t hashID);
392
397DEPRECATED static inline int cx_ecfp_generate_pair2(cx_curve_t curve,
398 cx_ecfp_public_key_t *pubkey,
399 cx_ecfp_private_key_t *privkey,
400 int keepprivate,
401 cx_md_t hashID)
402{
403 CX_THROW(cx_ecfp_generate_pair2_no_throw(curve, pubkey, privkey, keepprivate, hashID));
404 return 0;
405}
406
407#ifdef HAVE_ECC_TWISTED_EDWARDS
408
443WARN_UNUSED_RESULT cx_err_t cx_eddsa_get_public_key_no_throw(const cx_ecfp_private_key_t *pvkey,
444 cx_md_t hashID,
445 cx_ecfp_public_key_t *pukey,
446 uint8_t *a,
447 size_t a_len,
448 uint8_t *h,
449 size_t h_len);
450
455DEPRECATED static inline void cx_eddsa_get_public_key(const cx_ecfp_private_key_t *pvkey,
456 cx_md_t hashID,
457 cx_ecfp_public_key_t *pukey,
458 unsigned char *a,
459 unsigned int a_len,
460 unsigned char *h,
461 unsigned int h_len)
462{
463 CX_THROW(cx_eddsa_get_public_key_no_throw(pvkey, hashID, pukey, a, a_len, h, h_len));
464}
465
488WARN_UNUSED_RESULT cx_err_t cx_edwards_compress_point_no_throw(cx_curve_t curve,
489 uint8_t *p,
490 size_t p_len);
491
496DEPRECATED static inline void cx_edwards_compress_point(cx_curve_t curve, uint8_t *p, size_t p_len)
497{
498 CX_THROW(cx_edwards_compress_point_no_throw(curve, p, p_len));
499}
500
525WARN_UNUSED_RESULT cx_err_t cx_edwards_decompress_point_no_throw(cx_curve_t curve,
526 uint8_t *p,
527 size_t p_len);
528
533DEPRECATED static inline void cx_edwards_decompress_point(cx_curve_t curve,
534 uint8_t *p,
535 size_t p_len)
536{
537 CX_THROW(cx_edwards_decompress_point_no_throw(curve, p, p_len));
538}
539
544DEPRECATED static inline void cx_edward_compress_point(cx_curve_t curve, uint8_t *p, size_t p_len)
545{
546 CX_THROW(cx_edwards_compress_point_no_throw(curve, p, p_len));
547}
548
553DEPRECATED static inline void cx_edward_decompress_point(cx_curve_t curve, uint8_t *p, size_t p_len)
554{
555 CX_THROW(cx_edwards_decompress_point_no_throw(curve, p, p_len));
556}
557
558#endif // HAVE_ECC_TWISTED_EDWARDS
559
560#endif // HAVE_ECC
561
562#endif // LCX_ECFP_H
Hash functions.
#define CX_THROW(call)
unsigned char uint8_t
Definition usbd_conf.h:53