| // |
| 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/ecdsa.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <string.h> |
| 19 |
| 20 | #include <openssl/bn.h> |
| 21 | #include <openssl/err.h> |
| 22 | #include <openssl/mem.h> |
| 23 |
| 24 | #include "../../internal.h" |
| 25 | #include "../bn/internal.h" |
| 26 | #include "../ec/internal.h" |
| 27 | #include "../service_indicator/internal.h" |
| 28 | #include "internal.h" |
| 29 |
| 30 |
| 31 | // digest_to_scalar interprets |digest_len| bytes from |digest| as a scalar for |
| 32 | // ECDSA. |
| 33 | static void digest_to_scalar(const EC_GROUP *group, EC_SCALAR *out, |
| 34 | const uint8_t *digest, size_t digest_len) { |
| 35 | const BIGNUM *order = EC_GROUP_get0_order(group); |
| 36 | size_t num_bits = BN_num_bits(order); |
| 37 | // Need to truncate digest if it is too long: first truncate whole bytes. |
| 38 | size_t num_bytes = (num_bits + 7) / 8; |
| 39 | if (digest_len > num_bytes) { |
| 40 | digest_len = num_bytes; |
| 41 | } |
| 42 | bn_big_endian_to_words(out->words, order->width, digest, digest_len); |
| 43 |
| 44 | // If it is still too long, truncate remaining bits with a shift. |
| 45 | if (8 * digest_len > num_bits) { |
| 46 | bn_rshift_words(out->words, out->words, 8 - (num_bits & 0x7), order->width); |
| 47 | } |
| 48 |
| 49 | // |out| now has the same bit width as |order|, but this only bounds by |
| 50 | // 2*|order|. Subtract the order if out of range. |
| 51 | // |
| 52 | // Montgomery multiplication accepts the looser bounds, so this isn't strictly |
| 53 | // necessary, but it is a cleaner abstraction and has no performance impact. |
| 54 | BN_ULONG tmp[EC_MAX_WORDS]; |
| 55 | bn_reduce_once_in_place(out->words, 0 /* no carry */, order->d, tmp, |
| 56 | order->width); |
| 57 | } |
| 58 |
| 59 | int ecdsa_verify_fixed_no_self_test(const uint8_t *digest, size_t digest_len, |
| 60 | const uint8_t *sig, size_t sig_len, |
| 61 | const EC_KEY *eckey) { |
| 62 | const EC_GROUP *group = EC_KEY_get0_group(eckey); |
| 63 | const EC_POINT *pub_key = EC_KEY_get0_public_key(eckey); |
| 64 | if (group == NULL || pub_key == NULL || sig == NULL) { |
| 65 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_MISSING_PARAMETERS); |
| 66 | return 0; |
| 67 | } |
| 68 |
| 69 | size_t scalar_len = BN_num_bytes(EC_GROUP_get0_order(group)); |
| 70 | EC_SCALAR r, s, u1, u2, s_inv_mont, m; |
| 71 | if (sig_len != 2 * scalar_len || |
| 72 | !ec_scalar_from_bytes(group, &r, sig, scalar_len) || |
| 73 | ec_scalar_is_zero(group, &r) || |
| 74 | !ec_scalar_from_bytes(group, &s, sig + scalar_len, scalar_len) || |
| 75 | ec_scalar_is_zero(group, &s)) { |
| 76 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_BAD_SIGNATURE); |
| 77 | return 0; |
| 78 | } |
| 79 |
| 80 | // s_inv_mont = s^-1 in the Montgomery domain. |
| 81 | if (!ec_scalar_to_montgomery_inv_vartime(group, &s_inv_mont, &s)) { |
| 82 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_INTERNAL_ERROR); |
| 83 | return 0; |
| 84 | } |
| 85 |
| 86 | // u1 = m * s^-1 mod order |
| 87 | // u2 = r * s^-1 mod order |
| 88 | // |
| 89 | // |s_inv_mont| is in Montgomery form while |m| and |r| are not, so |u1| and |
| 90 | // |u2| will be taken out of Montgomery form, as desired. |
| 91 | digest_to_scalar(group, &m, digest, digest_len); |
| 92 | ec_scalar_mul_montgomery(group, &u1, &m, &s_inv_mont); |
| 93 | ec_scalar_mul_montgomery(group, &u2, &r, &s_inv_mont); |
| 94 |
| 95 | EC_JACOBIAN point; |
| 96 | if (!ec_point_mul_scalar_public(group, &point, &u1, &pub_key->raw, &u2)) { |
| 97 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB); |
| 98 | return 0; |
| 99 | } |
| 100 |
| 101 | if (!ec_cmp_x_coordinate(group, &point, &r)) { |
| 102 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_BAD_SIGNATURE); |
| 103 | return 0; |
| 104 | } |
| 105 |
| 106 | return 1; |
| 107 | } |
| 108 |
| 109 | int ecdsa_verify_fixed(const uint8_t *digest, size_t digest_len, |
| 110 | const uint8_t *sig, size_t sig_len, const EC_KEY *key) { |
| 111 | boringssl_ensure_ecc_self_test(); |
| 112 |
| 113 | return ecdsa_verify_fixed_no_self_test(digest, digest_len, sig, sig_len, key); |
| 114 | } |
| 115 |
| 116 | static int ecdsa_sign_impl(const EC_GROUP *group, int *out_retry, uint8_t *sig, |
| 117 | size_t *out_sig_len, size_t max_sig_len, |
| 118 | const EC_SCALAR *priv_key, const EC_SCALAR *k, |
| 119 | const uint8_t *digest, size_t digest_len) { |
| 120 | *out_retry = 0; |
| 121 |
| 122 | // Check that the size of the group order is FIPS compliant (FIPS 186-4 |
| 123 | // B.5.2). |
| 124 | const BIGNUM *order = EC_GROUP_get0_order(group); |
| 125 | if (BN_num_bits(order) < 160) { |
| 126 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_GROUP_ORDER); |
| 127 | return 0; |
| 128 | } |
| 129 |
| 130 | size_t sig_len = 2 * BN_num_bytes(order); |
| 131 | if (sig_len > max_sig_len) { |
| 132 | OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL); |
| 133 | return 0; |
| 134 | } |
| 135 |
| 136 | // Compute r, the x-coordinate of k * generator. |
| 137 | EC_JACOBIAN tmp_point; |
| 138 | EC_SCALAR r; |
| 139 | if (!ec_point_mul_scalar_base(group, &tmp_point, k) || |
| 140 | !ec_get_x_coordinate_as_scalar(group, &r, &tmp_point)) { |
| 141 | return 0; |
| 142 | } |
| 143 |
| 144 | if (constant_time_declassify_int(ec_scalar_is_zero(group, &r))) { |
| 145 | *out_retry = 1; |
| 146 | return 0; |
| 147 | } |
| 148 |
| 149 | // s = priv_key * r. Note if only one parameter is in the Montgomery domain, |
| 150 | // |ec_scalar_mod_mul_montgomery| will compute the answer in the normal |
| 151 | // domain. |
| 152 | EC_SCALAR s; |
| 153 | ec_scalar_to_montgomery(group, &s, &r); |
| 154 | ec_scalar_mul_montgomery(group, &s, priv_key, &s); |
| 155 |
| 156 | // s = m + priv_key * r. |
| 157 | EC_SCALAR tmp; |
| 158 | digest_to_scalar(group, &tmp, digest, digest_len); |
| 159 | ec_scalar_add(group, &s, &s, &tmp); |
| 160 |
| 161 | // s = k^-1 * (m + priv_key * r). First, we compute k^-1 in the Montgomery |
| 162 | // domain. This is |ec_scalar_to_montgomery| followed by |
| 163 | // |ec_scalar_inv0_montgomery|, but |ec_scalar_inv0_montgomery| followed by |
| 164 | // |ec_scalar_from_montgomery| is equivalent and slightly more efficient. |
| 165 | // Then, as above, only one parameter is in the Montgomery domain, so the |
| 166 | // result is in the normal domain. Finally, note k is non-zero (or computing r |
| 167 | // would fail), so the inverse must exist. |
| 168 | ec_scalar_inv0_montgomery(group, &tmp, k); // tmp = k^-1 R^2 |
| 169 | ec_scalar_from_montgomery(group, &tmp, &tmp); // tmp = k^-1 R |
| 170 | ec_scalar_mul_montgomery(group, &s, &s, &tmp); |
| 171 | if (constant_time_declassify_int(ec_scalar_is_zero(group, &s))) { |
| 172 | *out_retry = 1; |
| 173 | return 0; |
| 174 | } |
| 175 |
| 176 | CONSTTIME_DECLASSIFY(r.words, sizeof(r.words)); |
| 177 | CONSTTIME_DECLASSIFY(s.words, sizeof(r.words)); |
| 178 | size_t len; |
| 179 | ec_scalar_to_bytes(group, sig, &len, &r); |
| 180 | assert(len == sig_len / 2); |
| 181 | ec_scalar_to_bytes(group, sig + len, &len, &s); |
| 182 | assert(len == sig_len / 2); |
| 183 | *out_sig_len = sig_len; |
| 184 | return 1; |
| 185 | } |
| 186 |
| 187 | int ecdsa_sign_fixed_with_nonce_for_known_answer_test( |
| 188 | const uint8_t *digest, size_t digest_len, uint8_t *sig, size_t *out_sig_len, |
| 189 | size_t max_sig_len, const EC_KEY *eckey, const uint8_t *nonce, |
| 190 | size_t nonce_len) { |
| 191 | if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) { |
| 192 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NOT_IMPLEMENTED); |
| 193 | return 0; |
| 194 | } |
| 195 |
| 196 | const EC_GROUP *group = EC_KEY_get0_group(eckey); |
| 197 | if (group == NULL || eckey->priv_key == NULL) { |
| 198 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_PASSED_NULL_PARAMETER); |
| 199 | return 0; |
| 200 | } |
| 201 | const EC_SCALAR *priv_key = &eckey->priv_key->scalar; |
| 202 |
| 203 | EC_SCALAR k; |
| 204 | if (!ec_scalar_from_bytes(group, &k, nonce, nonce_len)) { |
| 205 | return 0; |
| 206 | } |
| 207 | int retry_ignored; |
| 208 | return ecdsa_sign_impl(group, &retry_ignored, sig, out_sig_len, max_sig_len, |
| 209 | priv_key, &k, digest, digest_len); |
| 210 | } |
| 211 |
| 212 | int ecdsa_sign_fixed(const uint8_t *digest, size_t digest_len, uint8_t *sig, |
| 213 | size_t *out_sig_len, size_t max_sig_len, |
| 214 | const EC_KEY *eckey) { |
| 215 | boringssl_ensure_ecc_self_test(); |
| 216 |
| 217 | if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) { |
| 218 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NOT_IMPLEMENTED); |
| 219 | return 0; |
| 220 | } |
| 221 |
| 222 | const EC_GROUP *group = EC_KEY_get0_group(eckey); |
| 223 | if (group == NULL || eckey->priv_key == NULL) { |
| 224 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_PASSED_NULL_PARAMETER); |
| 225 | return 0; |
| 226 | } |
| 227 | const BIGNUM *order = EC_GROUP_get0_order(group); |
| 228 | const EC_SCALAR *priv_key = &eckey->priv_key->scalar; |
| 229 |
| 230 | // Pass a SHA512 hash of the private key and digest as additional data |
| 231 | // into the RBG. This is a hardening measure against entropy failure. |
| 232 | static_assert(BCM_SHA512_DIGEST_LENGTH >= 32, |
| 233 | "additional_data is too large for SHA-512"); |
| 234 |
| 235 | FIPS_service_indicator_lock_state(); |
| 236 |
| 237 | SHA512_CTX sha; |
| 238 | uint8_t additional_data[BCM_SHA512_DIGEST_LENGTH]; |
| 239 | BCM_sha512_init(&sha); |
| 240 | BCM_sha512_update(&sha, priv_key->words, order->width * sizeof(BN_ULONG)); |
| 241 | BCM_sha512_update(&sha, digest, digest_len); |
| 242 | BCM_sha512_final(additional_data, &sha); |
| 243 |
| 244 | // Cap iterations so callers who supply invalid values as custom groups do not |
| 245 | // infinite loop. This does not impact valid parameters (e.g. those covered by |
| 246 | // FIPS) because the probability of requiring even one retry is negligible, |
| 247 | // let alone 32. |
| 248 | static const int kMaxIterations = 32; |
| 249 | int ret = 0; |
| 250 | int iters = 0; |
| 251 | for (;;) { |
| 252 | EC_SCALAR k; |
| 253 | if (!ec_random_nonzero_scalar(group, &k, additional_data)) { |
| 254 | goto out; |
| 255 | } |
| 256 |
| 257 | // TODO(davidben): Move this inside |ec_random_nonzero_scalar| or lower, so |
| 258 | // that all scalars we generate are, by default, secret. |
| 259 | CONSTTIME_SECRET(k.words, sizeof(k.words)); |
| 260 |
| 261 | int retry; |
| 262 | ret = ecdsa_sign_impl(group, &retry, sig, out_sig_len, max_sig_len, |
| 263 | priv_key, &k, digest, digest_len); |
| 264 | if (ret || !retry) { |
| 265 | goto out; |
| 266 | } |
| 267 |
| 268 | iters++; |
| 269 | if (iters > kMaxIterations) { |
| 270 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_TOO_MANY_ITERATIONS); |
| 271 | goto out; |
| 272 | } |
| 273 | } |
| 274 |
| 275 | out: |
| 276 | FIPS_service_indicator_unlock_state(); |
| 277 | return ret; |
| 278 | } |
| 279 |