Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: net/quic/crypto/curve25519_key_exchange.cc

Issue 12740002: QUIC/Crypto - Enabled curve25519 key exchange code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: deleted include of string.h Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 <string.h>
8
9 #include "base/logging.h" 7 #include "base/logging.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 // TODO(rtenneti): Remove the following line after support for curve25519 is
13 // added.
14 #define crypto_scalarmult_curve25519_SCALARBYTES 32
15
16 using base::StringPiece; 11 using base::StringPiece;
17 using std::string; 12 using std::string;
18 13
19 namespace net { 14 namespace net {
20 15
21 Curve25519KeyExchange::Curve25519KeyExchange() { 16 Curve25519KeyExchange::Curve25519KeyExchange() {
22 } 17 }
23 18
24 Curve25519KeyExchange::~Curve25519KeyExchange() { 19 Curve25519KeyExchange::~Curve25519KeyExchange() {
25 } 20 }
26 21
27 // static 22 // static
28 Curve25519KeyExchange* Curve25519KeyExchange::New( 23 Curve25519KeyExchange* Curve25519KeyExchange::New(
29 const StringPiece& private_key) { 24 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; 25 Curve25519KeyExchange* ka;
34
35 // We don't want to #include the NaCl headers in the public header file, so 26 // 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 27 // 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. 28 // assert that those values are equal to the values from the NaCl header.
38 COMPILE_ASSERT( 29 COMPILE_ASSERT(
39 sizeof(ka->private_key_) == crypto_scalarmult_curve25519_SCALARBYTES, 30 sizeof(ka->private_key_) == crypto::curve25519::kScalarBytes,
40 header_out_of_sync); 31 header_out_of_sync);
41 COMPILE_ASSERT( 32 COMPILE_ASSERT(
42 sizeof(ka->public_key_) == crypto_scalarmult_curve25519_BYTES, 33 sizeof(ka->public_key_) == crypto::curve25519::kBytes,
43 header_out_of_sync); 34 header_out_of_sync);
44 35
45 if (private_key.size() != crypto_scalarmult_curve25519_SCALARBYTES) { 36 if (private_key.size() != crypto::curve25519::kScalarBytes) {
46 return NULL; 37 return NULL;
47 } 38 }
48 39
49 ka = new Curve25519KeyExchange(); 40 ka = new Curve25519KeyExchange();
50 memcpy(ka->private_key_, private_key.data(), 41 memcpy(ka->private_key_, private_key.data(),
51 crypto_scalarmult_curve25519_SCALARBYTES); 42 crypto::curve25519::kScalarBytes);
43 crypto::curve25519::ScalarBaseMult(ka->private_key_, ka->public_key_);
52 return ka; 44 return ka;
53 #else
54 Curve25519KeyExchange* ka = new Curve25519KeyExchange();
55 memset(ka->public_key_, 0, arraysize(ka->public_key_));
56 return ka;
57 #endif
58 } 45 }
59 46
60 // static 47 // static
61 string Curve25519KeyExchange::NewPrivateKey(QuicRandom* rand) { 48 string Curve25519KeyExchange::NewPrivateKey(QuicRandom* rand) {
62 uint8 private_key[crypto_scalarmult_curve25519_SCALARBYTES]; 49 uint8 private_key[crypto::curve25519::kScalarBytes];
63 rand->RandBytes(private_key, sizeof(private_key)); 50 rand->RandBytes(private_key, sizeof(private_key));
64 51
65 // This makes |private_key| a valid scalar, as specified on 52 // This makes |private_key| a valid scalar, as specified on
66 // http://cr.yp.to/ecdh.html 53 // http://cr.yp.to/ecdh.html
67 private_key[0] &= 248; 54 private_key[0] &= 248;
68 private_key[31] &= 127; 55 private_key[31] &= 127;
69 private_key[31] |= 64; 56 private_key[31] |= 64;
70 return string(reinterpret_cast<char*>(private_key), sizeof(private_key)); 57 return string(reinterpret_cast<char*>(private_key), sizeof(private_key));
71 } 58 }
72 59
73 bool Curve25519KeyExchange::CalculateSharedKey( 60 bool Curve25519KeyExchange::CalculateSharedKey(
74 const StringPiece& peer_public_value, 61 const StringPiece& peer_public_value,
75 string* out_result) const { 62 string* out_result) const {
76 // TODO(rtenneti): Add support for curve25519. 63 if (peer_public_value.size() != crypto::curve25519::kBytes) {
77 #if 0
78 if (peer_public_value.size() != crypto_scalarmult_curve25519_BYTES) {
79 return false; 64 return false;
80 } 65 }
81 66
82 uint8 result[crypto_scalarmult_curve25519_BYTES]; 67 uint8 result[crypto::curve25519::kBytes];
83 crypto_scalarmult_curve25519( 68 crypto::curve25519::ScalarMult(
84 result, private_key_, 69 private_key_,
85 reinterpret_cast<const uint8*>(peer_public_value.data())); 70 reinterpret_cast<const uint8*>(peer_public_value.data()),
71 result);
86 out_result->assign(reinterpret_cast<char*>(result), sizeof(result)); 72 out_result->assign(reinterpret_cast<char*>(result), sizeof(result));
87 73
88 return true; 74 return true;
89 #else
90 out_result->assign("01234567", 8);
91 return true;
92 #endif
93 } 75 }
94 76
95 StringPiece Curve25519KeyExchange::public_value() const { 77 StringPiece Curve25519KeyExchange::public_value() const {
96 return StringPiece(reinterpret_cast<const char*>(public_key_), 78 return StringPiece(reinterpret_cast<const char*>(public_key_),
97 sizeof(public_key_)); 79 sizeof(public_key_));
98 } 80 }
99 81
100 CryptoTag Curve25519KeyExchange::tag() const { 82 CryptoTag Curve25519KeyExchange::tag() const {
101 return kC255; 83 return kC255;
102 } 84 }
103 85
104 } // namespace net 86 } // namespace net
OLDNEW
« no previous file with comments | « net/quic/crypto/curve25519_key_exchange.h ('k') | net/quic/crypto/curve25519_key_exchange_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698