rsa_exptmod.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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, [email protected], http://libtomcrypt.org
  10. */
  11. /* RSA Code by Tom St Denis */
  12. #include "mycrypt.h"
  13. #ifdef MRSA
  14. /* compute an RSA modular exponentiation */
  15. int rsa_exptmod(const unsigned char *in, unsigned long inlen,
  16. unsigned char *out, unsigned long *outlen, int which,
  17. prng_state *prng, int prng_idx,
  18. rsa_key *key)
  19. {
  20. mp_int tmp, tmpa, tmpb;
  21. unsigned long x;
  22. int err;
  23. _ARGCHK(in != NULL);
  24. _ARGCHK(out != NULL);
  25. _ARGCHK(outlen != NULL);
  26. _ARGCHK(key != NULL);
  27. /* valid prng? */
  28. if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) {
  29. return err;
  30. }
  31. /* is the key of the right type for the operation? */
  32. if (which == PK_PRIVATE && (key->type != PK_PRIVATE)) {
  33. return CRYPT_PK_NOT_PRIVATE;
  34. }
  35. /* must be a private or public operation */
  36. if (which != PK_PRIVATE && which != PK_PUBLIC) {
  37. return CRYPT_PK_INVALID_TYPE;
  38. }
  39. /* init and copy into tmp */
  40. if ((err = mp_init_multi(&tmp, &tmpa, &tmpb, NULL)) != MP_OKAY) { return mpi_to_ltc_error(err); }
  41. if ((err = mp_read_unsigned_bin(&tmp, (unsigned char *)in, (int)inlen)) != MP_OKAY) { goto error; }
  42. /* sanity check on the input */
  43. if (mp_cmp(&key->N, &tmp) == MP_LT) {
  44. err = CRYPT_PK_INVALID_SIZE;
  45. goto done;
  46. }
  47. /* are we using the private exponent and is the key optimized? */
  48. if (which == PK_PRIVATE) {
  49. /* tmpa = tmp^dP mod p */
  50. if ((err = tim_exptmod(prng, prng_idx, &tmp, &key->e, &key->dP, &key->p, &tmpa)) != MP_OKAY) { goto error; }
  51. /* tmpb = tmp^dQ mod q */
  52. if ((err = tim_exptmod(prng, prng_idx, &tmp, &key->e, &key->dQ, &key->q, &tmpb)) != MP_OKAY) { goto error; }
  53. /* tmp = (tmpa - tmpb) * qInv (mod p) */
  54. if ((err = mp_sub(&tmpa, &tmpb, &tmp)) != MP_OKAY) { goto error; }
  55. if ((err = mp_mulmod(&tmp, &key->qP, &key->p, &tmp)) != MP_OKAY) { goto error; }
  56. /* tmp = tmpb + q * tmp */
  57. if ((err = mp_mul(&tmp, &key->q, &tmp)) != MP_OKAY) { goto error; }
  58. if ((err = mp_add(&tmp, &tmpb, &tmp)) != MP_OKAY) { goto error; }
  59. } else {
  60. /* exptmod it */
  61. if ((err = mp_exptmod(&tmp, &key->e, &key->N, &tmp)) != MP_OKAY) { goto error; }
  62. }
  63. /* read it back */
  64. x = (unsigned long)mp_unsigned_bin_size(&key->N);
  65. if (x > *outlen) {
  66. err = CRYPT_BUFFER_OVERFLOW;
  67. goto done;
  68. }
  69. *outlen = x;
  70. /* convert it */
  71. zeromem(out, x);
  72. if ((err = mp_to_unsigned_bin(&tmp, out+(x-mp_unsigned_bin_size(&tmp)))) != MP_OKAY) { goto error; }
  73. /* clean up and return */
  74. err = CRYPT_OK;
  75. goto done;
  76. error:
  77. err = mpi_to_ltc_error(err);
  78. done:
  79. mp_clear_multi(&tmp, &tmpa, &tmpb, NULL);
  80. return err;
  81. }
  82. #endif