comparison libtomcrypt/src/pk/rsa/rsa_set.c @ 1471:6dba84798cd5

Update to libtomcrypt 1.18.1, merged with Dropbear changes
author Matt Johnston <matt@ucc.asn.au>
date Fri, 09 Feb 2018 21:44:05 +0800
parents
children
comparison
equal deleted inserted replaced
1470:8bba51a55704 1471:6dba84798cd5
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2 *
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
5 *
6 * The library is free for all purposes without any express
7 * guarantee it works.
8 */
9 #include "tomcrypt.h"
10
11
12 #ifdef LTC_MRSA
13
14 /**
15 Import RSA key from raw numbers
16
17 @param N RSA's N
18 @param Nlen RSA's N's length
19 @param e RSA's e
20 @param elen RSA's e's length
21 @param d RSA's d (only private key, NULL for public key)
22 @param dlen RSA's d's length
23 @param key [out] the destination for the imported key
24 @return CRYPT_OK if successful
25 */
26 int rsa_set_key(const unsigned char *N, unsigned long Nlen,
27 const unsigned char *e, unsigned long elen,
28 const unsigned char *d, unsigned long dlen,
29 rsa_key *key)
30 {
31 int err;
32
33 LTC_ARGCHK(key != NULL);
34 LTC_ARGCHK(N != NULL);
35 LTC_ARGCHK(e != NULL);
36 LTC_ARGCHK(ltc_mp.name != NULL);
37
38 err = mp_init_multi(&key->e, &key->d, &key->N, &key->dQ, &key->dP, &key->qP, &key->p, &key->q, NULL);
39 if (err != CRYPT_OK) return err;
40
41 if ((err = mp_read_unsigned_bin(key->N , (unsigned char *)N , Nlen)) != CRYPT_OK) { goto LBL_ERR; }
42 if ((err = mp_read_unsigned_bin(key->e , (unsigned char *)e , elen)) != CRYPT_OK) { goto LBL_ERR; }
43 if (d && dlen) {
44 if ((err = mp_read_unsigned_bin(key->d , (unsigned char *)d , dlen)) != CRYPT_OK) { goto LBL_ERR; }
45 key->type = PK_PRIVATE;
46 }
47 else {
48 key->type = PK_PUBLIC;
49 }
50 return CRYPT_OK;
51
52 LBL_ERR:
53 rsa_free(key);
54 return err;
55 }
56
57 /**
58 Import factors of an RSA key from raw numbers
59
60 Only for private keys.
61
62 @param p RSA's p
63 @param plen RSA's p's length
64 @param q RSA's q
65 @param qlen RSA's q's length
66 @param key [out] the destination for the imported key
67 @return CRYPT_OK if successful
68 */
69 int rsa_set_factors(const unsigned char *p, unsigned long plen,
70 const unsigned char *q, unsigned long qlen,
71 rsa_key *key)
72 {
73 int err;
74
75 LTC_ARGCHK(key != NULL);
76 LTC_ARGCHK(p != NULL);
77 LTC_ARGCHK(q != NULL);
78 LTC_ARGCHK(ltc_mp.name != NULL);
79
80 if (key->type != PK_PRIVATE) return CRYPT_PK_TYPE_MISMATCH;
81
82 if ((err = mp_read_unsigned_bin(key->p , (unsigned char *)p , plen)) != CRYPT_OK) { goto LBL_ERR; }
83 if ((err = mp_read_unsigned_bin(key->q , (unsigned char *)q , qlen)) != CRYPT_OK) { goto LBL_ERR; }
84 return CRYPT_OK;
85
86 LBL_ERR:
87 rsa_free(key);
88 return err;
89 }
90
91 /**
92 Import CRT parameters of an RSA key from raw numbers
93
94 Only for private keys.
95
96 @param dP RSA's dP
97 @param dPlen RSA's dP's length
98 @param dQ RSA's dQ
99 @param dQlen RSA's dQ's length
100 @param qP RSA's qP
101 @param qPlen RSA's qP's length
102 @param key [out] the destination for the imported key
103 @return CRYPT_OK if successful
104 */
105 int rsa_set_crt_params(const unsigned char *dP, unsigned long dPlen,
106 const unsigned char *dQ, unsigned long dQlen,
107 const unsigned char *qP, unsigned long qPlen,
108 rsa_key *key)
109 {
110 int err;
111
112 LTC_ARGCHK(key != NULL);
113 LTC_ARGCHK(dP != NULL);
114 LTC_ARGCHK(dQ != NULL);
115 LTC_ARGCHK(qP != NULL);
116 LTC_ARGCHK(ltc_mp.name != NULL);
117
118 if (key->type != PK_PRIVATE) return CRYPT_PK_TYPE_MISMATCH;
119
120 if ((err = mp_read_unsigned_bin(key->dP, (unsigned char *)dP, dPlen)) != CRYPT_OK) { goto LBL_ERR; }
121 if ((err = mp_read_unsigned_bin(key->dQ, (unsigned char *)dQ, dQlen)) != CRYPT_OK) { goto LBL_ERR; }
122 if ((err = mp_read_unsigned_bin(key->qP, (unsigned char *)qP, qPlen)) != CRYPT_OK) { goto LBL_ERR; }
123 return CRYPT_OK;
124
125 LBL_ERR:
126 rsa_free(key);
127 return err;
128 }
129
130 #endif /* LTC_MRSA */
131
132 /* ref: $Format:%D$ */
133 /* git commit: $Format:%H$ */
134 /* commit time: $Format:%ai$ */