libweave: Add p224_spake to build

libweave.gyp builds ibweave/external/crypto/.
All added code wrapped into weave namespace.

BUG=brillo:1270
TEST=`FEATURES=test emerge-gizmo libweave buffet`

Change-Id: I8f507e72c0a2ad61309b9d503f709bf770326f9d
Reviewed-on: https://chromium-review.googlesource.com/291429
Commit-Ready: Vitaly Buka <vitalybuka@chromium.org>
Tested-by: Vitaly Buka <vitalybuka@chromium.org>
Reviewed-by: Vitaly Buka <vitalybuka@chromium.org>
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
diff --git a/libweave/external/crypto/p224.cc b/libweave/external/crypto/p224.cc
index 11946a9..b7c66c5 100644
--- a/libweave/external/crypto/p224.cc
+++ b/libweave/external/crypto/p224.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Copyright 2012 The Chromium OS Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
@@ -7,11 +7,15 @@
 //
 // See http://www.imperialviolet.org/2010/12/04/ecc.html ([1]) for background.
 
-#include "crypto/p224.h"
+#include "libweave/external/crypto/p224.h"
 
 #include <string.h>
 
-#include "base/sys_byteorder.h"
+#include <base/sys_byteorder.h>
+
+namespace weave {
+namespace crypto {
+namespace p224 {
 
 namespace {
 
@@ -30,8 +34,6 @@
 // than we would really like. But it has the useful feature that we hit 2**224
 // exactly, making the reflections during a reduce much nicer.
 
-using crypto::p224::FieldElement;
-
 // kP is the P224 prime.
 const FieldElement kP = {
   1, 0, 0, 268431360,
@@ -81,9 +83,9 @@
   }
 }
 
-static const uint32 kTwo31p3 = (1u<<31) + (1u<<3);
-static const uint32 kTwo31m3 = (1u<<31) - (1u<<3);
-static const uint32 kTwo31m15m3 = (1u<<31) - (1u<<15) - (1u<<3);
+static const uint32 kTwo31p3 = (1u << 31) + (1u << 3);
+static const uint32 kTwo31m3 = (1u << 31) - (1u << 3);
+static const uint32 kTwo31m15m3 = (1u << 31) - (1u << 15) - (1u << 3);
 // kZero31ModP is 0 mod p where bit 31 is set in all limbs so that we can
 // subtract smaller amounts without underflow. See the section "Subtraction" in
 // [1] for why.
@@ -428,8 +430,6 @@
 // These functions deal with group elements. The group is an elliptic curve
 // group with a = -3 defined in FIPS 186-3, section D.2.2.
 
-using crypto::p224::Point;
-
 // kB is parameter of the elliptic curve.
 const FieldElement kB = {
   55967668, 11768882, 265861671, 185302395,
@@ -655,10 +655,6 @@
 
 }  // anonymous namespace
 
-namespace crypto {
-
-namespace p224 {
-
 bool Point::SetFromString(const base::StringPiece& in) {
   if (in.size() != 2*28)
     return false;
@@ -685,7 +681,7 @@
   Subtract(&rhs, rhs, three_x);
   Reduce(&rhs);
 
-  ::Add(&rhs, rhs, kB);
+  Add(&rhs, rhs, kB);
   Contract(&rhs);
   return memcmp(&lhs, &rhs, sizeof(lhs)) == 0;
 }
@@ -715,20 +711,20 @@
 }
 
 void ScalarMult(const Point& in, const uint8* scalar, Point* out) {
-  ::ScalarMult(out, in, scalar, 28);
+  ScalarMult(out, in, scalar, 28);
 }
 
 // kBasePoint is the base point (generator) of the elliptic curve group.
 static const Point kBasePoint = {
   {22813985, 52956513, 34677300, 203240812,
-   12143107, 133374265, 225162431, 191946955},
+    12143107, 133374265, 225162431, 191946955},
   {83918388, 223877528, 122119236, 123340192,
-   266784067, 263504429, 146143011, 198407736},
+    266784067, 263504429, 146143011, 198407736},
   {1, 0, 0, 0, 0, 0, 0, 0},
 };
 
 void ScalarBaseMult(const uint8* scalar, Point* out) {
-  ::ScalarMult(out, kBasePoint, scalar, 28);
+  ScalarMult(out, kBasePoint, scalar, 28);
 }
 
 void Add(const Point& a, const Point& b, Point* out) {
@@ -754,5 +750,5 @@
 }
 
 }  // namespace p224
-
 }  // namespace crypto
