_tth.c

filter: Statementstmt 62.5% 30 / 48 Branchbran 45.0% 9 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 57.3% 39 / 68 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« _crc32.c ↑ Summary _edonr.c »
1 /* tth.c - calculate TTH (Tiger Tree Hash) function.
2 *
3 * Copyright (c) 2007, Aleksey Kravchenko <rhash.admin@gmail.com>
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 * PERFORMANCE OF THIS SOFTWARE.
15 */
16  
17 #include <string.h>
18 #include "byte_order.h"
19 #include "tth.h"
20  
21 /**
22 * Initialize context before calculaing hash.
23 *
24 * @param ctx context to initialize
25 */
26 4 void rhash_tth_init(tth_ctx* ctx)
27 {
28 4 rhash_tiger_init(&ctx->tiger);
29 4 ctx->tiger.message[ ctx->tiger.length++ ] = 0x00;
30 4 ctx->block_count = 0;
31 4 }
32  
33 /**
34 * The core transformation.
35 *
36 * @param ctx algorithm state
37 */
38 4 static void rhash_tth_process_block(tth_ctx* ctx)
39 {
40 uint64_t it;
41 4 unsigned pos = 0;
42 unsigned char msg[24];
43  
44 4 ▶ for (it = 1; it & ctx->block_count; it <<= 1) {
Branch: 1 branch
BranchTrueFalseTotal
for (it = 1; it & ctx->block_count; it <<= 1) { 0 4 4
45 0 rhash_tiger_final(&ctx->tiger, msg);
46 0 rhash_tiger_init(&ctx->tiger);
47 0 ctx->tiger.message[ctx->tiger.length++] = 0x01;
48 0 rhash_tiger_update(&ctx->tiger, (unsigned char*)(ctx->stack + pos), 24);
49 /* note: we can cut this step, if the previous rhash_tiger_final saves directly to ctx->tiger.message+25; */
50 0 rhash_tiger_update(&ctx->tiger, msg, 24);
51 0 pos += 3;
52 }
53 4 rhash_tiger_final(&ctx->tiger, (unsigned char*)(ctx->stack + pos));
54 4 ctx->block_count++;
55 4 }
56  
57 /**
58 * Calculate message hash.
59 * Can be called repeatedly with chunks of the message to be hashed.
60 *
61 * @param ctx the algorithm context containing current hashing state
62 * @param msg message chunk
63 * @param size length of the message chunk
64 */
65 3 void rhash_tth_update(tth_ctx* ctx, const unsigned char* msg, size_t size)
66 {
67 3 size_t rest = 1025 - (size_t)ctx->tiger.length;
68 for (;;) {
69 3 ▶ if (size < rest) rest = size;
Branch: 1 branch
BranchTrueFalseTotal
if (size < rest) rest = size; 3 0 3
70 3 rhash_tiger_update(&ctx->tiger, msg, rest);
71 3 msg += rest;
72 3 size -= rest;
73 3 ▶ if (ctx->tiger.length < 1025) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->tiger.length < 1025) { 3 0 3
74 3 return;
75 }
76  
77 /* process block hash */
78 0 rhash_tth_process_block(ctx);
79  
80 /* init block hash */
81 0 rhash_tiger_init(&ctx->tiger);
82 0 ctx->tiger.message[ ctx->tiger.length++ ] = 0x00;
83 0 rest = 1024;
84 }
85 }
86  
87 /**
88 * Store calculated hash into the given array.
89 *
90 * @param ctx the algorithm context containing current hashing state
91 * @param result calculated hash in binary form
92 */
93 4 void rhash_tth_final(tth_ctx* ctx, unsigned char result[24])
94 {
95 4 uint64_t it = 1;
96 4 unsigned pos = 0;
97 unsigned char msg[24];
98 const unsigned char* last_message;
99  
100 /* process the bytes left in the context buffer */
101 4 ▶ if (ctx->tiger.length > 1 || ctx->block_count == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (ctx->tiger.length > 1 || ctx->block_count == 0) { 1 3 4
if (ctx->tiger.length > 1 || ctx->block_count == 0) { 1 0 1
102 4 rhash_tth_process_block(ctx);
103 }
104  
105 4 ▶ for (; it < ctx->block_count && (it & ctx->block_count) == 0; it <<= 1) pos += 3;
Branch: 2 branches
BranchTrueFalseTotal
for (; it < ctx->block_count && (it & ctx->block_count) == 0; it <<= 1) pos += 3; 0 4 4
for (; it < ctx->block_count && (it & ctx->block_count) == 0; it <<= 1) pos += 3; 0 0 0
106 4 last_message = (unsigned char*)(ctx->stack + pos);
107  
108 4 ▶ for (it <<= 1; it <= ctx->block_count; it <<= 1) {
Branch: 1 branch
BranchTrueFalseTotal
for (it <<= 1; it <= ctx->block_count; it <<= 1) { 0 4 4
109 /* merge TTH sums in the tree */
110 0 pos += 3;
111 0 ▶ if (it & ctx->block_count) {
Branch: 1 branch
BranchTrueFalseTotal
if (it & ctx->block_count) { 0 0 0
112 0 rhash_tiger_init(&ctx->tiger);
113 0 ctx->tiger.message[ ctx->tiger.length++ ] = 0x01;
114 0 rhash_tiger_update(&ctx->tiger, (unsigned char*)(ctx->stack + pos), 24);
115 0 rhash_tiger_update(&ctx->tiger, last_message, 24);
116  
117 0 rhash_tiger_final(&ctx->tiger, msg);
118 0 last_message = msg;
119 }
120 }
121  
122 /* save result hash */
123 4 memcpy(ctx->tiger.hash, last_message, tiger_hash_length);
124 4 ▶ if (result) memcpy(result, last_message, tiger_hash_length);
Branch: 1 branch
BranchTrueFalseTotal
if (result) memcpy(result, last_message, tiger_hash_length); 4 0 4
125 4 }
« _crc32.c ↑ Summary _edonr.c »