| // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/rsa.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <limits.h> |
| 19 | #include <string.h> |
| 20 |
| 21 | #include <openssl/bn.h> |
| 22 | #include <openssl/digest.h> |
| 23 | #include <openssl/err.h> |
| 24 | #include <openssl/mem.h> |
| 25 |
| 26 | #include "../../internal.h" |
| 27 | #include "../bcm_interface.h" |
| 28 | #include "../service_indicator/internal.h" |
| 29 | #include "internal.h" |
| 30 |
| 31 |
| 32 | int RSA_padding_add_PKCS1_type_1(uint8_t *to, size_t to_len, |
| 33 | const uint8_t *from, size_t from_len) { |
| 34 | // See RFC 8017, section 9.2. |
| 35 | if (to_len < RSA_PKCS1_PADDING_SIZE) { |
| 36 | OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL); |
| 37 | return 0; |
| 38 | } |
| 39 |
| 40 | if (from_len > to_len - RSA_PKCS1_PADDING_SIZE) { |
| 41 | OPENSSL_PUT_ERROR(RSA, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY); |
| 42 | return 0; |
| 43 | } |
| 44 |
| 45 | to[0] = 0; |
| 46 | to[1] = 1; |
| 47 | OPENSSL_memset(to + 2, 0xff, to_len - 3 - from_len); |
| 48 | to[to_len - from_len - 1] = 0; |
| 49 | OPENSSL_memcpy(to + to_len - from_len, from, from_len); |
| 50 | return 1; |
| 51 | } |
| 52 |
| 53 | int RSA_padding_check_PKCS1_type_1(uint8_t *out, size_t *out_len, |
| 54 | size_t max_out, const uint8_t *from, |
| 55 | size_t from_len) { |
| 56 | // See RFC 8017, section 9.2. This is part of signature verification and thus |
| 57 | // does not need to run in constant-time. |
| 58 | if (from_len < 2) { |
| 59 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_SMALL); |
| 60 | return 0; |
| 61 | } |
| 62 |
| 63 | // Check the header. |
| 64 | if (from[0] != 0 || from[1] != 1) { |
| 65 | OPENSSL_PUT_ERROR(RSA, RSA_R_BLOCK_TYPE_IS_NOT_01); |
| 66 | return 0; |
| 67 | } |
| 68 |
| 69 | // Scan over padded data, looking for the 00. |
| 70 | size_t pad; |
| 71 | for (pad = 2 /* header */; pad < from_len; pad++) { |
| 72 | if (from[pad] == 0x00) { |
| 73 | break; |
| 74 | } |
| 75 |
| 76 | if (from[pad] != 0xff) { |
| 77 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_FIXED_HEADER_DECRYPT); |
| 78 | return 0; |
| 79 | } |
| 80 | } |
| 81 |
| 82 | if (pad == from_len) { |
| 83 | OPENSSL_PUT_ERROR(RSA, RSA_R_NULL_BEFORE_BLOCK_MISSING); |
| 84 | return 0; |
| 85 | } |
| 86 |
| 87 | if (pad < 2 /* header */ + 8) { |
| 88 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_PAD_BYTE_COUNT); |
| 89 | return 0; |
| 90 | } |
| 91 |
| 92 | // Skip over the 00. |
| 93 | pad++; |
| 94 |
| 95 | if (from_len - pad > max_out) { |
| 96 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE); |
| 97 | return 0; |
| 98 | } |
| 99 |
| 100 | OPENSSL_memcpy(out, from + pad, from_len - pad); |
| 101 | *out_len = from_len - pad; |
| 102 | return 1; |
| 103 | } |
| 104 |
| 105 | int RSA_padding_add_none(uint8_t *to, size_t to_len, const uint8_t *from, |
| 106 | size_t from_len) { |
| 107 | if (from_len > to_len) { |
| 108 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); |
| 109 | return 0; |
| 110 | } |
| 111 |
| 112 | if (from_len < to_len) { |
| 113 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_SMALL); |
| 114 | return 0; |
| 115 | } |
| 116 |
| 117 | OPENSSL_memcpy(to, from, from_len); |
| 118 | return 1; |
| 119 | } |
| 120 |
| 121 | int PKCS1_MGF1(uint8_t *out, size_t len, const uint8_t *seed, size_t seed_len, |
| 122 | const EVP_MD *md) { |
| 123 | int ret = 0; |
| 124 | EVP_MD_CTX ctx; |
| 125 | EVP_MD_CTX_init(&ctx); |
| 126 | FIPS_service_indicator_lock_state(); |
| 127 |
| 128 | size_t md_len = EVP_MD_size(md); |
| 129 |
| 130 | for (uint32_t i = 0; len > 0; i++) { |
| 131 | uint8_t counter[4]; |
| 132 | counter[0] = (uint8_t)(i >> 24); |
| 133 | counter[1] = (uint8_t)(i >> 16); |
| 134 | counter[2] = (uint8_t)(i >> 8); |
| 135 | counter[3] = (uint8_t)i; |
| 136 | if (!EVP_DigestInit_ex(&ctx, md, NULL) || |
| 137 | !EVP_DigestUpdate(&ctx, seed, seed_len) || |
| 138 | !EVP_DigestUpdate(&ctx, counter, sizeof(counter))) { |
| 139 | goto err; |
| 140 | } |
| 141 |
| 142 | if (md_len <= len) { |
| 143 | if (!EVP_DigestFinal_ex(&ctx, out, NULL)) { |
| 144 | goto err; |
| 145 | } |
| 146 | out += md_len; |
| 147 | len -= md_len; |
| 148 | } else { |
| 149 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 150 | if (!EVP_DigestFinal_ex(&ctx, digest, NULL)) { |
| 151 | goto err; |
| 152 | } |
| 153 | OPENSSL_memcpy(out, digest, len); |
| 154 | len = 0; |
| 155 | } |
| 156 | } |
| 157 |
| 158 | ret = 1; |
| 159 |
| 160 | err: |
| 161 | EVP_MD_CTX_cleanup(&ctx); |
| 162 | FIPS_service_indicator_unlock_state(); |
| 163 | return ret; |
| 164 | } |
| 165 |
| 166 | static const uint8_t kPSSZeroes[] = {0, 0, 0, 0, 0, 0, 0, 0}; |
| 167 |
| 168 | int RSA_verify_PKCS1_PSS_mgf1(const RSA *rsa, const uint8_t *mHash, |
| 169 | const EVP_MD *Hash, const EVP_MD *mgf1Hash, |
| 170 | const uint8_t *EM, int sLen) { |
| 171 | if (mgf1Hash == NULL) { |
| 172 | mgf1Hash = Hash; |
| 173 | } |
| 174 |
| 175 | int ret = 0; |
| 176 | uint8_t *DB = NULL; |
| 177 | const uint8_t *H; |
| 178 | EVP_MD_CTX ctx; |
| 179 | EVP_MD_CTX_init(&ctx); |
| 180 | unsigned MSBits; |
| 181 | size_t emLen, maskedDBLen, salt_start; |
| 182 | FIPS_service_indicator_lock_state(); |
| 183 |
| 184 | // Negative sLen has special meanings: |
| 185 | // -1 sLen == hLen |
| 186 | // -2 salt length is autorecovered from signature |
| 187 | // -N reserved |
| 188 | size_t hLen = EVP_MD_size(Hash); |
| 189 | if (sLen == -1) { |
| 190 | sLen = (int)hLen; |
| 191 | } else if (sLen == -2) { |
| 192 | sLen = -2; |
| 193 | } else if (sLen < -2) { |
| 194 | OPENSSL_PUT_ERROR(RSA, RSA_R_SLEN_CHECK_FAILED); |
| 195 | goto err; |
| 196 | } |
| 197 |
| 198 | MSBits = (BN_num_bits(rsa->n) - 1) & 0x7; |
| 199 | emLen = RSA_size(rsa); |
| 200 | if (EM[0] & (0xFF << MSBits)) { |
| 201 | OPENSSL_PUT_ERROR(RSA, RSA_R_FIRST_OCTET_INVALID); |
| 202 | goto err; |
| 203 | } |
| 204 | if (MSBits == 0) { |
| 205 | EM++; |
| 206 | emLen--; |
| 207 | } |
| 208 | // |sLen| may be -2 for the non-standard salt length recovery mode. |
| 209 | if (emLen < hLen + 2 || (sLen >= 0 && emLen < hLen + (size_t)sLen + 2)) { |
| 210 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE); |
| 211 | goto err; |
| 212 | } |
| 213 | if (EM[emLen - 1] != 0xbc) { |
| 214 | OPENSSL_PUT_ERROR(RSA, RSA_R_LAST_OCTET_INVALID); |
| 215 | goto err; |
| 216 | } |
| 217 | maskedDBLen = emLen - hLen - 1; |
| 218 | H = EM + maskedDBLen; |
| 219 | DB = reinterpret_cast<uint8_t *>(OPENSSL_malloc(maskedDBLen)); |
| 220 | if (!DB) { |
| 221 | goto err; |
| 222 | } |
| 223 | if (!PKCS1_MGF1(DB, maskedDBLen, H, hLen, mgf1Hash)) { |
| 224 | goto err; |
| 225 | } |
| 226 | for (size_t i = 0; i < maskedDBLen; i++) { |
| 227 | DB[i] ^= EM[i]; |
| 228 | } |
| 229 | if (MSBits) { |
| 230 | DB[0] &= 0xFF >> (8 - MSBits); |
| 231 | } |
| 232 | // This step differs slightly from EMSA-PSS-VERIFY (RFC 8017) step 10 because |
| 233 | // it accepts a non-standard salt recovery flow. DB should be some number of |
| 234 | // zeros, a one, then the salt. |
| 235 | for (salt_start = 0; DB[salt_start] == 0 && salt_start < maskedDBLen - 1; |
| 236 | salt_start++) { |
| 237 | ; |
| 238 | } |
| 239 | if (DB[salt_start] != 0x1) { |
| 240 | OPENSSL_PUT_ERROR(RSA, RSA_R_SLEN_RECOVERY_FAILED); |
| 241 | goto err; |
| 242 | } |
| 243 | salt_start++; |
| 244 | // If a salt length was specified, check it matches. |
| 245 | if (sLen >= 0 && maskedDBLen - salt_start != (size_t)sLen) { |
| 246 | OPENSSL_PUT_ERROR(RSA, RSA_R_SLEN_CHECK_FAILED); |
| 247 | goto err; |
| 248 | } |
| 249 | uint8_t H_[EVP_MAX_MD_SIZE]; |
| 250 | if (!EVP_DigestInit_ex(&ctx, Hash, NULL) || |
| 251 | !EVP_DigestUpdate(&ctx, kPSSZeroes, sizeof(kPSSZeroes)) || |
| 252 | !EVP_DigestUpdate(&ctx, mHash, hLen) || |
| 253 | !EVP_DigestUpdate(&ctx, DB + salt_start, maskedDBLen - salt_start) || |
| 254 | !EVP_DigestFinal_ex(&ctx, H_, NULL)) { |
| 255 | goto err; |
| 256 | } |
| 257 | if (OPENSSL_memcmp(H_, H, hLen) != 0) { |
| 258 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_SIGNATURE); |
| 259 | goto err; |
| 260 | } |
| 261 |
| 262 | ret = 1; |
| 263 |
| 264 | err: |
| 265 | OPENSSL_free(DB); |
| 266 | EVP_MD_CTX_cleanup(&ctx); |
| 267 | FIPS_service_indicator_unlock_state(); |
| 268 | return ret; |
| 269 | } |
| 270 |
| 271 | int RSA_padding_add_PKCS1_PSS_mgf1(const RSA *rsa, unsigned char *EM, |
| 272 | const unsigned char *mHash, |
| 273 | const EVP_MD *Hash, const EVP_MD *mgf1Hash, |
| 274 | int sLenRequested) { |
| 275 | int ret = 0, digest_ok; |
| 276 | size_t maskedDBLen, MSBits, emLen; |
| 277 | size_t hLen; |
| 278 | unsigned char *H, *salt = NULL, *p; |
| 279 |
| 280 | if (mgf1Hash == NULL) { |
| 281 | mgf1Hash = Hash; |
| 282 | } |
| 283 |
| 284 | FIPS_service_indicator_lock_state(); |
| 285 | hLen = EVP_MD_size(Hash); |
| 286 |
| 287 | if (BN_is_zero(rsa->n)) { |
| 288 | OPENSSL_PUT_ERROR(RSA, RSA_R_EMPTY_PUBLIC_KEY); |
| 289 | goto err; |
| 290 | } |
| 291 |
| 292 | MSBits = (BN_num_bits(rsa->n) - 1) & 0x7; |
| 293 | emLen = RSA_size(rsa); |
| 294 | if (MSBits == 0) { |
| 295 | assert(emLen >= 1); |
| 296 | *EM++ = 0; |
| 297 | emLen--; |
| 298 | } |
| 299 |
| 300 | if (emLen < hLen + 2) { |
| 301 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); |
| 302 | goto err; |
| 303 | } |
| 304 |
| 305 | // Negative sLenRequested has special meanings: |
| 306 | // -1 sLen == hLen |
| 307 | // -2 salt length is maximized |
| 308 | // -N reserved |
| 309 | size_t sLen; |
| 310 | if (sLenRequested == -1) { |
| 311 | sLen = hLen; |
| 312 | } else if (sLenRequested == -2) { |
| 313 | sLen = emLen - hLen - 2; |
| 314 | } else if (sLenRequested < 0) { |
| 315 | OPENSSL_PUT_ERROR(RSA, RSA_R_SLEN_CHECK_FAILED); |
| 316 | goto err; |
| 317 | } else { |
| 318 | sLen = (size_t)sLenRequested; |
| 319 | } |
| 320 |
| 321 | if (emLen - hLen - 2 < sLen) { |
| 322 | OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); |
| 323 | goto err; |
| 324 | } |
| 325 |
| 326 | if (sLen > 0) { |
| 327 | salt = reinterpret_cast<uint8_t *>(OPENSSL_malloc(sLen)); |
| 328 | if (!salt) { |
| 329 | goto err; |
| 330 | } |
| 331 | BCM_rand_bytes(salt, sLen); |
| 332 | } |
| 333 | maskedDBLen = emLen - hLen - 1; |
| 334 | H = EM + maskedDBLen; |
| 335 |
| 336 | EVP_MD_CTX ctx; |
| 337 | EVP_MD_CTX_init(&ctx); |
| 338 | digest_ok = EVP_DigestInit_ex(&ctx, Hash, NULL) && |
| 339 | EVP_DigestUpdate(&ctx, kPSSZeroes, sizeof(kPSSZeroes)) && |
| 340 | EVP_DigestUpdate(&ctx, mHash, hLen) && |
| 341 | EVP_DigestUpdate(&ctx, salt, sLen) && |
| 342 | EVP_DigestFinal_ex(&ctx, H, NULL); |
| 343 | EVP_MD_CTX_cleanup(&ctx); |
| 344 | if (!digest_ok) { |
| 345 | goto err; |
| 346 | } |
| 347 |
| 348 | // Generate dbMask in place then perform XOR on it |
| 349 | if (!PKCS1_MGF1(EM, maskedDBLen, H, hLen, mgf1Hash)) { |
| 350 | goto err; |
| 351 | } |
| 352 |
| 353 | p = EM; |
| 354 | // Initial PS XORs with all zeroes which is a NOP so just update |
| 355 | // pointer. Note from a test above this value is guaranteed to |
| 356 | // be non-negative. |
| 357 | p += emLen - sLen - hLen - 2; |
| 358 | *p++ ^= 0x1; |
| 359 | if (sLen > 0) { |
| 360 | for (size_t i = 0; i < sLen; i++) { |
| 361 | *p++ ^= salt[i]; |
| 362 | } |
| 363 | } |
| 364 | if (MSBits) { |
| 365 | EM[0] &= 0xFF >> (8 - MSBits); |
| 366 | } |
| 367 |
| 368 | // H is already in place so just set final 0xbc |
| 369 |
| 370 | EM[emLen - 1] = 0xbc; |
| 371 |
| 372 | ret = 1; |
| 373 |
| 374 | err: |
| 375 | OPENSSL_free(salt); |
| 376 | FIPS_service_indicator_unlock_state(); |
| 377 |
| 378 | return ret; |
| 379 | } |
| 380 |