+}  // namespace weave
diff --git a/libweave/external/crypto/p224.h b/libweave/external/crypto/p224.h
index 2efecfa..a835ecd 100644
--- a/libweave/external/crypto/p224.h
+++ b/libweave/external/crypto/p224.h
@@ -1,16 +1,16 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Copyright 2012 The Chromium OS Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#ifndef CRYPTO_P224_H_
-#define CRYPTO_P224_H_
+#ifndef LIBWEAVE_EXTERNAL_CRYPTO_P224_H_
+#define LIBWEAVE_EXTERNAL_CRYPTO_P224_H_
 
 #include <string>
 
-#include "base/basictypes.h"
-#include "base/strings/string_piece.h"
-#include "crypto/crypto_export.h"
+#include <base/basictypes.h>
+#include <base/strings/string_piece.h>
 
+namespace weave {
 namespace crypto {
 
 // P224 implements an elliptic curve group, commonly known as P224 and defined
@@ -21,7 +21,7 @@
 // little endian order.
 typedef uint32 FieldElement[8];
 
-struct CRYPTO_EXPORT Point {
+struct Point {
   // SetFromString the value of the point from the 56 byte, external
   // representation. The external point representation is an (x, y) pair of a
   // point on the curve. Each field element is represented as a big-endian
@@ -41,20 +41,20 @@
 
 // ScalarMult computes *out = in*scalar where scalar is a 28-byte, big-endian
 // number.
-void CRYPTO_EXPORT ScalarMult(const Point& in, const uint8* scalar, Point* out);
+void ScalarMult(const Point& in, const uint8* scalar, Point* out);
 
 // ScalarBaseMult computes *out = g*scalar where g is the base point of the
 // curve and scalar is a 28-byte, big-endian number.
-void CRYPTO_EXPORT ScalarBaseMult(const uint8* scalar, Point* out);
+void ScalarBaseMult(const uint8* scalar, Point* out);
 
 // Add computes *out = a+b.
-void CRYPTO_EXPORT Add(const Point& a, const Point& b, Point* out);
+void Add(const Point& a, const Point& b, Point* out);
 
 // Negate calculates out = -a;
-void CRYPTO_EXPORT Negate(const Point& a, Point* out);
+void Negate(const Point& a, Point* out);
 
 }  // namespace p224
-
 }  // namespace crypto
+}  // namespace weave
 
-#endif  // CRYPTO_P224_H_
+#endif  // LIBWEAVE_EXTERNAL_CRYPTO_P224_H_
diff --git a/libweave/external/crypto/p224_spake.cc b/libweave/external/crypto/p224_spake.cc
index a6dec40..634c7e6 100644
--- a/libweave/external/crypto/p224_spake.cc
+++ b/libweave/external/crypto/p224_spake.cc
@@ -1,18 +1,21 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Copyright 2012 The Chromium OS Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
 // This code implements SPAKE2, a variant of EKE:
 //  http://www.di.ens.fr/~pointche/pub.php?reference=AbPo04
 
-#include <crypto/p224_spake.h>
+#include "libweave/external/crypto/p224_spake.h"
 
 #include <algorithm>
 
 #include <base/logging.h>
-#include <crypto/p224.h>
-#include <crypto/random.h>
-#include <crypto/secure_util.h>
+#include <base/rand_util.h>
+
+#include "libweave/external/crypto/p224.h"
+
+namespace weave {
+namespace crypto {
 
 namespace {
 
@@ -77,26 +80,41 @@
 //   return 0;
 // }
 
-const crypto::p224::Point kM = {
+const p224::Point kM = {
   {174237515, 77186811, 235213682, 33849492,
-   33188520, 48266885, 177021753, 81038478},
+    33188520, 48266885, 177021753, 81038478},
   {104523827, 245682244, 266509668, 236196369,
-   28372046, 145351378, 198520366, 113345994},
+    28372046, 145351378, 198520366, 113345994},
   {1, 0, 0, 0, 0, 0, 0, 0},
 };
 
-const crypto::p224::Point kN = {
+const p224::Point kN = {
   {136176322, 263523628, 251628795, 229292285,
-   5034302, 185981975, 171998428, 11653062},
+    5034302, 185981975, 171998428, 11653062},
   {197567436, 51226044, 60372156, 175772188,
-   42075930, 8083165, 160827401, 65097570},
+    42075930, 8083165, 160827401, 65097570},
   {1, 0, 0, 0, 0, 0, 0, 0},
 };
 
+// Performs a constant-time comparison of two strings, returning true if the
+// strings are equal.
+//
+// For cryptographic operations, comparison functions such as memcmp() may
+// expose side-channel information about input, allowing an attacker to
+// perform timing analysis to determine what the expected bits should be. In
+// order to avoid such attacks, the comparison must execute in constant time,
+// so as to not to reveal to the attacker where the difference(s) are.
+// For an example attack, see
+// http://groups.google.com/group/keyczar-discuss/browse_thread/thread/5571eca0948b2a13
+bool SecureMemEqual(const uint8_t* s1_ptr, const uint8_t* s2_ptr, size_t n) {
+  uint8_t tmp = 0;
+  for (size_t i = 0; i < n; ++i, ++s1_ptr, ++s2_ptr)
+    tmp |= *s1_ptr ^ *s2_ptr;
+  return (tmp == 0);
+}
+
 }  // anonymous namespace
 
-namespace crypto {
-
 P224EncryptedKeyExchange::P224EncryptedKeyExchange(
     PeerType peer_type, const base::StringPiece& password)
     : state_(kStateInitial),
@@ -105,7 +123,7 @@
   memset(&expected_authenticator_, 0, sizeof(expected_authenticator_));
 
   // x_ is a random scalar.
-  RandBytes(x_, sizeof(x_));
+  base::RandBytes(x_, sizeof(x_));
 
   // Calculate |password| hash to get SPAKE password value.
   SHA256HashString(std::string(password.data(), password.length()),
@@ -155,8 +173,8 @@
       error_ = "peer's hash had an incorrect size";
       return kResultFailed;
     }
-    if (!SecureMemEqual(message.data(), expected_authenticator_,
-                        message.size())) {
+    if (!SecureMemEqual(reinterpret_cast<const uint8_t*>(message.data()),
+                        expected_authenticator_, message.size())) {
       error_ = "peer's hash had incorrect value";
       return kResultFailed;
     }
@@ -266,3 +284,4 @@
 }
 
 }  // namespace crypto
+}  // namespace weave
diff --git a/libweave/external/crypto/p224_spake.h b/libweave/external/crypto/p224_spake.h
index 556b15c..aea280d 100644
--- a/libweave/external/crypto/p224_spake.h
+++ b/libweave/external/crypto/p224_spake.h
@@ -1,15 +1,19 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Copyright 2012 The Chromium OS Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#ifndef CRYPTO_P224_SPAKE_H_
-#define CRYPTO_P224_SPAKE_H_
+#ifndef LIBWEAVE_EXTERNAL_CRYPTO_P224_SPAKE_H_
+#define LIBWEAVE_EXTERNAL_CRYPTO_P224_SPAKE_H_
+
+#include <string>
 
 #include <base/gtest_prod_util.h>
 #include <base/strings/string_piece.h>
