Commit 13b49757 by Jamie Madill Committed by Commit Bot

Add base SHA-1 routines.

We will need these to implement a more secure version of hashing for the program binary cache. These versions of lightly modified from the Chrome versions to make compilation simpler. BUG=angleproject:1897 Change-Id: Ibcadb56c2316e3f39655a07fa28b5fbd6103e452 Reviewed-on: https://chromium-review.googlesource.com/550000Reviewed-by: 's avatarCorentin Wallez <cwallez@chromium.org> Commit-Queue: Jamie Madill <jmadill@chromium.org>
parent 96a483bc
Name: Chromium base:: helper Classes Name: Chromium base:: helper Classes
Short Name: base::numerics and base::MRUCache Short Name: base::numerics, base::MRUCachem, base::SHA1
Version: Version:
URL: https://chromium.googlesource.com/chromium/src/base/+/master URL: https://chromium.googlesource.com/chromium/src/base/+/master
SOURCE CODE: Copy the Chromium folder manually into this folder and run git cl format. SOURCE CODE: Copy the Chromium folder manually into this folder and run git cl format.
...@@ -12,6 +12,7 @@ License File: LICENSE in Chromium/src ...@@ -12,6 +12,7 @@ License File: LICENSE in Chromium/src
Description: Description:
base::numerics is a library for doing some simple safe math and conversions. base::numerics is a library for doing some simple safe math and conversions.
base::MRUCache is a few collections of most-recently-used caching structures. base::MRUCache is a few collections of most-recently-used caching structures.
base::SHA1 is a secure hashing algorithm.
To update the checkout, simply overwrite the folder with Chromium's latest, apply To update the checkout, simply overwrite the folder with Chromium's latest, apply
the appropriate namespace, and make sure the paths are correct (anglebase/ instead of the appropriate namespace, and make sure the paths are correct (anglebase/ instead of
......
//
// Copyright 2017 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// base_export.h: Compatiblity hacks for importing Chromium's base/SHA1.
#ifndef ANGLEBASE_BASE_EXPORT_H_
#define ANGLEBASE_BASE_EXPORT_H_
#define ANGLEBASE_EXPORT
#endif // ANGLEBASE_BASE_EXPORT_H_
\ No newline at end of file
// 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 "anglebase/sha1.h"
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "anglebase/sys_byteorder.h"
namespace angle
{
namespace base
{
// Implementation of SHA-1. Only handles data in byte-sized blocks,
// which simplifies the code a fair bit.
// 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
// 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();
// TODO(jhawkins): Replace this implementation with a per-platform
// implementation using each platform's crypto library. See
// http://crbug.com/47218
class SecureHashAlgorithm
{
public:
SecureHashAlgorithm() { Init(); }
static const int kDigestSizeBytes;
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;
};
static 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;
}
}
static inline uint32_t S(uint32_t n, uint32_t X)
{
return (X << n) | (X >> (32 - n));
}
static 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;
}
}
const int SecureHashAlgorithm::kDigestSizeBytes = 20;
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::Final()
{
Pad();
Process();
for (int t = 0; t < 5; ++t)
H[t] = ByteSwap(H[t]);
}
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::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;
}
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(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;
}
std::string SHA1HashString(const std::string &str)
{
char hash[SecureHashAlgorithm::kDigestSizeBytes];
SHA1HashBytes(reinterpret_cast<const unsigned char *>(str.c_str()), str.length(),
reinterpret_cast<unsigned char *>(hash));
return std::string(hash, SecureHashAlgorithm::kDigestSizeBytes);
}
void SHA1HashBytes(const unsigned char *data, size_t len, unsigned char *hash)
{
SecureHashAlgorithm sha;
sha.Update(data, len);
sha.Final();
memcpy(hash, sha.Digest(), SecureHashAlgorithm::kDigestSizeBytes);
}
} // namespace base
} // namespace angle
// 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.
#ifndef ANGLEBASE_SHA1_H_
#define ANGLEBASE_SHA1_H_
#include <stddef.h>
#include <string>
#include "anglebase/base_export.h"
namespace angle
{
namespace base
{
// These functions perform SHA-1 operations.
static const size_t kSHA1Length = 20; // Length in bytes of a SHA-1 hash.
// Computes the SHA-1 hash of the input string |str| and returns the full
// hash.
ANGLEBASE_EXPORT std::string SHA1HashString(const std::string &str);
// Computes the SHA-1 hash of the |len| bytes in |data| and puts the hash
// in |hash|. |hash| must be kSHA1Length bytes long.
ANGLEBASE_EXPORT void SHA1HashBytes(const unsigned char *data, size_t len, unsigned char *hash);
} // namespace base
} // namespace angle
#endif // ANGLEBASE_SHA1_H_
//
// Copyright 2017 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// sys_byteorder.h: Compatiblity hacks for importing Chromium's base/SHA1.
#ifndef ANGLEBASE_SYS_BYTEORDER_H_
#define ANGLEBASE_SYS_BYTEORDER_H_
namespace angle
{
namespace base
{
// Returns a value with all bytes in |x| swapped, i.e. reverses the endianness.
inline uint16_t ByteSwap(uint16_t x)
{
#if defined(_MSC_VER)
return _byteswap_ushort(x);
#else
return __builtin_bswap16(x);
#endif
}
inline uint32_t ByteSwap(uint32_t x)
{
#if defined(_MSC_VER)
return _byteswap_ulong(x);
#else
return __builtin_bswap32(x);
#endif
}
inline uint64_t ByteSwap(uint64_t x)
{
#if defined(_MSC_VER)
return _byteswap_uint64(x);
#else
return __builtin_bswap64(x);
#endif
}
} // namespace base
} // namespace angle
#endif // ANGLEBASE_SYS_BYTEORDER_H_
\ No newline at end of file
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
'common/platform.h', 'common/platform.h',
'common/string_utils.cpp', 'common/string_utils.cpp',
'common/string_utils.h', 'common/string_utils.h',
'common/third_party/base/anglebase/base_export.h',
'common/third_party/base/anglebase/containers/mru_cache.h', 'common/third_party/base/anglebase/containers/mru_cache.h',
'common/third_party/base/anglebase/logging.h', 'common/third_party/base/anglebase/logging.h',
'common/third_party/base/anglebase/macros.h', 'common/third_party/base/anglebase/macros.h',
...@@ -32,6 +33,9 @@ ...@@ -32,6 +33,9 @@
'common/third_party/base/anglebase/numerics/safe_conversions_impl.h', 'common/third_party/base/anglebase/numerics/safe_conversions_impl.h',
'common/third_party/base/anglebase/numerics/safe_math.h', 'common/third_party/base/anglebase/numerics/safe_math.h',
'common/third_party/base/anglebase/numerics/safe_math_impl.h', 'common/third_party/base/anglebase/numerics/safe_math_impl.h',
'common/third_party/base/anglebase/sha1.cc',
'common/third_party/base/anglebase/sha1.h',
'common/third_party/base/anglebase/sys_byteorder.h',
'common/third_party/murmurhash/MurmurHash3.cpp', 'common/third_party/murmurhash/MurmurHash3.cpp',
'common/third_party/murmurhash/MurmurHash3.h', 'common/third_party/murmurhash/MurmurHash3.h',
'common/tls.cpp', 'common/tls.cpp',
......
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