/src/boringssl/crypto/spake2plus/spake2plus.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2024 The BoringSSL Authors |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
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/base.h> |
16 | | |
17 | | #include <assert.h> |
18 | | #include <string.h> |
19 | | |
20 | | #include <openssl/bn.h> |
21 | | #include <openssl/bytestring.h> |
22 | | #include <openssl/crypto.h> |
23 | | #include <openssl/ec.h> |
24 | | #include <openssl/err.h> |
25 | | #include <openssl/evp.h> |
26 | | #include <openssl/hkdf.h> |
27 | | #include <openssl/hmac.h> |
28 | | #include <openssl/mem.h> |
29 | | #include <openssl/rand.h> |
30 | | #include <openssl/sha.h> |
31 | | |
32 | | #include "../fipsmodule/bn/internal.h" |
33 | | #include "../fipsmodule/ec/internal.h" |
34 | | #include "../internal.h" |
35 | | #include "./internal.h" |
36 | | |
37 | | BSSL_NAMESPACE_BEGIN |
38 | | namespace spake2plus { |
39 | | namespace { |
40 | | |
41 | | const uint8_t kDefaultAdditionalData[32] = {0}; |
42 | | |
43 | | // https://www.rfc-editor.org/rfc/rfc9383.html#appendix-B |
44 | | // seed: 1.2.840.10045.3.1.7 point generation seed (M) |
45 | | // M = |
46 | | // 02886e2f97ace46e55ba9dd7242579f2993b64e16ef3dcab95afd497333d8fa12f |
47 | | // |
48 | | // `M` is interpreted as a X9.62-format compressed point. This is then the |
49 | | // uncompressed form: |
50 | | const uint8_t kM_bytes[] = { |
51 | | 0x04, 0x88, 0x6e, 0x2f, 0x97, 0xac, 0xe4, 0x6e, 0x55, 0xba, 0x9d, |
52 | | 0xd7, 0x24, 0x25, 0x79, 0xf2, 0x99, 0x3b, 0x64, 0xe1, 0x6e, 0xf3, |
53 | | 0xdc, 0xab, 0x95, 0xaf, 0xd4, 0x97, 0x33, 0x3d, 0x8f, 0xa1, 0x2f, |
54 | | 0x5f, 0xf3, 0x55, 0x16, 0x3e, 0x43, 0xce, 0x22, 0x4e, 0x0b, 0x0e, |
55 | | 0x65, 0xff, 0x02, 0xac, 0x8e, 0x5c, 0x7b, 0xe0, 0x94, 0x19, 0xc7, |
56 | | 0x85, 0xe0, 0xca, 0x54, 0x7d, 0x55, 0xa1, 0x2e, 0x2d, 0x20}; |
57 | | |
58 | | // https://www.rfc-editor.org/rfc/rfc9383.html#appendix-B |
59 | | // seed: 1.2.840.10045.3.1.7 point generation seed (N) |
60 | | // N = |
61 | | // 03d8bbd6c639c62937b04d997f38c3770719c629d7014d49a24b4f98baa1292b49 |
62 | | // |
63 | | // `N` is interpreted as a X9.62-format compressed point. This is then the |
64 | | // uncompressed form: |
65 | | const uint8_t kN_bytes[] = { |
66 | | 0x04, 0xd8, 0xbb, 0xd6, 0xc6, 0x39, 0xc6, 0x29, 0x37, 0xb0, 0x4d, |
67 | | 0x99, 0x7f, 0x38, 0xc3, 0x77, 0x07, 0x19, 0xc6, 0x29, 0xd7, 0x01, |
68 | | 0x4d, 0x49, 0xa2, 0x4b, 0x4f, 0x98, 0xba, 0xa1, 0x29, 0x2b, 0x49, |
69 | | 0x07, 0xd6, 0x0a, 0xa6, 0xbf, 0xad, 0xe4, 0x50, 0x08, 0xa6, 0x36, |
70 | | 0x33, 0x7f, 0x51, 0x68, 0xc6, 0x4d, 0x9b, 0xd3, 0x60, 0x34, 0x80, |
71 | | 0x8c, 0xd5, 0x64, 0x49, 0x0b, 0x1e, 0x65, 0x6e, 0xdb, 0xe7}; |
72 | | |
73 | 0 | void UpdateWithLengthPrefix(SHA256_CTX *sha, Span<const uint8_t> data) { |
74 | 0 | uint8_t len_le[8]; |
75 | 0 | CRYPTO_store_u64_le(len_le, data.size()); |
76 | 0 | SHA256_Update(sha, len_le, sizeof(len_le)); |
77 | 0 | SHA256_Update(sha, data.data(), data.size()); |
78 | 0 | } |
79 | | |
80 | | void ConstantToJacobian(const EC_GROUP *group, EC_JACOBIAN *out, |
81 | 0 | bssl::Span<const uint8_t> in) { |
82 | 0 | EC_AFFINE point; |
83 | 0 | BSSL_CHECK(ec_point_from_uncompressed(group, &point, in.data(), in.size())); |
84 | 0 | ec_affine_to_jacobian(group, out, &point); |
85 | 0 | } |
86 | | |
87 | | void ScalarToSizedBuffer(const EC_GROUP *group, const EC_SCALAR *s, |
88 | 0 | Span<uint8_t> out_buf) { |
89 | 0 | size_t out_bytes; |
90 | 0 | ec_scalar_to_bytes(group, out_buf.data(), &out_bytes, s); |
91 | 0 | BSSL_CHECK(out_bytes == out_buf.size()); |
92 | 0 | } |
93 | | |
94 | 0 | bool AddLengthPrefixed(CBB *cbb, Span<const uint8_t> bytes) { |
95 | 0 | return CBB_add_u64le(cbb, bytes.size()) && |
96 | 0 | CBB_add_bytes(cbb, bytes.data(), bytes.size()); |
97 | 0 | } |
98 | | |
99 | | void InitTranscriptHash(SHA256_CTX *sha, Span<const uint8_t> context, |
100 | | Span<const uint8_t> id_prover, |
101 | 0 | Span<const uint8_t> id_verifier) { |
102 | 0 | SHA256_Init(sha); |
103 | 0 | UpdateWithLengthPrefix(sha, context); |
104 | 0 | UpdateWithLengthPrefix(sha, id_prover); |
105 | 0 | UpdateWithLengthPrefix(sha, id_verifier); |
106 | 0 | UpdateWithLengthPrefix(sha, kM_bytes); |
107 | 0 | UpdateWithLengthPrefix(sha, kN_bytes); |
108 | 0 | } |
109 | | |
110 | | bool ComputeTranscript(uint8_t out_prover_confirm[kConfirmSize], |
111 | | uint8_t out_verifier_confirm[kConfirmSize], |
112 | | uint8_t out_secret[kSecretSize], |
113 | | const uint8_t prover_share[kShareSize], |
114 | | const uint8_t verifier_share[kShareSize], |
115 | | SHA256_CTX *sha, const EC_AFFINE *Z, const EC_AFFINE *V, |
116 | 0 | const EC_SCALAR *w0) { |
117 | 0 | const EC_GROUP *group = EC_group_p256(); |
118 | |
|
119 | 0 | uint8_t Z_enc[kShareSize]; |
120 | 0 | size_t Z_enc_len = ec_point_to_bytes(group, Z, POINT_CONVERSION_UNCOMPRESSED, |
121 | 0 | Z_enc, sizeof(Z_enc)); |
122 | 0 | BSSL_CHECK(Z_enc_len == sizeof(Z_enc)); |
123 | |
|
124 | 0 | uint8_t V_enc[kShareSize]; |
125 | 0 | size_t V_enc_len = ec_point_to_bytes(group, V, POINT_CONVERSION_UNCOMPRESSED, |
126 | 0 | V_enc, sizeof(V_enc)); |
127 | 0 | BSSL_CHECK(V_enc_len == sizeof(V_enc)); |
128 | |
|
129 | 0 | uint8_t w0_enc[kVerifierSize]; |
130 | 0 | ScalarToSizedBuffer(group, w0, w0_enc); |
131 | |
|
132 | 0 | uint8_t K_main[SHA256_DIGEST_LENGTH]; |
133 | 0 | UpdateWithLengthPrefix(sha, Span(prover_share, kShareSize)); |
134 | 0 | UpdateWithLengthPrefix(sha, Span(verifier_share, kShareSize)); |
135 | 0 | UpdateWithLengthPrefix(sha, Z_enc); |
136 | 0 | UpdateWithLengthPrefix(sha, V_enc); |
137 | 0 | UpdateWithLengthPrefix(sha, w0_enc); |
138 | 0 | SHA256_Final(K_main, sha); |
139 | |
|
140 | 0 | auto confirmation_str = StringAsBytes("ConfirmationKeys"); |
141 | 0 | uint8_t keys[kSecretSize * 2]; |
142 | 0 | if (!HKDF(keys, sizeof(keys), EVP_sha256(), K_main, sizeof(K_main), nullptr, |
143 | 0 | 0, confirmation_str.data(), confirmation_str.size())) { |
144 | 0 | return false; |
145 | 0 | } |
146 | | |
147 | 0 | auto secret_info_str = StringAsBytes("SharedKey"); |
148 | 0 | if (!HKDF(out_secret, kSecretSize, EVP_sha256(), K_main, sizeof(K_main), |
149 | 0 | nullptr, 0, secret_info_str.data(), secret_info_str.size())) { |
150 | 0 | return false; |
151 | 0 | } |
152 | | |
153 | 0 | unsigned prover_confirm_len; |
154 | 0 | if (HMAC(EVP_sha256(), keys, kSecretSize, verifier_share, kShareSize, |
155 | 0 | out_prover_confirm, &prover_confirm_len) == nullptr) { |
156 | 0 | return false; |
157 | 0 | } |
158 | 0 | BSSL_CHECK(prover_confirm_len == kConfirmSize); |
159 | |
|
160 | 0 | unsigned verifier_confirm_len; |
161 | 0 | if (HMAC(EVP_sha256(), keys + kSecretSize, kSecretSize, prover_share, |
162 | 0 | kShareSize, out_verifier_confirm, |
163 | 0 | &verifier_confirm_len) == nullptr) { |
164 | 0 | return false; |
165 | 0 | } |
166 | 0 | BSSL_CHECK(verifier_confirm_len == kConfirmSize); |
167 | |
|
168 | 0 | return true; |
169 | 0 | } |
170 | | |
171 | | } // namespace |
172 | | |
173 | | bool Register(Span<uint8_t> out_w0, Span<uint8_t> out_w1, |
174 | | Span<uint8_t> out_registration_record, |
175 | | Span<const uint8_t> password, Span<const uint8_t> id_prover, |
176 | 0 | Span<const uint8_t> id_verifier) { |
177 | 0 | if (out_w0.size() != kVerifierSize || out_w1.size() != kVerifierSize || |
178 | 0 | out_registration_record.size() != kRegistrationRecordSize) { |
179 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_INTERNAL_ERROR); |
180 | 0 | return false; |
181 | 0 | } |
182 | | |
183 | | // Offline registration format from: |
184 | | // https://www.rfc-editor.org/rfc/rfc9383.html#section-3.2 |
185 | 0 | ScopedCBB mhf_input; |
186 | 0 | if (!CBB_init(mhf_input.get(), password.size() + id_prover.size() + |
187 | 0 | id_verifier.size() + |
188 | 0 | 3 * sizeof(uint64_t)) || // |
189 | 0 | !AddLengthPrefixed(mhf_input.get(), password) || |
190 | 0 | !AddLengthPrefixed(mhf_input.get(), id_prover) || |
191 | 0 | !AddLengthPrefixed(mhf_input.get(), id_verifier) || |
192 | 0 | !CBB_flush(mhf_input.get())) { |
193 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_INTERNAL_ERROR); |
194 | 0 | return false; |
195 | 0 | } |
196 | | |
197 | | // https://neuromancer.sk/std/nist/P-256 |
198 | | // sage: p = |
199 | | // 0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff |
200 | | // ....: K = GF(p) |
201 | | // ....: a = |
202 | | // K(0xffffffff00000001000000000000000000000000fffffffffffffffffffffffc) |
203 | | // ....: b = |
204 | | // K(0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b) |
205 | | // ....: E = EllipticCurve(K, (a, b)) |
206 | | // ....: G = |
207 | | // E(0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296, |
208 | | // ....: 0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5) |
209 | | // ....: |
210 | | // E.set_order(0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc63 |
211 | | // ....: 2551 * 0x1) |
212 | | // sage: k = 64 |
213 | | // sage: L = (2 * (ceil(log(p)/log(2)) + k)) / 8 |
214 | | |
215 | | // RFC 9383 Section 3.2 |
216 | 0 | constexpr size_t kKDFOutputSize = 80; |
217 | 0 | constexpr size_t kKDFOutputWords = kKDFOutputSize / BN_BYTES; |
218 | |
|
219 | 0 | uint8_t key[kKDFOutputSize]; |
220 | 0 | if (!EVP_PBE_scrypt((const char *)CBB_data(mhf_input.get()), |
221 | 0 | CBB_len(mhf_input.get()), nullptr, 0, |
222 | 0 | /*N=*/32768, /*r=*/8, /*p=*/1, |
223 | 0 | /*max_mem=*/1024 * 1024 * 33, key, kKDFOutputSize)) { |
224 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_INTERNAL_ERROR); |
225 | 0 | return false; |
226 | 0 | } |
227 | | |
228 | 0 | const EC_GROUP *group = EC_group_p256(); |
229 | 0 | BN_ULONG w0_words[kKDFOutputWords / 2]; |
230 | 0 | bn_big_endian_to_words(w0_words, kKDFOutputWords / 2, key, |
231 | 0 | kKDFOutputSize / 2); |
232 | 0 | EC_SCALAR w0; |
233 | 0 | ec_scalar_reduce(group, &w0, w0_words, kKDFOutputWords / 2); |
234 | 0 | ScalarToSizedBuffer(group, &w0, out_w0); |
235 | |
|
236 | 0 | BN_ULONG w1_words[kKDFOutputWords / 2]; |
237 | 0 | bn_big_endian_to_words(w1_words, kKDFOutputWords / 2, |
238 | 0 | key + kKDFOutputSize / 2, kKDFOutputSize / 2); |
239 | 0 | EC_SCALAR w1; |
240 | 0 | ec_scalar_reduce(group, &w1, w1_words, kKDFOutputWords / 2); |
241 | 0 | ScalarToSizedBuffer(group, &w1, out_w1); |
242 | |
|
243 | 0 | EC_JACOBIAN L_j; |
244 | 0 | EC_AFFINE L; |
245 | 0 | if (!ec_point_mul_scalar_base(group, &L_j, &w1) || // |
246 | 0 | !ec_jacobian_to_affine(group, &L, &L_j) || // |
247 | 0 | !ec_point_to_bytes(group, &L, POINT_CONVERSION_UNCOMPRESSED, |
248 | 0 | out_registration_record.data(), |
249 | 0 | kRegistrationRecordSize)) { |
250 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_INTERNAL_ERROR); |
251 | 0 | return false; |
252 | 0 | } |
253 | | |
254 | 0 | return true; |
255 | 0 | } |
256 | | |
257 | 0 | Prover::Prover() = default; |
258 | 0 | Prover::~Prover() = default; |
259 | | |
260 | | bool Prover::Init(Span<const uint8_t> context, Span<const uint8_t> id_prover, |
261 | | Span<const uint8_t> id_verifier, Span<const uint8_t> w0, |
262 | 0 | Span<const uint8_t> w1, Span<const uint8_t> x) { |
263 | 0 | const EC_GROUP *group = EC_group_p256(); |
264 | |
|
265 | 0 | if (!ec_scalar_from_bytes(group, &w0_, w0.data(), w0.size()) || |
266 | 0 | !ec_scalar_from_bytes(group, &w1_, w1.data(), w1.size()) || |
267 | 0 | (!x.empty() && |
268 | 0 | !ec_scalar_from_bytes(group, &x_, x.data(), x.size())) || // |
269 | 0 | (x.empty() && !ec_random_scalar(group, &x_, kDefaultAdditionalData))) { |
270 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_INTERNAL_ERROR); |
271 | 0 | return false; |
272 | 0 | } |
273 | | |
274 | 0 | InitTranscriptHash(&transcript_hash_, context, id_prover, id_verifier); |
275 | |
|
276 | 0 | return true; |
277 | 0 | } |
278 | | |
279 | 0 | bool Prover::GenerateShare(Span<uint8_t> out_share) { |
280 | 0 | if (state_ != State::kInit || out_share.size() != kShareSize) { |
281 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_INTERNAL_ERROR); |
282 | 0 | return false; |
283 | 0 | } |
284 | | |
285 | | // Compute X = x×P + w0×M. |
286 | | // TODO(crbug.com/383778231): This could be sped up with a constant-time, |
287 | | // two-point multiplication. |
288 | 0 | const EC_GROUP *group = EC_group_p256(); |
289 | 0 | EC_JACOBIAN l; |
290 | 0 | if (!ec_point_mul_scalar_base(group, &l, &x_)) { |
291 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_INTERNAL_ERROR); |
292 | 0 | return false; |
293 | 0 | } |
294 | | |
295 | 0 | EC_JACOBIAN M_j; |
296 | 0 | ConstantToJacobian(group, &M_j, kM_bytes); |
297 | |
|
298 | 0 | EC_JACOBIAN r; |
299 | 0 | if (!ec_point_mul_scalar(group, &r, &M_j, &w0_)) { |
300 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_INTERNAL_ERROR); |
301 | 0 | return false; |
302 | 0 | } |
303 | | |
304 | 0 | EC_JACOBIAN X_j; |
305 | 0 | group->meth->add(group, &X_j, &l, &r); |
306 | 0 | if (!ec_jacobian_to_affine(group, &X_, &X_j)) { |
307 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_INTERNAL_ERROR); |
308 | 0 | return false; |
309 | 0 | } |
310 | | |
311 | 0 | size_t written = ec_point_to_bytes(group, &X_, POINT_CONVERSION_UNCOMPRESSED, |
312 | 0 | out_share.data(), kShareSize); |
313 | 0 | BSSL_CHECK(written == kShareSize); |
314 | |
|
315 | 0 | memcpy(share_, out_share.data(), kShareSize); |
316 | 0 | state_ = State::kShareGenerated; |
317 | 0 | return true; |
318 | 0 | } |
319 | | |
320 | | bool Prover::ComputeConfirmation(Span<uint8_t> out_confirm, |
321 | | Span<uint8_t> out_secret, |
322 | | Span<const uint8_t> peer_share, |
323 | 0 | Span<const uint8_t> peer_confirm) { |
324 | 0 | if (state_ != State::kShareGenerated || out_confirm.size() != kConfirmSize || |
325 | 0 | out_secret.size() != kSecretSize || peer_share.size() != kShareSize || |
326 | 0 | peer_confirm.size() != kConfirmSize) { |
327 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_INTERNAL_ERROR); |
328 | 0 | return false; |
329 | 0 | } |
330 | | |
331 | 0 | const EC_GROUP *group = EC_group_p256(); |
332 | 0 | EC_AFFINE Y; |
333 | 0 | if (!ec_point_from_uncompressed(group, &Y, peer_share.data(), |
334 | 0 | peer_share.size())) { |
335 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_INTERNAL_ERROR); |
336 | 0 | return false; |
337 | 0 | } |
338 | | |
339 | 0 | EC_JACOBIAN N_j; |
340 | 0 | ConstantToJacobian(group, &N_j, kN_bytes); |
341 | |
|
342 | 0 | EC_JACOBIAN r; |
343 | 0 | if (!ec_point_mul_scalar(group, &r, &N_j, &w0_)) { |
344 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_INTERNAL_ERROR); |
345 | 0 | return false; |
346 | 0 | } |
347 | | |
348 | 0 | ec_felem_neg(group, &r.Y, &r.Y); |
349 | |
|
350 | 0 | EC_JACOBIAN Y_j; |
351 | 0 | ec_affine_to_jacobian(group, &Y_j, &Y); |
352 | |
|
353 | 0 | EC_JACOBIAN t; |
354 | 0 | group->meth->add(group, &t, &Y_j, &r); |
355 | |
|
356 | 0 | EC_JACOBIAN tmp; |
357 | 0 | EC_AFFINE Z, V; |
358 | | // TODO(crbug.com/383778231): The two affine conversions could be batched |
359 | | // together. |
360 | 0 | if (!ec_point_mul_scalar(group, &tmp, &t, &x_) || // |
361 | 0 | !ec_jacobian_to_affine(group, &Z, &tmp) || // |
362 | 0 | !ec_point_mul_scalar(group, &tmp, &t, &w1_) || // |
363 | 0 | !ec_jacobian_to_affine(group, &V, &tmp)) { |
364 | 0 | return 0; |
365 | 0 | } |
366 | | |
367 | 0 | uint8_t verifier_confirm[kConfirmSize]; |
368 | 0 | if (!ComputeTranscript(out_confirm.data(), verifier_confirm, |
369 | 0 | out_secret.data(), share_, peer_share.data(), |
370 | 0 | &transcript_hash_, &Z, &V, &w0_) || |
371 | 0 | CRYPTO_memcmp(verifier_confirm, peer_confirm.data(), |
372 | 0 | sizeof(verifier_confirm)) != 0) { |
373 | 0 | return 0; |
374 | 0 | } |
375 | | |
376 | 0 | state_ = State::kDone; |
377 | 0 | return true; |
378 | 0 | } |
379 | | |
380 | 0 | Verifier::Verifier() = default; |
381 | 0 | Verifier::~Verifier() = default; |
382 | | |
383 | | bool Verifier::Init(Span<const uint8_t> context, Span<const uint8_t> id_prover, |
384 | | Span<const uint8_t> id_verifier, Span<const uint8_t> w0, |
385 | | Span<const uint8_t> registration_record, |
386 | 0 | Span<const uint8_t> y) { |
387 | 0 | const EC_GROUP *group = EC_group_p256(); |
388 | |
|
389 | 0 | if (!ec_scalar_from_bytes(group, &w0_, w0.data(), w0.size()) || |
390 | 0 | !ec_point_from_uncompressed(group, &L_, registration_record.data(), |
391 | 0 | registration_record.size()) || // |
392 | 0 | (!y.empty() && |
393 | 0 | !ec_scalar_from_bytes(group, &y_, y.data(), y.size())) || // |
394 | 0 | (y.empty() && !ec_random_scalar(group, &y_, kDefaultAdditionalData))) { |
395 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_INTERNAL_ERROR); |
396 | 0 | return false; |
397 | 0 | } |
398 | | |
399 | 0 | InitTranscriptHash(&transcript_hash_, context, id_prover, id_verifier); |
400 | |
|
401 | 0 | return true; |
402 | 0 | } |
403 | | |
404 | | |
405 | | bool Verifier::ProcessProverShare(Span<uint8_t> out_share, |
406 | | Span<uint8_t> out_confirm, |
407 | | Span<uint8_t> out_secret, |
408 | 0 | Span<const uint8_t> prover_share) { |
409 | 0 | if (state_ != State::kInit || // |
410 | 0 | out_share.size() != kShareSize || out_confirm.size() != kConfirmSize || |
411 | 0 | out_secret.size() != kSecretSize || prover_share.size() != kShareSize) { |
412 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_INTERNAL_ERROR); |
413 | 0 | return false; |
414 | 0 | } |
415 | | |
416 | 0 | const EC_GROUP *group = EC_group_p256(); |
417 | 0 | EC_JACOBIAN l, r, M_j, N_j; |
418 | 0 | ConstantToJacobian(group, &M_j, kM_bytes); |
419 | 0 | ConstantToJacobian(group, &N_j, kN_bytes); |
420 | | |
421 | | // Compute Y = y×P + w0×M. |
422 | | // TODO(crbug.com/383778231): This could be sped up with a constant-time, |
423 | | // two-point multiplication. |
424 | 0 | if (!ec_point_mul_scalar_base(group, &l, &y_) || |
425 | 0 | !ec_point_mul_scalar(group, &r, &N_j, &w0_)) { |
426 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_INTERNAL_ERROR); |
427 | 0 | return false; |
428 | 0 | } |
429 | | |
430 | 0 | EC_JACOBIAN Y_j; |
431 | 0 | EC_AFFINE Y; |
432 | 0 | group->meth->add(group, &Y_j, &l, &r); |
433 | 0 | if (!ec_jacobian_to_affine(group, &Y, &Y_j)) { |
434 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_INTERNAL_ERROR); |
435 | 0 | return false; |
436 | 0 | } |
437 | | |
438 | 0 | const size_t written = ec_point_to_bytes( |
439 | 0 | group, &Y, POINT_CONVERSION_UNCOMPRESSED, out_share.data(), kShareSize); |
440 | 0 | BSSL_CHECK(written == kShareSize); |
441 | |
|
442 | 0 | EC_JACOBIAN r2; |
443 | 0 | EC_AFFINE X; |
444 | 0 | if (!ec_point_from_uncompressed(group, &X, prover_share.data(), |
445 | 0 | prover_share.size()) || |
446 | 0 | !ec_point_mul_scalar(group, &r2, &M_j, &w0_)) { |
447 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_INTERNAL_ERROR); |
448 | 0 | return false; |
449 | 0 | } |
450 | | |
451 | 0 | ec_felem_neg(group, &r2.Y, &r2.Y); |
452 | |
|
453 | 0 | EC_JACOBIAN X_j, T; |
454 | 0 | ec_affine_to_jacobian(group, &X_j, &X); |
455 | 0 | group->meth->add(group, &T, &X_j, &r2); |
456 | | |
457 | | // TODO(crbug.com/383778231): The two affine conversions could be batched |
458 | | // together. |
459 | 0 | EC_JACOBIAN tmp; |
460 | 0 | EC_AFFINE Z; |
461 | 0 | if (!ec_point_mul_scalar(group, &tmp, &T, &y_) || // |
462 | 0 | !ec_jacobian_to_affine(group, &Z, &tmp)) { |
463 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_INTERNAL_ERROR); |
464 | 0 | return false; |
465 | 0 | } |
466 | | |
467 | 0 | EC_JACOBIAN L_j; |
468 | 0 | EC_AFFINE V; |
469 | 0 | ec_affine_to_jacobian(group, &L_j, &L_); |
470 | 0 | if (!ec_point_mul_scalar(group, &tmp, &L_j, &y_) || // |
471 | 0 | !ec_jacobian_to_affine(group, &V, &tmp)) { |
472 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_INTERNAL_ERROR); |
473 | 0 | return false; |
474 | 0 | } |
475 | | |
476 | 0 | if (!ComputeTranscript(confirm_, out_confirm.data(), out_secret.data(), |
477 | 0 | prover_share.data(), out_share.data(), |
478 | 0 | &transcript_hash_, &Z, &V, &w0_)) { |
479 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_INTERNAL_ERROR); |
480 | 0 | return false; |
481 | 0 | } |
482 | | |
483 | 0 | state_ = State::kProverShareSeen; |
484 | 0 | return true; |
485 | 0 | } |
486 | | |
487 | 0 | bool Verifier::VerifyProverConfirmation(Span<const uint8_t> peer_confirm) { |
488 | 0 | if (state_ != State::kProverShareSeen || // |
489 | 0 | peer_confirm.size() != kConfirmSize || // |
490 | 0 | CRYPTO_memcmp(confirm_, peer_confirm.data(), sizeof(confirm_)) != 0) { |
491 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_INTERNAL_ERROR); |
492 | 0 | return false; |
493 | 0 | } |
494 | | |
495 | 0 | state_ = State::kDone; |
496 | 0 | return true; |
497 | 0 | } |
498 | | |
499 | | } // namespace spake2plus |
500 | | |
501 | | BSSL_NAMESPACE_END |