ReactOS 0.4.16-dev-303-g11d5cb8
rsa.c
Go to the documentation of this file.
1/*
2 * dlls/rsaenh/rsa.c
3 * RSA public key cryptographic functions
4 *
5 * Copyright 2004 Michael Jung
6 * Based on public domain code by Tom St Denis (tomstdenis@iahu.ca)
7 *
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.
12 *
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.
17 *
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
21 */
22
23/*
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
28 * original version.
29 */
30
31#include "tomcrypt.h"
32
33static const struct {
35} mpi_to_ltc_codes[] = {
36 { MP_OKAY , CRYPT_OK},
37 { MP_MEM , CRYPT_MEM},
39};
40
41/* convert a MPI error to a LTC error (Possibly the most powerful function ever! Oh wait... no) */
42static int mpi_to_ltc_error(int err)
43{
44 int x;
45
46 for (x = 0; x < (int)(sizeof(mpi_to_ltc_codes)/sizeof(mpi_to_ltc_codes[0])); x++) {
47 if (err == mpi_to_ltc_codes[x].mpi_code) {
48 return mpi_to_ltc_codes[x].ltc_code;
49 }
50 }
51 return CRYPT_ERROR;
52}
53
54extern int gen_rand_impl(unsigned char *dst, unsigned int len);
55
56static int rand_prime_helper(unsigned char *dst, int len, void *dat)
57{
58 return gen_rand_impl(dst, len) ? len : 0;
59}
60
61static int rand_prime(mp_int *N, long len)
62{
63 int type;
64
65 /* get type */
66 if (len < 0) {
68 len = -len;
69 } else {
70 /* This seems to be what MS CSP's do: */
72 /* Original LibTomCrypt: type = 0; */
73 }
74
75 /* allow sizes between 2 and 256 bytes for a prime size */
76 if (len < 16 || len > 8192) {
77 printf("Invalid prime size!\n");
79 }
80
81 /* New prime generation makes the code even more cryptoish-insane. Do you know what this means!!!
82 -- Gir: Yeah, oh wait, er, no.
83 */
85}
86
87int rsa_make_key(int size, long e, rsa_key *key)
88{
89 mp_int p, q, tmp1, tmp2, tmp3;
90 int err;
91
92 if ((size < (MIN_RSA_SIZE/8)) || (size > (MAX_RSA_SIZE/8))) {
94 }
95
96 if ((e < 3) || ((e & 1) == 0)) {
97 return CRYPT_INVALID_ARG;
98 }
99
100 if ((err = mp_init_multi(&p, &q, &tmp1, &tmp2, &tmp3, NULL)) != MP_OKAY) {
101 return mpi_to_ltc_error(err);
102 }
103
104 /* make primes p and q (optimization provided by Wayne Scott) */
105 if ((err = mp_set_int(&tmp3, e)) != MP_OKAY) { goto error; } /* tmp3 = e */
106
107 /* make prime "p" */
108 do {
109 if ((err = rand_prime(&p, size*4)) != CRYPT_OK) { goto done; }
110 if ((err = mp_sub_d(&p, 1, &tmp1)) != MP_OKAY) { goto error; } /* tmp1 = p-1 */
111 if ((err = mp_gcd(&tmp1, &tmp3, &tmp2)) != MP_OKAY) { goto error; } /* tmp2 = gcd(p-1, e) */
112 } while (mp_cmp_d(&tmp2, 1) != 0); /* while e divides p-1 */
113
114 /* make prime "q" */
115 do {
116 if ((err = rand_prime(&q, size*4)) != CRYPT_OK) { goto done; }
117 if ((err = mp_sub_d(&q, 1, &tmp1)) != MP_OKAY) { goto error; } /* tmp1 = q-1 */
118 if ((err = mp_gcd(&tmp1, &tmp3, &tmp2)) != MP_OKAY) { goto error; } /* tmp2 = gcd(q-1, e) */
119 } while (mp_cmp_d(&tmp2, 1) != 0); /* while e divides q-1 */
120
121 /* tmp1 = lcm(p-1, q-1) */
122 if ((err = mp_sub_d(&p, 1, &tmp2)) != MP_OKAY) { goto error; } /* tmp2 = p-1 */
123 /* tmp1 = q-1 (previous do/while loop) */
124 if ((err = mp_lcm(&tmp1, &tmp2, &tmp1)) != MP_OKAY) { goto error; } /* tmp1 = lcm(p-1, q-1) */
125
126 /* make key */
127 if ((err = mp_init_multi(&key->e, &key->d, &key->N, &key->dQ, &key->dP,
128 &key->qP, &key->p, &key->q, NULL)) != MP_OKAY) {
129 goto error;
130 }
131
132 if ((err = mp_set_int(&key->e, e)) != MP_OKAY) { goto error2; } /* key->e = e */
133 if ((err = mp_invmod(&key->e, &tmp1, &key->d)) != MP_OKAY) { goto error2; } /* key->d = 1/e mod lcm(p-1,q-1) */
134 if ((err = mp_mul(&p, &q, &key->N)) != MP_OKAY) { goto error2; } /* key->N = pq */
135
136 /* optimize for CRT now */
137 /* find d mod q-1 and d mod p-1 */
138 if ((err = mp_sub_d(&p, 1, &tmp1)) != MP_OKAY) { goto error2; } /* tmp1 = q-1 */
139 if ((err = mp_sub_d(&q, 1, &tmp2)) != MP_OKAY) { goto error2; } /* tmp2 = p-1 */
140 if ((err = mp_mod(&key->d, &tmp1, &key->dP)) != MP_OKAY) { goto error2; } /* dP = d mod p-1 */
141 if ((err = mp_mod(&key->d, &tmp2, &key->dQ)) != MP_OKAY) { goto error2; } /* dQ = d mod q-1 */
142 if ((err = mp_invmod(&q, &p, &key->qP)) != MP_OKAY) { goto error2; } /* qP = 1/q mod p */
143
144 if ((err = mp_copy(&p, &key->p)) != MP_OKAY) { goto error2; }
145 if ((err = mp_copy(&q, &key->q)) != MP_OKAY) { goto error2; }
146
147 /* shrink ram required */
148 if ((err = mp_shrink(&key->e)) != MP_OKAY) { goto error2; }
149 if ((err = mp_shrink(&key->d)) != MP_OKAY) { goto error2; }
150 if ((err = mp_shrink(&key->N)) != MP_OKAY) { goto error2; }
151 if ((err = mp_shrink(&key->dQ)) != MP_OKAY) { goto error2; }
152 if ((err = mp_shrink(&key->dP)) != MP_OKAY) { goto error2; }
153 if ((err = mp_shrink(&key->qP)) != MP_OKAY) { goto error2; }
154 if ((err = mp_shrink(&key->p)) != MP_OKAY) { goto error2; }
155 if ((err = mp_shrink(&key->q)) != MP_OKAY) { goto error2; }
156
157 /* set key type (in this case it's CRT optimized) */
158 key->type = PK_PRIVATE;
159
160 /* return ok and free temps */
161 err = CRYPT_OK;
162 goto done;
163error2:
164 mp_clear_multi(&key->d, &key->e, &key->N, &key->dQ, &key->dP,
165 &key->qP, &key->p, &key->q, NULL);
166error:
168done:
169 mp_clear_multi(&tmp3, &tmp2, &tmp1, &p, &q, NULL);
170 return err;
171}
172
174{
175 mp_clear_multi(&key->e, &key->d, &key->N, &key->dQ, &key->dP,
176 &key->qP, &key->p, &key->q, NULL);
177}
178
179/* compute an RSA modular exponentiation */
180int rsa_exptmod(const unsigned char *in, unsigned long inlen,
181 unsigned char *out, unsigned long *outlen, int which,
182 rsa_key *key)
183{
184 mp_int tmp, tmpa, tmpb;
185 unsigned long x;
186 int err;
187
188 /* is the key of the right type for the operation? */
189 if (which == PK_PRIVATE && (key->type != PK_PRIVATE)) {
191 }
192
193 /* must be a private or public operation */
194 if (which != PK_PRIVATE && which != PK_PUBLIC) {
196 }
197
198 /* init and copy into tmp */
199 if ((err = mp_init_multi(&tmp, &tmpa, &tmpb, NULL)) != MP_OKAY) { return mpi_to_ltc_error(err); }
200 if ((err = mp_read_unsigned_bin(&tmp, in, (int)inlen)) != MP_OKAY) { goto error; }
201
202 /* sanity check on the input */
203 if (mp_cmp(&key->N, &tmp) == MP_LT) {
205 goto done;
206 }
207
208 /* are we using the private exponent and is the key optimized? */
209 if (which == PK_PRIVATE) {
210 /* tmpa = tmp^dP mod p */
211 if ((err = mpi_to_ltc_error(mp_exptmod(&tmp, &key->dP, &key->p, &tmpa))) != MP_OKAY) { goto error; }
212
213 /* tmpb = tmp^dQ mod q */
214 if ((err = mpi_to_ltc_error(mp_exptmod(&tmp, &key->dQ, &key->q, &tmpb))) != MP_OKAY) { goto error; }
215
216 /* tmp = (tmpa - tmpb) * qInv (mod p) */
217 if ((err = mp_sub(&tmpa, &tmpb, &tmp)) != MP_OKAY) { goto error; }
218 if ((err = mp_mulmod(&tmp, &key->qP, &key->p, &tmp)) != MP_OKAY) { goto error; }
219
220 /* tmp = tmpb + q * tmp */
221 if ((err = mp_mul(&tmp, &key->q, &tmp)) != MP_OKAY) { goto error; }
222 if ((err = mp_add(&tmp, &tmpb, &tmp)) != MP_OKAY) { goto error; }
223 } else {
224 /* exptmod it */
225 if ((err = mp_exptmod(&tmp, &key->e, &key->N, &tmp)) != MP_OKAY) { goto error; }
226 }
227
228 /* read it back */
229 x = (unsigned long)mp_unsigned_bin_size(&key->N);
230 if (x > *outlen) {
232 goto done;
233 }
234 *outlen = x;
235
236 /* convert it */
237 memset(out, 0, x);
238 if ((err = mp_to_unsigned_bin(&tmp, out+(x-mp_unsigned_bin_size(&tmp)))) != MP_OKAY) { goto error; }
239
240 /* clean up and return */
241 err = CRYPT_OK;
242 goto done;
243error:
245done:
246 mp_clear_multi(&tmp, &tmpa, &tmpb, NULL);
247 return err;
248}
#define N
Definition: crc32.c:57
#define rsa_free
Definition: compat-1.3.h:2238
#define NULL
Definition: types.h:112
#define MP_VAL
Definition: tomcrypt.h:202
#define LTM_PRIME_BBS
Definition: tomcrypt.h:209
#define MAX_RSA_SIZE
Definition: tomcrypt.h:454
#define PK_PUBLIC
Definition: tomcrypt.h:450
#define MP_MEM
Definition: tomcrypt.h:201
#define LTM_PRIME_2MSB_ON
Definition: tomcrypt.h:212
#define MP_LT
Definition: tomcrypt.h:193
@ CRYPT_ERROR
Definition: tomcrypt.h:44
@ CRYPT_MEM
Definition: tomcrypt.h:61
@ CRYPT_PK_INVALID_TYPE
Definition: tomcrypt.h:69
@ CRYPT_INVALID_PRIME_SIZE
Definition: tomcrypt.h:75
@ CRYPT_INVALID_ARG
Definition: tomcrypt.h:66
@ CRYPT_INVALID_KEYSIZE
Definition: tomcrypt.h:47
@ CRYPT_PK_NOT_PRIVATE
Definition: tomcrypt.h:64
@ CRYPT_BUFFER_OVERFLOW
Definition: tomcrypt.h:51
@ CRYPT_OK
Definition: tomcrypt.h:43
@ CRYPT_PK_INVALID_SIZE
Definition: tomcrypt.h:73
#define MIN_RSA_SIZE
Definition: tomcrypt.h:453
#define MP_OKAY
Definition: tomcrypt.h:200
#define PK_PRIVATE
Definition: tomcrypt.h:449
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
#define printf
Definition: freeldr.h:97
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
GLdouble GLdouble GLdouble GLdouble q
Definition: gl.h:2063
GLsizeiptr size
Definition: glext.h:5919
GLuint in
Definition: glext.h:9616
GLenum GLenum dst
Definition: glext.h:6340
GLfloat GLfloat p
Definition: glext.h:8902
GLenum GLsizei len
Definition: glext.h:6722
#define e
Definition: ke_i.h:82
#define error(str)
Definition: mkdosfs.c:1605
int mp_copy(const mp_int *a, mp_int *b)
Definition: mpi.c:1156
int mp_to_unsigned_bin(const mp_int *a, unsigned char *b)
Definition: mpi.c:3875
void mp_clear_multi(mp_int *mp,...)
Definition: mpi.c:1032
int mp_unsigned_bin_size(const mp_int *a)
Definition: mpi.c:3899
int mp_shrink(mp_int *a)
Definition: mpi.c:3714
int mp_prime_rabin_miller_trials(int size)
Definition: mpi.c:3382
int mp_add(mp_int *a, mp_int *b, mp_int *c)
Definition: mpi.c:891
int mp_sub(mp_int *a, mp_int *b, mp_int *c)
Definition: mpi.c:3771
int mp_invmod(const mp_int *a, mp_int *b, mp_int *c)
Definition: mpi.c:2391
int mp_gcd(const mp_int *a, const mp_int *b, mp_int *c)
Definition: mpi.c:2228
int mp_mod(const mp_int *a, mp_int *b, mp_int *c)
Definition: mpi.c:2847
int mp_lcm(const mp_int *a, const mp_int *b, mp_int *c)
Definition: mpi.c:2807
int mp_sub_d(mp_int *a, mp_digit b, mp_int *c)
Definition: mpi.c:3808
int mp_cmp_d(const mp_int *a, mp_digit b)
Definition: mpi.c:1067
int mp_cmp(const mp_int *a, const mp_int *b)
Definition: mpi.c:1046
int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback cb, void *dat)
Definition: mpi.c:3412
int mp_mulmod(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
Definition: mpi.c:3138
int mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c)
Definition: mpi.c:3502
int mp_exptmod(const mp_int *G, const mp_int *X, mp_int *P, mp_int *Y)
Definition: mpi.c:1917
int mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
Definition: mpi.c:3107
int mp_set_int(mp_int *a, unsigned long b)
Definition: mpi.c:3687
int mp_init_multi(mp_int *mp,...)
Definition: mpi.c:2354
#define long
Definition: qsort.c:33
#define err(...)
static FILE * out
Definition: regtests2xml.c:44
#define error2(s, a, b)
Definition: debug.h:126
#define memset(x, y, z)
Definition: compat.h:39
Definition: copy.c:22
static GLenum which
Definition: wgl_font.c:159
int rsa_make_key(int size, long e, rsa_key *key)
Definition: rsa.c:87
static int rand_prime(mp_int *N, long len)
Definition: rsa.c:61
static const struct @542 mpi_to_ltc_codes[]
static int rand_prime_helper(unsigned char *dst, int len, void *dat)
Definition: rsa.c:56
int gen_rand_impl(unsigned char *dst, unsigned int len)
int mpi_code
Definition: rsa.c:34
int ltc_code
Definition: rsa.c:34
static int mpi_to_ltc_error(int err)
Definition: rsa.c:42
int rsa_exptmod(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen, int which, rsa_key *key)
Definition: rsa.c:180