static int bn_cmp_words_consttime(const BN_ULONG *a, size_t a_len, const BN_ULONG *b, size_t b_len) { OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t), crypto_word_t_too_small); int ret = 0; // Process the common words in little-endian order. size_t min = a_len < b_len ? a_len : b_len; for (size_t i = 0; i < min; i++) { crypto_word_t eq = constant_time_eq_w(a[i], b[i]); crypto_word_t lt = constant_time_lt_w(a[i], b[i]); ret = constant_time_select_int(eq, ret, constant_time_select_int(lt, -1, 1)); } // If |a| or |b| has non-zero words beyond |min|, they take precedence. if (a_len < b_len) { crypto_word_t mask = 0; for (size_t i = a_len; i < b_len; i++) { mask |= b[i]; } ret = constant_time_select_int(constant_time_is_zero_w(mask), ret, -1); } else if (b_len < a_len) { crypto_word_t mask = 0; for (size_t i = b_len; i < a_len; i++) { mask |= a[i]; } ret = constant_time_select_int(constant_time_is_zero_w(mask), ret, 1); } return ret; }
static void ec_GFp_simple_mul_single(const EC_GROUP *group, EC_RAW_POINT *r, const EC_RAW_POINT *p, const EC_SCALAR *scalar) { // This is a generic implementation for uncommon curves that not do not // warrant a tuned one. It uses unsigned digits so that the doubling case in // |ec_GFp_simple_add| is always unreachable, erring on safety and simplicity. // Compute a table of the first 32 multiples of |p| (including infinity). EC_RAW_POINT precomp[32]; ec_GFp_simple_point_set_to_infinity(group, &precomp[0]); ec_GFp_simple_point_copy(&precomp[1], p); for (size_t j = 2; j < OPENSSL_ARRAY_SIZE(precomp); j++) { if (j & 1) { ec_GFp_simple_add(group, &precomp[j], &precomp[1], &precomp[j - 1]); } else { ec_GFp_simple_dbl(group, &precomp[j], &precomp[j / 2]); } } // Divide bits in |scalar| into windows. unsigned bits = BN_num_bits(&group->order); int r_is_at_infinity = 1; for (unsigned i = bits - 1; i < bits; i--) { if (!r_is_at_infinity) { ec_GFp_simple_dbl(group, r, r); } if (i % 5 == 0) { // Compute the next window value. const size_t width = group->order.width; uint8_t window = bn_is_bit_set_words(scalar->words, width, i + 4) << 4; window |= bn_is_bit_set_words(scalar->words, width, i + 3) << 3; window |= bn_is_bit_set_words(scalar->words, width, i + 2) << 2; window |= bn_is_bit_set_words(scalar->words, width, i + 1) << 1; window |= bn_is_bit_set_words(scalar->words, width, i); // Select the entry in constant-time. EC_RAW_POINT tmp; OPENSSL_memset(&tmp, 0, sizeof(EC_RAW_POINT)); for (size_t j = 0; j < OPENSSL_ARRAY_SIZE(precomp); j++) { BN_ULONG mask = constant_time_eq_w(j, window); ec_felem_select(group, &tmp.X, mask, &precomp[j].X, &tmp.X); ec_felem_select(group, &tmp.Y, mask, &precomp[j].Y, &tmp.Y); ec_felem_select(group, &tmp.Z, mask, &precomp[j].Z, &tmp.Z); } if (r_is_at_infinity) { ec_GFp_simple_point_copy(r, &tmp); r_is_at_infinity = 0; } else { ec_GFp_simple_add(group, r, r, &tmp); } } } if (r_is_at_infinity) { ec_GFp_simple_point_set_to_infinity(group, r); } }
/* reverse_and_mulX_ghash interprets the bytes |b->c| as a reversed element of * the GHASH field, multiplies that by 'x' and serialises the result back into * |b|, but with GHASH's backwards bit ordering. */ static void reverse_and_mulX_ghash(polyval_block *b) { uint64_t hi = b->u[0]; uint64_t lo = b->u[1]; const crypto_word_t carry = constant_time_eq_w(hi & 1, 1); hi >>= 1; hi |= lo << 63; lo >>= 1; lo ^= ((uint64_t) constant_time_select_w(carry, 0xe1, 0)) << 56; b->u[0] = CRYPTO_bswap8(lo); b->u[1] = CRYPTO_bswap8(hi); }
int EVP_tls_cbc_remove_padding(crypto_word_t *out_padding_ok, size_t *out_len, const uint8_t *in, size_t in_len, size_t block_size, size_t mac_size) { const size_t overhead = 1 /* padding length byte */ + mac_size; /* These lengths are all public so we can test them in non-constant time. */ if (overhead > in_len) { return 0; } size_t padding_length = in[in_len - 1]; crypto_word_t good = constant_time_ge_w(in_len, overhead + padding_length); /* The padding consists of a length byte at the end of the record and * then that many bytes of padding, all with the same value as the * length byte. Thus, with the length byte included, there are i+1 * bytes of padding. * * We can't check just |padding_length+1| bytes because that leaks * decrypted information. Therefore we always have to check the maximum * amount of padding possible. (Again, the length of the record is * public information so we can use it.) */ size_t to_check = 256; /* maximum amount of padding, inc length byte. */ if (to_check > in_len) { to_check = in_len; } for (size_t i = 0; i < to_check; i++) { uint8_t mask = constant_time_ge_8(padding_length, i); uint8_t b = in[in_len - 1 - i]; /* The final |padding_length+1| bytes should all have the value * |padding_length|. Therefore the XOR should be zero. */ good &= ~(mask & (padding_length ^ b)); } /* If any of the final |padding_length+1| bytes had the wrong value, * one or more of the lower eight bits of |good| will be cleared. */ good = constant_time_eq_w(0xff, good & 0xff); /* Always treat |padding_length| as zero on error. If, assuming block size of * 16, a padding of [<15 arbitrary bytes> 15] treated |padding_length| as 16 * and returned -1, distinguishing good MAC and bad padding from bad MAC and * bad padding would give POODLE's padding oracle. */ padding_length = good & (padding_length + 1); *out_len = in_len - padding_length; *out_padding_ok = good; return 1; }
void EVP_tls_cbc_copy_mac(uint8_t *out, size_t md_size, const uint8_t *in, size_t in_len, size_t orig_len) { uint8_t rotated_mac1[EVP_MAX_MD_SIZE], rotated_mac2[EVP_MAX_MD_SIZE]; uint8_t *rotated_mac = rotated_mac1; uint8_t *rotated_mac_tmp = rotated_mac2; /* mac_end is the index of |in| just after the end of the MAC. */ size_t mac_end = in_len; size_t mac_start = mac_end - md_size; assert(orig_len >= in_len); assert(in_len >= md_size); assert(md_size <= EVP_MAX_MD_SIZE); /* scan_start contains the number of bytes that we can ignore because * the MAC's position can only vary by 255 bytes. */ size_t scan_start = 0; /* This information is public so it's safe to branch based on it. */ if (orig_len > md_size + 255 + 1) { scan_start = orig_len - (md_size + 255 + 1); } size_t rotate_offset = 0; uint8_t mac_started = 0; OPENSSL_memset(rotated_mac, 0, md_size); for (size_t i = scan_start, j = 0; i < orig_len; i++, j++) { if (j >= md_size) { j -= md_size; } crypto_word_t is_mac_start = constant_time_eq_w(i, mac_start); mac_started |= is_mac_start; uint8_t mac_ended = constant_time_ge_8(i, mac_end); rotated_mac[j] |= in[i] & mac_started & ~mac_ended; /* Save the offset that |mac_start| is mapped to. */ rotate_offset |= j & is_mac_start; } /* Now rotate the MAC. We rotate in log(md_size) steps, one for each bit * position. */ for (size_t offset = 1; offset < md_size; offset <<= 1, rotate_offset >>= 1) { /* Rotate by |offset| iff the corresponding bit is set in * |rotate_offset|, placing the result in |rotated_mac_tmp|. */ const uint8_t skip_rotate = (rotate_offset & 1) - 1; for (size_t i = 0, j = offset; i < md_size; i++, j++) { if (j >= md_size) { j -= md_size; } rotated_mac_tmp[i] = constant_time_select_8(skip_rotate, rotated_mac[i], rotated_mac[j]); } /* Swap pointers so |rotated_mac| contains the (possibly) rotated value. * Note the number of iterations and thus the identity of these pointers is * public information. */ uint8_t *tmp = rotated_mac; rotated_mac = rotated_mac_tmp; rotated_mac_tmp = tmp; } OPENSSL_memcpy(out, rotated_mac, md_size); }