From 15f090d3b2f0bf3994aacd00ab07da3b1f65ea00 Mon Sep 17 00:00:00 2001 From: Michael Vrable Date: Wed, 26 Sep 2012 07:54:25 -0700 Subject: [PATCH] Rework hash implementations to provide additional algorithms. Provide a generic hash interface, rework SHA-1 to use the interface, and also add code for SHA-224/SHA-256. --- Makefile | 4 +- hash.cc | 105 ++++++++++ hash.h | 65 +++++++ main.cc | 10 +- store.cc | 8 +- third_party/sha1.cc | 38 ++++ third_party/sha256.cc | 439 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 661 insertions(+), 8 deletions(-) create mode 100644 hash.cc create mode 100644 hash.h create mode 100644 third_party/sha256.cc diff --git a/Makefile b/Makefile index e707305..a57062c 100644 --- a/Makefile +++ b/Makefile @@ -5,8 +5,8 @@ CXXFLAGS=-O -Wall -Wextra -D_FILE_OFFSET_BITS=64 $(DEBUG) \ -DCUMULUS_VERSION=$(shell cat version) LDFLAGS=$(DEBUG) $(shell pkg-config --libs $(PACKAGES)) -THIRD_PARTY_SRCS=chunk.cc sha1.cc -SRCS=exclude.cc localdb.cc main.cc metadata.cc ref.cc remote.cc \ +THIRD_PARTY_SRCS=chunk.cc sha1.cc sha256.cc +SRCS=exclude.cc hash.cc localdb.cc main.cc metadata.cc ref.cc remote.cc \ store.cc subfile.cc util.cc $(addprefix third_party/,$(THIRD_PARTY_SRCS)) OBJS=$(SRCS:.cc=.o) diff --git a/hash.cc b/hash.cc new file mode 100644 index 0000000..2054bcc --- /dev/null +++ b/hash.cc @@ -0,0 +1,105 @@ +/* Cumulus: Smart Filesystem Backup to Dumb Servers + * + * Copyright (C) 2012 Michael Vrable + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include +#include + +#include "hash.h" + +using std::map; +using std::string; + +static map hash_registry; + +void Hash::Register(const std::string& name, Hash *(*constructor)()) +{ + printf("Registered hash algorithm %s\n", name.c_str()); + hash_registry.insert(make_pair(name, constructor)); +} + +Hash *Hash::New() +{ + // TODO: Make generic + return New("sha224"); +} + +Hash *Hash::New(const std::string& name) +{ + Hash *(*constructor)() = hash_registry[name]; + if (!constructor) + return NULL; + else + return constructor(); +} + +bool Hash::update_from_file(const char *filename) +{ + FILE *f = fopen(filename, "rb"); + if (f == NULL) + return false; + + while (!feof(f)) { + char buf[4096]; + size_t bytes = fread(buf, 1, sizeof(buf), f); + + if (ferror(f)) { + fclose(f); + return false; + } + + update(buf, bytes); + } + + fclose(f); + return true; +} + +const uint8_t *Hash::digest() +{ + if (!digest_bytes) { + digest_bytes = finalize(); + } + + return digest_bytes; +} + +string Hash::digest_str() +{ + const uint8_t *raw_digest = digest(); + size_t len = digest_size(); + char hexbuf[len*2 + 1]; + + hexbuf[0] = '\0'; + for (size_t i = 0; i < len; i++) { + snprintf(&hexbuf[2*i], 3, "%02x", raw_digest[i]); + } + + return name() + "=" + hexbuf; +} + +void sha1_register(); +void sha256_register(); + +void hash_init() +{ + sha1_register(); + sha256_register(); +} diff --git a/hash.h b/hash.h new file mode 100644 index 0000000..12d5b47 --- /dev/null +++ b/hash.h @@ -0,0 +1,65 @@ +/* Cumulus: Smart Filesystem Backup to Dumb Servers + * + * Copyright (C) 2012 Michael Vrable + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +/* A generic interface for computing digests of data, used for both + * content-based deduplication and for data integrity verification. */ + +#ifndef CUMULUS_HASH_H +#define CUMULUS_HASH_H 1 + +#include +#include + +/* An object-oriented wrapper around checksumming functionality. */ +class Hash { +public: + Hash() : digest_bytes(NULL) { } + virtual ~Hash() { } + + // + virtual void update(const void *data, size_t len) = 0; + // Returns the size of the buffer returned by digest, in bytes. + virtual size_t digest_size() const = 0; + // Returns the name of the hash algorithm. + virtual std::string name() const = 0; + + // Calls update with the contents of the data found in the specified file. + bool update_from_file(const char *filename); + // Finalizes the digest and returns a pointer to a raw byte array + // containing the hash. + const uint8_t *digest(); + + // Returns the digest in text form: "=". + std::string digest_str(); + + //typedef Hash *(*HashConstructor)(); + static void Register(const std::string& name, Hash *(*constructor)()); + static Hash *New(); + static Hash *New(const std::string& name); + +protected: + virtual const uint8_t *finalize() = 0; + +private: + const uint8_t *digest_bytes; +}; + +void hash_init(); + +#endif diff --git a/main.cc b/main.cc index 297e805..d25788d 100644 --- a/main.cc +++ b/main.cc @@ -49,6 +49,7 @@ #include #include "exclude.h" +#include "hash.h" #include "localdb.h" #include "metadata.h" #include "remote.h" @@ -244,7 +245,7 @@ int64_t dumpfile(int fd, dictionary &file_info, const string &path, /* If the file is new or changed, we must read in the contents a block at a * time. */ if (!cached) { - SHA1Checksum hash; + Hash *hash = Hash::New(); Subfile subfile(db); subfile.load_old_blocks(old_blocks); @@ -258,7 +259,7 @@ int64_t dumpfile(int fd, dictionary &file_info, const string &path, break; } - hash.process(block_buf, bytes); + hash->update(block_buf, bytes); // Sparse file processing: if we read a block of all zeroes, encode // that explicitly. @@ -343,7 +344,8 @@ int64_t dumpfile(int fd, dictionary &file_info, const string &path, status = "old"; } - file_info["checksum"] = hash.checksum_str(); + file_info["checksum"] = hash->digest_str(); + delete hash; } // Sanity check: if we are rebuilding the statcache, but the file looks @@ -683,6 +685,8 @@ void usage(const char *program) int main(int argc, char *argv[]) { + hash_init(); + string backup_dest = "", backup_script = ""; string localdb_dir = ""; string backup_scheme = ""; diff --git a/store.cc b/store.cc index 9d4c883..f87b671 100644 --- a/store.cc +++ b/store.cc @@ -41,6 +41,7 @@ #include #include +#include "hash.h" #include "store.h" #include "ref.h" #include "util.h" @@ -338,7 +339,8 @@ void LbsObject::checksum() { assert(written); - SHA1Checksum hash; - hash.process(data, data_len); - ref.set_checksum(hash.checksum_str()); + Hash *hash = Hash::New(); + hash->update(data, data_len); + ref.set_checksum(hash->digest_str()); + delete hash; } diff --git a/third_party/sha1.cc b/third_party/sha1.cc index 7111f94..32a6429 100644 --- a/third_party/sha1.cc +++ b/third_party/sha1.cc @@ -27,6 +27,7 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#include "../hash.h" #include "sha1.h" #include @@ -392,3 +393,40 @@ string SHA1Checksum::checksum_str() return result; } + +class SHA1Hash : public Hash { +public: + SHA1Hash(); + static Hash *New() { return new SHA1Hash; } + virtual void update(const void *data, size_t len); + virtual size_t digest_size() const { return 20; } + virtual std::string name() const { return "sha1"; } + +protected: + const uint8_t *finalize(); + +private: + struct sha1_ctx ctx; + char resbuf[20] __attribute__ ((__aligned__ (__alignof__ (md5_uint32)))); +}; + +SHA1Hash::SHA1Hash() +{ + sha1_init_ctx(&ctx); +} + +void SHA1Hash::update(const void *data, size_t len) +{ + sha1_process_bytes(data, len, &ctx); +} + +const uint8_t *SHA1Hash::finalize() +{ + sha1_finish_ctx(&ctx, resbuf); + return (const uint8_t *)resbuf; +} + +void sha1_register() +{ + Hash::Register("sha1", SHA1Hash::New); +} diff --git a/third_party/sha256.cc b/third_party/sha256.cc new file mode 100644 index 0000000..968b63b --- /dev/null +++ b/third_party/sha256.cc @@ -0,0 +1,439 @@ +/* sha256.cc - Message digests for Cumulus using SHA256 + * + * This code is adapted from crypto/sha256_generic.c and include/crypto/sha.n + * from the Linux kernel version 3.4. + */ + +/* SHA-256, as specified in + * http://csrc.nist.gov/groups/STM/cavp/documents/shs/sha256-384-512.pdf + * + * SHA-256 code by Jean-Luc Cooke . + * + * Copyright (c) Jean-Luc Cooke + * Copyright (c) Andrew McDonald + * Copyright (c) 2002 James Morris + * SHA224 Support Copyright 2007 Intel Corporation + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + */ + +#define _BSD_SOURCE +#include +#include +#include +#include +#include + +#include "../hash.h" + +/* Adaptation code for a non-kernel build environment. */ +typedef uint8_t u8; +typedef uint32_t u32; +typedef uint64_t u64; + +static inline u32 ror32(u32 x, int n) +{ + return (x >> n) | (x << (32 - n)); +} + +static inline u32 cpu_to_be32(u32 x) +{ + return htobe32(x); +} + +static inline u64 cpu_to_be64(u64 x) +{ + return htobe64(x); +} + +/* Originally from sha.h. */ + +#define SHA224_DIGEST_SIZE 28 +#define SHA224_BLOCK_SIZE 64 + +#define SHA256_DIGEST_SIZE 32 +#define SHA256_BLOCK_SIZE 64 + +#define SHA224_H0 0xc1059ed8UL +#define SHA224_H1 0x367cd507UL +#define SHA224_H2 0x3070dd17UL +#define SHA224_H3 0xf70e5939UL +#define SHA224_H4 0xffc00b31UL +#define SHA224_H5 0x68581511UL +#define SHA224_H6 0x64f98fa7UL +#define SHA224_H7 0xbefa4fa4UL + +#define SHA256_H0 0x6a09e667UL +#define SHA256_H1 0xbb67ae85UL +#define SHA256_H2 0x3c6ef372UL +#define SHA256_H3 0xa54ff53aUL +#define SHA256_H4 0x510e527fUL +#define SHA256_H5 0x9b05688cUL +#define SHA256_H6 0x1f83d9abUL +#define SHA256_H7 0x5be0cd19UL + +struct sha256_state { + u64 count; + u32 state[SHA256_DIGEST_SIZE / 4]; + u8 buf[SHA256_BLOCK_SIZE]; +}; + +/* Originally from sha256_generic.c */ + +static inline u32 Ch(u32 x, u32 y, u32 z) +{ + return z ^ (x & (y ^ z)); +} + +static inline u32 Maj(u32 x, u32 y, u32 z) +{ + return (x & y) | (z & (x | y)); +} + +#define e0(x) (ror32(x, 2) ^ ror32(x,13) ^ ror32(x,22)) +#define e1(x) (ror32(x, 6) ^ ror32(x,11) ^ ror32(x,25)) +#define s0(x) (ror32(x, 7) ^ ror32(x,18) ^ (x >> 3)) +#define s1(x) (ror32(x,17) ^ ror32(x,19) ^ (x >> 10)) + +static inline void LOAD_OP(int I, u32 *W, const u8 *input) +{ + W[I] = (input[4*I] << 24) | (input[4*I+1] << 16) + | (input[4*I+2] << 8) | input[4*I+3]; +} + +static inline void BLEND_OP(int I, u32 *W) +{ + W[I] = s1(W[I-2]) + W[I-7] + s0(W[I-15]) + W[I-16]; +} + +static void sha256_transform(u32 *state, const u8 *input) +{ + u32 a, b, c, d, e, f, g, h, t1, t2; + u32 W[64]; + int i; + + /* load the input */ + for (i = 0; i < 16; i++) + LOAD_OP(i, W, input); + + /* now blend */ + for (i = 16; i < 64; i++) + BLEND_OP(i, W); + + /* load the state into our registers */ + a=state[0]; b=state[1]; c=state[2]; d=state[3]; + e=state[4]; f=state[5]; g=state[6]; h=state[7]; + + /* now iterate */ + t1 = h + e1(e) + Ch(e,f,g) + 0x428a2f98 + W[ 0]; + t2 = e0(a) + Maj(a,b,c); d+=t1; h=t1+t2; + t1 = g + e1(d) + Ch(d,e,f) + 0x71374491 + W[ 1]; + t2 = e0(h) + Maj(h,a,b); c+=t1; g=t1+t2; + t1 = f + e1(c) + Ch(c,d,e) + 0xb5c0fbcf + W[ 2]; + t2 = e0(g) + Maj(g,h,a); b+=t1; f=t1+t2; + t1 = e + e1(b) + Ch(b,c,d) + 0xe9b5dba5 + W[ 3]; + t2 = e0(f) + Maj(f,g,h); a+=t1; e=t1+t2; + t1 = d + e1(a) + Ch(a,b,c) + 0x3956c25b + W[ 4]; + t2 = e0(e) + Maj(e,f,g); h+=t1; d=t1+t2; + t1 = c + e1(h) + Ch(h,a,b) + 0x59f111f1 + W[ 5]; + t2 = e0(d) + Maj(d,e,f); g+=t1; c=t1+t2; + t1 = b + e1(g) + Ch(g,h,a) + 0x923f82a4 + W[ 6]; + t2 = e0(c) + Maj(c,d,e); f+=t1; b=t1+t2; + t1 = a + e1(f) + Ch(f,g,h) + 0xab1c5ed5 + W[ 7]; + t2 = e0(b) + Maj(b,c,d); e+=t1; a=t1+t2; + + t1 = h + e1(e) + Ch(e,f,g) + 0xd807aa98 + W[ 8]; + t2 = e0(a) + Maj(a,b,c); d+=t1; h=t1+t2; + t1 = g + e1(d) + Ch(d,e,f) + 0x12835b01 + W[ 9]; + t2 = e0(h) + Maj(h,a,b); c+=t1; g=t1+t2; + t1 = f + e1(c) + Ch(c,d,e) + 0x243185be + W[10]; + t2 = e0(g) + Maj(g,h,a); b+=t1; f=t1+t2; + t1 = e + e1(b) + Ch(b,c,d) + 0x550c7dc3 + W[11]; + t2 = e0(f) + Maj(f,g,h); a+=t1; e=t1+t2; + t1 = d + e1(a) + Ch(a,b,c) + 0x72be5d74 + W[12]; + t2 = e0(e) + Maj(e,f,g); h+=t1; d=t1+t2; + t1 = c + e1(h) + Ch(h,a,b) + 0x80deb1fe + W[13]; + t2 = e0(d) + Maj(d,e,f); g+=t1; c=t1+t2; + t1 = b + e1(g) + Ch(g,h,a) + 0x9bdc06a7 + W[14]; + t2 = e0(c) + Maj(c,d,e); f+=t1; b=t1+t2; + t1 = a + e1(f) + Ch(f,g,h) + 0xc19bf174 + W[15]; + t2 = e0(b) + Maj(b,c,d); e+=t1; a=t1+t2; + + t1 = h + e1(e) + Ch(e,f,g) + 0xe49b69c1 + W[16]; + t2 = e0(a) + Maj(a,b,c); d+=t1; h=t1+t2; + t1 = g + e1(d) + Ch(d,e,f) + 0xefbe4786 + W[17]; + t2 = e0(h) + Maj(h,a,b); c+=t1; g=t1+t2; + t1 = f + e1(c) + Ch(c,d,e) + 0x0fc19dc6 + W[18]; + t2 = e0(g) + Maj(g,h,a); b+=t1; f=t1+t2; + t1 = e + e1(b) + Ch(b,c,d) + 0x240ca1cc + W[19]; + t2 = e0(f) + Maj(f,g,h); a+=t1; e=t1+t2; + t1 = d + e1(a) + Ch(a,b,c) + 0x2de92c6f + W[20]; + t2 = e0(e) + Maj(e,f,g); h+=t1; d=t1+t2; + t1 = c + e1(h) + Ch(h,a,b) + 0x4a7484aa + W[21]; + t2 = e0(d) + Maj(d,e,f); g+=t1; c=t1+t2; + t1 = b + e1(g) + Ch(g,h,a) + 0x5cb0a9dc + W[22]; + t2 = e0(c) + Maj(c,d,e); f+=t1; b=t1+t2; + t1 = a + e1(f) + Ch(f,g,h) + 0x76f988da + W[23]; + t2 = e0(b) + Maj(b,c,d); e+=t1; a=t1+t2; + + t1 = h + e1(e) + Ch(e,f,g) + 0x983e5152 + W[24]; + t2 = e0(a) + Maj(a,b,c); d+=t1; h=t1+t2; + t1 = g + e1(d) + Ch(d,e,f) + 0xa831c66d + W[25]; + t2 = e0(h) + Maj(h,a,b); c+=t1; g=t1+t2; + t1 = f + e1(c) + Ch(c,d,e) + 0xb00327c8 + W[26]; + t2 = e0(g) + Maj(g,h,a); b+=t1; f=t1+t2; + t1 = e + e1(b) + Ch(b,c,d) + 0xbf597fc7 + W[27]; + t2 = e0(f) + Maj(f,g,h); a+=t1; e=t1+t2; + t1 = d + e1(a) + Ch(a,b,c) + 0xc6e00bf3 + W[28]; + t2 = e0(e) + Maj(e,f,g); h+=t1; d=t1+t2; + t1 = c + e1(h) + Ch(h,a,b) + 0xd5a79147 + W[29]; + t2 = e0(d) + Maj(d,e,f); g+=t1; c=t1+t2; + t1 = b + e1(g) + Ch(g,h,a) + 0x06ca6351 + W[30]; + t2 = e0(c) + Maj(c,d,e); f+=t1; b=t1+t2; + t1 = a + e1(f) + Ch(f,g,h) + 0x14292967 + W[31]; + t2 = e0(b) + Maj(b,c,d); e+=t1; a=t1+t2; + + t1 = h + e1(e) + Ch(e,f,g) + 0x27b70a85 + W[32]; + t2 = e0(a) + Maj(a,b,c); d+=t1; h=t1+t2; + t1 = g + e1(d) + Ch(d,e,f) + 0x2e1b2138 + W[33]; + t2 = e0(h) + Maj(h,a,b); c+=t1; g=t1+t2; + t1 = f + e1(c) + Ch(c,d,e) + 0x4d2c6dfc + W[34]; + t2 = e0(g) + Maj(g,h,a); b+=t1; f=t1+t2; + t1 = e + e1(b) + Ch(b,c,d) + 0x53380d13 + W[35]; + t2 = e0(f) + Maj(f,g,h); a+=t1; e=t1+t2; + t1 = d + e1(a) + Ch(a,b,c) + 0x650a7354 + W[36]; + t2 = e0(e) + Maj(e,f,g); h+=t1; d=t1+t2; + t1 = c + e1(h) + Ch(h,a,b) + 0x766a0abb + W[37]; + t2 = e0(d) + Maj(d,e,f); g+=t1; c=t1+t2; + t1 = b + e1(g) + Ch(g,h,a) + 0x81c2c92e + W[38]; + t2 = e0(c) + Maj(c,d,e); f+=t1; b=t1+t2; + t1 = a + e1(f) + Ch(f,g,h) + 0x92722c85 + W[39]; + t2 = e0(b) + Maj(b,c,d); e+=t1; a=t1+t2; + + t1 = h + e1(e) + Ch(e,f,g) + 0xa2bfe8a1 + W[40]; + t2 = e0(a) + Maj(a,b,c); d+=t1; h=t1+t2; + t1 = g + e1(d) + Ch(d,e,f) + 0xa81a664b + W[41]; + t2 = e0(h) + Maj(h,a,b); c+=t1; g=t1+t2; + t1 = f + e1(c) + Ch(c,d,e) + 0xc24b8b70 + W[42]; + t2 = e0(g) + Maj(g,h,a); b+=t1; f=t1+t2; + t1 = e + e1(b) + Ch(b,c,d) + 0xc76c51a3 + W[43]; + t2 = e0(f) + Maj(f,g,h); a+=t1; e=t1+t2; + t1 = d + e1(a) + Ch(a,b,c) + 0xd192e819 + W[44]; + t2 = e0(e) + Maj(e,f,g); h+=t1; d=t1+t2; + t1 = c + e1(h) + Ch(h,a,b) + 0xd6990624 + W[45]; + t2 = e0(d) + Maj(d,e,f); g+=t1; c=t1+t2; + t1 = b + e1(g) + Ch(g,h,a) + 0xf40e3585 + W[46]; + t2 = e0(c) + Maj(c,d,e); f+=t1; b=t1+t2; + t1 = a + e1(f) + Ch(f,g,h) + 0x106aa070 + W[47]; + t2 = e0(b) + Maj(b,c,d); e+=t1; a=t1+t2; + + t1 = h + e1(e) + Ch(e,f,g) + 0x19a4c116 + W[48]; + t2 = e0(a) + Maj(a,b,c); d+=t1; h=t1+t2; + t1 = g + e1(d) + Ch(d,e,f) + 0x1e376c08 + W[49]; + t2 = e0(h) + Maj(h,a,b); c+=t1; g=t1+t2; + t1 = f + e1(c) + Ch(c,d,e) + 0x2748774c + W[50]; + t2 = e0(g) + Maj(g,h,a); b+=t1; f=t1+t2; + t1 = e + e1(b) + Ch(b,c,d) + 0x34b0bcb5 + W[51]; + t2 = e0(f) + Maj(f,g,h); a+=t1; e=t1+t2; + t1 = d + e1(a) + Ch(a,b,c) + 0x391c0cb3 + W[52]; + t2 = e0(e) + Maj(e,f,g); h+=t1; d=t1+t2; + t1 = c + e1(h) + Ch(h,a,b) + 0x4ed8aa4a + W[53]; + t2 = e0(d) + Maj(d,e,f); g+=t1; c=t1+t2; + t1 = b + e1(g) + Ch(g,h,a) + 0x5b9cca4f + W[54]; + t2 = e0(c) + Maj(c,d,e); f+=t1; b=t1+t2; + t1 = a + e1(f) + Ch(f,g,h) + 0x682e6ff3 + W[55]; + t2 = e0(b) + Maj(b,c,d); e+=t1; a=t1+t2; + + t1 = h + e1(e) + Ch(e,f,g) + 0x748f82ee + W[56]; + t2 = e0(a) + Maj(a,b,c); d+=t1; h=t1+t2; + t1 = g + e1(d) + Ch(d,e,f) + 0x78a5636f + W[57]; + t2 = e0(h) + Maj(h,a,b); c+=t1; g=t1+t2; + t1 = f + e1(c) + Ch(c,d,e) + 0x84c87814 + W[58]; + t2 = e0(g) + Maj(g,h,a); b+=t1; f=t1+t2; + t1 = e + e1(b) + Ch(b,c,d) + 0x8cc70208 + W[59]; + t2 = e0(f) + Maj(f,g,h); a+=t1; e=t1+t2; + t1 = d + e1(a) + Ch(a,b,c) + 0x90befffa + W[60]; + t2 = e0(e) + Maj(e,f,g); h+=t1; d=t1+t2; + t1 = c + e1(h) + Ch(h,a,b) + 0xa4506ceb + W[61]; + t2 = e0(d) + Maj(d,e,f); g+=t1; c=t1+t2; + t1 = b + e1(g) + Ch(g,h,a) + 0xbef9a3f7 + W[62]; + t2 = e0(c) + Maj(c,d,e); f+=t1; b=t1+t2; + t1 = a + e1(f) + Ch(f,g,h) + 0xc67178f2 + W[63]; + t2 = e0(b) + Maj(b,c,d); e+=t1; a=t1+t2; + + state[0] += a; state[1] += b; state[2] += c; state[3] += d; + state[4] += e; state[5] += f; state[6] += g; state[7] += h; +} + + +static int sha224_init(struct sha256_state *sctx) +{ + sctx->state[0] = SHA224_H0; + sctx->state[1] = SHA224_H1; + sctx->state[2] = SHA224_H2; + sctx->state[3] = SHA224_H3; + sctx->state[4] = SHA224_H4; + sctx->state[5] = SHA224_H5; + sctx->state[6] = SHA224_H6; + sctx->state[7] = SHA224_H7; + sctx->count = 0; + + return 0; +} + +static int sha256_init(struct sha256_state *sctx) +{ + sctx->state[0] = SHA256_H0; + sctx->state[1] = SHA256_H1; + sctx->state[2] = SHA256_H2; + sctx->state[3] = SHA256_H3; + sctx->state[4] = SHA256_H4; + sctx->state[5] = SHA256_H5; + sctx->state[6] = SHA256_H6; + sctx->state[7] = SHA256_H7; + sctx->count = 0; + + return 0; +} + +static int sha256_update(struct sha256_state *sctx, const u8 *data, + unsigned int len) +{ + unsigned int partial, done; + const u8 *src; + + partial = sctx->count & 0x3f; + sctx->count += len; + done = 0; + src = data; + + if ((partial + len) > 63) { + if (partial) { + done = -partial; + memcpy(sctx->buf + partial, data, done + 64); + src = sctx->buf; + } + + do { + sha256_transform(sctx->state, src); + done += 64; + src = data + done; + } while (done + 63 < len); + + partial = 0; + } + memcpy(sctx->buf + partial, src, len - done); + + return 0; +} + +static int sha256_final(struct sha256_state *sctx, u32 *dst) +{ + //__be64 bits; + u64 bits; + unsigned int index, pad_len; + int i; + static const u8 padding[64] = { 0x80, }; + + /* Save number of bits */ + bits = cpu_to_be64(sctx->count << 3); + + /* Pad out to 56 mod 64. */ + index = sctx->count & 0x3f; + pad_len = (index < 56) ? (56 - index) : ((64+56) - index); + sha256_update(sctx, padding, pad_len); + + /* Append length (before padding) */ + sha256_update(sctx, (const u8 *)&bits, sizeof(bits)); + + /* Store state in digest */ + for (i = 0; i < 8; i++) + dst[i] = cpu_to_be32(sctx->state[i]); + + return 0; +} + +static int sha224_final(struct sha256_state *sctx, u32 *hash) +{ + u32 D[SHA256_DIGEST_SIZE / 4]; + + sha256_final(sctx, D); + + memcpy(hash, D, SHA224_DIGEST_SIZE); + + return 0; +} + +class SHA256Hash : public Hash { +public: + SHA256Hash(); + static Hash *New() { return new SHA256Hash; } + virtual void update(const void *data, size_t len); + virtual size_t digest_size() const { return SHA256_DIGEST_SIZE; } + virtual std::string name() const { return "sha256"; } + +protected: + const uint8_t *finalize(); + +private: + struct sha256_state state; + u32 digest_buf[SHA256_DIGEST_SIZE / 4]; +}; + +SHA256Hash::SHA256Hash() +{ + sha256_init(&state); +} + +void SHA256Hash::update(const void *data, size_t len) +{ + sha256_update(&state, static_cast(data), len); +} + +const uint8_t *SHA256Hash::finalize() +{ + sha256_final(&state, digest_buf); + return reinterpret_cast(digest_buf); +} + +class SHA224Hash : public Hash { +public: + SHA224Hash(); + static Hash *New() { return new SHA224Hash; } + virtual void update(const void *data, size_t len); + virtual size_t digest_size() const { return SHA224_DIGEST_SIZE; } + virtual std::string name() const { return "sha224"; } + +protected: + const uint8_t *finalize(); + +private: + struct sha256_state state; + u32 digest_buf[SHA224_DIGEST_SIZE / 4]; +}; + +SHA224Hash::SHA224Hash() +{ + sha224_init(&state); +} + +void SHA224Hash::update(const void *data, size_t len) +{ + sha256_update(&state, static_cast(data), len); +} + +const uint8_t *SHA224Hash::finalize() +{ + sha224_final(&state, digest_buf); + return reinterpret_cast(digest_buf); +} + +void sha256_register() +{ + Hash::Register("sha224", SHA224Hash::New); + Hash::Register("sha256", SHA256Hash::New); +} -- 2.20.1