pkcs_1_oaep_decode.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. /* OAEP Padding for PKCS #1 -- Tom St Denis */
  13. #ifdef PKCS_1
  14. int pkcs_1_oaep_decode(const unsigned char *msg, unsigned long msglen,
  15. const unsigned char *lparam, unsigned long lparamlen,
  16. unsigned long modulus_bitlen, int hash_idx,
  17. unsigned char *out, unsigned long *outlen,
  18. int *res)
  19. {
  20. unsigned char *DB, *seed, *mask;
  21. unsigned long hLen, x, y, modulus_len;
  22. int err;
  23. _ARGCHK(msg != NULL);
  24. _ARGCHK(out != NULL);
  25. _ARGCHK(outlen != NULL);
  26. _ARGCHK(res != NULL);
  27. /* default to invalid packet */
  28. *res = 0;
  29. /* test valid hash */
  30. if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
  31. return err;
  32. }
  33. hLen = hash_descriptor[hash_idx].hashsize;
  34. modulus_len = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0);
  35. /* allocate ram for DB/mask/salt of size modulus_len */
  36. DB = XMALLOC(modulus_len);
  37. mask = XMALLOC(modulus_len);
  38. seed = XMALLOC(modulus_len);
  39. if (DB == NULL || mask == NULL || seed == NULL) {
  40. if (DB != NULL) {
  41. XFREE(DB);
  42. }
  43. if (mask != NULL) {
  44. XFREE(mask);
  45. }
  46. if (seed != NULL) {
  47. XFREE(seed);
  48. }
  49. return CRYPT_MEM;
  50. }
  51. /* test message size */
  52. if (msglen != modulus_len) {
  53. err = CRYPT_PK_INVALID_SIZE;
  54. goto __ERR;
  55. }
  56. /* ok so it's now in the form
  57. 0x00 || maskedseed || maskedDB
  58. 1 || hLen || modulus_len - hLen - 1
  59. */
  60. /* must have leading 0x00 byte */
  61. if (msg[0] != 0x00) {
  62. err = CRYPT_OK;
  63. goto __ERR;
  64. }
  65. /* now read the masked seed */
  66. for (x = 1, y = 0; y < hLen; y++) {
  67. seed[y] = msg[x++];
  68. }
  69. /* now read the masked DB */
  70. for (y = 0; y < modulus_len - hLen - 1; y++) {
  71. DB[y] = msg[x++];
  72. }
  73. /* compute MGF1 of maskedDB (hLen) */
  74. if ((err = pkcs_1_mgf1(DB, modulus_len - hLen - 1, hash_idx, mask, hLen)) != CRYPT_OK) {
  75. goto __ERR;
  76. }
  77. /* XOR against seed */
  78. for (y = 0; y < hLen; y++) {
  79. seed[y] ^= mask[y];
  80. }
  81. /* compute MGF1 of seed (k - hlen - 1) */
  82. if ((err = pkcs_1_mgf1(seed, hLen, hash_idx, mask, modulus_len - hLen - 1)) != CRYPT_OK) {
  83. goto __ERR;
  84. }
  85. /* xor against DB */
  86. for (y = 0; y < (modulus_len - hLen - 1); y++) {
  87. DB[y] ^= mask[y];
  88. }
  89. /* now DB == lhash || PS || 0x01 || M, PS == k - mlen - 2hlen - 2 zeroes */
  90. /* compute lhash and store it in seed [reuse temps!] */
  91. x = modulus_len;
  92. if (lparam != NULL) {
  93. if ((err = hash_memory(hash_idx, lparam, lparamlen, seed, &x)) != CRYPT_OK) {
  94. goto __ERR;
  95. }
  96. } else {
  97. /* can't pass hash_memory a NULL so use DB with zero length */
  98. if ((err = hash_memory(hash_idx, DB, 0, seed, &x)) != CRYPT_OK) {
  99. goto __ERR;
  100. }
  101. }
  102. /* compare the lhash'es */
  103. if (memcmp(seed, DB, hLen) != 0) {
  104. err = CRYPT_OK;
  105. goto __ERR;
  106. }
  107. /* now zeroes before a 0x01 */
  108. for (x = hLen; x < (modulus_len - hLen - 1) && DB[x] == 0x00; x++) {
  109. /* step... */
  110. }
  111. /* error out if wasn't 0x01 */
  112. if (x == (modulus_len - hLen - 1) || DB[x] != 0x01) {
  113. err = CRYPT_OK;
  114. goto __ERR;
  115. }
  116. /* rest is the message (and skip 0x01) */
  117. if ((modulus_len - hLen - 1) - ++x > *outlen) {
  118. err = CRYPT_BUFFER_OVERFLOW;
  119. goto __ERR;
  120. }
  121. /* copy message */
  122. *outlen = (modulus_len - hLen - 1) - x;
  123. for (y = 0; x != (modulus_len - hLen - 1); ) {
  124. out[y++] = DB[x++];
  125. }
  126. /* valid packet */
  127. *res = 1;
  128. err = CRYPT_OK;
  129. __ERR:
  130. #ifdef CLEAN_STACK
  131. zeromem(DB, modulus_len);
  132. zeromem(seed, modulus_len);
  133. zeromem(mask, modulus_len);
  134. #endif
  135. XFREE(seed);
  136. XFREE(mask);
  137. XFREE(DB);
  138. return err;
  139. }
  140. #endif /* PKCS_1 */