3 * RSA public key cryptographic functions
5 * Copyright 2004 Michael Jung
6 * Based on public domain code by Tom St Denis (tomstdenis@iahu.ca)
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * This file contains code from the LibTomCrypt cryptographic
25 * library written by Tom St Denis (tomstdenis@iahu.ca). LibTomCrypt
26 * is in the public domain. The code in this file is tailored to
27 * special requirements. Take a look at http://libtomcrypt.org for the
35 int mpi_code
, ltc_code
;
36 } mpi_to_ltc_codes
[] = {
37 { MP_OKAY
, CRYPT_OK
},
38 { MP_MEM
, CRYPT_MEM
},
39 { MP_VAL
, CRYPT_INVALID_ARG
},
42 /* convert a MPI error to a LTC error (Possibly the most powerful function ever! Oh wait... no) */
43 static int mpi_to_ltc_error(int err
)
47 for (x
= 0; x
< ARRAY_SIZE(mpi_to_ltc_codes
); x
++) {
48 if (err
== mpi_to_ltc_codes
[x
].mpi_code
) {
49 return mpi_to_ltc_codes
[x
].ltc_code
;
55 extern int gen_rand_impl(unsigned char *dst
, unsigned int len
);
57 static int rand_prime_helper(unsigned char *dst
, int len
, void *dat
)
59 return gen_rand_impl(dst
, len
) ? len
: 0;
62 static int rand_prime(mp_int
*N
, long len
)
71 /* This seems to be what MS CSP's do: */
72 type
= LTM_PRIME_2MSB_ON
;
73 /* Original LibTomCrypt: type = 0; */
76 /* allow sizes between 2 and 256 bytes for a prime size */
77 if (len
< 16 || len
> 8192) {
78 printf("Invalid prime size!\n");
79 return CRYPT_INVALID_PRIME_SIZE
;
82 /* New prime generation makes the code even more cryptoish-insane. Do you know what this means!!!
83 -- Gir: Yeah, oh wait, er, no.
85 return mpi_to_ltc_error(mp_prime_random_ex(N
, mp_prime_rabin_miller_trials(len
), len
, type
, rand_prime_helper
, NULL
));
88 int rsa_make_key(int size
, long e
, rsa_key
*key
)
90 mp_int p
, q
, tmp1
, tmp2
, tmp3
;
93 if ((size
< (MIN_RSA_SIZE
/8)) || (size
> (MAX_RSA_SIZE
/8))) {
94 return CRYPT_INVALID_KEYSIZE
;
97 if ((e
< 3) || ((e
& 1) == 0)) {
98 return CRYPT_INVALID_ARG
;
101 if ((err
= mp_init_multi(&p
, &q
, &tmp1
, &tmp2
, &tmp3
, NULL
)) != MP_OKAY
) {
102 return mpi_to_ltc_error(err
);
105 /* make primes p and q (optimization provided by Wayne Scott) */
106 if ((err
= mp_set_int(&tmp3
, e
)) != MP_OKAY
) { goto error
; } /* tmp3 = e */
110 if ((err
= rand_prime(&p
, size
*4)) != CRYPT_OK
) { goto done
; }
111 if ((err
= mp_sub_d(&p
, 1, &tmp1
)) != MP_OKAY
) { goto error
; } /* tmp1 = p-1 */
112 if ((err
= mp_gcd(&tmp1
, &tmp3
, &tmp2
)) != MP_OKAY
) { goto error
; } /* tmp2 = gcd(p-1, e) */
113 } while (mp_cmp_d(&tmp2
, 1) != 0); /* while e divides p-1 */
117 if ((err
= rand_prime(&q
, size
*4)) != CRYPT_OK
) { goto done
; }
118 if ((err
= mp_sub_d(&q
, 1, &tmp1
)) != MP_OKAY
) { goto error
; } /* tmp1 = q-1 */
119 if ((err
= mp_gcd(&tmp1
, &tmp3
, &tmp2
)) != MP_OKAY
) { goto error
; } /* tmp2 = gcd(q-1, e) */
120 } while (mp_cmp_d(&tmp2
, 1) != 0); /* while e divides q-1 */
122 /* tmp1 = lcm(p-1, q-1) */
123 if ((err
= mp_sub_d(&p
, 1, &tmp2
)) != MP_OKAY
) { goto error
; } /* tmp2 = p-1 */
124 /* tmp1 = q-1 (previous do/while loop) */
125 if ((err
= mp_lcm(&tmp1
, &tmp2
, &tmp1
)) != MP_OKAY
) { goto error
; } /* tmp1 = lcm(p-1, q-1) */
128 if ((err
= mp_init_multi(&key
->e
, &key
->d
, &key
->N
, &key
->dQ
, &key
->dP
,
129 &key
->qP
, &key
->p
, &key
->q
, NULL
)) != MP_OKAY
) {
133 if ((err
= mp_set_int(&key
->e
, e
)) != MP_OKAY
) { goto error2
; } /* key->e = e */
134 if ((err
= mp_invmod(&key
->e
, &tmp1
, &key
->d
)) != MP_OKAY
) { goto error2
; } /* key->d = 1/e mod lcm(p-1,q-1) */
135 if ((err
= mp_mul(&p
, &q
, &key
->N
)) != MP_OKAY
) { goto error2
; } /* key->N = pq */
137 /* optimize for CRT now */
138 /* find d mod q-1 and d mod p-1 */
139 if ((err
= mp_sub_d(&p
, 1, &tmp1
)) != MP_OKAY
) { goto error2
; } /* tmp1 = q-1 */
140 if ((err
= mp_sub_d(&q
, 1, &tmp2
)) != MP_OKAY
) { goto error2
; } /* tmp2 = p-1 */
141 if ((err
= mp_mod(&key
->d
, &tmp1
, &key
->dP
)) != MP_OKAY
) { goto error2
; } /* dP = d mod p-1 */
142 if ((err
= mp_mod(&key
->d
, &tmp2
, &key
->dQ
)) != MP_OKAY
) { goto error2
; } /* dQ = d mod q-1 */
143 if ((err
= mp_invmod(&q
, &p
, &key
->qP
)) != MP_OKAY
) { goto error2
; } /* qP = 1/q mod p */
145 if ((err
= mp_copy(&p
, &key
->p
)) != MP_OKAY
) { goto error2
; }
146 if ((err
= mp_copy(&q
, &key
->q
)) != MP_OKAY
) { goto error2
; }
148 /* shrink ram required */
149 if ((err
= mp_shrink(&key
->e
)) != MP_OKAY
) { goto error2
; }
150 if ((err
= mp_shrink(&key
->d
)) != MP_OKAY
) { goto error2
; }
151 if ((err
= mp_shrink(&key
->N
)) != MP_OKAY
) { goto error2
; }
152 if ((err
= mp_shrink(&key
->dQ
)) != MP_OKAY
) { goto error2
; }
153 if ((err
= mp_shrink(&key
->dP
)) != MP_OKAY
) { goto error2
; }
154 if ((err
= mp_shrink(&key
->qP
)) != MP_OKAY
) { goto error2
; }
155 if ((err
= mp_shrink(&key
->p
)) != MP_OKAY
) { goto error2
; }
156 if ((err
= mp_shrink(&key
->q
)) != MP_OKAY
) { goto error2
; }
158 /* set key type (in this case it's CRT optimized) */
159 key
->type
= PK_PRIVATE
;
161 /* return ok and free temps */
165 mp_clear_multi(&key
->d
, &key
->e
, &key
->N
, &key
->dQ
, &key
->dP
,
166 &key
->qP
, &key
->p
, &key
->q
, NULL
);
168 err
= mpi_to_ltc_error(err
);
170 mp_clear_multi(&tmp3
, &tmp2
, &tmp1
, &p
, &q
, NULL
);
174 void rsa_free(rsa_key
*key
)
176 mp_clear_multi(&key
->e
, &key
->d
, &key
->N
, &key
->dQ
, &key
->dP
,
177 &key
->qP
, &key
->p
, &key
->q
, NULL
);
180 /* compute an RSA modular exponentiation */
181 int rsa_exptmod(const unsigned char *in
, unsigned long inlen
,
182 unsigned char *out
, unsigned long *outlen
, int which
,
185 mp_int tmp
, tmpa
, tmpb
;
189 /* is the key of the right type for the operation? */
190 if (which
== PK_PRIVATE
&& (key
->type
!= PK_PRIVATE
)) {
191 return CRYPT_PK_NOT_PRIVATE
;
194 /* must be a private or public operation */
195 if (which
!= PK_PRIVATE
&& which
!= PK_PUBLIC
) {
196 return CRYPT_PK_INVALID_TYPE
;
199 /* init and copy into tmp */
200 if ((err
= mp_init_multi(&tmp
, &tmpa
, &tmpb
, NULL
)) != MP_OKAY
) { return mpi_to_ltc_error(err
); }
201 if ((err
= mp_read_unsigned_bin(&tmp
, in
, (int)inlen
)) != MP_OKAY
) { goto error
; }
203 /* sanity check on the input */
204 if (mp_cmp(&key
->N
, &tmp
) == MP_LT
) {
205 err
= CRYPT_PK_INVALID_SIZE
;
209 /* are we using the private exponent and is the key optimized? */
210 if (which
== PK_PRIVATE
) {
211 /* tmpa = tmp^dP mod p */
212 if ((err
= mpi_to_ltc_error(mp_exptmod(&tmp
, &key
->dP
, &key
->p
, &tmpa
))) != MP_OKAY
) { goto error
; }
214 /* tmpb = tmp^dQ mod q */
215 if ((err
= mpi_to_ltc_error(mp_exptmod(&tmp
, &key
->dQ
, &key
->q
, &tmpb
))) != MP_OKAY
) { goto error
; }
217 /* tmp = (tmpa - tmpb) * qInv (mod p) */
218 if ((err
= mp_sub(&tmpa
, &tmpb
, &tmp
)) != MP_OKAY
) { goto error
; }
219 if ((err
= mp_mulmod(&tmp
, &key
->qP
, &key
->p
, &tmp
)) != MP_OKAY
) { goto error
; }
221 /* tmp = tmpb + q * tmp */
222 if ((err
= mp_mul(&tmp
, &key
->q
, &tmp
)) != MP_OKAY
) { goto error
; }
223 if ((err
= mp_add(&tmp
, &tmpb
, &tmp
)) != MP_OKAY
) { goto error
; }
226 if ((err
= mp_exptmod(&tmp
, &key
->e
, &key
->N
, &tmp
)) != MP_OKAY
) { goto error
; }
230 x
= (unsigned long)mp_unsigned_bin_size(&key
->N
);
232 err
= CRYPT_BUFFER_OVERFLOW
;
239 if ((err
= mp_to_unsigned_bin(&tmp
, out
+(x
-mp_unsigned_bin_size(&tmp
)))) != MP_OKAY
) { goto error
; }
241 /* clean up and return */
245 err
= mpi_to_ltc_error(err
);
247 mp_clear_multi(&tmp
, &tmpa
, &tmpb
, NULL
);