-#include <crypto/p224.h>
-#include <crypto/sha2.h>
 
+#include "libweave/external/crypto/p224.h"
+#include "libweave/external/crypto/sha2.h"
+
+namespace weave {
 namespace crypto {
 
 // P224EncryptedKeyExchange implements SPAKE2, a variant of Encrypted
@@ -32,7 +36,7 @@
 //   kResultSuccess: The authentication was successful.
 //
 // In each exchange, each peer always sends a message.
-class CRYPTO_EXPORT P224EncryptedKeyExchange {
+class P224EncryptedKeyExchange {
  public:
   enum Result {
     kResultPending,
@@ -122,5 +126,6 @@
 };
 
 }  // namespace crypto
+}  // namespace weave
 
-#endif  // CRYPTO_P224_SPAKE_H_
+#endif  // LIBWEAVE_EXTERNAL_CRYPTO_P224_SPAKE_H_
diff --git a/libweave/external/crypto/p224_spake_unittest.cc b/libweave/external/crypto/p224_spake_unittest.cc
index 15b5be2..03c9072 100644
--- a/libweave/external/crypto/p224_spake_unittest.cc
+++ b/libweave/external/crypto/p224_spake_unittest.cc
@@ -1,15 +1,16 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright 2011 The Chromium OS 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 "crypto/p224_spake.h"
+#include "libweave/external/crypto/p224_spake.h"
 
 #include <string>
 
-#include "base/logging.h"
-#include "base/strings/string_number_conversions.h"
-#include "testing/gtest/include/gtest/gtest.h"
+#include <base/logging.h>
+#include <base/strings/string_number_conversions.h>
+#include <gtest/gtest.h>
 
+namespace weave {
 namespace crypto {
 
 namespace {
@@ -172,3 +173,4 @@
 }
 
 }  // namespace crypto
+}  // namespace weave
diff --git a/libweave/external/crypto/p224_unittest.cc b/libweave/external/crypto/p224_unittest.cc
index aaf5f59..5d149da 100644
--- a/libweave/external/crypto/p224_unittest.cc
+++ b/libweave/external/crypto/p224_unittest.cc
@@ -1,14 +1,15 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Copyright 2012 The Chromium OS 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 <string.h>
 #include <stdio.h>
+#include <string.h>
 
-#include "crypto/p224.h"
+#include <gtest/gtest.h>
 
-#include "testing/gtest/include/gtest/gtest.h"
+#include "libweave/external/crypto/p224.h"
 
+namespace weave {
 namespace crypto {
 
 using p224::Point;
@@ -777,8 +778,8 @@
   const std::string external = point.ToString();
 
   ASSERT_EQ(external.size(), 56u);
-  EXPECT_TRUE(memcmp(external.data(), kBasePointExternal,
-                     sizeof(kBasePointExternal)) == 0);
+  EXPECT_EQ(0, memcmp(external.data(), kBasePointExternal,
+                      sizeof(kBasePointExternal)));
 }
 
 TEST(P224, ScalarBaseMult) {
@@ -788,8 +789,8 @@
     p224::ScalarBaseMult(kNISTTestVectors[i].scalar, &point);
     const std::string external = point.ToString();
     ASSERT_EQ(external.size(), 56u);
-    EXPECT_TRUE(memcmp(external.data(), kNISTTestVectors[i].affine,
-                       external.size()) == 0);
+    EXPECT_EQ(0, memcmp(external.data(), kNISTTestVectors[i].affine,
+                        external.size()));
   }
 }
 
@@ -803,7 +804,7 @@
 
   p224::Negate(b, &minus_b);
   p224::Add(a, b, &sum);
-  EXPECT_TRUE(memcmp(&sum, &a, sizeof(sum)) != 0);
+  EXPECT_NE(0, memcmp(&sum, &a, sizeof(sum)));
   p224::Add(minus_b, sum, &a_again);
   EXPECT_TRUE(a_again.ToString() == a.ToString());
 }
@@ -815,10 +816,11 @@
   // Test that x^0 = ∞.
   Point a;
   p224::ScalarBaseMult(reinterpret_cast<const uint8*>(zeros), &a);
-  EXPECT_TRUE(memcmp(zeros, a.ToString().data(), sizeof(zeros)) == 0);
+  EXPECT_EQ(0, memcmp(zeros, a.ToString().data(), sizeof(zeros)));
 
   // We shouldn't allow ∞ to be imported.
   EXPECT_FALSE(a.SetFromString(std::string(zeros, sizeof(zeros))));
 }
 
 }  // namespace crypto
