rsa_exptmod.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 && key->type != PK_PRIVATE_OPTIMIZED)) {
  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) { goto error; }
  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 && key->type == PK_PRIVATE_OPTIMIZED) {
  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*qP + tmpb*pQ mod N */
  54. if ((err = mp_mul(&tmpa, &key->qP, &tmpa)) != MP_OKAY) { goto error; }
  55. if ((err = mp_mul(&tmpb, &key->pQ, &tmpb)) != MP_OKAY) { goto error; }
  56. if ((err = mp_addmod(&tmpa, &tmpb, &key->N, &tmp)) != MP_OKAY) { goto error; }
  57. } else {
  58. /* exptmod it */
  59. if (which == PK_PRIVATE) {
  60. if ((err = tim_exptmod(prng, prng_idx, &tmp, &key->e, &key->d, &key->N, &tmp)) != MP_OKAY) { goto error; }
  61. } else {
  62. if ((err = mp_exptmod(&tmp, &key->e, &key->N, &tmp)) != MP_OKAY) { goto error; }
  63. }
  64. }
  65. /* read it back */
  66. x = (unsigned long)mp_unsigned_bin_size(&key->N);
  67. if (x > *outlen) {
  68. err = CRYPT_BUFFER_OVERFLOW;
  69. goto done;
  70. }
  71. *outlen = x;
  72. /* convert it */
  73. zeromem(out, x);
  74. if ((err = mp_to_unsigned_bin(&tmp, out+(x-mp_unsigned_bin_size(&tmp)))) != MP_OKAY) { goto error; }
  75. /* clean up and return */
  76. err = CRYPT_OK;
  77. goto done;
  78. error:
  79. err = mpi_to_ltc_error(err);
  80. done:
  81. mp_clear_multi(&tmp, &tmpa, &tmpb, NULL);
  82. return err;
  83. }
  84. #endif