/src/boringssl/crypto/fipsmodule/slhdsa/address.h
Line | Count | Source |
1 | | // Copyright 2024 The BoringSSL Authors |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #ifndef OPENSSL_HEADER_CRYPTO_FIPSMODULE_SLHDSA_ADDRESS_H |
16 | | #define OPENSSL_HEADER_CRYPTO_FIPSMODULE_SLHDSA_ADDRESS_H |
17 | | |
18 | | #include <openssl/mem.h> |
19 | | |
20 | | #include "../../internal.h" |
21 | | #include "./params.h" |
22 | | |
23 | | |
24 | | BSSL_NAMESPACE_BEGIN |
25 | | |
26 | 0 | #define SLHDSA_ADDR_TYPE_WOTS 0 |
27 | 0 | #define SLHDSA_ADDR_TYPE_WOTSPK 1 |
28 | 0 | #define SLHDSA_ADDR_TYPE_HASHTREE 2 |
29 | 0 | #define SLHDSA_ADDR_TYPE_FORSTREE 3 |
30 | 0 | #define SLHDSA_ADDR_TYPE_FORSPK 4 |
31 | 0 | #define SLHDSA_ADDR_TYPE_WOTSPRF 5 |
32 | 0 | #define SLHDSA_ADDR_TYPE_FORSPRF 6 |
33 | | |
34 | | inline void slhdsa_set_chain_addr(const slh_dsa_config *config, |
35 | 0 | uint8_t addr[32], uint32_t chain) { |
36 | 0 | if (config->compressed_addresses) { |
37 | 0 | bssl::CRYPTO_store_u32_be(addr + SLHDSA_ADDR_COMP_OFFSET_CHAIN, chain); |
38 | 0 | } else { |
39 | 0 | bssl::CRYPTO_store_u32_be(addr + SLHDSA_ADDR_FULL_OFFSET_CHAIN, chain); |
40 | 0 | } |
41 | 0 | } |
42 | | |
43 | | inline void slhdsa_set_hash_addr(const slh_dsa_config *config, uint8_t addr[32], |
44 | 0 | uint32_t hash) { |
45 | 0 | if (config->compressed_addresses) { |
46 | 0 | bssl::CRYPTO_store_u32_be(addr + SLHDSA_ADDR_COMP_OFFSET_HASH, hash); |
47 | 0 | } else { |
48 | 0 | bssl::CRYPTO_store_u32_be(addr + SLHDSA_ADDR_FULL_OFFSET_HASH, hash); |
49 | 0 | } |
50 | 0 | } |
51 | | |
52 | | inline void slhdsa_set_keypair_addr(const slh_dsa_config *config, |
53 | 0 | uint8_t addr[32], uint32_t keypair) { |
54 | 0 | if (config->compressed_addresses) { |
55 | 0 | bssl::CRYPTO_store_u32_be(addr + SLHDSA_ADDR_COMP_OFFSET_KEYPAIR, keypair); |
56 | 0 | } else { |
57 | 0 | bssl::CRYPTO_store_u32_be(addr + SLHDSA_ADDR_FULL_OFFSET_KEYPAIR, keypair); |
58 | 0 | } |
59 | 0 | } |
60 | | |
61 | | inline void slhdsa_copy_keypair_addr(const slh_dsa_config *config, |
62 | 0 | uint8_t out[32], const uint8_t in[32]) { |
63 | 0 | if (config->compressed_addresses) { |
64 | 0 | bssl::OPENSSL_memcpy(out, in, SLHDSA_ADDR_COMP_OFFSET_TYPE); |
65 | 0 | bssl::OPENSSL_memcpy(out + SLHDSA_ADDR_COMP_OFFSET_KEYPAIR, |
66 | 0 | in + SLHDSA_ADDR_COMP_OFFSET_KEYPAIR, 4); |
67 | 0 | } else { |
68 | 0 | bssl::OPENSSL_memcpy(out, in, SLHDSA_ADDR_FULL_OFFSET_TYPE); |
69 | 0 | bssl::OPENSSL_memcpy(out + SLHDSA_ADDR_FULL_OFFSET_KEYPAIR, |
70 | 0 | in + SLHDSA_ADDR_FULL_OFFSET_KEYPAIR, 4); |
71 | 0 | } |
72 | 0 | } |
73 | | |
74 | | inline void slhdsa_set_layer_addr(const slh_dsa_config *config, |
75 | 0 | uint8_t addr[32], uint32_t layer) { |
76 | 0 | if (config->compressed_addresses) { |
77 | 0 | addr[SLHDSA_ADDR_COMP_OFFSET_LAYER] = (uint8_t)layer; |
78 | 0 | } else { |
79 | 0 | bssl::CRYPTO_store_u32_be(addr + SLHDSA_ADDR_FULL_OFFSET_LAYER, layer); |
80 | 0 | } |
81 | 0 | } |
82 | | |
83 | | inline void slhdsa_set_tree_addr(const slh_dsa_config *config, uint8_t addr[32], |
84 | 0 | uint64_t tree) { |
85 | 0 | if (config->compressed_addresses) { |
86 | 0 | bssl::CRYPTO_store_u64_be(addr + SLHDSA_ADDR_COMP_OFFSET_TREE, tree); |
87 | 0 | } else { |
88 | | // The tree address is 12 bytes in this configuration. Just zero the top |
89 | | // four bytes. |
90 | 0 | bssl::OPENSSL_memset(addr + SLHDSA_ADDR_FULL_OFFSET_TREE, 0, 4); |
91 | 0 | bssl::CRYPTO_store_u64_be(addr + SLHDSA_ADDR_FULL_OFFSET_TREE + 4, tree); |
92 | 0 | } |
93 | 0 | } |
94 | | |
95 | | inline void slhdsa_set_type(const slh_dsa_config *config, uint8_t addr[32], |
96 | 0 | uint32_t type) { |
97 | | // FIPS 205 relies on this setting parts of the address to 0, so we do it |
98 | | // here to avoid confusion. |
99 | | // |
100 | | // The behavior here is only correct for the SHA-2 instantiations. |
101 | 0 | if (config->compressed_addresses) { |
102 | 0 | bssl::OPENSSL_memset(addr + SLHDSA_ADDR_COMP_ZERO_START, 0, |
103 | 0 | SLHDSA_ADDR_COMP_ZERO_LEN); |
104 | 0 | addr[SLHDSA_ADDR_COMP_OFFSET_TYPE] = (uint8_t)type; |
105 | 0 | } else { |
106 | 0 | bssl::OPENSSL_memset(addr + SLHDSA_ADDR_FULL_ZERO_START, 0, |
107 | 0 | SLHDSA_ADDR_FULL_ZERO_LEN); |
108 | 0 | bssl::CRYPTO_store_u32_be(addr + SLHDSA_ADDR_FULL_OFFSET_TYPE, type); |
109 | 0 | } |
110 | 0 | } |
111 | | |
112 | | inline void slhdsa_set_tree_height(const slh_dsa_config *config, |
113 | 0 | uint8_t addr[32], uint32_t tree_height) { |
114 | 0 | if (config->compressed_addresses) { |
115 | 0 | bssl::CRYPTO_store_u32_be(addr + SLHDSA_ADDR_COMP_OFFSET_TREE_HEIGHT, |
116 | 0 | tree_height); |
117 | 0 | } else { |
118 | 0 | bssl::CRYPTO_store_u32_be(addr + SLHDSA_ADDR_FULL_OFFSET_TREE_HEIGHT, |
119 | 0 | tree_height); |
120 | 0 | } |
121 | 0 | } |
122 | | |
123 | | inline void slhdsa_set_tree_index(const slh_dsa_config *config, |
124 | 0 | uint8_t addr[32], uint32_t tree_index) { |
125 | 0 | if (config->compressed_addresses) { |
126 | 0 | bssl::CRYPTO_store_u32_be(addr + SLHDSA_ADDR_COMP_OFFSET_TREE_INDEX, |
127 | 0 | tree_index); |
128 | 0 | } else { |
129 | 0 | bssl::CRYPTO_store_u32_be(addr + SLHDSA_ADDR_FULL_OFFSET_TREE_INDEX, |
130 | 0 | tree_index); |
131 | 0 | } |
132 | 0 | } |
133 | | |
134 | | inline uint32_t slhdsa_get_tree_index(const slh_dsa_config *config, |
135 | 0 | uint8_t addr[32]) { |
136 | 0 | if (config->compressed_addresses) { |
137 | 0 | return bssl::CRYPTO_load_u32_be(addr + SLHDSA_ADDR_COMP_OFFSET_TREE_INDEX); |
138 | 0 | } |
139 | 0 | return bssl::CRYPTO_load_u32_be(addr + SLHDSA_ADDR_FULL_OFFSET_TREE_INDEX); |
140 | 0 | } |
141 | | |
142 | | BSSL_NAMESPACE_END |
143 | | |
144 | | #endif // OPENSSL_HEADER_CRYPTO_FIPSMODULE_SLHDSA_ADDRESS_H |