+}  // namespace weave
diff --git a/libweave/external/crypto/random.cc b/libweave/external/crypto/random.cc
deleted file mode 100644
index a19bb1a..0000000
--- a/libweave/external/crypto/random.cc
+++ /dev/null
@@ -1,19 +0,0 @@
-// Copyright (c) 2012 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 "crypto/random.h"
-
-#include "base/rand_util.h"
-
-namespace crypto {
-
-void RandBytes(void *bytes, size_t length) {
-  // It's OK to call base::RandBytes(), because it's already strongly random.
-  // But _other_ code should go through this function to ensure that code which
-  // needs secure randomness is easily discoverable.
-  base::RandBytes(bytes, length);
-}
-
-}  // namespace crypto
-
diff --git a/libweave/external/crypto/random.h b/libweave/external/crypto/random.h
deleted file mode 100644
index 002616b..0000000
--- a/libweave/external/crypto/random.h
+++ /dev/null
@@ -1,21 +0,0 @@
-// Copyright (c) 2012 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.
-
-#ifndef CRYPTO_RANDOM_H_
-#define CRYPTO_RANDOM_H_
-
-#include <stddef.h>
-
-#include "crypto/crypto_export.h"
-
-namespace crypto {
-
-// Fills the given buffer with |length| random bytes of cryptographically
-// secure random numbers.
-// |length| must be positive.
-CRYPTO_EXPORT void RandBytes(void *bytes, size_t length);
-
-}
-
-#endif
diff --git a/libweave/external/crypto/secure_util.cc b/libweave/external/crypto/secure_util.cc
deleted file mode 100644
index 3fe8aa9..0000000
--- a/libweave/external/crypto/secure_util.cc
+++ /dev/null
@@ -1,19 +0,0 @@
-// Copyright (c) 2011 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 "crypto/secure_util.h"
-
-namespace crypto {
-
-bool SecureMemEqual(const void* s1, const void* s2, size_t n) {
-  const unsigned char* s1_ptr = reinterpret_cast<const unsigned char*>(s1);
-  const unsigned char* s2_ptr = reinterpret_cast<const unsigned char*>(s2);
-  unsigned char tmp = 0;
-  for (size_t i = 0; i < n; ++i, ++s1_ptr, ++s2_ptr)
-    tmp |= *s1_ptr ^ *s2_ptr;
-  return (tmp == 0);
-}
-
-}  // namespace crypto
-
diff --git a/libweave/external/crypto/secure_util.h b/libweave/external/crypto/secure_util.h
deleted file mode 100644
index cfe05ca..0000000
--- a/libweave/external/crypto/secure_util.h
+++ /dev/null
@@ -1,29 +0,0 @@
-// Copyright (c) 2012 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.
-
-#ifndef CRYPTO_SECURE_UTIL_H_
-#define CRYPTO_SECURE_UTIL_H_
-
-#include <stddef.h>
-
-#include "crypto/crypto_export.h"
-
-namespace crypto {
-
-// Performs a constant-time comparison of two strings, returning true if the
-// strings are equal.
-//
-// For cryptographic operations, comparison functions such as memcmp() may
-// expose side-channel information about input, allowing an attacker to
-// perform timing analysis to determine what the expected bits should be. In
-// order to avoid such attacks, the comparison must execute in constant time,
-// so as to not to reveal to the attacker where the difference(s) are.
-// For an example attack, see
-// http://groups.google.com/group/keyczar-discuss/browse_thread/thread/5571eca0948b2a13
-CRYPTO_EXPORT bool SecureMemEqual(const void* s1, const void* s2, size_t n);
-
-}  // namespace crypto
-
-#endif  // CRYPTO_SECURE_UTIL_H_
-
diff --git a/libweave/external/crypto/sha2.cc b/libweave/external/crypto/sha2.cc
index 6f36237..59a111d 100644
--- a/libweave/external/crypto/sha2.cc
+++ b/libweave/external/crypto/sha2.cc
@@ -1,25 +1,33 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Copyright 2012 The Chromium OS 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 "crypto/sha2.h"
+#include "libweave/external/crypto/sha2.h"
 
