rsa_pk1.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/constant_time_locl.h"
  10. #include <stdio.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/bn.h>
  13. #include <openssl/rsa.h>
  14. #include <openssl/rand.h>
  15. int RSA_padding_add_PKCS1_type_1(unsigned char *to, int tlen,
  16. const unsigned char *from, int flen)
  17. {
  18. int j;
  19. unsigned char *p;
  20. if (flen > (tlen - RSA_PKCS1_PADDING_SIZE)) {
  21. RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1,
  22. RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
  23. return 0;
  24. }
  25. p = (unsigned char *)to;
  26. *(p++) = 0;
  27. *(p++) = 1; /* Private Key BT (Block Type) */
  28. /* pad out with 0xff data */
  29. j = tlen - 3 - flen;
  30. memset(p, 0xff, j);
  31. p += j;
  32. *(p++) = '\0';
  33. memcpy(p, from, (unsigned int)flen);
  34. return 1;
  35. }
  36. int RSA_padding_check_PKCS1_type_1(unsigned char *to, int tlen,
  37. const unsigned char *from, int flen,
  38. int num)
  39. {
  40. int i, j;
  41. const unsigned char *p;
  42. p = from;
  43. /*
  44. * The format is
  45. * 00 || 01 || PS || 00 || D
  46. * PS - padding string, at least 8 bytes of FF
  47. * D - data.
  48. */
  49. if (num < 11)
  50. return -1;
  51. /* Accept inputs with and without the leading 0-byte. */
  52. if (num == flen) {
  53. if ((*p++) != 0x00) {
  54. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
  55. RSA_R_INVALID_PADDING);
  56. return -1;
  57. }
  58. flen--;
  59. }
  60. if ((num != (flen + 1)) || (*(p++) != 0x01)) {
  61. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
  62. RSA_R_BLOCK_TYPE_IS_NOT_01);
  63. return -1;
  64. }
  65. /* scan over padding data */
  66. j = flen - 1; /* one for type. */
  67. for (i = 0; i < j; i++) {
  68. if (*p != 0xff) { /* should decrypt to 0xff */
  69. if (*p == 0) {
  70. p++;
  71. break;
  72. } else {
  73. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
  74. RSA_R_BAD_FIXED_HEADER_DECRYPT);
  75. return -1;
  76. }
  77. }
  78. p++;
  79. }
  80. if (i == j) {
  81. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
  82. RSA_R_NULL_BEFORE_BLOCK_MISSING);
  83. return -1;
  84. }
  85. if (i < 8) {
  86. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
  87. RSA_R_BAD_PAD_BYTE_COUNT);
  88. return -1;
  89. }
  90. i++; /* Skip over the '\0' */
  91. j -= i;
  92. if (j > tlen) {
  93. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1, RSA_R_DATA_TOO_LARGE);
  94. return -1;
  95. }
  96. memcpy(to, p, (unsigned int)j);
  97. return j;
  98. }
  99. int RSA_padding_add_PKCS1_type_2(unsigned char *to, int tlen,
  100. const unsigned char *from, int flen)
  101. {
  102. int i, j;
  103. unsigned char *p;
  104. if (flen > (tlen - 11)) {
  105. RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2,
  106. RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
  107. return 0;
  108. }
  109. p = (unsigned char *)to;
  110. *(p++) = 0;
  111. *(p++) = 2; /* Public Key BT (Block Type) */
  112. /* pad out with non-zero random data */
  113. j = tlen - 3 - flen;
  114. if (RAND_bytes(p, j) <= 0)
  115. return 0;
  116. for (i = 0; i < j; i++) {
  117. if (*p == '\0')
  118. do {
  119. if (RAND_bytes(p, 1) <= 0)
  120. return 0;
  121. } while (*p == '\0');
  122. p++;
  123. }
  124. *(p++) = '\0';
  125. memcpy(p, from, (unsigned int)flen);
  126. return 1;
  127. }
  128. int RSA_padding_check_PKCS1_type_2(unsigned char *to, int tlen,
  129. const unsigned char *from, int flen,
  130. int num)
  131. {
  132. int i;
  133. /* |em| is the encoded message, zero-padded to exactly |num| bytes */
  134. unsigned char *em = NULL;
  135. unsigned int good, found_zero_byte;
  136. int zero_index = 0, msg_index, mlen = -1;
  137. if (tlen < 0 || flen < 0)
  138. return -1;
  139. /*
  140. * PKCS#1 v1.5 decryption. See "PKCS #1 v2.2: RSA Cryptography Standard",
  141. * section 7.2.2.
  142. */
  143. if (flen > num)
  144. goto err;
  145. if (num < 11)
  146. goto err;
  147. if (flen != num) {
  148. em = OPENSSL_zalloc(num);
  149. if (em == NULL) {
  150. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2, ERR_R_MALLOC_FAILURE);
  151. return -1;
  152. }
  153. /*
  154. * Caller is encouraged to pass zero-padded message created with
  155. * BN_bn2binpad, but if it doesn't, we do this zero-padding copy
  156. * to avoid leaking that information. The copy still leaks some
  157. * side-channel information, but it's impossible to have a fixed
  158. * memory access pattern since we can't read out of the bounds of
  159. * |from|.
  160. */
  161. memcpy(em + num - flen, from, flen);
  162. from = em;
  163. }
  164. good = constant_time_is_zero(from[0]);
  165. good &= constant_time_eq(from[1], 2);
  166. found_zero_byte = 0;
  167. for (i = 2; i < num; i++) {
  168. unsigned int equals0 = constant_time_is_zero(from[i]);
  169. zero_index =
  170. constant_time_select_int(~found_zero_byte & equals0, i,
  171. zero_index);
  172. found_zero_byte |= equals0;
  173. }
  174. /*
  175. * PS must be at least 8 bytes long, and it starts two bytes into |from|.
  176. * If we never found a 0-byte, then |zero_index| is 0 and the check
  177. * also fails.
  178. */
  179. good &= constant_time_ge((unsigned int)(zero_index), 2 + 8);
  180. /*
  181. * Skip the zero byte. This is incorrect if we never found a zero-byte
  182. * but in this case we also do not copy the message out.
  183. */
  184. msg_index = zero_index + 1;
  185. mlen = num - msg_index;
  186. /*
  187. * For good measure, do this check in constant time as well; it could
  188. * leak something if |tlen| was assuming valid padding.
  189. */
  190. good &= constant_time_ge((unsigned int)(tlen), (unsigned int)(mlen));
  191. /*
  192. * We can't continue in constant-time because we need to copy the result
  193. * and we cannot fake its length. This unavoidably leaks timing
  194. * information at the API boundary.
  195. */
  196. if (!good) {
  197. mlen = -1;
  198. goto err;
  199. }
  200. memcpy(to, from + msg_index, mlen);
  201. err:
  202. OPENSSL_clear_free(em, num);
  203. if (mlen == -1)
  204. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,
  205. RSA_R_PKCS_DECODING_ERROR);
  206. return mlen;
  207. }