Embedded SDK
Embedded SDK
lcx_rsa.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 #ifdef HAVE_RSA
27 
28 #ifndef LCX_RSA_H
29 #define LCX_RSA_H
30 
31 #include "lcx_wrappers.h"
32 #include "lcx_hash.h"
33 #include "lcx_sha256.h"
34 #include "lcx_sha512.h"
35 #include <stddef.h>
36 #include <stdint.h>
37 
45 struct cx_rsa_public_key_s {
46  size_t size;
47  uint8_t e[4];
48  uint8_t n[1];
49 };
50 
54 struct cx_rsa_private_key_s {
55  size_t size;
56  uint8_t d[1];
57  uint8_t n[1];
58 };
60 typedef struct cx_rsa_public_key_s cx_rsa_public_key_t;
62 typedef struct cx_rsa_private_key_s cx_rsa_private_key_t;
63 
65 struct cx_rsa_1024_public_key_s {
66  size_t size;
67  uint8_t e[4];
68  uint8_t n[128];
69 };
71 struct cx_rsa_1024_private_key_s {
72  size_t size;
73  uint8_t d[128];
74  uint8_t n[128];
75 };
77 typedef struct cx_rsa_1024_public_key_s cx_rsa_1024_public_key_t;
79 typedef struct cx_rsa_1024_private_key_s cx_rsa_1024_private_key_t;
80 
82 struct cx_rsa_2048_public_key_s {
83  size_t size;
84  uint8_t e[4];
85  uint8_t n[256];
86 };
88 struct cx_rsa_2048_private_key_s {
89  size_t size;
90  uint8_t d[256];
91  uint8_t n[256];
92 };
94 typedef struct cx_rsa_2048_public_key_s cx_rsa_2048_public_key_t;
96 typedef struct cx_rsa_2048_private_key_s cx_rsa_2048_private_key_t;
97 
99 struct cx_rsa_3072_public_key_s {
100  size_t size;
101  uint8_t e[4];
102  uint8_t n[384];
103 };
105 struct cx_rsa_3072_private_key_s {
106  size_t size;
107  uint8_t d[384];
108  uint8_t n[384];
109 };
111 typedef struct cx_rsa_3072_public_key_s cx_rsa_3072_public_key_t;
113 typedef struct cx_rsa_3072_private_key_s cx_rsa_3072_private_key_t;
114 
116 struct cx_rsa_4096_public_key_s {
117  size_t size;
118  uint8_t e[4];
119  uint8_t n[512];
120 };
122 struct cx_rsa_4096_private_key_s {
123  size_t size;
124  uint8_t d[512];
125  uint8_t n[512];
126 };
128 typedef struct cx_rsa_4096_public_key_s cx_rsa_4096_public_key_t;
130 typedef struct cx_rsa_4096_private_key_s cx_rsa_4096_private_key_t;
131 
155 WARN_UNUSED_RESULT cx_err_t cx_rsa_init_public_key_no_throw(const uint8_t *exponent,
156  size_t exponent_len,
157  const uint8_t *modulus,
158  size_t modulus_len,
159  cx_rsa_public_key_t *key);
160 
165 DEPRECATED static inline int cx_rsa_init_public_key(const unsigned char *exponent,
166  unsigned int exponent_len,
167  const unsigned char *modulus,
168  unsigned int modulus_len,
169  cx_rsa_public_key_t *key)
170 {
171  CX_THROW(cx_rsa_init_public_key_no_throw(exponent, exponent_len, modulus, modulus_len, key));
172  return modulus_len;
173 }
174 
198 WARN_UNUSED_RESULT cx_err_t cx_rsa_init_private_key_no_throw(const uint8_t *exponent,
199  size_t exponent_len,
200  const uint8_t *modulus,
201  size_t modulus_len,
202  cx_rsa_private_key_t *key);
203 
208 DEPRECATED static inline int cx_rsa_init_private_key(const unsigned char *exponent,
209  unsigned int exponent_len,
210  const unsigned char *modulus,
211  unsigned int modulus_len,
212  cx_rsa_private_key_t *key)
213 {
214  CX_THROW(cx_rsa_init_private_key_no_throw(exponent, exponent_len, modulus, modulus_len, key));
215  return modulus_len;
216 }
217 
252 WARN_UNUSED_RESULT cx_err_t cx_rsa_generate_pair_no_throw(size_t modulus_len,
253  cx_rsa_public_key_t *public_key,
254  cx_rsa_private_key_t *private_key,
255  const uint8_t *pub_exponent,
256  size_t exponent_len,
257  const uint8_t *externalPQ);
258 
263 DEPRECATED static inline int cx_rsa_generate_pair(unsigned int modulus_len,
264  cx_rsa_public_key_t *public_key,
265  cx_rsa_private_key_t *private_key,
266  const unsigned char *pub_exponent,
267  unsigned int exponent_len,
268  const unsigned char *externalPQ)
269 {
270  CX_THROW(cx_rsa_generate_pair_no_throw(
271  modulus_len, public_key, private_key, pub_exponent, exponent_len, externalPQ));
272  return modulus_len;
273 }
274 
314 WARN_UNUSED_RESULT cx_err_t cx_rsa_sign_with_salt_len(const cx_rsa_private_key_t *key,
315  uint32_t mode,
316  cx_md_t hashID,
317  const uint8_t *hash,
318  size_t hash_len,
319  uint8_t *sig,
320  size_t sig_len,
321  size_t salt_len);
322 
361 WARN_UNUSED_RESULT cx_err_t cx_rsa_sign_no_throw(const cx_rsa_private_key_t *key,
362  uint32_t mode,
363  cx_md_t hashID,
364  const uint8_t *hash,
365  size_t hash_len,
366  uint8_t *sig,
367  size_t sig_len);
368 
373 DEPRECATED static inline int cx_rsa_sign(const cx_rsa_private_key_t *key,
374  int mode,
375  cx_md_t hashID,
376  const unsigned char *hash,
377  unsigned int hash_len,
378  unsigned char *sig,
379  unsigned int sig_len)
380 {
381  CX_THROW(cx_rsa_sign_no_throw(key, mode, hashID, hash, hash_len, sig, sig_len));
382  return key->size;
383 }
384 
418 bool cx_rsa_verify_with_salt_len(const cx_rsa_public_key_t *key,
419  uint32_t mode,
420  cx_md_t hashID,
421  const uint8_t *hash,
422  size_t hash_len,
423  uint8_t *sig,
424  size_t sig_len,
425  size_t salt_len);
426 
462 bool cx_rsa_verify(const cx_rsa_public_key_t *key,
463  uint32_t mode,
464  cx_md_t hashID,
465  const uint8_t *hash,
466  size_t hash_len,
467  uint8_t *sig,
468  size_t sig_len);
469 
502 WARN_UNUSED_RESULT cx_err_t cx_rsa_encrypt_no_throw(const cx_rsa_public_key_t *key,
503  uint32_t mode,
504  cx_md_t hashID,
505  const uint8_t *mesg,
506  size_t mesg_len,
507  uint8_t *enc,
508  size_t enc_len);
509 
514 DEPRECATED static inline int cx_rsa_encrypt(const cx_rsa_public_key_t *key,
515  int mode,
516  cx_md_t hashID,
517  const unsigned char *mesg,
518  unsigned int mesg_len,
519  unsigned char *enc,
520  unsigned int enc_len)
521 {
522  CX_THROW(cx_rsa_encrypt_no_throw(key, mode, hashID, mesg, mesg_len, enc, enc_len));
523  return key->size;
524 }
525 
558 WARN_UNUSED_RESULT cx_err_t cx_rsa_decrypt_no_throw(const cx_rsa_private_key_t *key,
559  uint32_t mode,
560  cx_md_t hashID,
561  const uint8_t *mesg,
562  size_t mesg_len,
563  uint8_t *dec,
564  size_t *dec_len);
565 
570 DEPRECATED static inline int cx_rsa_decrypt(const cx_rsa_private_key_t *key,
571  int mode,
572  cx_md_t hashID,
573  const unsigned char *mesg,
574  unsigned int mesg_len,
575  unsigned char *dec,
576  unsigned int dec_len)
577 {
578  size_t dec_len_ = dec_len;
579  CX_THROW(cx_rsa_decrypt_no_throw(key, mode, hashID, mesg, mesg_len, dec, &dec_len_));
580  return dec_len_;
581 }
582 
583 #endif
584 
585 #endif // HAVE_RSA
Hash functions.
SHA-2 (Secure Hash Algorithm 2)
SHA-2 (Secure Hash Algorithm 2)
#define CX_THROW(call)
Definition: lcx_wrappers.h:15
unsigned char uint8_t
Definition: usbd_conf.h:53