ReactOS 0.4.15-dev-8100-g1887773
rsa.c File Reference
#include "tomcrypt.h"
Include dependency graph for rsa.c:

Go to the source code of this file.

Functions

static int mpi_to_ltc_error (int err)
 
int gen_rand_impl (unsigned char *dst, unsigned int len)
 
static int rand_prime_helper (unsigned char *dst, int len, void *dat)
 
static int rand_prime (mp_int *N, long len)
 
int rsa_make_key (int size, long e, rsa_key *key)
 
void rsa_free (rsa_key *key)
 
int rsa_exptmod (const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen, int which, rsa_key *key)
 

Variables

struct {
   int   mpi_code
 
   int   ltc_code
 
mpi_to_ltc_codes []
 

Function Documentation

◆ gen_rand_impl()

int gen_rand_impl ( unsigned char dst,
unsigned int  len 
)

Referenced by rand_prime_helper().

◆ mpi_to_ltc_error()

static int mpi_to_ltc_error ( int  err)
static

Definition at line 42 of file rsa.c.

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}
@ CRYPT_ERROR
Definition: tomcrypt.h:44
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
#define err(...)
static const struct @546 mpi_to_ltc_codes[]
int mpi_code
Definition: rsa.c:34

Referenced by rand_prime(), rsa_exptmod(), and rsa_make_key().

◆ rand_prime()

static int rand_prime ( mp_int N,
long  len 
)
static

Definition at line 61 of file rsa.c.

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}
#define N
Definition: crc32.c:57
#define NULL
Definition: types.h:112
#define LTM_PRIME_BBS
Definition: tomcrypt.h:209
#define LTM_PRIME_2MSB_ON
Definition: tomcrypt.h:212
@ CRYPT_INVALID_PRIME_SIZE
Definition: tomcrypt.h:75
#define printf
Definition: freeldr.h:97
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
GLenum GLsizei len
Definition: glext.h:6722
int mp_prime_rabin_miller_trials(int size)
Definition: mpi.c:3382
int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback cb, void *dat)
Definition: mpi.c:3412
static int rand_prime_helper(unsigned char *dst, int len, void *dat)
Definition: rsa.c:56
static int mpi_to_ltc_error(int err)
Definition: rsa.c:42

Referenced by rsa_make_key().

◆ rand_prime_helper()

static int rand_prime_helper ( unsigned char dst,
int  len,
void dat 
)
static

Definition at line 56 of file rsa.c.

57{
58 return gen_rand_impl(dst, len) ? len : 0;
59}
GLenum GLenum dst
Definition: glext.h:6340
int gen_rand_impl(unsigned char *dst, unsigned int len)

Referenced by rand_prime().

◆ rsa_exptmod()

int rsa_exptmod ( const unsigned char in,
unsigned long  inlen,
unsigned char out,
unsigned long outlen,
int  which,
rsa_key key 
)

Definition at line 180 of file rsa.c.

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 PK_PUBLIC
Definition: tomcrypt.h:450
#define MP_LT
Definition: tomcrypt.h:193
#define MP_OKAY
Definition: tomcrypt.h:200
@ CRYPT_PK_INVALID_TYPE
Definition: tomcrypt.h:69
@ 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 PK_PRIVATE
Definition: tomcrypt.h:449
GLuint in
Definition: glext.h:9616
#define error(str)
Definition: mkdosfs.c:1605
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_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_cmp(const mp_int *a, const mp_int *b)
Definition: mpi.c:1046
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_init_multi(mp_int *mp,...)
Definition: mpi.c:2354
#define long
Definition: qsort.c:33
static FILE * out
Definition: regtests2xml.c:44
#define memset(x, y, z)
Definition: compat.h:39
Definition: copy.c:22
static GLenum which
Definition: wgl_font.c:159

Referenced by encrypt_block_impl().

◆ rsa_free()

void rsa_free ( rsa_key key)

Definition at line 173 of file rsa.c.

174{
175 mp_clear_multi(&key->e, &key->d, &key->N, &key->dQ, &key->dP,
176 &key->qP, &key->p, &key->q, NULL);
177}

◆ rsa_make_key()

int rsa_make_key ( int  size,
long  e,
rsa_key key 
)

Definition at line 87 of file rsa.c.

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}
#define MAX_RSA_SIZE
Definition: tomcrypt.h:454
#define MIN_RSA_SIZE
Definition: tomcrypt.h:453
@ CRYPT_INVALID_ARG
Definition: tomcrypt.h:66
@ CRYPT_INVALID_KEYSIZE
Definition: tomcrypt.h:47
GLdouble GLdouble GLdouble GLdouble q
Definition: gl.h:2063
GLsizeiptr size
Definition: glext.h:5919
GLfloat GLfloat p
Definition: glext.h:8902
#define e
Definition: ke_i.h:82
int mp_copy(const mp_int *a, mp_int *b)
Definition: mpi.c:1156
int mp_shrink(mp_int *a)
Definition: mpi.c:3714
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_set_int(mp_int *a, unsigned long b)
Definition: mpi.c:3687
#define error2(s, a, b)
Definition: debug.h:126
static int rand_prime(mp_int *N, long len)
Definition: rsa.c:61

Referenced by new_key_impl().

Variable Documentation

◆ ltc_code

int ltc_code

Definition at line 34 of file rsa.c.

◆ mpi_code

int mpi_code

Definition at line 34 of file rsa.c.

Referenced by mpi_to_ltc_error().

◆ 

const struct { ... } mpi_to_ltc_codes[]
Initial value:
= {
}
#define MP_VAL
Definition: tomcrypt.h:202
#define MP_MEM
Definition: tomcrypt.h:201
@ CRYPT_MEM
Definition: tomcrypt.h:61

Referenced by mpi_to_ltc_error().