| 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 "net/quic/core/crypto/curve25519_key_exchange.h" | 5 #include "net/quic/core/crypto/curve25519_key_exchange.h" |
| 6 | 6 |
| 7 #include <cstdint> |
| 8 |
| 7 #include "base/logging.h" | 9 #include "base/logging.h" |
| 8 #include "crypto/curve25519.h" | |
| 9 #include "net/quic/core/crypto/quic_random.h" | 10 #include "net/quic/core/crypto/quic_random.h" |
| 11 #include "third_party/boringssl/src/include/openssl/curve25519.h" |
| 10 | 12 |
| 11 using base::StringPiece; | 13 using base::StringPiece; |
| 12 using std::string; | 14 using std::string; |
| 13 | 15 |
| 14 namespace net { | 16 namespace net { |
| 15 | 17 |
| 16 Curve25519KeyExchange::Curve25519KeyExchange() {} | 18 Curve25519KeyExchange::Curve25519KeyExchange() {} |
| 17 | 19 |
| 18 Curve25519KeyExchange::~Curve25519KeyExchange() {} | 20 Curve25519KeyExchange::~Curve25519KeyExchange() {} |
| 19 | 21 |
| 20 // static | 22 // static |
| 21 Curve25519KeyExchange* Curve25519KeyExchange::New(StringPiece private_key) { | 23 Curve25519KeyExchange* Curve25519KeyExchange::New(StringPiece private_key) { |
| 22 Curve25519KeyExchange* ka; | 24 Curve25519KeyExchange* ka; |
| 23 // We don't want to #include the NaCl headers in the public header file, so | 25 // We don't want to #include the BoringSSL headers in the public header file, |
| 24 // we use literals for the sizes of private_key_ and public_key_. Here we | 26 // so we use literals for the sizes of private_key_ and public_key_. Here we |
| 25 // assert that those values are equal to the values from the NaCl header. | 27 // assert that those values are equal to the values from the BoringSSL |
| 26 static_assert(sizeof(ka->private_key_) == crypto::curve25519::kScalarBytes, | 28 static_assert(sizeof(ka->private_key_) == X25519_PRIVATE_KEY_LEN, |
| 27 "header out of sync"); | 29 "header out of sync"); |
| 28 static_assert(sizeof(ka->public_key_) == crypto::curve25519::kBytes, | 30 static_assert(sizeof(ka->public_key_) == X25519_PUBLIC_VALUE_LEN, |
| 29 "header out of sync"); | 31 "header out of sync"); |
| 30 | 32 |
| 31 if (private_key.size() != crypto::curve25519::kScalarBytes) { | 33 if (private_key.size() != X25519_PRIVATE_KEY_LEN) { |
| 32 return nullptr; | 34 return nullptr; |
| 33 } | 35 } |
| 34 | 36 |
| 35 ka = new Curve25519KeyExchange(); | 37 ka = new Curve25519KeyExchange(); |
| 36 memcpy(ka->private_key_, private_key.data(), | 38 memcpy(ka->private_key_, private_key.data(), X25519_PRIVATE_KEY_LEN); |
| 37 crypto::curve25519::kScalarBytes); | 39 X25519_public_from_private(ka->public_key_, ka->private_key_); |
| 38 crypto::curve25519::ScalarBaseMult(ka->private_key_, ka->public_key_); | |
| 39 return ka; | 40 return ka; |
| 40 } | 41 } |
| 41 | 42 |
| 42 // static | 43 // static |
| 43 string Curve25519KeyExchange::NewPrivateKey(QuicRandom* rand) { | 44 string Curve25519KeyExchange::NewPrivateKey(QuicRandom* rand) { |
| 44 uint8_t private_key[crypto::curve25519::kScalarBytes]; | 45 uint8_t private_key[X25519_PRIVATE_KEY_LEN]; |
| 45 rand->RandBytes(private_key, sizeof(private_key)); | 46 rand->RandBytes(private_key, sizeof(private_key)); |
| 46 | |
| 47 // This makes |private_key| a valid scalar, as specified on | |
| 48 // http://cr.yp.to/ecdh.html | |
| 49 private_key[0] &= 248; | |
| 50 private_key[31] &= 127; | |
| 51 private_key[31] |= 64; | |
| 52 return string(reinterpret_cast<char*>(private_key), sizeof(private_key)); | 47 return string(reinterpret_cast<char*>(private_key), sizeof(private_key)); |
| 53 } | 48 } |
| 54 | 49 |
| 55 KeyExchange* Curve25519KeyExchange::NewKeyPair(QuicRandom* rand) const { | 50 KeyExchange* Curve25519KeyExchange::NewKeyPair(QuicRandom* rand) const { |
| 56 const string private_value = NewPrivateKey(rand); | 51 const string private_value = NewPrivateKey(rand); |
| 57 return Curve25519KeyExchange::New(private_value); | 52 return Curve25519KeyExchange::New(private_value); |
| 58 } | 53 } |
| 59 | 54 |
| 60 bool Curve25519KeyExchange::CalculateSharedKey(StringPiece peer_public_value, | 55 bool Curve25519KeyExchange::CalculateSharedKey(StringPiece peer_public_value, |
| 61 string* out_result) const { | 56 string* out_result) const { |
| 62 if (peer_public_value.size() != crypto::curve25519::kBytes) { | 57 if (peer_public_value.size() != X25519_PUBLIC_VALUE_LEN) { |
| 63 return false; | 58 return false; |
| 64 } | 59 } |
| 65 | 60 |
| 66 uint8_t result[crypto::curve25519::kBytes]; | 61 uint8_t result[X25519_PUBLIC_VALUE_LEN]; |
| 67 if (!crypto::curve25519::ScalarMult( | 62 if (!X25519(result, private_key_, |
| 68 private_key_, | 63 reinterpret_cast<const uint8_t*>(peer_public_value.data()))) { |
| 69 reinterpret_cast<const uint8_t*>(peer_public_value.data()), result)) { | |
| 70 return false; | 64 return false; |
| 71 } | 65 } |
| 66 |
| 72 out_result->assign(reinterpret_cast<char*>(result), sizeof(result)); | 67 out_result->assign(reinterpret_cast<char*>(result), sizeof(result)); |
| 73 | |
| 74 return true; | 68 return true; |
| 75 } | 69 } |
| 76 | 70 |
| 77 StringPiece Curve25519KeyExchange::public_value() const { | 71 StringPiece Curve25519KeyExchange::public_value() const { |
| 78 return StringPiece(reinterpret_cast<const char*>(public_key_), | 72 return StringPiece(reinterpret_cast<const char*>(public_key_), |
| 79 sizeof(public_key_)); | 73 sizeof(public_key_)); |
| 80 } | 74 } |
| 81 | 75 |
| 82 QuicTag Curve25519KeyExchange::tag() const { | 76 QuicTag Curve25519KeyExchange::tag() const { |
| 83 return kC255; | 77 return kC255; |
| 84 } | 78 } |
| 85 | 79 |
| 86 } // namespace net | 80 } // namespace net |
| OLD | NEW |