Embedded SDK
Embedded SDK
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 
37 struct cx_ecfp_public_key_s {
38  cx_curve_t curve;
39  size_t W_len;
40  uint8_t W[1];
41 };
42 
44 struct cx_ecfp_private_key_s {
45  cx_curve_t curve;
46  size_t d_len;
47  uint8_t d[1];
48 };
49 
51 struct cx_ecfp_256_public_key_s {
52  cx_curve_t curve;
53  size_t W_len;
54  uint8_t W[65];
55 };
57 struct cx_ecfp_256_private_key_s {
58  cx_curve_t curve;
59  size_t d_len;
60  uint8_t d[32];
61 };
63 struct cx_ecfp_256_extended_private_key_s {
64  cx_curve_t curve;
65  size_t d_len;
66  uint8_t d[64];
67 };
69 typedef struct cx_ecfp_256_public_key_s cx_ecfp_256_public_key_t;
71 typedef struct cx_ecfp_256_private_key_s cx_ecfp_256_private_key_t;
73 typedef 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 */
76 typedef struct cx_ecfp_256_public_key_s cx_ecfp_public_key_t;
77 typedef struct cx_ecfp_256_private_key_s cx_ecfp_private_key_t;
78 
80 struct cx_ecfp_384_public_key_s {
81  cx_curve_t curve;
82  size_t W_len;
83  uint8_t W[97];
84 };
86 struct cx_ecfp_384_private_key_s {
87  cx_curve_t curve;
88  size_t d_len;
89  uint8_t d[48];
90 };
92 typedef struct cx_ecfp_384_private_key_s cx_ecfp_384_private_key_t;
94 typedef struct cx_ecfp_384_public_key_s cx_ecfp_384_public_key_t;
95 
97 struct cx_ecfp_512_public_key_s {
98  cx_curve_t curve;
99  size_t W_len;
100  uint8_t W[129];
101 };
103 struct cx_ecfp_512_private_key_s {
104  cx_curve_t curve;
105  size_t d_len;
106  uint8_t d[64];
107 };
109 struct cx_ecfp_512_extented_private_key_s {
110  cx_curve_t curve;
111  size_t d_len;
112  uint8_t d[128];
113 };
115 typedef struct cx_ecfp_512_public_key_s cx_ecfp_512_public_key_t;
117 typedef struct cx_ecfp_512_private_key_s cx_ecfp_512_private_key_t;
119 typedef struct cx_ecfp_512_extented_private_key_s cx_ecfp_512_extented_private_key_t;
120 
122 struct cx_ecfp_640_public_key_s {
123  cx_curve_t curve;
124  size_t W_len;
125  uint8_t W[161];
126 };
128 struct cx_ecfp_640_private_key_s {
129  cx_curve_t curve;
130  size_t d_len;
131  uint8_t d[80];
132 };
134 typedef struct cx_ecfp_640_public_key_s cx_ecfp_640_public_key_t;
136 typedef struct cx_ecfp_640_private_key_s cx_ecfp_640_private_key_t;
137 
163 WARN_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 
172 DEPRECATED 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 
213 WARN_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 
222 DEPRECATED 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 
261 WARN_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 
270 DEPRECATED 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 
299 WARN_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 
308 DEPRECATED 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 
342 WARN_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 
351 DEPRECATED 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 
387 WARN_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 
397 DEPRECATED 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 
443 WARN_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 
455 DEPRECATED 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 
488 WARN_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 
496 DEPRECATED 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 
525 WARN_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 
533 DEPRECATED 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 
544 DEPRECATED 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 
553 DEPRECATED 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)
Definition: lcx_wrappers.h:15
unsigned char uint8_t
Definition: usbd_conf.h:53