| 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 | #include <openssl/base.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <string.h> |
| 19 |
| 20 | #include <openssl/sha.h> |
| 21 |
| 22 | #include "../../internal.h" |
| 23 | #include "./params.h" |
| 24 | #include "./thash.h" |
| 25 |
| 26 |
| 27 | // Internal thash function used by F, H, and T_l (Section 11.2, pages 44-46) |
| 28 | static void slhdsa_thash(uint8_t output[BCM_SLHDSA_SHA2_128S_N], |
| 29 | const uint8_t *input, size_t input_blocks, |
| 30 | const uint8_t pk_seed[BCM_SLHDSA_SHA2_128S_N], |
| 31 | uint8_t addr[32]) { |
| 32 | SHA256_CTX sha256; |
| 33 | SHA256_Init(&sha256); |
| 34 |
| 35 | // Process pubseed with padding to full block. |
| 36 | static const uint8_t kZeros[64 - BCM_SLHDSA_SHA2_128S_N] = {0}; |
| 37 | SHA256_Update(&sha256, pk_seed, BCM_SLHDSA_SHA2_128S_N); |
| 38 | SHA256_Update(&sha256, kZeros, sizeof(kZeros)); |
| 39 | SHA256_Update(&sha256, addr, SLHDSA_SHA2_128S_SHA256_ADDR_BYTES); |
| 40 | SHA256_Update(&sha256, input, input_blocks * BCM_SLHDSA_SHA2_128S_N); |
| 41 |
| 42 | uint8_t hash[32]; |
| 43 | SHA256_Final(hash, &sha256); |
| 44 | OPENSSL_memcpy(output, hash, BCM_SLHDSA_SHA2_128S_N); |
| 45 | } |
| 46 |
| 47 | // Implements PRF_msg function (Section 4.1, page 11 and Section 11.2, pages |
| 48 | // 44-46) |
| 49 | void slhdsa_thash_prfmsg(uint8_t output[BCM_SLHDSA_SHA2_128S_N], |
| 50 | const uint8_t sk_prf[BCM_SLHDSA_SHA2_128S_N], |
| 51 | const uint8_t entropy[BCM_SLHDSA_SHA2_128S_N], |
| 52 | const uint8_t header[BCM_SLHDSA_M_PRIME_HEADER_LEN], |
| 53 | const uint8_t *ctx, size_t ctx_len, const uint8_t *msg, |
| 54 | size_t msg_len) { |
| 55 | // Compute HMAC-SHA256(sk_prf, entropy || header || ctx || msg). We inline |
| 56 | // HMAC to avoid an allocation. |
| 57 | uint8_t hmac_key[SHA256_CBLOCK]; |
| 58 | static_assert(BCM_SLHDSA_SHA2_128S_N <= SHA256_CBLOCK, |
| 59 | "HMAC key is larger than block size"); |
| 60 | OPENSSL_memcpy(hmac_key, sk_prf, BCM_SLHDSA_SHA2_128S_N); |
| 61 | for (size_t i = 0; i < BCM_SLHDSA_SHA2_128S_N; i++) { |
| 62 | hmac_key[i] ^= 0x36; |
| 63 | } |
| 64 | OPENSSL_memset(hmac_key + BCM_SLHDSA_SHA2_128S_N, 0x36, |
| 65 | sizeof(hmac_key) - BCM_SLHDSA_SHA2_128S_N); |
| 66 |
| 67 | SHA256_CTX sha_ctx; |
| 68 | SHA256_Init(&sha_ctx); |
| 69 | SHA256_Update(&sha_ctx, hmac_key, sizeof(hmac_key)); |
| 70 | SHA256_Update(&sha_ctx, entropy, BCM_SLHDSA_SHA2_128S_N); |
| 71 | if (header) { |
| 72 | SHA256_Update(&sha_ctx, header, BCM_SLHDSA_M_PRIME_HEADER_LEN); |
| 73 | } |
| 74 | SHA256_Update(&sha_ctx, ctx, ctx_len); |
| 75 | SHA256_Update(&sha_ctx, msg, msg_len); |
| 76 | uint8_t hash[SHA256_DIGEST_LENGTH]; |
| 77 | SHA256_Final(hash, &sha_ctx); |
| 78 |
| 79 | for (size_t i = 0; i < BCM_SLHDSA_SHA2_128S_N; i++) { |
| 80 | hmac_key[i] ^= 0x36 ^ 0x5c; |
| 81 | } |
| 82 | OPENSSL_memset(hmac_key + BCM_SLHDSA_SHA2_128S_N, 0x5c, |
| 83 | sizeof(hmac_key) - BCM_SLHDSA_SHA2_128S_N); |
| 84 |
| 85 | SHA256_Init(&sha_ctx); |
| 86 | SHA256_Update(&sha_ctx, hmac_key, sizeof(hmac_key)); |
| 87 | SHA256_Update(&sha_ctx, hash, sizeof(hash)); |
| 88 | SHA256_Final(hash, &sha_ctx); |
| 89 |
| 90 | // Truncate to BCM_SLHDSA_SHA2_128S_N bytes |
| 91 | OPENSSL_memcpy(output, hash, BCM_SLHDSA_SHA2_128S_N); |
| 92 | } |
| 93 |
| 94 | // Implements H_msg function (Section 4.1, page 11 and Section 11.2, pages |
| 95 | // 44-46) |
| 96 | void slhdsa_thash_hmsg(uint8_t output[SLHDSA_SHA2_128S_DIGEST_SIZE], |
| 97 | const uint8_t r[BCM_SLHDSA_SHA2_128S_N], |
| 98 | const uint8_t pk_seed[BCM_SLHDSA_SHA2_128S_N], |
| 99 | const uint8_t pk_root[BCM_SLHDSA_SHA2_128S_N], |
| 100 | const uint8_t header[BCM_SLHDSA_M_PRIME_HEADER_LEN], |
| 101 | const uint8_t *ctx, size_t ctx_len, const uint8_t *msg, |
| 102 | size_t msg_len) { |
| 103 | // MGF1-SHA-256(R || PK.seed || SHA-256(R || PK.seed || PK.root || header || |
| 104 | // ctx || M), m) input_buffer stores R || PK_SEED || SHA256(..) || 4-byte |
| 105 | // index |
| 106 | uint8_t input_buffer[2 * BCM_SLHDSA_SHA2_128S_N + 32 + 4] = {0}; |
| 107 | OPENSSL_memcpy(input_buffer, r, BCM_SLHDSA_SHA2_128S_N); |
| 108 | OPENSSL_memcpy(input_buffer + BCM_SLHDSA_SHA2_128S_N, pk_seed, |
| 109 | BCM_SLHDSA_SHA2_128S_N); |
| 110 |
| 111 | // Inner hash |
| 112 | SHA256_CTX sha_ctx; |
| 113 | SHA256_Init(&sha_ctx); |
| 114 | SHA256_Update(&sha_ctx, r, BCM_SLHDSA_SHA2_128S_N); |
| 115 | SHA256_Update(&sha_ctx, pk_seed, BCM_SLHDSA_SHA2_128S_N); |
| 116 | SHA256_Update(&sha_ctx, pk_root, BCM_SLHDSA_SHA2_128S_N); |
| 117 | if (header) { |
| 118 | SHA256_Update(&sha_ctx, header, BCM_SLHDSA_M_PRIME_HEADER_LEN); |
| 119 | } |
| 120 | SHA256_Update(&sha_ctx, ctx, ctx_len); |
| 121 | SHA256_Update(&sha_ctx, msg, msg_len); |
| 122 | // Write directly into the input buffer |
| 123 | SHA256_Final(input_buffer + 2 * BCM_SLHDSA_SHA2_128S_N, &sha_ctx); |
| 124 |
| 125 | // MGF1-SHA-256 |
| 126 | uint8_t hash[32]; |
| 127 | static_assert(SLHDSA_SHA2_128S_DIGEST_SIZE < sizeof(hash), |
| 128 | "More MGF1 iterations required"); |
| 129 | SHA256(input_buffer, sizeof(input_buffer), hash); |
| 130 | OPENSSL_memcpy(output, hash, SLHDSA_SHA2_128S_DIGEST_SIZE); |
| 131 | } |
| 132 |
| 133 | // Implements PRF function (Section 4.1, page 11 and Section 11.2, pages 44-46) |
| 134 | void slhdsa_thash_prf(uint8_t output[BCM_SLHDSA_SHA2_128S_N], |
| 135 | const uint8_t pk_seed[BCM_SLHDSA_SHA2_128S_N], |
| 136 | const uint8_t sk_seed[BCM_SLHDSA_SHA2_128S_N], |
| 137 | uint8_t addr[32]) { |
| 138 | slhdsa_thash(output, sk_seed, 1, pk_seed, addr); |
| 139 | } |
| 140 |
| 141 | // Implements T_l function for WOTS+ public key compression (Section 4.1, page |
| 142 | // 11 and Section 11.2, pages 44-46) |
| 143 | void slhdsa_thash_tl(uint8_t output[BCM_SLHDSA_SHA2_128S_N], |
| 144 | const uint8_t input[SLHDSA_SHA2_128S_WOTS_BYTES], |
| 145 | const uint8_t pk_seed[BCM_SLHDSA_SHA2_128S_N], |
| 146 | uint8_t addr[32]) { |
| 147 | slhdsa_thash(output, input, SLHDSA_SHA2_128S_WOTS_LEN, pk_seed, addr); |
| 148 | } |
| 149 |
| 150 | // Implements H function (Section 4.1, page 11 and Section 11.2, pages 44-46) |
| 151 | void slhdsa_thash_h(uint8_t output[BCM_SLHDSA_SHA2_128S_N], |
| 152 | const uint8_t input[2 * BCM_SLHDSA_SHA2_128S_N], |
| 153 | const uint8_t pk_seed[BCM_SLHDSA_SHA2_128S_N], |
| 154 | uint8_t addr[32]) { |
| 155 | slhdsa_thash(output, input, 2, pk_seed, addr); |
| 156 | } |
| 157 |
| 158 | // Implements F function (Section 4.1, page 11 and Section 11.2, pages 44-46) |
| 159 | void slhdsa_thash_f(uint8_t output[BCM_SLHDSA_SHA2_128S_N], |
| 160 | const uint8_t input[BCM_SLHDSA_SHA2_128S_N], |
| 161 | const uint8_t pk_seed[BCM_SLHDSA_SHA2_128S_N], |
| 162 | uint8_t addr[32]) { |
| 163 | slhdsa_thash(output, input, 1, pk_seed, addr); |
| 164 | } |
| 165 |
| 166 | // Implements T_k function for FORS public key compression (Section 4.1, page 11 |
| 167 | // and Section 11.2, pages 44-46) |
| 168 | void slhdsa_thash_tk( |
| 169 | uint8_t output[BCM_SLHDSA_SHA2_128S_N], |
| 170 | const uint8_t input[SLHDSA_SHA2_128S_FORS_TREES * BCM_SLHDSA_SHA2_128S_N], |
| 171 | const uint8_t pk_seed[BCM_SLHDSA_SHA2_128S_N], uint8_t addr[32]) { |
| 172 | slhdsa_thash(output, input, SLHDSA_SHA2_128S_FORS_TREES, pk_seed, addr); |
| 173 | } |
| 174 |