1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4 
5 
6 #ifdef LTC_MRSA
7 
8 /**
9   Import RSA key from raw numbers
10 
11   @param N       RSA's N
12   @param Nlen    RSA's N's length
13   @param e       RSA's e
14   @param elen    RSA's e's length
15   @param d       RSA's d  (only private key, NULL for public key)
16   @param dlen    RSA's d's length
17   @param key     [out] the destination for the imported key
18   @return CRYPT_OK if successful
19 */
rsa_set_key(const unsigned char * N,unsigned long Nlen,const unsigned char * e,unsigned long elen,const unsigned char * d,unsigned long dlen,rsa_key * key)20 int rsa_set_key(const unsigned char *N,  unsigned long Nlen,
21                 const unsigned char *e,  unsigned long elen,
22                 const unsigned char *d,  unsigned long dlen,
23                 rsa_key *key)
24 {
25    int err;
26 
27    LTC_ARGCHK(key         != NULL);
28    LTC_ARGCHK(N           != NULL);
29    LTC_ARGCHK(e           != NULL);
30    LTC_ARGCHK(ltc_mp.name != NULL);
31 
32    if ((err = rsa_init(key)) != CRYPT_OK) return err;
33 
34    if ((err = mp_read_unsigned_bin(key->N , (unsigned char *)N , Nlen)) != CRYPT_OK)    { goto LBL_ERR; }
35    if ((err = mp_read_unsigned_bin(key->e , (unsigned char *)e , elen)) != CRYPT_OK)    { goto LBL_ERR; }
36    if (d && dlen) {
37       if ((err = mp_read_unsigned_bin(key->d , (unsigned char *)d , dlen)) != CRYPT_OK) { goto LBL_ERR; }
38       key->type = PK_PRIVATE;
39    }
40    else {
41       key->type = PK_PUBLIC;
42    }
43    return CRYPT_OK;
44 
45 LBL_ERR:
46    rsa_free(key);
47    return err;
48 }
49 
50 /**
51   Import factors of an RSA key from raw numbers
52 
53   Only for private keys.
54 
55   @param p       RSA's p
56   @param plen    RSA's p's length
57   @param q       RSA's q
58   @param qlen    RSA's q's length
59   @param key     [out] the destination for the imported key
60   @return CRYPT_OK if successful
61 */
rsa_set_factors(const unsigned char * p,unsigned long plen,const unsigned char * q,unsigned long qlen,rsa_key * key)62 int rsa_set_factors(const unsigned char *p,  unsigned long plen,
63                     const unsigned char *q,  unsigned long qlen,
64                     rsa_key *key)
65 {
66    int err;
67 
68    LTC_ARGCHK(key         != NULL);
69    LTC_ARGCHK(p           != NULL);
70    LTC_ARGCHK(q           != NULL);
71    LTC_ARGCHK(ltc_mp.name != NULL);
72 
73    if (key->type != PK_PRIVATE) return CRYPT_PK_TYPE_MISMATCH;
74 
75    if ((err = mp_read_unsigned_bin(key->p , (unsigned char *)p , plen)) != CRYPT_OK) { goto LBL_ERR; }
76    if ((err = mp_read_unsigned_bin(key->q , (unsigned char *)q , qlen)) != CRYPT_OK) { goto LBL_ERR; }
77    return CRYPT_OK;
78 
79 LBL_ERR:
80    rsa_free(key);
81    return err;
82 }
83 
84 /**
85   Import CRT parameters of an RSA key from raw numbers
86 
87   Only for private keys.
88 
89   @param dP      RSA's dP
90   @param dPlen   RSA's dP's length
91   @param dQ      RSA's dQ
92   @param dQlen   RSA's dQ's length
93   @param qP      RSA's qP
94   @param qPlen   RSA's qP's length
95   @param key     [out] the destination for the imported key
96   @return CRYPT_OK if successful
97 */
rsa_set_crt_params(const unsigned char * dP,unsigned long dPlen,const unsigned char * dQ,unsigned long dQlen,const unsigned char * qP,unsigned long qPlen,rsa_key * key)98 int rsa_set_crt_params(const unsigned char *dP, unsigned long dPlen,
99                        const unsigned char *dQ, unsigned long dQlen,
100                        const unsigned char *qP, unsigned long qPlen,
101                        rsa_key *key)
102 {
103    int err;
104 
105    LTC_ARGCHK(key         != NULL);
106    LTC_ARGCHK(dP          != NULL);
107    LTC_ARGCHK(dQ          != NULL);
108    LTC_ARGCHK(qP          != NULL);
109    LTC_ARGCHK(ltc_mp.name != NULL);
110 
111    if (key->type != PK_PRIVATE) return CRYPT_PK_TYPE_MISMATCH;
112 
113    if ((err = mp_read_unsigned_bin(key->dP, (unsigned char *)dP, dPlen)) != CRYPT_OK) { goto LBL_ERR; }
114    if ((err = mp_read_unsigned_bin(key->dQ, (unsigned char *)dQ, dQlen)) != CRYPT_OK) { goto LBL_ERR; }
115    if ((err = mp_read_unsigned_bin(key->qP, (unsigned char *)qP, qPlen)) != CRYPT_OK) { goto LBL_ERR; }
116    return CRYPT_OK;
117 
118 LBL_ERR:
119    rsa_free(key);
120    return err;
121 }
122 
123 #endif /* LTC_MRSA */
124