-#include "base/memory/scoped_ptr.h"
-#include "base/stl_util.h"
-#include "crypto/secure_hash.h"
+#include <algorithm>
+#include <openssl/sha.h>
 
+#include <base/memory/scoped_ptr.h>
+
+namespace weave {
 namespace crypto {
 
-void SHA256HashString(const base::StringPiece& str, void* output, size_t len) {
-  scoped_ptr<SecureHash> ctx(SecureHash::Create(SecureHash::SHA256));
-  ctx->Update(str.data(), str.length());
-  ctx->Finish(output, len);
+void SHA256HashString(const base::StringPiece& str, uint8_t* output,
+                      size_t len) {
+  std::string hash = SHA256HashString(str);
+  len = std::min(hash.size(), len);
+  std::copy(hash.begin(), hash.begin() + len, output);
 }
 
 std::string SHA256HashString(const base::StringPiece& str) {
-  std::string output(kSHA256Length, 0);
-  SHA256HashString(str, string_as_array(&output), output.size());
-  return output;
+  SHA256_CTX sha_context;
+  SHA256_Init(&sha_context);
+  SHA256_Update(&sha_context, str.data(), str.size());
+
+  std::string hash(kSHA256Length, 0);
+  SHA256_Final(reinterpret_cast<uint8_t*>(&hash[0]), &sha_context);
+  return hash;
 }
 
 }  // namespace crypto
+}  // namespace weave
diff --git a/libweave/external/crypto/sha2.h b/libweave/external/crypto/sha2.h
index 7e279d3..34f6af8 100644
--- a/libweave/external/crypto/sha2.h
+++ b/libweave/external/crypto/sha2.h
@@ -1,15 +1,15 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Copyright 2012 The Chromium OS Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#ifndef CRYPTO_SHA2_H_
-#define CRYPTO_SHA2_H_
+#ifndef LIBWEAVE_EXTERNAL_CRYPTO_SHA2_H_
+#define LIBWEAVE_EXTERNAL_CRYPTO_SHA2_H_
 
 #include <string>
 
-#include "base/strings/string_piece.h"
-#include "crypto/crypto_export.h"
+#include <base/strings/string_piece.h>
 
+namespace weave {
 namespace crypto {
 
 // These functions perform SHA-256 operations.
@@ -21,13 +21,14 @@
 // Computes the SHA-256 hash of the input string 'str' and stores the first
 // 'len' bytes of the hash in the output buffer 'output'.  If 'len' > 32,
 // only 32 bytes (the full hash) are stored in the 'output' buffer.
-CRYPTO_EXPORT void SHA256HashString(const base::StringPiece& str,
-                                    void* output, size_t len);
+void SHA256HashString(const base::StringPiece& str, uint8_t* output,
+                      size_t len);
 
 // Convenience version of the above that returns the result in a 32-byte
 // string.
-CRYPTO_EXPORT std::string SHA256HashString(const base::StringPiece& str);
+std::string SHA256HashString(const base::StringPiece& str);
 
 }  // namespace crypto
+}  // namespace weave
 
-#endif  // CRYPTO_SHA2_H_
+#endif  // LIBWEAVE_EXTERNAL_CRYPTO_SHA2_H_
diff --git a/libweave/external/crypto/sha2_unittest.cc b/libweave/external/crypto/sha2_unittest.cc
index 78da136..a128840 100644
--- a/libweave/external/crypto/sha2_unittest.cc
+++ b/libweave/external/crypto/sha2_unittest.cc
@@ -1,11 +1,13 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright 2011 The Chromium OS 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 "crypto/sha2.h"
+#include "libweave/external/crypto/sha2.h"
 
-#include "base/basictypes.h"
-#include "testing/gtest/include/gtest/gtest.h"
+#include <base/basictypes.h>
+#include <gtest/gtest.h>
+
+namespace weave {
 
 TEST(Sha256Test, Test1) {
   // Example B.1 from FIPS 180-2: one-block message.
@@ -98,3 +100,5 @@
   for (size_t i = 0; i < sizeof(output_truncated3); i++)
     EXPECT_EQ(expected3[i], static_cast<int>(output_truncated3[i]));
 }
+
+}  // namespace weave
diff --git a/libweave/libweave.gyp b/libweave/libweave.gyp
index 527c246..0f0a543 100644
--- a/libweave/libweave.gyp
+++ b/libweave/libweave.gyp
@@ -14,6 +14,15 @@
   },
   'targets': [
     {
+      'target_name': 'libweave_external',
+      'type': 'static_library',
+      'sources': [
+        'external/crypto/p224.cc',
+        'external/crypto/p224_spake.cc',
+        'external/crypto/sha2.cc',
+      ],
+    },
+    {
       'target_name': 'libweave_common',
       'type': 'static_library',
       'sources': [
@@ -69,6 +78,7 @@
       ],
       'dependencies': [
         'libweave_common',
+        'libweave_external',
       ],
       'sources': [
         'src/empty.cc',
@@ -100,10 +110,14 @@
           },
           'dependencies': [
             'libweave_common',
+            'libweave_external',
             'libweave-test-<(libbase_ver)',
           ],
           'includes': ['../common-mk/common_test.gypi'],
           'sources': [
+            'external/crypto/p224_spake_unittest.cc',
+            'external/crypto/p224_unittest.cc',
+            'external/crypto/sha2_unittest.cc',
             'src/base_api_handler_unittest.cc',
             'src/buffet_config_unittest.cc',
             'src/commands/cloud_command_proxy_unittest.cc',
diff --git a/libweave/src/privet/security_manager.cc b/libweave/src/privet/security_manager.cc
index 86a09dc..fe33de9 100644
--- a/libweave/src/privet/security_manager.cc
+++ b/libweave/src/privet/security_manager.cc
@@ -21,7 +21,7 @@
 #include <chromeos/data_encoding.h>
 #include <chromeos/key_value_store.h>
 #include <chromeos/strings/string_utils.h>
-#include <crypto/p224_spake.h>
+#include "libweave/external/crypto/p224_spake.h"
 
 #include "libweave/src/privet/constants.h"
 #include "libweave/src/privet/openssl_utils.h"
diff --git a/libweave/src/privet/security_manager_unittest.cc b/libweave/src/privet/security_manager_unittest.cc
index cf1e044..9c995be 100644
--- a/libweave/src/privet/security_manager_unittest.cc
+++ b/libweave/src/privet/security_manager_unittest.cc
@@ -22,7 +22,7 @@
 #include <chromeos/data_encoding.h>
 #include <chromeos/key_value_store.h>
 #include <chromeos/strings/string_utils.h>
-#include <crypto/p224_spake.h>
+#include "libweave/external/crypto/p224_spake.h"
 #include <gmock/gmock.h>
 #include <gtest/gtest.h>