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

Unified Diff: net/quic/crypto/curve25519_key_exchange.cc

Issue 12381018: QUIC - Some sketching of the crypto handshake. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years, 10 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/quic/crypto/curve25519_key_exchange.h ('k') | net/quic/crypto/key_exchange.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/quic/crypto/curve25519_key_exchange.cc
===================================================================
--- net/quic/crypto/curve25519_key_exchange.cc (revision 0)
+++ net/quic/crypto/curve25519_key_exchange.cc (revision 0)
@@ -0,0 +1,101 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/quic/crypto/curve25519_key_exchange.h"
+
+#include <string.h>
+
+#include "base/logging.h"
+#include "net/quic/crypto/quic_random.h"
+
+// 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.
+// is added.
+#define crypto_scalarmult_curve25519_SCALARBYTES 32
+
+using base::StringPiece;
+using std::string;
+
+namespace net {
+
+Curve25519KeyExchange::Curve25519KeyExchange() {
+}
+
+Curve25519KeyExchange::~Curve25519KeyExchange() {
+}
+
+// static
+Curve25519KeyExchange* Curve25519KeyExchange::New(
+ const StringPiece& private_key) {
+// TODO(rtenneti): Add support for curve25519.
+#if 0
+ crypto_scalarmult_curve25519_base(ka->public_key_, ka->private_key_);
+ Curve25519KeyExchange* ka;
+
+ // We don't want to #include the NaCl headers in the public header file, so
+ // we use literals for the sizes of private_key_ and public_key_. Here we
+ // assert that those values are equal to the values from the NaCl header.
+ COMPILE_ASSERT(
+ sizeof(ka->private_key_) == crypto_scalarmult_curve25519_SCALARBYTES,
+ header_out_of_sync);
+ COMPILE_ASSERT(
+ sizeof(ka->public_key_) == crypto_scalarmult_curve25519_BYTES,
+ header_out_of_sync);
+
+ if (private_key.size() != crypto_scalarmult_curve25519_SCALARBYTES) {
+ return NULL;
+ }
+
+ ka = new Curve25519KeyExchange();
+ memcpy(ka->private_key_, private_key.data(),
+ crypto_scalarmult_curve25519_SCALARBYTES);
+ return ka;
+#else
+ return NULL;
+#endif
+}
+
+// static
+string Curve25519KeyExchange::NewPrivateKey(QuicRandom* rand) {
+ uint8 private_key[crypto_scalarmult_curve25519_SCALARBYTES];
+ rand->RandBytes(private_key, sizeof(private_key));
+
+ // This makes |private_key| a valid scalar, as specified on
+ // http://cr.yp.to/ecdh.html
+ private_key[0] &= 248;
+ private_key[31] &= 127;
+ private_key[31] |= 64;
+ return string(reinterpret_cast<char*>(private_key), sizeof(private_key));
+}
+
+bool Curve25519KeyExchange::CalculateSharedKey(
+ const StringPiece& public_value,
+ string* out_result) const {
+// TODO(rtenneti): Add support for curve25519.
+#if 0
+ if (public_value.size() != crypto_scalarmult_curve25519_BYTES) {
+ return false;
+ }
+
+ uint8 result[crypto_scalarmult_curve25519_BYTES];
+ crypto_scalarmult_curve25519(
+ result, private_key_,
+ reinterpret_cast<const uint8*>(public_value.data()));
+ out_result->assign(reinterpret_cast<char*>(result), sizeof(result));
+
+ return true;
+#else
+ return false;
+#endif
+}
+
+StringPiece Curve25519KeyExchange::public_value() const {
+ return StringPiece(reinterpret_cast<const char*>(public_key_),
+ sizeof(public_key_));
+}
+
+CryptoTag Curve25519KeyExchange::tag() const {
+ return kC255;
+}
+
+} // namespace net
« no previous file with comments | « net/quic/crypto/curve25519_key_exchange.h ('k') | net/quic/crypto/key_exchange.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698