rsa_pk1.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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.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 < RSA_PKCS1_PADDING_SIZE)
  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 - RSA_PKCS1_PADDING_SIZE)) {
  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, mask;
  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 || num < RSA_PKCS1_PADDING_SIZE) {
  144. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,
  145. RSA_R_PKCS_DECODING_ERROR);
  146. return -1;
  147. }
  148. em = OPENSSL_malloc(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. Trouble is that since we can't read out of |from|'s
  156. * bounds, it's impossible to have an invariant memory access pattern
  157. * in case |from| was not zero-padded in advance.
  158. */
  159. for (from += flen, em += num, i = 0; i < num; i++) {
  160. mask = ~constant_time_is_zero(flen);
  161. flen -= 1 & mask;
  162. from -= 1 & mask;
  163. *--em = *from & mask;
  164. }
  165. good = constant_time_is_zero(em[0]);
  166. good &= constant_time_eq(em[1], 2);
  167. /* scan over padding data */
  168. found_zero_byte = 0;
  169. for (i = 2; i < num; i++) {
  170. unsigned int equals0 = constant_time_is_zero(em[i]);
  171. zero_index = constant_time_select_int(~found_zero_byte & equals0,
  172. i, zero_index);
  173. found_zero_byte |= equals0;
  174. }
  175. /*
  176. * PS must be at least 8 bytes long, and it starts two bytes into |em|.
  177. * If we never found a 0-byte, then |zero_index| is 0 and the check
  178. * also fails.
  179. */
  180. good &= constant_time_ge(zero_index, 2 + 8);
  181. /*
  182. * Skip the zero byte. This is incorrect if we never found a zero-byte
  183. * but in this case we also do not copy the message out.
  184. */
  185. msg_index = zero_index + 1;
  186. mlen = num - msg_index;
  187. /*
  188. * For good measure, do this check in constant time as well.
  189. */
  190. good &= constant_time_ge(tlen, mlen);
  191. /*
  192. * Move the result in-place by |num|-RSA_PKCS1_PADDING_SIZE-|mlen| bytes to the left.
  193. * Then if |good| move |mlen| bytes from |em|+RSA_PKCS1_PADDING_SIZE to |to|.
  194. * Otherwise leave |to| unchanged.
  195. * Copy the memory back in a way that does not reveal the size of
  196. * the data being copied via a timing side channel. This requires copying
  197. * parts of the buffer multiple times based on the bits set in the real
  198. * length. Clear bits do a non-copy with identical access pattern.
  199. * The loop below has overall complexity of O(N*log(N)).
  200. */
  201. tlen = constant_time_select_int(constant_time_lt(num - RSA_PKCS1_PADDING_SIZE, tlen),
  202. num - RSA_PKCS1_PADDING_SIZE, tlen);
  203. for (msg_index = 1; msg_index < num - RSA_PKCS1_PADDING_SIZE; msg_index <<= 1) {
  204. mask = ~constant_time_eq(msg_index & (num - RSA_PKCS1_PADDING_SIZE - mlen), 0);
  205. for (i = RSA_PKCS1_PADDING_SIZE; i < num - msg_index; i++)
  206. em[i] = constant_time_select_8(mask, em[i + msg_index], em[i]);
  207. }
  208. for (i = 0; i < tlen; i++) {
  209. mask = good & constant_time_lt(i, mlen);
  210. to[i] = constant_time_select_8(mask, em[i + RSA_PKCS1_PADDING_SIZE], to[i]);
  211. }
  212. OPENSSL_clear_free(em, num);
  213. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2, RSA_R_PKCS_DECODING_ERROR);
  214. err_clear_last_constant_time(1 & good);
  215. return constant_time_select_int(good, mlen, -1);
  216. }