OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "net/quic/crypto/curve25519_key_exchange.h" | |
6 | |
7 #include <string.h> | |
8 | |
9 #include "base/logging.h" | |
10 #include "net/quic/crypto/quic_random.h" | |
11 | |
12 // TODO(rtenneti): Remove the following two lines after support for curve25519 | |
wtc
2013/03/02 00:43:42
Nit: two lines => line
ramant (doing other things)
2013/03/02 00:52:08
Done.
| |
13 // is added. | |
14 #define crypto_scalarmult_curve25519_SCALARBYTES 32 | |
15 | |
16 using base::StringPiece; | |
17 using std::string; | |
18 | |
19 namespace net { | |
20 | |
21 Curve25519KeyExchange::Curve25519KeyExchange() { | |
22 } | |
23 | |
24 Curve25519KeyExchange::~Curve25519KeyExchange() { | |
25 } | |
26 | |
27 // static | |
28 Curve25519KeyExchange* Curve25519KeyExchange::New( | |
29 const StringPiece& private_key) { | |
30 // TODO(rtenneti): Add support for curve25519. | |
31 #if 0 | |
32 crypto_scalarmult_curve25519_base(ka->public_key_, ka->private_key_); | |
33 Curve25519KeyExchange* ka; | |
34 | |
35 // We don't want to #include the NaCl headers in the public header file, so | |
36 // we use literals for the sizes of private_key_ and public_key_. Here we | |
37 // assert that those values are equal to the values from the NaCl header. | |
38 COMPILE_ASSERT( | |
39 sizeof(ka->private_key_) == crypto_scalarmult_curve25519_SCALARBYTES, | |
40 header_out_of_sync); | |
41 COMPILE_ASSERT( | |
42 sizeof(ka->public_key_) == crypto_scalarmult_curve25519_BYTES, | |
43 header_out_of_sync); | |
44 | |
45 if (private_key.size() != crypto_scalarmult_curve25519_SCALARBYTES) { | |
46 return NULL; | |
47 } | |
48 | |
49 ka = new Curve25519KeyExchange(); | |
50 memcpy(ka->private_key_, private_key.data(), | |
51 crypto_scalarmult_curve25519_SCALARBYTES); | |
52 return ka; | |
53 #else | |
54 return NULL; | |
55 #endif | |
56 } | |
57 | |
58 // static | |
59 string Curve25519KeyExchange::NewPrivateKey(QuicRandom* rand) { | |
60 uint8 private_key[crypto_scalarmult_curve25519_SCALARBYTES]; | |
61 rand->RandBytes(private_key, sizeof(private_key)); | |
62 | |
63 // This makes |private_key| a valid scalar, as specified on | |
64 // http://cr.yp.to/ecdh.html | |
65 private_key[0] &= 248; | |
66 private_key[31] &= 127; | |
67 private_key[31] |= 64; | |
68 return string(reinterpret_cast<char*>(private_key), sizeof(private_key)); | |
69 } | |
70 | |
71 bool Curve25519KeyExchange::CalculateSharedKey( | |
72 const StringPiece& public_value, | |
73 string* out_result) const { | |
74 // TODO(rtenneti): Add support for curve25519. | |
75 #if 0 | |
76 if (public_value.size() != crypto_scalarmult_curve25519_BYTES) { | |
77 return false; | |
78 } | |
79 | |
80 uint8 result[crypto_scalarmult_curve25519_BYTES]; | |
81 crypto_scalarmult_curve25519( | |
82 result, private_key_, | |
83 reinterpret_cast<const uint8*>(public_value.data())); | |
84 out_result->assign(reinterpret_cast<char*>(result), sizeof(result)); | |
85 | |
86 return true; | |
87 #else | |
88 return false; | |
89 #endif | |
90 } | |
91 | |
92 StringPiece Curve25519KeyExchange::public_value() const { | |
93 return StringPiece(reinterpret_cast<const char*>(public_key_), | |
94 sizeof(public_key_)); | |
95 } | |
96 | |
97 CryptoTag Curve25519KeyExchange::tag() const { | |
98 return kC255; | |
99 } | |
100 | |
101 } // namespace net | |
OLD | NEW |