Commit 6d53dfef authored by Penny MacNeil's avatar Penny MacNeil Committed by Commit Bot

[chrome_elf] Add a tiny SHA1 string hash source_set.

(Code taken from base/sha1.*.)

BUG=769590
TEST=chrome_elf_unittests.exe, SHA1Test.*

Change-Id: Ife457801650800e1c3e34653e2bfc68f9c5b801d
Reviewed-on: https://chromium-review.googlesource.com/742194
Commit-Queue: Penny MacNeil <pennymac@chromium.org>
Reviewed-by: 's avatarRobert Shield <robertshield@chromium.org>
Cr-Commit-Position: refs/heads/master@{#513892}
parent f9184bc6
......@@ -101,18 +101,6 @@ static_library("test_stubs") {
## source sets
##------------------------------------------------------------------------------
source_set("security") {
sources = [
"chrome_elf_security.cc",
"chrome_elf_security.h",
]
deps = [
":constants",
":nt_registry",
"//chrome/install_static:install_static_util",
]
}
source_set("constants") {
sources = [
"chrome_elf_constants.cc",
......@@ -130,6 +118,25 @@ source_set("dll_hash") {
]
}
source_set("security") {
sources = [
"chrome_elf_security.cc",
"chrome_elf_security.h",
]
deps = [
":constants",
":nt_registry",
"//chrome/install_static:install_static_util",
]
}
source_set("sha1") {
sources = [
"sha1/sha1.cc",
"sha1/sha1.h",
]
}
##------------------------------------------------------------------------------
## chrome_elf sub targets
##------------------------------------------------------------------------------
......@@ -243,6 +250,7 @@ test("chrome_elf_unittests") {
"hook_util/test/hook_util_test.cc",
"nt_registry/nt_registry_unittest.cc",
"run_all_unittests.cc",
"sha1/sha1_unittest.cc",
"whitelist/whitelist_ime_unittest.cc",
]
include_dirs = [ "$target_gen_dir" ]
......@@ -255,6 +263,7 @@ test("chrome_elf_unittests") {
":hook_util_test_dll",
":nt_registry",
":security",
":sha1",
":whitelist",
"//base",
"//base/test:test_support",
......
// Copyright 2017 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.
//------------------------------------------------------------------------------
// * This code is taken from base/sha1, with small changes.
//------------------------------------------------------------------------------
#include "chrome_elf/sha1/sha1.h"
#include <stddef.h>
#include <stdint.h>
#include <string.h>
namespace elf_sha1 {
namespace {
// Usage example:
//
// SecureHashAlgorithm sha;
// while(there is data to hash)
// sha.Update(moredata, size of data);
// sha.Final();
// memcpy(somewhere, sha.Digest(), 20);
//
// to reuse the instance of sha, call sha.Init();
class SecureHashAlgorithm {
public:
SecureHashAlgorithm() { Init(); }
void Init();
void Update(const void* data, size_t nbytes);
void Final();
// 20 bytes of message digest.
const unsigned char* Digest() const {
return reinterpret_cast<const unsigned char*>(H);
}
private:
void Pad();
void Process();
uint32_t A, B, C, D, E;
uint32_t H[5];
union {
uint32_t W[80];
uint8_t M[64];
};
uint32_t cursor;
uint64_t l;
};
//------------------------------------------------------------------------------
// Private functions
//------------------------------------------------------------------------------
// Identifier names follow notation in FIPS PUB 180-3, where you'll
// also find a description of the algorithm:
// http://csrc.nist.gov/publications/fips/fips180-3/fips180-3_final.pdf
inline uint32_t f(uint32_t t, uint32_t B, uint32_t C, uint32_t D) {
if (t < 20) {
return (B & C) | ((~B) & D);
} else if (t < 40) {
return B ^ C ^ D;
} else if (t < 60) {
return (B & C) | (B & D) | (C & D);
} else {
return B ^ C ^ D;
}
}
inline uint32_t S(uint32_t n, uint32_t X) {
return (X << n) | (X >> (32 - n));
}
inline uint32_t K(uint32_t t) {
if (t < 20) {
return 0x5a827999;
} else if (t < 40) {
return 0x6ed9eba1;
} else if (t < 60) {
return 0x8f1bbcdc;
} else {
return 0xca62c1d6;
}
}
void SecureHashAlgorithm::Init() {
A = 0;
B = 0;
C = 0;
D = 0;
E = 0;
cursor = 0;
l = 0;
H[0] = 0x67452301;
H[1] = 0xefcdab89;
H[2] = 0x98badcfe;
H[3] = 0x10325476;
H[4] = 0xc3d2e1f0;
}
void SecureHashAlgorithm::Update(const void* data, size_t nbytes) {
const uint8_t* d = reinterpret_cast<const uint8_t*>(data);
while (nbytes--) {
M[cursor++] = *d++;
if (cursor >= 64)
Process();
l += 8;
}
}
void SecureHashAlgorithm::Final() {
Pad();
Process();
for (size_t t = 0; t < 5; ++t)
H[t] = _byteswap_ulong(H[t]);
}
void SecureHashAlgorithm::Process() {
uint32_t t;
// Each a...e corresponds to a section in the FIPS 180-3 algorithm.
// a.
//
// W and M are in a union, so no need to memcpy.
// memcpy(W, M, sizeof(M));
for (t = 0; t < 16; ++t)
W[t] = _byteswap_ulong(W[t]);
// b.
for (t = 16; t < 80; ++t)
W[t] = S(1, W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16]);
// c.
A = H[0];
B = H[1];
C = H[2];
D = H[3];
E = H[4];
// d.
for (t = 0; t < 80; ++t) {
uint32_t TEMP = S(5, A) + f(t, B, C, D) + E + W[t] + K(t);
E = D;
D = C;
C = S(30, B);
B = A;
A = TEMP;
}
// e.
H[0] += A;
H[1] += B;
H[2] += C;
H[3] += D;
H[4] += E;
cursor = 0;
}
void SecureHashAlgorithm::Pad() {
M[cursor++] = 0x80;
if (cursor > 64 - 8) {
// pad out to next block
while (cursor < 64)
M[cursor++] = 0;
Process();
}
while (cursor < 64 - 8)
M[cursor++] = 0;
M[cursor++] = (l >> 56) & 0xff;
M[cursor++] = (l >> 48) & 0xff;
M[cursor++] = (l >> 40) & 0xff;
M[cursor++] = (l >> 32) & 0xff;
M[cursor++] = (l >> 24) & 0xff;
M[cursor++] = (l >> 16) & 0xff;
M[cursor++] = (l >> 8) & 0xff;
M[cursor++] = l & 0xff;
}
// Computes the SHA-1 hash of the |len| bytes in |data| and puts the hash
// in |hash|. |hash| must be kSHA1Length bytes long.
void SHA1HashBytes(const unsigned char* data, size_t len, unsigned char* hash) {
SecureHashAlgorithm sha;
sha.Update(data, len);
sha.Final();
::memcpy(hash, sha.Digest(), kSHA1Length);
}
} // namespace
//------------------------------------------------------------------------------
// Public functions
//------------------------------------------------------------------------------
std::string SHA1HashString(const std::string& str) {
char hash[kSHA1Length] = {};
SHA1HashBytes(reinterpret_cast<const unsigned char*>(str.c_str()),
str.length(), reinterpret_cast<unsigned char*>(hash));
return std::string(hash, kSHA1Length);
}
} // namespace elf_sha1
// Copyright 2017 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.
//------------------------------------------------------------------------------
// * This code is taken from base/sha1, with small changes.
//------------------------------------------------------------------------------
#ifndef CHROME_ELF_SHA1_SHA1_H_
#define CHROME_ELF_SHA1_SHA1_H_
#include <stddef.h>
#include <string>
namespace elf_sha1 {
// Length in bytes of a SHA-1 hash.
constexpr size_t kSHA1Length = 20;
// Computes the SHA1 hash of the input string |str| and returns the full
// hash. The returned SHA1 will be 20 bytes in length.
std::string SHA1HashString(const std::string& str);
} // namespace elf_sha1
#endif // CHROME_ELF_SHA1_SHA1_H_
// Copyright 2017 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.
//------------------------------------------------------------------------------
// * This code is taken from base/sha1, with small changes.
//------------------------------------------------------------------------------
#include "chrome_elf/sha1/sha1.h"
#include <stddef.h>
#include <string>
#include "testing/gtest/include/gtest/gtest.h"
namespace {
TEST(SHA1Test, Test1) {
// Example A.1 from FIPS 180-2: one-block message.
std::string input = "abc";
int expected[] = {0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e,
0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, 0x9c, 0xd0, 0xd8, 0x9d};
std::string output = elf_sha1::SHA1HashString(input);
for (size_t i = 0; i < elf_sha1::kSHA1Length; i++)
EXPECT_EQ(expected[i], output[i] & 0xFF);
}
TEST(SHA1Test, Test2) {
// Example A.2 from FIPS 180-2: multi-block message.
std::string input =
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
int expected[] = {0x84, 0x98, 0x3e, 0x44, 0x1c, 0x3b, 0xd2, 0x6e, 0xba, 0xae,
0x4a, 0xa1, 0xf9, 0x51, 0x29, 0xe5, 0xe5, 0x46, 0x70, 0xf1};
std::string output = elf_sha1::SHA1HashString(input);
for (size_t i = 0; i < elf_sha1::kSHA1Length; i++)
EXPECT_EQ(expected[i], output[i] & 0xFF);
}
TEST(SHA1Test, Test3) {
// Example A.3 from FIPS 180-2: long message.
std::string input(1000000, 'a');
int expected[] = {0x34, 0xaa, 0x97, 0x3c, 0xd4, 0xc4, 0xda, 0xa4, 0xf6, 0x1e,
0xeb, 0x2b, 0xdb, 0xad, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6f};
std::string output = elf_sha1::SHA1HashString(input);
for (size_t i = 0; i < elf_sha1::kSHA1Length; i++)
EXPECT_EQ(expected[i], output[i] & 0xFF);
}
} // namespace
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