xref: /aosp_15_r20/external/cronet/third_party/boringssl/src/crypto/spx/fors.c (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 /* Copyright (c) 2023, Google LLC
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include <openssl/base.h>
16 
17 #include <string.h>
18 
19 #include "./address.h"
20 #include "./fors.h"
21 #include "./params.h"
22 #include "./spx_util.h"
23 #include "./thash.h"
24 
spx_fors_sk_gen(uint8_t * fors_sk,uint32_t idx,const uint8_t sk_seed[SPX_N],const uint8_t pk_seed[SPX_N],uint8_t addr[32])25 void spx_fors_sk_gen(uint8_t *fors_sk, uint32_t idx,
26                      const uint8_t sk_seed[SPX_N], const uint8_t pk_seed[SPX_N],
27                      uint8_t addr[32]) {
28   uint8_t sk_addr[32];
29   memcpy(sk_addr, addr, sizeof(sk_addr));
30 
31   spx_set_type(sk_addr, SPX_ADDR_TYPE_FORSPRF);
32   spx_copy_keypair_addr(sk_addr, addr);
33   spx_set_tree_index(sk_addr, idx);
34   spx_thash_prf(fors_sk, pk_seed, sk_seed, sk_addr);
35 }
36 
spx_fors_treehash(uint8_t root_node[SPX_N],const uint8_t sk_seed[SPX_N],uint32_t i,uint32_t z,const uint8_t pk_seed[SPX_N],uint8_t addr[32])37 void spx_fors_treehash(uint8_t root_node[SPX_N], const uint8_t sk_seed[SPX_N],
38                        uint32_t i /*target node index*/,
39                        uint32_t z /*target node height*/,
40                        const uint8_t pk_seed[SPX_N], uint8_t addr[32]) {
41 
42   BSSL_CHECK(z <= SPX_FORS_HEIGHT);
43   BSSL_CHECK(i < (uint32_t)(SPX_FORS_TREES * (1 << (SPX_FORS_HEIGHT - z))));
44 
45   if (z == 0) {
46     uint8_t sk[SPX_N];
47     spx_set_tree_height(addr, 0);
48     spx_set_tree_index(addr, i);
49     spx_fors_sk_gen(sk, i, sk_seed, pk_seed, addr);
50     spx_thash_f(root_node, sk, pk_seed, addr);
51   } else {
52     // Stores left node and right node.
53     uint8_t nodes[2 * SPX_N];
54     spx_fors_treehash(nodes, sk_seed, 2 * i, z - 1, pk_seed, addr);
55     spx_fors_treehash(nodes + SPX_N, sk_seed, 2 * i + 1, z - 1, pk_seed, addr);
56     spx_set_tree_height(addr, z);
57     spx_set_tree_index(addr, i);
58     spx_thash_h(root_node, nodes, pk_seed, addr);
59   }
60 }
61 
spx_fors_sign(uint8_t * fors_sig,const uint8_t message[SPX_FORS_MSG_BYTES],const uint8_t sk_seed[SPX_N],const uint8_t pk_seed[SPX_N],uint8_t addr[32])62 void spx_fors_sign(uint8_t *fors_sig, const uint8_t message[SPX_FORS_MSG_BYTES],
63                    const uint8_t sk_seed[SPX_N], const uint8_t pk_seed[SPX_N],
64                    uint8_t addr[32]) {
65   uint32_t indices[SPX_FORS_TREES];
66 
67   // Derive FORS indices compatible with the NIST changes.
68   spx_base_b(indices, SPX_FORS_TREES, message, /*log2_b=*/SPX_FORS_HEIGHT);
69 
70   for (size_t i = 0; i < SPX_FORS_TREES; ++i) {
71     spx_set_tree_height(addr, 0);
72     // Write the FORS secret key element to the correct position.
73     spx_fors_sk_gen(fors_sig + i * SPX_N * (SPX_FORS_HEIGHT + 1),
74                     i * (1 << SPX_FORS_HEIGHT) + indices[i], sk_seed, pk_seed,
75                     addr);
76     for (size_t j = 0; j < SPX_FORS_HEIGHT; ++j) {
77       size_t s = (indices[i] / (1 << j)) ^ 1;
78       // Write the FORS auth path element to the correct position.
79       spx_fors_treehash(fors_sig + SPX_N * (i * (SPX_FORS_HEIGHT + 1) + j + 1),
80                         sk_seed, i * (1ULL << (SPX_FORS_HEIGHT - j)) + s, j,
81                         pk_seed, addr);
82     }
83   }
84 }
85 
spx_fors_pk_from_sig(uint8_t * fors_pk,const uint8_t fors_sig[SPX_FORS_BYTES],const uint8_t message[SPX_FORS_MSG_BYTES],const uint8_t pk_seed[SPX_N],uint8_t addr[32])86 void spx_fors_pk_from_sig(uint8_t *fors_pk,
87                           const uint8_t fors_sig[SPX_FORS_BYTES],
88                           const uint8_t message[SPX_FORS_MSG_BYTES],
89                           const uint8_t pk_seed[SPX_N], uint8_t addr[32]) {
90   uint32_t indices[SPX_FORS_TREES];
91   uint8_t tmp[2 * SPX_N];
92   uint8_t roots[SPX_FORS_TREES * SPX_N];
93 
94   // Derive FORS indices compatible with the NIST changes.
95   spx_base_b(indices, SPX_FORS_TREES, message, /*log2_b=*/SPX_FORS_HEIGHT);
96 
97   for (size_t i = 0; i < SPX_FORS_TREES; ++i) {
98     // Pointer to current sk and authentication path
99     const uint8_t *sk = fors_sig + i * SPX_N * (SPX_FORS_HEIGHT + 1);
100     const uint8_t *auth = fors_sig + i * SPX_N * (SPX_FORS_HEIGHT + 1) + SPX_N;
101     uint8_t nodes[2 * SPX_N];
102 
103     spx_set_tree_height(addr, 0);
104     spx_set_tree_index(addr, (i * (1 << SPX_FORS_HEIGHT)) + indices[i]);
105 
106     spx_thash_f(nodes, sk, pk_seed, addr);
107 
108     for (size_t j = 0; j < SPX_FORS_HEIGHT; ++j) {
109       spx_set_tree_height(addr, j + 1);
110 
111       // Even node
112       if (((indices[i] / (1 << j)) % 2) == 0) {
113         spx_set_tree_index(addr, spx_get_tree_index(addr) / 2);
114         memcpy(tmp, nodes, SPX_N);
115         memcpy(tmp + SPX_N, auth + j * SPX_N, SPX_N);
116         spx_thash_h(nodes + SPX_N, tmp, pk_seed, addr);
117       } else {
118         spx_set_tree_index(addr, (spx_get_tree_index(addr) - 1) / 2);
119         memcpy(tmp, auth + j * SPX_N, SPX_N);
120         memcpy(tmp + SPX_N, nodes, SPX_N);
121         spx_thash_h(nodes + SPX_N, tmp, pk_seed, addr);
122       }
123       memcpy(nodes, nodes + SPX_N, SPX_N);
124     }
125     memcpy(roots + i * SPX_N, nodes, SPX_N);
126   }
127 
128   uint8_t forspk_addr[32];
129   memcpy(forspk_addr, addr, sizeof(forspk_addr));
130   spx_set_type(forspk_addr, SPX_ADDR_TYPE_FORSPK);
131   spx_copy_keypair_addr(forspk_addr, addr);
132   spx_thash_tk(fors_pk, roots, pk_seed, forspk_addr);
133 }
134