| 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/crypto/curve25519_key_exchange.h" | 5 #include "net/quic/crypto/curve25519_key_exchange.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "crypto/curve25519.h" | 8 #include "crypto/curve25519.h" |
| 9 #include "net/quic/crypto/quic_random.h" | 9 #include "net/quic/crypto/quic_random.h" |
| 10 | 10 |
| 11 using base::StringPiece; | 11 using base::StringPiece; |
| 12 using std::string; | 12 using std::string; |
| 13 | 13 |
| 14 namespace net { | 14 namespace net { |
| 15 | 15 |
| 16 Curve25519KeyExchange::Curve25519KeyExchange() {} | 16 Curve25519KeyExchange::Curve25519KeyExchange() {} |
| 17 | 17 |
| 18 Curve25519KeyExchange::~Curve25519KeyExchange() {} | 18 Curve25519KeyExchange::~Curve25519KeyExchange() {} |
| 19 | 19 |
| 20 // static | 20 // static |
| 21 Curve25519KeyExchange* Curve25519KeyExchange::New( | 21 Curve25519KeyExchange* Curve25519KeyExchange::New(StringPiece private_key) { |
| 22 const StringPiece& private_key) { | |
| 23 Curve25519KeyExchange* ka; | 22 Curve25519KeyExchange* ka; |
| 24 // We don't want to #include the NaCl headers in the public header file, so | 23 // We don't want to #include the NaCl headers in the public header file, so |
| 25 // we use literals for the sizes of private_key_ and public_key_. Here we | 24 // we use literals for the sizes of private_key_ and public_key_. Here we |
| 26 // assert that those values are equal to the values from the NaCl header. | 25 // assert that those values are equal to the values from the NaCl header. |
| 27 static_assert(sizeof(ka->private_key_) == crypto::curve25519::kScalarBytes, | 26 static_assert(sizeof(ka->private_key_) == crypto::curve25519::kScalarBytes, |
| 28 "header out of sync"); | 27 "header out of sync"); |
| 29 static_assert(sizeof(ka->public_key_) == crypto::curve25519::kBytes, | 28 static_assert(sizeof(ka->public_key_) == crypto::curve25519::kBytes, |
| 30 "header out of sync"); | 29 "header out of sync"); |
| 31 | 30 |
| 32 if (private_key.size() != crypto::curve25519::kScalarBytes) { | 31 if (private_key.size() != crypto::curve25519::kScalarBytes) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 51 private_key[31] &= 127; | 50 private_key[31] &= 127; |
| 52 private_key[31] |= 64; | 51 private_key[31] |= 64; |
| 53 return string(reinterpret_cast<char*>(private_key), sizeof(private_key)); | 52 return string(reinterpret_cast<char*>(private_key), sizeof(private_key)); |
| 54 } | 53 } |
| 55 | 54 |
| 56 KeyExchange* Curve25519KeyExchange::NewKeyPair(QuicRandom* rand) const { | 55 KeyExchange* Curve25519KeyExchange::NewKeyPair(QuicRandom* rand) const { |
| 57 const string private_value = NewPrivateKey(rand); | 56 const string private_value = NewPrivateKey(rand); |
| 58 return Curve25519KeyExchange::New(private_value); | 57 return Curve25519KeyExchange::New(private_value); |
| 59 } | 58 } |
| 60 | 59 |
| 61 bool Curve25519KeyExchange::CalculateSharedKey( | 60 bool Curve25519KeyExchange::CalculateSharedKey(StringPiece peer_public_value, |
| 62 const StringPiece& peer_public_value, | 61 string* out_result) const { |
| 63 string* out_result) const { | |
| 64 if (peer_public_value.size() != crypto::curve25519::kBytes) { | 62 if (peer_public_value.size() != crypto::curve25519::kBytes) { |
| 65 return false; | 63 return false; |
| 66 } | 64 } |
| 67 | 65 |
| 68 uint8_t result[crypto::curve25519::kBytes]; | 66 uint8_t result[crypto::curve25519::kBytes]; |
| 69 if (!crypto::curve25519::ScalarMult( | 67 if (!crypto::curve25519::ScalarMult( |
| 70 private_key_, | 68 private_key_, |
| 71 reinterpret_cast<const uint8_t*>(peer_public_value.data()), result)) { | 69 reinterpret_cast<const uint8_t*>(peer_public_value.data()), result)) { |
| 72 return false; | 70 return false; |
| 73 } | 71 } |
| 74 out_result->assign(reinterpret_cast<char*>(result), sizeof(result)); | 72 out_result->assign(reinterpret_cast<char*>(result), sizeof(result)); |
| 75 | 73 |
| 76 return true; | 74 return true; |
| 77 } | 75 } |
| 78 | 76 |
| 79 StringPiece Curve25519KeyExchange::public_value() const { | 77 StringPiece Curve25519KeyExchange::public_value() const { |
| 80 return StringPiece(reinterpret_cast<const char*>(public_key_), | 78 return StringPiece(reinterpret_cast<const char*>(public_key_), |
| 81 sizeof(public_key_)); | 79 sizeof(public_key_)); |
| 82 } | 80 } |
| 83 | 81 |
| 84 QuicTag Curve25519KeyExchange::tag() const { | 82 QuicTag Curve25519KeyExchange::tag() const { |
| 85 return kC255; | 83 return kC255; |
| 86 } | 84 } |
| 87 | 85 |
| 88 } // namespace net | 86 } // namespace net |
| OLD | NEW |