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/basictypes.h" | |
8 #include "base/logging.h" | 7 #include "base/logging.h" |
9 #include "crypto/curve25519.h" | 8 #include "crypto/curve25519.h" |
10 #include "net/quic/crypto/quic_random.h" | 9 #include "net/quic/crypto/quic_random.h" |
11 | 10 |
12 using base::StringPiece; | 11 using base::StringPiece; |
13 using std::string; | 12 using std::string; |
14 | 13 |
15 namespace net { | 14 namespace net { |
16 | 15 |
17 Curve25519KeyExchange::Curve25519KeyExchange() {} | 16 Curve25519KeyExchange::Curve25519KeyExchange() {} |
(...skipping 18 matching lines...) Expand all Loading... |
36 | 35 |
37 ka = new Curve25519KeyExchange(); | 36 ka = new Curve25519KeyExchange(); |
38 memcpy(ka->private_key_, private_key.data(), | 37 memcpy(ka->private_key_, private_key.data(), |
39 crypto::curve25519::kScalarBytes); | 38 crypto::curve25519::kScalarBytes); |
40 crypto::curve25519::ScalarBaseMult(ka->private_key_, ka->public_key_); | 39 crypto::curve25519::ScalarBaseMult(ka->private_key_, ka->public_key_); |
41 return ka; | 40 return ka; |
42 } | 41 } |
43 | 42 |
44 // static | 43 // static |
45 string Curve25519KeyExchange::NewPrivateKey(QuicRandom* rand) { | 44 string Curve25519KeyExchange::NewPrivateKey(QuicRandom* rand) { |
46 uint8 private_key[crypto::curve25519::kScalarBytes]; | 45 uint8_t private_key[crypto::curve25519::kScalarBytes]; |
47 rand->RandBytes(private_key, sizeof(private_key)); | 46 rand->RandBytes(private_key, sizeof(private_key)); |
48 | 47 |
49 // This makes |private_key| a valid scalar, as specified on | 48 // This makes |private_key| a valid scalar, as specified on |
50 // http://cr.yp.to/ecdh.html | 49 // http://cr.yp.to/ecdh.html |
51 private_key[0] &= 248; | 50 private_key[0] &= 248; |
52 private_key[31] &= 127; | 51 private_key[31] &= 127; |
53 private_key[31] |= 64; | 52 private_key[31] |= 64; |
54 return string(reinterpret_cast<char*>(private_key), sizeof(private_key)); | 53 return string(reinterpret_cast<char*>(private_key), sizeof(private_key)); |
55 } | 54 } |
56 | 55 |
57 KeyExchange* Curve25519KeyExchange::NewKeyPair(QuicRandom* rand) const { | 56 KeyExchange* Curve25519KeyExchange::NewKeyPair(QuicRandom* rand) const { |
58 const string private_value = NewPrivateKey(rand); | 57 const string private_value = NewPrivateKey(rand); |
59 return Curve25519KeyExchange::New(private_value); | 58 return Curve25519KeyExchange::New(private_value); |
60 } | 59 } |
61 | 60 |
62 bool Curve25519KeyExchange::CalculateSharedKey( | 61 bool Curve25519KeyExchange::CalculateSharedKey( |
63 const StringPiece& peer_public_value, | 62 const StringPiece& peer_public_value, |
64 string* out_result) const { | 63 string* out_result) const { |
65 if (peer_public_value.size() != crypto::curve25519::kBytes) { | 64 if (peer_public_value.size() != crypto::curve25519::kBytes) { |
66 return false; | 65 return false; |
67 } | 66 } |
68 | 67 |
69 uint8 result[crypto::curve25519::kBytes]; | 68 uint8_t result[crypto::curve25519::kBytes]; |
70 if (!crypto::curve25519::ScalarMult( | 69 if (!crypto::curve25519::ScalarMult( |
71 private_key_, | 70 private_key_, |
72 reinterpret_cast<const uint8*>(peer_public_value.data()), result)) { | 71 reinterpret_cast<const uint8_t*>(peer_public_value.data()), result)) { |
73 return false; | 72 return false; |
74 } | 73 } |
75 out_result->assign(reinterpret_cast<char*>(result), sizeof(result)); | 74 out_result->assign(reinterpret_cast<char*>(result), sizeof(result)); |
76 | 75 |
77 return true; | 76 return true; |
78 } | 77 } |
79 | 78 |
80 StringPiece Curve25519KeyExchange::public_value() const { | 79 StringPiece Curve25519KeyExchange::public_value() const { |
81 return StringPiece(reinterpret_cast<const char*>(public_key_), | 80 return StringPiece(reinterpret_cast<const char*>(public_key_), |
82 sizeof(public_key_)); | 81 sizeof(public_key_)); |
83 } | 82 } |
84 | 83 |
85 QuicTag Curve25519KeyExchange::tag() const { | 84 QuicTag Curve25519KeyExchange::tag() const { |
86 return kC255; | 85 return kC255; |
87 } | 86 } |
88 | 87 |
89 } // namespace net | 88 } // namespace net |
OLD | NEW |