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

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

Issue 2193073003: Move shared files in net/quic/ into net/quic/core/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: io_thread_unittest.cc Created 4 years, 4 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
OLDNEW
(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 "base/logging.h"
8 #include "crypto/curve25519.h"
9 #include "net/quic/crypto/quic_random.h"
10
11 using base::StringPiece;
12 using std::string;
13
14 namespace net {
15
16 Curve25519KeyExchange::Curve25519KeyExchange() {}
17
18 Curve25519KeyExchange::~Curve25519KeyExchange() {}
19
20 // static
21 Curve25519KeyExchange* Curve25519KeyExchange::New(StringPiece private_key) {
22 Curve25519KeyExchange* ka;
23 // We don't want to #include the NaCl headers in the public header file, so
24 // we use literals for the sizes of private_key_ and public_key_. Here we
25 // assert that those values are equal to the values from the NaCl header.
26 static_assert(sizeof(ka->private_key_) == crypto::curve25519::kScalarBytes,
27 "header out of sync");
28 static_assert(sizeof(ka->public_key_) == crypto::curve25519::kBytes,
29 "header out of sync");
30
31 if (private_key.size() != crypto::curve25519::kScalarBytes) {
32 return nullptr;
33 }
34
35 ka = new Curve25519KeyExchange();
36 memcpy(ka->private_key_, private_key.data(),
37 crypto::curve25519::kScalarBytes);
38 crypto::curve25519::ScalarBaseMult(ka->private_key_, ka->public_key_);
39 return ka;
40 }
41
42 // static
43 string Curve25519KeyExchange::NewPrivateKey(QuicRandom* rand) {
44 uint8_t private_key[crypto::curve25519::kScalarBytes];
45 rand->RandBytes(private_key, sizeof(private_key));
46
47 // This makes |private_key| a valid scalar, as specified on
48 // http://cr.yp.to/ecdh.html
49 private_key[0] &= 248;
50 private_key[31] &= 127;
51 private_key[31] |= 64;
52 return string(reinterpret_cast<char*>(private_key), sizeof(private_key));
53 }
54
55 KeyExchange* Curve25519KeyExchange::NewKeyPair(QuicRandom* rand) const {
56 const string private_value = NewPrivateKey(rand);
57 return Curve25519KeyExchange::New(private_value);
58 }
59
60 bool Curve25519KeyExchange::CalculateSharedKey(StringPiece peer_public_value,
61 string* out_result) const {
62 if (peer_public_value.size() != crypto::curve25519::kBytes) {
63 return false;
64 }
65
66 uint8_t result[crypto::curve25519::kBytes];
67 if (!crypto::curve25519::ScalarMult(
68 private_key_,
69 reinterpret_cast<const uint8_t*>(peer_public_value.data()), result)) {
70 return false;
71 }
72 out_result->assign(reinterpret_cast<char*>(result), sizeof(result));
73
74 return true;
75 }
76
77 StringPiece Curve25519KeyExchange::public_value() const {
78 return StringPiece(reinterpret_cast<const char*>(public_key_),
79 sizeof(public_key_));
80 }
81
82 QuicTag Curve25519KeyExchange::tag() const {
83 return kC255;
84 }
85
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