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

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

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 }
int gen_rand_impl(unsigned char *dst, unsigned int len)
GLenum GLsizei len
Definition: glext.h:6722
GLenum GLenum dst
Definition: glext.h:6340

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)) {
190  return CRYPT_PK_NOT_PRIVATE;
191  }
192 
193  /* must be a private or public operation */
194  if (which != PK_PRIVATE && which != PK_PUBLIC) {
195  return CRYPT_PK_INVALID_TYPE;
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;
243 error:
245 done:
246  mp_clear_multi(&tmp, &tmpa, &tmpb, NULL);
247  return err;
248 }
int mp_add(mp_int *a, mp_int *b, mp_int *c)
Definition: mpi.c:891
void mp_clear_multi(mp_int *mp,...)
Definition: mpi.c:1032
#define error(str)
Definition: mkdosfs.c:1605
static int mpi_to_ltc_error(int err)
Definition: rsa.c:42
static GLenum which
Definition: wgl_font.c:159
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
int mp_cmp(const mp_int *a, const mp_int *b)
Definition: mpi.c:1046
#define MP_LT
Definition: tomcrypt.h:193
smooth NULL
Definition: ftsmooth.c:416
int mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
Definition: mpi.c:3107
#define PK_PRIVATE
Definition: tomcrypt.h:449
static FILE * out
Definition: regtests2xml.c:44
int mp_to_unsigned_bin(const mp_int *a, unsigned char *b)
Definition: mpi.c:3875
#define err(...)
#define MP_OKAY
Definition: tomcrypt.h:200
int mp_exptmod(const mp_int *G, const mp_int *X, mp_int *P, mp_int *Y)
Definition: mpi.c:1917
GLuint in
Definition: glext.h:9616
int mp_unsigned_bin_size(const mp_int *a)
Definition: mpi.c:3899
#define long
Definition: qsort.c:33
int mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c)
Definition: mpi.c:3502
#define PK_PUBLIC
Definition: tomcrypt.h:450
#define memset(x, y, z)
Definition: compat.h:39
int mp_sub(mp_int *a, mp_int *b, mp_int *c)
Definition: mpi.c:3771
int mp_init_multi(mp_int *mp,...)
Definition: mpi.c:2354
Definition: path.c:42
int mp_mulmod(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
Definition: mpi.c:3138

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 }
void mp_clear_multi(mp_int *mp,...)
Definition: mpi.c:1032
smooth NULL
Definition: ftsmooth.c:416
Definition: path.c:42

◆ 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))) {
93  return CRYPT_INVALID_KEYSIZE;
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;
163 error2:
164  mp_clear_multi(&key->d, &key->e, &key->N, &key->dQ, &key->dP,
165  &key->qP, &key->p, &key->q, NULL);
166 error:
168 done:
169  mp_clear_multi(&tmp3, &tmp2, &tmp1, &p, &q, NULL);
170  return err;
171 }
int mp_set_int(mp_int *a, unsigned long b)
Definition: mpi.c:3687
void mp_clear_multi(mp_int *mp,...)
Definition: mpi.c:1032
static int rand_prime(mp_int *N, long len)
Definition: rsa.c:61
#define error(str)
Definition: mkdosfs.c:1605
static int mpi_to_ltc_error(int err)
Definition: rsa.c:42
int mp_lcm(const mp_int *a, const mp_int *b, mp_int *c)
Definition: mpi.c:2807
int mp_shrink(mp_int *a)
Definition: mpi.c:3714
int mp_mod(const mp_int *a, mp_int *b, mp_int *c)
Definition: mpi.c:2847
#define e
Definition: ke_i.h:82
int mp_cmp_d(const mp_int *a, mp_digit b)
Definition: mpi.c:1067
smooth NULL
Definition: ftsmooth.c:416
int mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
Definition: mpi.c:3107
#define MIN_RSA_SIZE
Definition: tomcrypt.h:453
#define error2(s, a, b)
Definition: debug.h:110
#define PK_PRIVATE
Definition: tomcrypt.h:449
#define MAX_RSA_SIZE
Definition: tomcrypt.h:454
GLsizeiptr size
Definition: glext.h:5919
GLdouble GLdouble GLdouble GLdouble q
Definition: gl.h:2063
#define err(...)
int mp_sub_d(mp_int *a, mp_digit b, mp_int *c)
Definition: mpi.c:3808
#define MP_OKAY
Definition: tomcrypt.h:200
int mp_gcd(const mp_int *a, const mp_int *b, mp_int *c)
Definition: mpi.c:2228
GLfloat GLfloat p
Definition: glext.h:8902
int mp_copy(const mp_int *a, mp_int *b)
Definition: mpi.c:1156
int mp_init_multi(mp_int *mp,...)
Definition: mpi.c:2354
Definition: path.c:42
int mp_invmod(const mp_int *a, mp_int *b, mp_int *c)
Definition: mpi.c:2391

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().

◆ mpi_to_ltc_codes

const { ... } mpi_to_ltc_codes[]
Initial value:
= {
}
#define MP_MEM
Definition: tomcrypt.h:201
#define MP_VAL
Definition: tomcrypt.h:202
#define MP_OKAY
Definition: tomcrypt.h:200

Referenced by mpi_to_ltc_error().