diff options
author | John Wiegley <johnw@newartisans.com> | 2023-11-22 16:47:21 -0800 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2024-08-05 08:35:56 -0700 |
commit | db0661dbb51e9082e47926c31e93bdc97b491bf9 (patch) | |
tree | e60f7dc32ebe9c24cb26eeb8c8438de7e891fe6b /src | |
parent | e6dae78c033ea970a459b1a0ccc2f1310d1bff96 (diff) | |
download | fork-ledger-db0661dbb51e9082e47926c31e93bdc97b491bf9.tar.gz fork-ledger-db0661dbb51e9082e47926c31e93bdc97b491bf9.tar.bz2 fork-ledger-db0661dbb51e9082e47926c31e93bdc97b491bf9.zip |
Add support for hash chaining to detect modifications in postings
The following details of a posting contribute to its hash:
fullname of account
string representation of amount
Each posting hashes contributes to the transaction hash, which is compromised
of:
previous transaction’s hash (as encountered in parsing order)
actual date
optional auxiliary date
optional code
payee
hashes of all postings
Note that this means that changes in the “code” or any of the comments
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/generate.cc | 2 | ||||
-rw-r--r-- | src/global.cc | 2 | ||||
-rw-r--r-- | src/item.h | 4 | ||||
-rw-r--r-- | src/journal.cc | 4 | ||||
-rw-r--r-- | src/journal.h | 4 | ||||
-rw-r--r-- | src/post.cc | 24 | ||||
-rw-r--r-- | src/post.h | 2 | ||||
-rw-r--r-- | src/precmd.cc | 2 | ||||
-rw-r--r-- | src/session.cc | 9 | ||||
-rw-r--r-- | src/session.h | 3 | ||||
-rw-r--r-- | src/sha512.cc | 479 | ||||
-rw-r--r-- | src/textual.cc | 65 | ||||
-rw-r--r-- | src/xact.cc | 15 | ||||
-rw-r--r-- | src/xact.h | 2 |
15 files changed, 592 insertions, 28 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0d6bc085..d996a5a1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -51,7 +51,8 @@ set(LEDGER_SOURCES times.cc error.cc utils.cc - wcwidth.cc) + wcwidth.cc + sha512.cc) if (HAVE_GPGME) list(APPEND LEDGER_SOURCES diff --git a/src/generate.cc b/src/generate.cc index 08713999..98986c67 100644 --- a/src/generate.cc +++ b/src/generate.cc @@ -365,7 +365,7 @@ void generate_posts_iterator::increment() parsing_context.get_current().journal = session.journal.get(); parsing_context.get_current().scope = &session; - if (session.journal->read(parsing_context) != 0) { + if (session.journal->read(parsing_context, false) != 0) { VERIFY(session.journal->xacts.back()->valid()); posts.reset(*session.journal->xacts.back()); post = *posts++; diff --git a/src/global.cc b/src/global.cc index d6f0e395..ad1001ef 100644 --- a/src/global.cc +++ b/src/global.cc @@ -116,7 +116,7 @@ void global_scope_t::parse_init(path init_file) parsing_context.get_current().journal = session().journal.get(); parsing_context.get_current().scope = &report(); - if (session().journal->read(parsing_context) > 0 || + if (session().journal->read(parsing_context, false) > 0 || session().journal->auto_xacts.size() > 0 || session().journal->period_xacts.size() > 0) { throw_(parse_error, _f("Transactions found in initialization file '%1%'") @@ -199,6 +199,10 @@ public: return _state; } + virtual string hash(string _nonce) const { + return ""; + } + virtual void define(const symbol_t::kind_t, const string&, expr_t::ptr_op_t); virtual expr_t::ptr_op_t lookup(const symbol_t::kind_t kind, diff --git a/src/journal.cc b/src/journal.cc index a6559e3d..b5ae3f1b 100644 --- a/src/journal.cc +++ b/src/journal.cc @@ -466,7 +466,7 @@ bool journal_t::remove_xact(xact_t * xact) return true; } -std::size_t journal_t::read(parse_context_stack_t& context) +std::size_t journal_t::read(parse_context_stack_t& context, bool store_hashes) { std::size_t count = 0; try { @@ -485,7 +485,7 @@ std::size_t journal_t::read(parse_context_stack_t& context) if (! current.master) current.master = master; - count = read_textual(context); + count = read_textual(context, store_hashes); if (count > 0) { if (! current.pathname.empty()) sources.push_back(fileinfo_t(current.pathname)); diff --git a/src/journal.h b/src/journal.h index 06bcbd4f..d68618bf 100644 --- a/src/journal.h +++ b/src/journal.h @@ -184,7 +184,7 @@ public: return period_xacts.end(); } - std::size_t read(parse_context_stack_t& context); + std::size_t read(parse_context_stack_t& context, bool store_hashes); bool has_xdata(); void clear_xdata(); @@ -193,7 +193,7 @@ public: private: - std::size_t read_textual(parse_context_stack_t& context); + std::size_t read_textual(parse_context_stack_t& context, bool store_hashes); bool should_check_payees(); bool payee_not_registered(const string& name); diff --git a/src/post.cc b/src/post.cc index e2063a36..9d799a9e 100644 --- a/src/post.cc +++ b/src/post.cc @@ -671,6 +671,30 @@ void post_t::set_reported_account(account_t * acct) acct->xdata().reported_posts.push_back(this); } +extern "C" unsigned char *SHA512( + void *data, unsigned int data_len, unsigned char *digest); + +namespace { + std::string bufferToHex(const unsigned char* buffer, std::size_t size) { + std::ostringstream oss; + oss << std::hex << std::setfill('0'); + for(std::size_t i = 0; i < size; ++i) + oss << std::setw(2) << static_cast<int>(buffer[i]); + return oss.str(); + } +} + +string post_t::hash(string nonce) const { + unsigned char data[128]; + std::ostringstream repr; + repr << nonce; + repr << account->fullname(); + repr << amount.to_string(); + string repr_str(repr.str()); + SHA512((void *)repr_str.c_str(), repr_str.length(), data); + return bufferToHex(data, 64 /*SHA512_DIGEST_LENGTH*/); +} + void extend_post(post_t& post, journal_t& journal) { commodity_t& comm(post.amount.commodity()); @@ -245,6 +245,8 @@ public: return const_cast<post_t *>(this)->reported_account(); } + virtual string hash(string nonce) const; + friend class xact_t; struct compare_by_date_and_sequence diff --git a/src/precmd.cc b/src/precmd.cc index 8ec25d71..25c28fc8 100644 --- a/src/precmd.cc +++ b/src/precmd.cc @@ -74,7 +74,7 @@ namespace { parsing_context.get_current().journal = report.session.journal.get(); parsing_context.get_current().scope = &report.session; - report.session.journal->read(parsing_context); + report.session.journal->read(parsing_context, false); report.session.journal->clear_xdata(); } } diff --git a/src/session.cc b/src/session.cc index 7cf1a666..6958603e 100644 --- a/src/session.cc +++ b/src/session.cc @@ -134,7 +134,7 @@ std::size_t session_t::read_data(const string& master_account) parsing_context.push(*price_db_path); parsing_context.get_current().journal = journal.get(); try { - if (journal->read(parsing_context) > 0) + if (journal->read(parsing_context, HANDLED(hashes)) > 0) throw_(parse_error, _("Transactions not allowed in price history file")); } catch (...) { @@ -169,7 +169,7 @@ std::size_t session_t::read_data(const string& master_account) parsing_context.get_current().journal = journal.get(); parsing_context.get_current().master = acct; try { - xact_count += journal->read(parsing_context); + xact_count += journal->read(parsing_context, HANDLED(hashes)); } catch (...) { parsing_context.pop(); @@ -230,7 +230,7 @@ journal_t * session_t::read_journal_from_string(const string& data) parsing_context.get_current().journal = journal.get(); parsing_context.get_current().master = journal->master; try { - journal->read(parsing_context); + journal->read(parsing_context, HANDLED(hashes)); } catch (...) { parsing_context.pop(); @@ -331,6 +331,9 @@ option_t<session_t> * session_t::lookup_option(const char * p) case 'f': OPT_(file_); // -f break; + case 'h': + OPT(hashes); + break; case 'i': OPT(input_date_format_); break; diff --git a/src/session.h b/src/session.h index c296f0fb..dcb554eb 100644 --- a/src/session.h +++ b/src/session.h @@ -112,6 +112,7 @@ public: HANDLER(decimal_comma).report(out); HANDLER(time_colon).report(out); HANDLER(file_).report(out); + HANDLER(hashes).report(out); HANDLER(input_date_format_).report(out); HANDLER(explicit).report(out); HANDLER(master_account_).report(out); @@ -162,6 +163,8 @@ public: data_files.push_back(str); }); + OPTION(session_t, hashes); + OPTION_(session_t, input_date_format_, DO_(str) { // This changes static variables inside times.h, which affects the // basic date parser. diff --git a/src/sha512.cc b/src/sha512.cc new file mode 100644 index 00000000..2573524c --- /dev/null +++ b/src/sha512.cc @@ -0,0 +1,479 @@ +/* + * FILE: sha2.c + * AUTHOR: Aaron D. Gifford - http://www.aarongifford.com/ + * + * Copyright (c) 2000-2001, Aaron D. Gifford + * All rights reserved. + * + * Modified by Jelte Jansen to fit in ldns, and not clash with any + * system-defined SHA code. + * Changes: + * - Renamed (external) functions and constants to fit ldns style + * - Removed _End and _Data functions + * - Added ldns_shaX(data, len, digest) convenience functions + * - Removed prototypes of _Transform functions and made those static + * Modified by Wouter, and trimmed, to provide SHA512 for getentropy_fallback. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $Id: sha2.c,v 1.1 2001/11/08 00:01:51 adg Exp adg $ + */ +#include <string.h> /* memcpy()/memset() or bcopy()/bzero() */ +#include <assert.h> /* assert() */ + +extern "C" { + +/* do we have sha512 header defs */ +#ifndef SHA512_DIGEST_LENGTH +#define SHA512_BLOCK_LENGTH 128 +#define SHA512_DIGEST_LENGTH 64 +#define SHA512_DIGEST_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1) +typedef struct _SHA512_CTX { + uint64_t state[8]; + uint64_t bitcount[2]; + uint8_t buffer[SHA512_BLOCK_LENGTH]; +} SHA512_CTX; +#endif /* do we have sha512 header defs */ + +void SHA512_Init(SHA512_CTX*); +void SHA512_Update(SHA512_CTX*, void*, size_t); +void SHA512_Final(uint8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*); +unsigned char *SHA512(void *data, unsigned int data_len, unsigned char *digest); + + +/*** SHA-256/384/512 Machine Architecture Definitions *****************/ +/* + * BYTE_ORDER NOTE: + * + * Please make sure that your system defines BYTE_ORDER. If your + * architecture is little-endian, make sure it also defines + * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are + * equivilent. + * + * If your system does not define the above, then you can do so by + * hand like this: + * + * #define LITTLE_ENDIAN 1234 + * #define BIG_ENDIAN 4321 + * + * And for little-endian machines, add: + * + * #define BYTE_ORDER LITTLE_ENDIAN + * + * Or for big-endian machines: + * + * #define BYTE_ORDER BIG_ENDIAN + * + * The FreeBSD machine this was written on defines BYTE_ORDER + * appropriately by including <sys/types.h> (which in turn includes + * <machine/endian.h> where the appropriate definitions are actually + * made). + */ +#if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN) +#error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN +#endif + +typedef uint8_t sha2_byte; /* Exactly 1 byte */ +typedef uint32_t sha2_word32; /* Exactly 4 bytes */ +#ifdef S_SPLINT_S +typedef unsigned long long sha2_word64; /* lint 8 bytes */ +#else +typedef uint64_t sha2_word64; /* Exactly 8 bytes */ +#endif + +/*** SHA-256/384/512 Various Length Definitions ***********************/ +#define SHA512_SHORT_BLOCK_LENGTH (SHA512_BLOCK_LENGTH - 16) + + +/*** ENDIAN REVERSAL MACROS *******************************************/ +#if BYTE_ORDER == LITTLE_ENDIAN +#define REVERSE32(w,x) { \ + sha2_word32 tmp = (w); \ + tmp = (tmp >> 16) | (tmp << 16); \ + (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \ +} +#ifndef S_SPLINT_S +#define REVERSE64(w,x) { \ + sha2_word64 tmp = (w); \ + tmp = (tmp >> 32) | (tmp << 32); \ + tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | \ + ((tmp & 0x00ff00ff00ff00ffULL) << 8); \ + (x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | \ + ((tmp & 0x0000ffff0000ffffULL) << 16); \ +} +#else /* splint */ +#define REVERSE64(w,x) /* splint */ +#endif /* splint */ +#endif /* BYTE_ORDER == LITTLE_ENDIAN */ + +/* + * Macro for incrementally adding the unsigned 64-bit integer n to the + * unsigned 128-bit integer (represented using a two-element array of + * 64-bit words): + */ +#define ADDINC128(w,n) { \ + (w)[0] += (sha2_word64)(n); \ + if ((w)[0] < (n)) { \ + (w)[1]++; \ + } \ +} +#ifdef S_SPLINT_S +#undef ADDINC128 +#define ADDINC128(w,n) /* splint */ +#endif + +/* + * Macros for copying blocks of memory and for zeroing out ranges + * of memory. Using these macros makes it easy to switch from + * using memset()/memcpy() and using bzero()/bcopy(). + * + * Please define either SHA2_USE_MEMSET_MEMCPY or define + * SHA2_USE_BZERO_BCOPY depending on which function set you + * choose to use: + */ +#if !defined(SHA2_USE_MEMSET_MEMCPY) && !defined(SHA2_USE_BZERO_BCOPY) +/* Default to memset()/memcpy() if no option is specified */ +#define SHA2_USE_MEMSET_MEMCPY 1 +#endif +#if defined(SHA2_USE_MEMSET_MEMCPY) && defined(SHA2_USE_BZERO_BCOPY) +/* Abort with an error if BOTH options are defined */ +#error Define either SHA2_USE_MEMSET_MEMCPY or SHA2_USE_BZERO_BCOPY, not both! +#endif + +#ifdef SHA2_USE_MEMSET_MEMCPY +#define MEMSET_BZERO(p,l) memset((p), 0, (l)) +#define MEMCPY_BCOPY(d,s,l) memcpy((d), (s), (l)) +#endif +#ifdef SHA2_USE_BZERO_BCOPY +#define MEMSET_BZERO(p,l) bzero((p), (l)) +#define MEMCPY_BCOPY(d,s,l) bcopy((s), (d), (l)) +#endif + + +/*** THE SIX LOGICAL FUNCTIONS ****************************************/ +/* + * Bit shifting and rotation (used by the six SHA-XYZ logical functions: + * + * NOTE: The naming of R and S appears backwards here (R is a SHIFT and + * S is a ROTATION) because the SHA-256/384/512 description document + * (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this + * same "backwards" definition. + */ +/* Shift-right (used in SHA-256, SHA-384, and SHA-512): */ +#define R(b,x) ((x) >> (b)) +/* 64-bit Rotate-right (used in SHA-384 and SHA-512): */ +#define S64(b,x) (((x) >> (b)) | ((x) << (64 - (b)))) + +/* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */ +#define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z))) +#define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) + +/* Four of six logical functions used in SHA-384 and SHA-512: */ +#define Sigma0_512(x) (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x))) +#define Sigma1_512(x) (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x))) +#define sigma0_512(x) (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7, (x))) +#define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ R( 6, (x))) + +/*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/ +/* Hash constant words K for SHA-384 and SHA-512: */ +static const sha2_word64 K512[80] = { + 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, + 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, + 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, + 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, + 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, + 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, + 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, + 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL, + 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, + 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, + 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, + 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, + 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, + 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, + 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, + 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, + 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, + 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, + 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, + 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL, + 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, + 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, + 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, + 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, + 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, + 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, + 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, + 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, + 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, + 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, + 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, + 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL, + 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, + 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, + 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, + 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, + 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, + 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL, + 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, + 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL +}; + +/* initial hash value H for SHA-512 */ +static const sha2_word64 sha512_initial_hash_value[8] = { + 0x6a09e667f3bcc908ULL, + 0xbb67ae8584caa73bULL, + 0x3c6ef372fe94f82bULL, + 0xa54ff53a5f1d36f1ULL, + 0x510e527fade682d1ULL, + 0x9b05688c2b3e6c1fULL, + 0x1f83d9abfb41bd6bULL, + 0x5be0cd19137e2179ULL +}; + +typedef union _ldns_sha2_buffer_union { + uint8_t* theChars; + uint64_t* theLongs; +} ldns_sha2_buffer_union; + +/*** SHA-512: *********************************************************/ +void SHA512_Init(SHA512_CTX* context) { + if (context == (SHA512_CTX*)0) { + return; + } + MEMCPY_BCOPY(context->state, sha512_initial_hash_value, SHA512_DIGEST_LENGTH); + MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH); + context->bitcount[0] = context->bitcount[1] = 0; +} + +static void SHA512_Transform(SHA512_CTX* context, + const sha2_word64* data) { + sha2_word64 a, b, c, d, e, f, g, h, s0, s1; + sha2_word64 T1, T2, *W512 = (sha2_word64*)context->buffer; + int j; + + /* initialize registers with the prev. intermediate value */ + a = context->state[0]; + b = context->state[1]; + c = context->state[2]; + d = context->state[3]; + e = context->state[4]; + f = context->state[5]; + g = context->state[6]; + h = context->state[7]; + + j = 0; + do { +#if BYTE_ORDER == LITTLE_ENDIAN + /* Convert TO host byte order */ + REVERSE64(*data++, W512[j]); + /* Apply the SHA-512 compression function to update a..h */ + T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j]; +#else /* BYTE_ORDER == LITTLE_ENDIAN */ + /* Apply the SHA-512 compression function to update a..h with copy */ + T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++); +#endif /* BYTE_ORDER == LITTLE_ENDIAN */ + T2 = Sigma0_512(a) + Maj(a, b, c); + h = g; + g = f; + f = e; + e = d + T1; + d = c; + c = b; + b = a; + a = T1 + T2; + + j++; + } while (j < 16); + + do { + /* Part of the message block expansion: */ + s0 = W512[(j+1)&0x0f]; + s0 = sigma0_512(s0); + s1 = W512[(j+14)&0x0f]; + s1 = sigma1_512(s1); + + /* Apply the SHA-512 compression function to update a..h */ + T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + + (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); + T2 = Sigma0_512(a) + Maj(a, b, c); + h = g; + g = f; + f = e; + e = d + T1; + d = c; + c = b; + b = a; + a = T1 + T2; + + j++; + } while (j < 80); + + /* Compute the current intermediate hash value */ + context->state[0] += a; + context->state[1] += b; + context->state[2] += c; + context->state[3] += d; + context->state[4] += e; + context->state[5] += f; + context->state[6] += g; + context->state[7] += h; + + /* Clean up */ + a = b = c = d = e = f = g = h = T1 = T2 = 0; +} + +void SHA512_Update(SHA512_CTX* context, void *datain, size_t len) { + size_t freespace, usedspace; + const sha2_byte* data = (const sha2_byte*)datain; + + if (len == 0) { + /* Calling with no data is valid - we do nothing */ + return; + } + + /* Sanity check: */ + assert(context != (SHA512_CTX*)0 && data != (sha2_byte*)0); + + usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH; + if (usedspace > 0) { + /* Calculate how much free space is available in the buffer */ + freespace = SHA512_BLOCK_LENGTH - usedspace; + + if (len >= freespace) { + /* Fill the buffer completely and process it */ + MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace); + ADDINC128(context->bitcount, freespace << 3); + len -= freespace; + data += freespace; + SHA512_Transform(context, (sha2_word64*)context->buffer); + } else { + /* The buffer is not yet full */ + MEMCPY_BCOPY(&context->buffer[usedspace], data, len); + ADDINC128(context->bitcount, len << 3); + /* Clean up: */ + usedspace = freespace = 0; + return; + } + } + while (len >= SHA512_BLOCK_LENGTH) { + /* Process as many complete blocks as we can */ + SHA512_Transform(context, (sha2_word64*)data); + ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3); + len -= SHA512_BLOCK_LENGTH; + data += SHA512_BLOCK_LENGTH; + } + if (len > 0) { + /* There's left-overs, so save 'em */ + MEMCPY_BCOPY(context->buffer, data, len); + ADDINC128(context->bitcount, len << 3); + } + /* Clean up: */ + usedspace = freespace = 0; +} + +static void SHA512_Last(SHA512_CTX* context) { + size_t usedspace; + ldns_sha2_buffer_union cast_var; + + usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH; +#if BYTE_ORDER == LITTLE_ENDIAN + /* Convert FROM host byte order */ + REVERSE64(context->bitcount[0],context->bitcount[0]); + REVERSE64(context->bitcount[1],context->bitcount[1]); +#endif + if (usedspace > 0) { + /* Begin padding with a 1 bit: */ + context->buffer[usedspace++] = 0x80; + + if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) { + /* Set-up for the last transform: */ + MEMSET_BZERO(&context->buffer[usedspace], SHA512_SHORT_BLOCK_LENGTH - usedspace); + } else { + if (usedspace < SHA512_BLOCK_LENGTH) { + MEMSET_BZERO(&context->buffer[usedspace], SHA512_BLOCK_LENGTH - usedspace); + } + /* Do second-to-last transform: */ + SHA512_Transform(context, (sha2_word64*)context->buffer); + + /* And set-up for the last transform: */ + MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH - 2); + } + } else { + /* Prepare for final transform: */ + MEMSET_BZERO(context->buffer, SHA512_SHORT_BLOCK_LENGTH); + + /* Begin padding with a 1 bit: */ + *context->buffer = 0x80; + } + /* Store the length of input data (in bits): */ + cast_var.theChars = context->buffer; + cast_var.theLongs[SHA512_SHORT_BLOCK_LENGTH / 8] = context->bitcount[1]; + cast_var.theLongs[SHA512_SHORT_BLOCK_LENGTH / 8 + 1] = context->bitcount[0]; + + /* final transform: */ + SHA512_Transform(context, (sha2_word64*)context->buffer); +} + +void SHA512_Final(sha2_byte digest[], SHA512_CTX* context) { + sha2_word64 *d = (sha2_word64*)digest; + + /* Sanity check: */ + assert(context != (SHA512_CTX*)0); + + /* If no digest buffer is passed, we don't bother doing this: */ + if (digest != (sha2_byte*)0) { + SHA512_Last(context); + + /* Save the hash data for output: */ +#if BYTE_ORDER == LITTLE_ENDIAN + { + /* Convert TO host byte order */ + int j; + for (j = 0; j < 8; j++) { + REVERSE64(context->state[j],context->state[j]); + *d++ = context->state[j]; + } + } +#else + MEMCPY_BCOPY(d, context->state, SHA512_DIGEST_LENGTH); +#endif + } + + /* Zero out state data */ + MEMSET_BZERO(context, sizeof(SHA512_CTX)); +} + +unsigned char * +SHA512(void *data, unsigned int data_len, unsigned char *digest) +{ + SHA512_CTX ctx; + SHA512_Init(&ctx); + SHA512_Update(&ctx, data, data_len); + SHA512_Final(digest, &ctx); + return digest; +} + +} diff --git a/src/textual.cc b/src/textual.cc index 2da123a7..9ad62602 100644 --- a/src/textual.cc +++ b/src/textual.cc @@ -79,6 +79,7 @@ namespace { instance_t * parent; std::list<application_t> apply_stack; bool no_assertions; + bool store_hashes; #if TIMELOG_SUPPORT time_log_t timelog; #endif @@ -86,10 +87,12 @@ namespace { instance_t(parse_context_stack_t& _context_stack, parse_context_t& _context, instance_t * _parent = NULL, - const bool _no_assertions = false) + const bool _no_assertions = false, + const bool _store_hashes = false) : context_stack(_context_stack), context(_context), in(*context.stream.get()), parent(_parent), - no_assertions(_no_assertions), timelog(context) {} + no_assertions(_no_assertions), store_hashes(_store_hashes), + timelog(context) {} virtual string description() { return _("textual parser"); @@ -136,7 +139,7 @@ namespace { } #endif - void read_next_directive(bool& error_flag); + xact_t * read_next_directive(bool& error_flag, xact_t * previous_xact); #if TIMELOG_SUPPORT void clock_in_directive(char * line, bool capitalized); @@ -176,7 +179,8 @@ namespace { void apply_year_directive(char * line); void end_apply_directive(char * line); - void xact_directive(char * line, std::streamsize len); + xact_t * xact_directive(char * line, std::streamsize len, + xact_t * previous_xact); void period_xact_directive(char * line); void automated_xact_directive(char * line); void price_xact_directive(char * line); @@ -207,7 +211,8 @@ namespace { xact_t * parse_xact(char * line, std::streamsize len, - account_t * account); + account_t * account, + xact_t * previous_xact); virtual expr_t::ptr_op_t lookup(const symbol_t::kind_t kind, const string& name); @@ -247,10 +252,13 @@ void instance_t::parse() context.curr_pos = in.tellg(); bool error_flag = false; + xact_t * previous_xact = NULL; while (in.good() && ! in.eof()) { try { - read_next_directive(error_flag); + if (xact_t * xact = read_next_directive(error_flag, previous_xact)) { + previous_xact = xact; + } } catch (const std::exception& err) { error_flag = true; @@ -348,12 +356,12 @@ std::streamsize instance_t::read_line(char *& line) return 0; } -void instance_t::read_next_directive(bool& error_flag) +xact_t * instance_t::read_next_directive(bool& error_flag, xact_t * previous_xact) { char * line; std::streamsize len = read_line(line); if (len == 0 || line == NULL) - return; + return NULL; if (! std::isspace(line[0])) error_flag = false; @@ -389,8 +397,7 @@ void instance_t::read_next_directive(bool& error_flag) case '7': case '8': case '9': - xact_directive(line, len); - break; + return xact_directive(line, len, previous_xact); case '=': // automated xact automated_xact_directive(line); break; @@ -449,6 +456,8 @@ void instance_t::read_next_directive(bool& error_flag) } break; } + + return NULL; } #if TIMELOG_SUPPORT @@ -711,16 +720,17 @@ void instance_t::period_xact_directive(char * line) } } -void instance_t::xact_directive(char * line, std::streamsize len) +xact_t * instance_t::xact_directive(char * line, std::streamsize len, + xact_t * previous_xact) { TRACE_START(xacts, 1, "Time spent handling transactions:"); - if (xact_t * xact = parse_xact(line, len, top_account())) { + if (xact_t * xact = parse_xact(line, len, top_account(), previous_xact)) { unique_ptr<xact_t> manager(xact); if (context.journal->add_xact(xact)) { - manager.release(); // it's owned by the journal now context.count++; + return manager.release(); // it's owned by the journal now } // It's perfectly valid for the journal to reject the xact, which it // will do if the xact has no substantive effect (for example, a @@ -730,6 +740,8 @@ void instance_t::xact_directive(char * line, std::streamsize len) } TRACE_STOP(xacts, 1); + + return NULL; } void instance_t::include_directive(char * line) @@ -793,7 +805,7 @@ void instance_t::include_directive(char * line) context_stack.get_current().scope = scope; try { instance_t instance(context_stack, context_stack.get_current(), - this, no_assertions); + this, no_assertions, store_hashes); instance.apply_stack.push_front(application_t("account", master)); instance.parse(); } @@ -1818,7 +1830,8 @@ bool instance_t::parse_posts(account_t * account, xact_t * instance_t::parse_xact(char * line, std::streamsize len, - account_t * account) + account_t * account, + xact_t * previous_xact) { TRACE_START(xact_text, 1, "Time spent parsing transaction text:"); @@ -2007,6 +2020,22 @@ xact_t * instance_t::parse_xact(char * line, TRACE_STOP(xact_details, 1); + if (store_hashes) { + string expected_hash = + xact->hash(previous_xact && + previous_xact->has_tag("Hash") ? + previous_xact->get_tag("Hash")->to_string() : ""); + if (xact->has_tag("Hash")) { + string current_hash = xact->get_tag("Hash")->to_string(); + if (expected_hash != current_hash) { + throw_(parse_error, _f("Expected hash %1% != %2%") % + expected_hash % current_hash); + } + } else { + xact->set_tag("Hash", string_value(expected_hash)); + } + } + return xact.release(); } @@ -2027,12 +2056,14 @@ expr_t::ptr_op_t instance_t::lookup(const symbol_t::kind_t kind, return context.scope->lookup(kind, name); } -std::size_t journal_t::read_textual(parse_context_stack_t& context_stack) +std::size_t journal_t::read_textual(parse_context_stack_t& context_stack, + bool store_hashes) { TRACE_START(parsing_total, 1, "Total time spent parsing text:"); { instance_t instance(context_stack, context_stack.get_current(), NULL, - checking_style == journal_t::CHECK_PERMISSIVE); + checking_style == journal_t::CHECK_PERMISSIVE, + store_hashes); instance.apply_stack.push_front (application_t("account", context_stack.get_current().master)); instance.parse(); diff --git a/src/xact.cc b/src/xact.cc index 55e816cb..0b9bd9cd 100644 --- a/src/xact.cc +++ b/src/xact.cc @@ -581,6 +581,21 @@ bool xact_t::valid() const return true; } +string xact_t::hash(string nonce) const { + std::ostringstream repr; + repr << nonce; + repr << date(); + repr << aux_date(); + repr << code; + repr << payee; + string hash(repr.str()); + posts_list all_posts(posts.begin(), posts.end()); + foreach (post_t * post, all_posts) { + hash = post->hash(hash); + } + return hash; +} + namespace { bool post_pred(expr_t::ptr_op_t op, post_t& post) { @@ -127,6 +127,8 @@ public: const string& name); virtual bool valid() const; + + virtual string hash(string nonce) const; }; class auto_xact_t : public xact_base_t |