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