~ubuntu-branches/debian/lenny/dropbear/lenny

« back to all changes in this revision

Viewing changes to libtomcrypt/rsa_exptmod.c

  • Committer: Bazaar Package Importer
  • Author(s): Gerrit Pape
  • Date: 2005-05-25 22:38:17 UTC
  • mfrom: (1.2.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050525223817-fdl653extybmz1zb
Tags: 0.45-3
* debian/dropbear.init: init script prints human readable message in case
  it's disabled (closes: #309099).
* debian/dropbear.postinst: configure: restart service through init script
  instead of start.
* debian/dropbear.prerm: set -u -> set -e.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
 
15
15
#ifdef MRSA
16
16
 
 
17
/* compute an RSA modular exponentiation */
17
18
int rsa_exptmod(const unsigned char *in,   unsigned long inlen,
18
19
                      unsigned char *out,  unsigned long *outlen, int which,
19
20
                      prng_state    *prng, int           prng_idx,
28
29
   _ARGCHK(outlen != NULL);
29
30
   _ARGCHK(key    != NULL);
30
31
   
 
32
   /* valid prng? */
31
33
   if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) {
32
34
      return err;
33
35
   }
34
36
 
35
 
   if (which == PK_PRIVATE && (key->type != PK_PRIVATE && key->type != PK_PRIVATE_OPTIMIZED)) {
 
37
   /* is the key of the right type for the operation? */
 
38
   if (which == PK_PRIVATE && (key->type != PK_PRIVATE)) {
36
39
      return CRYPT_PK_NOT_PRIVATE;
37
40
   }
38
41
 
42
45
   }
43
46
 
44
47
   /* init and copy into tmp */
45
 
   if ((err = mp_init_multi(&tmp, &tmpa, &tmpb, NULL)) != MP_OKAY)                     { goto error; }
 
48
   if ((err = mp_init_multi(&tmp, &tmpa, &tmpb, NULL)) != MP_OKAY)                     { return mpi_to_ltc_error(err); }
46
49
   if ((err = mp_read_unsigned_bin(&tmp, (unsigned char *)in, (int)inlen)) != MP_OKAY) { goto error; }
47
50
 
48
51
   /* sanity check on the input */
52
55
   }
53
56
 
54
57
   /* are we using the private exponent and is the key optimized? */
55
 
   if (which == PK_PRIVATE && key->type == PK_PRIVATE_OPTIMIZED) {
 
58
   if (which == PK_PRIVATE) {
56
59
      /* tmpa = tmp^dP mod p */
57
60
      if ((err = tim_exptmod(prng, prng_idx, &tmp, &key->e, &key->dP, &key->p, &tmpa)) != MP_OKAY)    { goto error; }
58
61
 
59
62
      /* tmpb = tmp^dQ mod q */
60
63
      if ((err = tim_exptmod(prng, prng_idx, &tmp, &key->e,  &key->dQ, &key->q, &tmpb)) != MP_OKAY)    { goto error; }
61
64
 
62
 
      /* tmp = tmpa*qP + tmpb*pQ mod N */
63
 
      if ((err = mp_mul(&tmpa, &key->qP, &tmpa)) != MP_OKAY)                { goto error; }
64
 
      if ((err = mp_mul(&tmpb, &key->pQ, &tmpb)) != MP_OKAY)                { goto error; }
65
 
      if ((err = mp_addmod(&tmpa, &tmpb, &key->N, &tmp)) != MP_OKAY)        { goto error; }
 
65
      /* tmp = (tmpa - tmpb) * qInv (mod p) */
 
66
      if ((err = mp_sub(&tmpa, &tmpb, &tmp)) != MP_OKAY)                    { goto error; }
 
67
      if ((err = mp_mulmod(&tmp, &key->qP, &key->p, &tmp)) != MP_OKAY)      { goto error; }
 
68
 
 
69
      /* tmp = tmpb + q * tmp */
 
70
      if ((err = mp_mul(&tmp, &key->q, &tmp)) != MP_OKAY)                   { goto error; }
 
71
      if ((err = mp_add(&tmp, &tmpb, &tmp)) != MP_OKAY)                     { goto error; }
66
72
   } else {
67
73
      /* exptmod it */
68
 
      if (which == PK_PRIVATE) {
69
 
         if ((err = tim_exptmod(prng, prng_idx, &tmp, &key->e, &key->d, &key->N, &tmp)) != MP_OKAY) { goto error; }
70
 
      } else {
71
 
         if ((err = mp_exptmod(&tmp, &key->e, &key->N, &tmp)) != MP_OKAY) { goto error; }
72
 
      }
 
74
      if ((err = mp_exptmod(&tmp, &key->e, &key->N, &tmp)) != MP_OKAY) { goto error; }
73
75
   }
74
76
 
75
77
   /* read it back */