pkcs_1_v15_es_decode.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #include "mycrypt.h"
  12. /* PKCS #1 v1.5 Encryption Padding -- Tom St Denis */
  13. #ifdef PKCS_1
  14. int pkcs_1_v15_es_decode(const unsigned char *msg, unsigned long msglen,
  15. unsigned long modulus_bitlen,
  16. unsigned char *out, unsigned long outlen,
  17. int *res)
  18. {
  19. unsigned long x, modulus_bytelen;
  20. _ARGCHK(msg != NULL);
  21. _ARGCHK(out != NULL);
  22. _ARGCHK(res != NULL);
  23. /* default to failed */
  24. *res = 0;
  25. modulus_bytelen = (modulus_bitlen>>3) + (modulus_bitlen & 7 ? 1 : 0);
  26. /* must be at least modulus_bytelen bytes long */
  27. if (msglen != modulus_bytelen) {
  28. return CRYPT_INVALID_ARG;
  29. }
  30. /* should start with 0x00 0x02 */
  31. if (msg[0] != 0x00 || msg[1] != 0x02) {
  32. return CRYPT_OK;
  33. }
  34. /* skip over PS */
  35. x = 2 + (modulus_bytelen - outlen - 3);
  36. /* should be 0x00 */
  37. if (msg[x++] != 0x00) {
  38. return CRYPT_OK;
  39. }
  40. /* the message is left */
  41. if (x + outlen > modulus_bytelen) {
  42. return CRYPT_PK_INVALID_SIZE;
  43. }
  44. XMEMCPY(out, msg + x, outlen);
  45. *res = 1;
  46. return CRYPT_OK;
  47. }
  48. #endif