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