OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "crypto/curve25519.h" | 5 #include "crypto/curve25519.h" |
6 | 6 |
| 7 #include "crypto/secure_util.h" |
| 8 |
7 // Curve25519 is specified in terms of byte strings, not numbers, so all | 9 // Curve25519 is specified in terms of byte strings, not numbers, so all |
8 // implementations take and return the same sequence of bits. So the byte | 10 // implementations take and return the same sequence of bits. So the byte |
9 // order is implicitly specified as in, say, SHA1. | 11 // order is implicitly specified as in, say, SHA1. |
10 // | 12 // |
11 // Prototype for |curve25519_donna| function in | 13 // Prototype for |curve25519_donna| function in |
12 // third_party/curve25519-donna/curve25519-donna.c | 14 // third_party/curve25519-donna/curve25519-donna.c |
13 extern "C" int curve25519_donna(uint8*, const uint8*, const uint8*); | 15 extern "C" int curve25519_donna(uint8_t*, const uint8_t*, const uint8_t*); |
14 | 16 |
15 namespace crypto { | 17 namespace crypto { |
16 | 18 |
17 namespace curve25519 { | 19 namespace curve25519 { |
18 | 20 |
19 void ScalarMult(const uint8* private_key, | 21 bool ScalarMult(const uint8_t* private_key, |
20 const uint8* peer_public_key, | 22 const uint8_t* peer_public_key, |
21 uint8* shared_key) { | 23 uint8_t* shared_key) { |
22 curve25519_donna(shared_key, private_key, peer_public_key); | 24 curve25519_donna(shared_key, private_key, peer_public_key); |
| 25 |
| 26 // The all-zero output results when the input is a point of small order. |
| 27 static const uint8_t kZeros[32] = {0}; |
| 28 return !SecureMemEqual(shared_key, kZeros, 32); |
23 } | 29 } |
24 | 30 |
25 // kBasePoint is the base point (generator) of the elliptic curve group. | 31 // kBasePoint is the base point (generator) of the elliptic curve group. |
26 // It is little-endian version of '9' followed by 31 zeros. | 32 // It is little-endian version of '9' followed by 31 zeros. |
27 // See "Computing public keys" section of http://cr.yp.to/ecdh.html. | 33 // See "Computing public keys" section of http://cr.yp.to/ecdh.html. |
28 static const unsigned char kBasePoint[32] = {9}; | 34 static const uint8_t kBasePoint[32] = {9}; |
29 | 35 |
30 void ScalarBaseMult(const uint8* private_key, uint8* public_key) { | 36 void ScalarBaseMult(const uint8_t* private_key, uint8_t* public_key) { |
31 curve25519_donna(public_key, private_key, kBasePoint); | 37 curve25519_donna(public_key, private_key, kBasePoint); |
32 } | 38 } |
33 | 39 |
34 } // namespace curve25519 | 40 } // namespace curve25519 |
35 | 41 |
36 } // namespace crypto | 42 } // namespace crypto |
OLD | NEW |