]> git.ozlabs.org Git - ccan/blob - ccan/crypto/shachain/shachain.c
crypto/shachain: detect if we're inserting a bogus hash.
[ccan] / ccan / crypto / shachain / shachain.c
1 /* MIT (BSD) license - see LICENSE file for details */
2 #include <ccan/crypto/shachain/shachain.h>
3 #include <ccan/ilog/ilog.h>
4 #include <limits.h>
5 #include <string.h>
6 #include <assert.h>
7
8 static void change_bit(unsigned char *arr, size_t index)
9 {
10         arr[index / CHAR_BIT] ^= (1 << (index % CHAR_BIT));
11 }
12
13 /* We can only ever *unset* bits, so to must only have bits in from. */
14 static bool can_derive(shachain_index_t from, shachain_index_t to)
15 {
16         return (~from & to) == 0;
17 }
18
19 static void derive(shachain_index_t from, shachain_index_t to,
20                    const struct sha256 *from_hash,
21                    struct sha256 *hash)
22 {
23         shachain_index_t branches;
24         int i;
25
26         assert(can_derive(from, to));
27
28         /* We start with the first hash. */
29         *hash = *from_hash;
30
31         /* This represents the bits set in from, and not to. */
32         branches = from ^ to;
33         for (i = ilog64(branches) - 1; i >= 0; i--) {
34                 if (((branches >> i) & 1)) {
35                         change_bit(hash->u.u8, i);
36                         sha256(hash, hash, 1);
37                 }
38         }
39 }
40
41 void shachain_from_seed(const struct sha256 *seed, shachain_index_t index,
42                         struct sha256 *hash)
43 {
44         derive((shachain_index_t)-1ULL, index, seed, hash);
45 }
46
47 void shachain_init(struct shachain *shachain)
48 {
49         shachain->num_valid = 0;
50 }
51
52 bool shachain_add_hash(struct shachain *chain,
53                        shachain_index_t index, const struct sha256 *hash)
54 {
55         int i;
56
57         for (i = 0; i < chain->num_valid; i++) {
58                 /* If we could derive this value, we don't need it,
59                  * not any others (since they're in order). */
60                 if (can_derive(index, chain->known[i].index)) {
61                         struct sha256 expect;
62
63                         /* Make sure the others derive as expected! */
64                         derive(index, chain->known[i].index, hash, &expect);
65                         if (memcmp(&expect, &chain->known[i].hash,
66                                    sizeof(expect)) != 0)
67                                 return false;
68                         break;
69                 }
70         }
71
72         /* This can happen if you skip indices! */
73         assert(i < sizeof(chain->known) / sizeof(chain->known[0]));
74         chain->known[i].index = index;
75         chain->known[i].hash = *hash;
76         chain->num_valid = i+1;
77         return true;
78 }
79
80 bool shachain_get_hash(const struct shachain *chain,
81                        shachain_index_t index, struct sha256 *hash)
82 {
83         int i;
84
85         for (i = 0; i < chain->num_valid; i++) {
86                 /* If we can get from key to index only by resetting bits,
87                  * we can derive from it => index has no bits key doesn't. */
88                 if (!can_derive(chain->known[i].index, index))
89                         continue;
90
91                 derive(chain->known[i].index, index, &chain->known[i].hash,
92                        hash);
93                 return true;
94         }
95         return false;
96 }