]> git.ozlabs.org Git - ccan/blob - ccan/crypto/shachain/shachain.c
crypto/shachain: a bit more common code.
[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 void 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                         break;
62         }
63
64         /* This can happen if you skip indices! */
65         assert(i < sizeof(chain->known) / sizeof(chain->known[0]));
66         chain->known[i].index = index;
67         chain->known[i].hash = *hash;
68         chain->num_valid = i+1;
69 }
70
71 bool shachain_get_hash(const struct shachain *chain,
72                        shachain_index_t index, struct sha256 *hash)
73 {
74         int i;
75
76         for (i = 0; i < chain->num_valid; i++) {
77                 /* If we can get from key to index only by resetting bits,
78                  * we can derive from it => index has no bits key doesn't. */
79                 if (!can_derive(chain->known[i].index, index))
80                         continue;
81
82                 derive(chain->known[i].index, index, &chain->known[i].hash,
83                        hash);
84                 return true;
85         }
86         return false;
87 }