• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
10  */
11 #include "tomcrypt.h"
12 
13 /**
14   @file katja_make_key.c
15   Katja key generation, Tom St Denis
16 */
17 
18 #ifdef MKAT
19 
20 /**
21    Create a Katja key
22    @param prng     An active PRNG state
23    @param wprng    The index of the PRNG desired
24    @param size     The size of the modulus (key size) desired (octets)
25    @param key      [out] Destination of a newly created private key pair
26    @return CRYPT_OK if successful, upon error all allocated ram is freed
27 */
katja_make_key(prng_state * prng,int wprng,int size,katja_key * key)28 int katja_make_key(prng_state *prng, int wprng, int size, katja_key *key)
29 {
30    void *p, *q, *tmp1, *tmp2;
31    int    err;
32 
33    LTC_ARGCHK(key != NULL);
34    LTC_ARGCHK(ltc_mp.name != NULL);
35 
36    if ((size < (MIN_KAT_SIZE/8)) || (size > (MAX_KAT_SIZE/8))) {
37       return CRYPT_INVALID_KEYSIZE;
38    }
39 
40    if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
41       return err;
42    }
43 
44    if ((err = mp_init_multi(&p, &q, &tmp1, &tmp2, NULL)) != CRYPT_OK) {
45       return err;
46    }
47 
48    /* divide size by three  */
49    size   = (((size << 3) / 3) + 7) >> 3;
50 
51    /* make prime "q" (we negate size to make q == 3 mod 4) */
52    if ((err = rand_prime(q, -size, prng, wprng)) != CRYPT_OK)      { goto done; }
53    if ((err = mp_sub_d(q, 1, tmp1)) != CRYPT_OK)                   { goto done; }
54 
55    /* make prime "p" */
56    do {
57       if ((err = rand_prime(p, size+1, prng, wprng)) != CRYPT_OK)  { goto done; }
58       if ((err = mp_gcd(p, tmp1, tmp2)) != CRYPT_OK)               { goto done; }
59    } while (mp_cmp_d(tmp2, 1) != LTC_MP_EQ);
60 
61    /* make key */
62    if ((err = mp_init_multi(&key->d, &key->N, &key->dQ, &key->dP,
63                      &key->qP, &key->p, &key->q, &key->pq, NULL)) != CRYPT_OK) {
64       goto error;
65    }
66 
67    /* n=p^2q and 1/n mod pq */
68    if ((err = mp_copy( p,  key->p)) != CRYPT_OK)                       { goto error2; }
69    if ((err = mp_copy( q,  key->q)) != CRYPT_OK)                       { goto error2; }
70    if ((err = mp_mul(key->p, key->q, key->pq)) != CRYPT_OK)            { goto error2; } /* tmp1 = pq  */
71    if ((err = mp_mul(key->pq, key->p, key->N)) != CRYPT_OK)            { goto error2; } /* N = p^2q   */
72    if ((err = mp_sub_d( p, 1,  tmp1)) != CRYPT_OK)                     { goto error2; } /* tmp1 = q-1 */
73    if ((err = mp_sub_d( q, 1,  tmp2)) != CRYPT_OK)                     { goto error2; } /* tmp2 = p-1 */
74    if ((err = mp_lcm(tmp1, tmp2, key->d)) != CRYPT_OK)                 { goto error2; } /* tmp1 = lcd(p-1,q-1) */
75    if ((err = mp_invmod( key->N,  key->d,  key->d)) != CRYPT_OK)       { goto error2; } /* key->d = 1/N mod pq */
76 
77    /* optimize for CRT now */
78    /* find d mod q-1 and d mod p-1 */
79    if ((err = mp_mod( key->d,  tmp1,  key->dP)) != CRYPT_OK)           { goto error2; } /* dP = d mod p-1 */
80    if ((err = mp_mod( key->d,  tmp2,  key->dQ)) != CRYPT_OK)           { goto error2; } /* dQ = d mod q-1 */
81    if ((err = mp_invmod( q,  p,  key->qP)) != CRYPT_OK)                { goto error2; } /* qP = 1/q mod p */
82 
83    /* set key type (in this case it's CRT optimized) */
84    key->type = PK_PRIVATE;
85 
86    /* return ok and free temps */
87    err       = CRYPT_OK;
88    goto done;
89 error2:
90    mp_clear_multi( key->d,  key->N,  key->dQ,  key->dP,  key->qP,  key->p,  key->q, key->pq, NULL);
91 error:
92 done:
93    mp_clear_multi( tmp2,  tmp1,  p,  q, NULL);
94    return err;
95 }
96 
97 #endif
98 
99 /* $Source: /cvs/libtom/libtomcrypt/src/pk/katja/katja_make_key.c,v $ */
100 /* $Revision: 1.10 $ */
101 /* $Date: 2006/03/31 14:15:35 $ */
102