Commit 903f7188 authored by Daniel McArdle's avatar Daniel McArdle Committed by Commit Bot

Replace base::MD5 implementation with boringssl for non-NaCl builds

We cannot delete the base::MD5 implementation until NaCl is
removed. However, this CL effectively unships the code from binaries
where NaCl is not enabled.

Bug: 755368
Change-Id: I1a0b3a1f71a2798af26d2819a7112e01fb8f5781
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1596358
Commit-Queue: Daniel McArdle <dmcardle@chromium.org>
Auto-Submit: Daniel McArdle <dmcardle@chromium.org>
Reviewed-by: default avatarRyan Sleevi <rsleevi@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarSam McNally <sammc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#658833}
parent ad5f37db
...@@ -307,8 +307,6 @@ jumbo_component("base") { ...@@ -307,8 +307,6 @@ jumbo_component("base") {
"guid.h", "guid.h",
"hash/hash.cc", "hash/hash.cc",
"hash/hash.h", "hash/hash.h",
"hash/md5.cc",
"hash/md5.h",
"hash/sha1.cc", "hash/sha1.cc",
"hash/sha1.h", "hash/sha1.h",
"immediate_crash.h", "immediate_crash.h",
...@@ -1565,6 +1563,25 @@ jumbo_component("base") { ...@@ -1565,6 +1563,25 @@ jumbo_component("base") {
] ]
} }
# Use the base implementation of hash functions when building for
# NaCl. Otherwise, use boringssl. Building boringssl for NaCl opens
# a can of worms surrounding the nacl_io library.
#
# TODO(crbug.com/702997) Use only boringssl when NaCl is removed.
sources += [ "hash/md5.h" ]
if (is_nacl) {
sources += [
"hash/md5_nacl.cc",
"hash/md5_nacl.h",
]
} else {
sources += [
"hash/md5_boringssl.cc",
"hash/md5_boringssl.h",
]
public_deps += [ "//third_party/boringssl" ]
}
# NaCl. # NaCl.
if (is_nacl) { if (is_nacl) {
# We reset sources_assignment_filter in order to explicitly include # We reset sources_assignment_filter in order to explicitly include
......
...@@ -2,6 +2,7 @@ include_rules = [ ...@@ -2,6 +2,7 @@ include_rules = [
"+jni", "+jni",
"+third_party/ashmem", "+third_party/ashmem",
"+third_party/apple_apsl", "+third_party/apple_apsl",
"+third_party/boringssl/src/include",
"+third_party/ced", "+third_party/ced",
"+third_party/lss", "+third_party/lss",
"+third_party/modp_b64", "+third_party/modp_b64",
......
...@@ -5,13 +5,17 @@ ...@@ -5,13 +5,17 @@
#ifndef BASE_HASH_MD5_H_ #ifndef BASE_HASH_MD5_H_
#define BASE_HASH_MD5_H_ #define BASE_HASH_MD5_H_
#include <stddef.h> #include <string>
#include <stdint.h>
#include "base/base_export.h" #include "base/base_export.h"
#include "base/strings/string_piece.h" #include "base/strings/string_piece.h"
#include "build/build_config.h"
namespace base { #if defined(OS_NACL)
#include "base/hash/md5_nacl.h"
#else
#include "base/hash/md5_boringssl.h"
#endif
// MD5 stands for Message Digest algorithm 5. // MD5 stands for Message Digest algorithm 5.
// MD5 is a robust hash function, designed for cyptography, but often used // MD5 is a robust hash function, designed for cyptography, but often used
...@@ -36,14 +40,7 @@ namespace base { ...@@ -36,14 +40,7 @@ namespace base {
// //
// You can call MD5DigestToBase16() to generate a string of the digest. // You can call MD5DigestToBase16() to generate a string of the digest.
// The output of an MD5 operation. namespace base {
struct MD5Digest {
uint8_t a[16];
};
// Used for storing intermediate data during an MD5 computation. Callers
// should not access the data.
typedef char MD5Context[88];
// Initializes the given MD5 context structure for subsequent calls to // Initializes the given MD5 context structure for subsequent calls to
// MD5Update(). // MD5Update().
...@@ -57,12 +54,6 @@ BASE_EXPORT void MD5Update(MD5Context* context, const StringPiece& data); ...@@ -57,12 +54,6 @@ BASE_EXPORT void MD5Update(MD5Context* context, const StringPiece& data);
// Finalizes the MD5 operation and fills the buffer with the digest. // Finalizes the MD5 operation and fills the buffer with the digest.
BASE_EXPORT void MD5Final(MD5Digest* digest, MD5Context* context); BASE_EXPORT void MD5Final(MD5Digest* digest, MD5Context* context);
// MD5IntermediateFinal() generates a digest without finalizing the MD5
// operation. Can be used to generate digests for the input seen thus far,
// without affecting the digest generated for the entire input.
BASE_EXPORT void MD5IntermediateFinal(MD5Digest* digest,
const MD5Context* context);
// Converts a digest into human-readable hexadecimal. // Converts a digest into human-readable hexadecimal.
BASE_EXPORT std::string MD5DigestToBase16(const MD5Digest& digest); BASE_EXPORT std::string MD5DigestToBase16(const MD5Digest& digest);
......
// Copyright (c) 2019 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 "base/hash/md5.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
namespace base {
void MD5Init(MD5Context* context) {
MD5_Init(context);
}
void MD5Update(MD5Context* context, const StringPiece& data) {
MD5_Update(context, reinterpret_cast<const uint8_t*>(data.data()),
data.size());
}
void MD5Final(MD5Digest* digest, MD5Context* context) {
MD5_Final(digest->a, context);
}
std::string MD5DigestToBase16(const MD5Digest& digest) {
return ToLowerASCII(HexEncode(digest.a, MD5_DIGEST_LENGTH));
}
void MD5Sum(const void* data, size_t length, MD5Digest* digest) {
MD5(reinterpret_cast<const uint8_t*>(data), length, digest->a);
}
std::string MD5String(const StringPiece& str) {
MD5Digest digest;
MD5Sum(str.data(), str.size(), &digest);
return MD5DigestToBase16(digest);
}
} // namespace base
// Copyright (c) 2019 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 BASE_HASH_MD5_BORINGSSL_H_
#define BASE_HASH_MD5_BORINGSSL_H_
#include <stdint.h>
#include "third_party/boringssl/src/include/openssl/md5.h"
namespace base {
// The output of an MD5 operation.
struct MD5Digest {
uint8_t a[MD5_DIGEST_LENGTH];
};
// Used for storing intermediate data during an MD5 computation. Callers
// should not access the data.
typedef MD5_CTX MD5Context;
} // namespace base
#endif // BASE_HASH_MD5_BORINGSSL_H_
...@@ -21,10 +21,10 @@ ...@@ -21,10 +21,10 @@
* will fill a supplied 16-byte array with the digest. * will fill a supplied 16-byte array with the digest.
*/ */
#include "base/hash/md5.h"
#include <stddef.h> #include <stddef.h>
#include "base/hash/md5.h"
namespace { namespace {
struct Context { struct Context {
...@@ -261,14 +261,6 @@ void MD5Final(MD5Digest* digest, MD5Context* context) { ...@@ -261,14 +261,6 @@ void MD5Final(MD5Digest* digest, MD5Context* context) {
memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */ memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
} }
void MD5IntermediateFinal(MD5Digest* digest, const MD5Context* context) {
/* MD5Final mutates the MD5Context*. Make a copy for generating the
intermediate value. */
MD5Context context_copy;
memcpy(&context_copy, context, sizeof(context_copy));
MD5Final(digest, &context_copy);
}
std::string MD5DigestToBase16(const MD5Digest& digest) { std::string MD5DigestToBase16(const MD5Digest& digest) {
static char const zEncode[] = "0123456789abcdef"; static char const zEncode[] = "0123456789abcdef";
......
// Copyright (c) 2019 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 BASE_HASH_MD5_NACL_H_
#define BASE_HASH_MD5_NACL_H_
#include <stdint.h>
namespace base {
// The output of an MD5 operation.
struct MD5Digest {
uint8_t a[16];
};
// Used for storing intermediate data during an MD5 computation. Callers
// should not access the data.
typedef char MD5Context[88];
} // namespace base
#endif // BASE_HASH_MD5_NACL_H_
...@@ -2,13 +2,12 @@ ...@@ -2,13 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "base/hash/md5.h"
#include <string.h> #include <string.h>
#include <memory> #include <memory>
#include <string> #include <string>
#include "base/hash/md5.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
namespace base { namespace base {
...@@ -183,49 +182,4 @@ TEST(MD5, ContextWithStringData) { ...@@ -183,49 +182,4 @@ TEST(MD5, ContextWithStringData) {
EXPECT_EQ(expected, actual); EXPECT_EQ(expected, actual);
} }
// Test that a digest generated by MD5IntermediateFinal() gives the same results
// as an independently-calculated digest, and also does not modify the context.
TEST(MD5, IntermediateFinal) {
// Independent context over the header.
MD5Context check_header_context;
MD5Init(&check_header_context);
// Independent context over entire input.
MD5Context check_full_context;
MD5Init(&check_full_context);
// Context intermediate digest will be calculated from.
MD5Context context;
MD5Init(&context);
static const char kHeader[] = "header data";
static const char kBody[] = "payload data";
MD5Update(&context, kHeader);
MD5Update(&check_header_context, kHeader);
MD5Update(&check_full_context, kHeader);
MD5Digest check_header_digest;
MD5Final(&check_header_digest, &check_header_context);
MD5Digest header_digest;
MD5IntermediateFinal(&header_digest, &context);
MD5Update(&context, kBody);
MD5Update(&check_full_context, kBody);
MD5Digest check_full_digest;
MD5Final(&check_full_digest, &check_full_context);
MD5Digest digest;
MD5Final(&digest, &context);
// The header and full digest pairs are the same, and they aren't the same as
// each other.
EXPECT_TRUE(
!memcmp(&header_digest, &check_header_digest, sizeof(header_digest)));
EXPECT_TRUE(!memcmp(&digest, &check_full_digest, sizeof(digest)));
EXPECT_TRUE(memcmp(&digest, &header_digest, sizeof(digest)));
}
} // namespace base } // namespace base
...@@ -51,6 +51,7 @@ source_set("ziparchiver_unittests") { ...@@ -51,6 +51,7 @@ source_set("ziparchiver_unittests") {
deps = [ deps = [
":zip_archiver_library", ":zip_archiver_library",
"//base",
"//testing/gtest", "//testing/gtest",
] ]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment