| 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 | #include <openssl/base.h> |
| 16 |
| 17 | #include <cstdint> |
| 18 | #include <string> |
| 19 | #include <vector> |
| 20 |
| 21 | #include <gtest/gtest.h> |
| 22 | #include <openssl/obj.h> |
| 23 | #include <openssl/slhdsa.h> |
| 24 |
| 25 | #include "../fipsmodule/slhdsa/params.h" |
| 26 | #include "../test/file_test.h" |
| 27 | #include "../test/test_util.h" |
| 28 |
| 29 | namespace { |
| 30 |
| 31 | TEST(SLHDSATest, KeyGeneration) { |
| 32 | const uint8_t seed[3 * BCM_SLHDSA_SHA2_128S_N] = {0}; |
| 33 | const uint8_t expected_pub[] = { |
| 34 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 35 | 0x00, 0x00, 0x00, 0x00, 0x00, 0xbe, 0x6b, 0xd7, 0xe8, 0xe1, 0x98, |
| 36 | 0xea, 0xf6, 0x2d, 0x57, 0x2f, 0x13, 0xfc, 0x79, 0xf2, 0x6f, |
| 37 | }; |
| 38 | const uint8_t expected_priv[] = { |
| 39 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 40 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 41 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 42 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 43 | 0x00, 0x00, 0x00, 0x00, 0xbe, 0x6b, 0xd7, 0xe8, 0xe1, 0x98, 0xea, |
| 44 | 0xf6, 0x2d, 0x57, 0x2f, 0x13, 0xfc, 0x79, 0xf2, 0x6f, |
| 45 | }; |
| 46 |
| 47 | uint8_t pub[SLHDSA_SHA2_128S_PUBLIC_KEY_BYTES]; |
| 48 | uint8_t priv[SLHDSA_SHA2_128S_PRIVATE_KEY_BYTES]; |
| 49 | BCM_slhdsa_sha2_128s_generate_key_from_seed(pub, priv, seed); |
| 50 | EXPECT_EQ(Bytes(pub), Bytes(expected_pub)); |
| 51 | EXPECT_EQ(Bytes(priv), Bytes(expected_priv)); |
| 52 |
| 53 | uint8_t pub2[SLHDSA_SHA2_128S_PUBLIC_KEY_BYTES]; |
| 54 | SLHDSA_SHA2_128S_public_from_private(pub2, priv); |
| 55 | EXPECT_EQ(Bytes(pub2), Bytes(expected_pub)); |
| 56 | } |
| 57 |
| 58 | TEST(SLHDSATest, BasicSignVerify) { |
| 59 | uint8_t pub[SLHDSA_SHA2_128S_PUBLIC_KEY_BYTES]; |
| 60 | uint8_t priv[SLHDSA_SHA2_128S_PRIVATE_KEY_BYTES]; |
| 61 | SLHDSA_SHA2_128S_generate_key(pub, priv); |
| 62 |
| 63 | uint8_t kMessage[] = {0x42}; |
| 64 | uint8_t kContext[256] = {0}; |
| 65 | uint8_t signature[SLHDSA_SHA2_128S_SIGNATURE_BYTES]; |
| 66 | ASSERT_TRUE(SLHDSA_SHA2_128S_sign(signature, priv, kMessage, sizeof(kMessage), |
| 67 | nullptr, 0)); |
| 68 | EXPECT_EQ(SLHDSA_SHA2_128S_verify(signature, sizeof(signature), pub, kMessage, |
| 69 | sizeof(kMessage), nullptr, 0), |
| 70 | 1); |
| 71 | EXPECT_EQ(SLHDSA_SHA2_128S_verify(signature, sizeof(signature), pub, kMessage, |
| 72 | sizeof(kMessage), kContext, 1), |
| 73 | 0); |
| 74 | EXPECT_EQ(SLHDSA_SHA2_128S_verify(signature, sizeof(signature), pub, kMessage, |
| 75 | sizeof(kMessage), kContext, 256), |
| 76 | 0); |
| 77 |
| 78 | ASSERT_TRUE(SLHDSA_SHA2_128S_sign(signature, priv, kMessage, sizeof(kMessage), |
| 79 | kContext, 1)); |
| 80 | EXPECT_EQ(SLHDSA_SHA2_128S_verify(signature, sizeof(signature), pub, kMessage, |
| 81 | sizeof(kMessage), kContext, 1), |
| 82 | 1); |
| 83 | } |
| 84 |
| 85 | TEST(SLHDSATest, BasicNonstandardPrehashSignVerify) { |
| 86 | uint8_t pub[SLHDSA_SHA2_128S_PUBLIC_KEY_BYTES]; |
| 87 | uint8_t priv[SLHDSA_SHA2_128S_PRIVATE_KEY_BYTES]; |
| 88 | SLHDSA_SHA2_128S_generate_key(pub, priv); |
| 89 |
| 90 | // Use a constant 48-byte value as the prehashed message |
| 91 | uint8_t kHashedMessage[48] = {0x42}; |
| 92 | uint8_t kContext[256] = {0}; |
| 93 | uint8_t signature[SLHDSA_SHA2_128S_SIGNATURE_BYTES]; |
| 94 | ASSERT_TRUE(SLHDSA_SHA2_128S_prehash_warning_nonstandard_sign( |
| 95 | signature, priv, kHashedMessage, sizeof(kHashedMessage), NID_sha384, |
| 96 | nullptr, 0)); |
| 97 | EXPECT_EQ(SLHDSA_SHA2_128S_prehash_warning_nonstandard_verify( |
| 98 | signature, sizeof(signature), pub, kHashedMessage, |
| 99 | sizeof(kHashedMessage), NID_sha384, nullptr, 0), |
| 100 | 1); |
| 101 | EXPECT_EQ(SLHDSA_SHA2_128S_prehash_warning_nonstandard_verify( |
| 102 | signature, sizeof(signature), pub, kHashedMessage, |
| 103 | sizeof(kHashedMessage), NID_sha384, kContext, 1), |
| 104 | 0); |
| 105 | EXPECT_EQ(SLHDSA_SHA2_128S_prehash_warning_nonstandard_verify( |
| 106 | signature, sizeof(signature), pub, kHashedMessage, |
| 107 | sizeof(kHashedMessage), NID_sha384, kContext, 256), |
| 108 | 0); |
| 109 |
| 110 | ASSERT_TRUE(SLHDSA_SHA2_128S_prehash_warning_nonstandard_sign( |
| 111 | signature, priv, kHashedMessage, sizeof(kHashedMessage), NID_sha384, |
| 112 | kContext, 1)); |
| 113 | EXPECT_EQ(SLHDSA_SHA2_128S_prehash_warning_nonstandard_verify( |
| 114 | signature, sizeof(signature), pub, kHashedMessage, |
| 115 | sizeof(kHashedMessage), NID_sha384, kContext, 1), |
| 116 | 1); |
| 117 |
| 118 | uint8_t kWrongLengthHash[16] = {0x42}; |
| 119 | EXPECT_FALSE(SLHDSA_SHA2_128S_prehash_warning_nonstandard_sign( |
| 120 | signature, priv, kWrongLengthHash, sizeof(kWrongLengthHash), NID_sha384, |
| 121 | nullptr, 0)); |
| 122 | } |
| 123 |
| 124 | static void NISTKeyGenerationFileTest(FileTest *t) { |
| 125 | std::vector<uint8_t> seed, expected_pub, expected_priv; |
| 126 | ASSERT_TRUE(t->GetBytes(&seed, "seed")); |
| 127 | ASSERT_EQ(seed.size(), 48u); |
| 128 | ASSERT_TRUE(t->GetBytes(&expected_pub, "pub")); |
| 129 | ASSERT_TRUE(t->GetBytes(&expected_priv, "priv")); |
| 130 |
| 131 | uint8_t pub[SLHDSA_SHA2_128S_PUBLIC_KEY_BYTES]; |
| 132 | uint8_t priv[SLHDSA_SHA2_128S_PRIVATE_KEY_BYTES]; |
| 133 | BCM_slhdsa_sha2_128s_generate_key_from_seed(pub, priv, seed.data()); |
| 134 |
| 135 | EXPECT_EQ(Bytes(pub), Bytes(expected_pub)); |
| 136 | EXPECT_EQ(Bytes(priv), Bytes(expected_priv)); |
| 137 | } |
| 138 |
| 139 | TEST(SLHDSATest, NISTKeyGeneration) { |
| 140 | FileTestGTest("crypto/slhdsa/slhdsa_keygen.txt", NISTKeyGenerationFileTest); |
| 141 | } |
| 142 |
| 143 | static void NISTSignatureGenerationFileTest(FileTest *t) { |
| 144 | std::vector<uint8_t> priv, entropy, msg, expected_sig; |
| 145 | ASSERT_TRUE(t->GetBytes(&priv, "priv")); |
| 146 | ASSERT_EQ(priv.size(), |
| 147 | static_cast<size_t>(SLHDSA_SHA2_128S_PRIVATE_KEY_BYTES)); |
| 148 | ASSERT_TRUE(t->GetBytes(&entropy, "entropy")); |
| 149 | ASSERT_EQ(entropy.size(), static_cast<size_t>(BCM_SLHDSA_SHA2_128S_N)); |
| 150 | ASSERT_TRUE(t->GetBytes(&msg, "msg")); |
| 151 | ASSERT_TRUE(t->GetBytes(&expected_sig, "sig")); |
| 152 |
| 153 | uint8_t sig[SLHDSA_SHA2_128S_SIGNATURE_BYTES]; |
| 154 | BCM_slhdsa_sha2_128s_sign_internal(sig, priv.data(), nullptr, nullptr, 0, |
| 155 | msg.data(), msg.size(), entropy.data()); |
| 156 |
| 157 | EXPECT_EQ(Bytes(sig), Bytes(expected_sig)); |
| 158 | } |
| 159 |
| 160 | TEST(SLHDSATest, NISTSignatureGeneration) { |
| 161 | FileTestGTest("crypto/slhdsa/slhdsa_siggen.txt", |
| 162 | NISTSignatureGenerationFileTest); |
| 163 | } |
| 164 |
| 165 | static void NISTSignatureVerificationFileTest(FileTest *t) { |
| 166 | std::vector<uint8_t> pub, msg, sig; |
| 167 | std::string valid; |
| 168 | ASSERT_TRUE(t->GetBytes(&pub, "pub")); |
| 169 | ASSERT_EQ(pub.size(), static_cast<size_t>(SLHDSA_SHA2_128S_PUBLIC_KEY_BYTES)); |
| 170 | ASSERT_TRUE(t->GetBytes(&msg, "msg")); |
| 171 | ASSERT_TRUE(t->GetBytes(&sig, "sig")); |
| 172 | ASSERT_TRUE(t->GetAttribute(&valid, "valid")); |
| 173 |
| 174 | int ok = bcm_success(BCM_slhdsa_sha2_128s_verify_internal( |
| 175 | sig.data(), sig.size(), pub.data(), nullptr, nullptr, 0, msg.data(), |
| 176 | msg.size())); |
| 177 | EXPECT_EQ(ok, valid == "true"); |
| 178 | } |
| 179 |
| 180 | TEST(SLHDSATest, NISTSignatureVerification) { |
| 181 | FileTestGTest("crypto/slhdsa/slhdsa_sigver.txt", |
| 182 | NISTSignatureVerificationFileTest); |
| 183 | } |
| 184 |
| 185 | static void NISTPrehashSignatureVerificationFileTest(FileTest *t) { |
| 186 | std::vector<uint8_t> pub, msg, sig, context; |
| 187 | ASSERT_TRUE(t->GetBytes(&pub, "pub")); |
| 188 | ASSERT_EQ(pub.size(), static_cast<size_t>(SLHDSA_SHA2_128S_PUBLIC_KEY_BYTES)); |
| 189 | ASSERT_TRUE(t->GetBytes(&msg, "msg")); |
| 190 | ASSERT_TRUE(t->GetBytes(&sig, "sig")); |
| 191 | ASSERT_EQ(sig.size(), size_t{SLHDSA_SHA2_128S_SIGNATURE_BYTES}); |
| 192 | ASSERT_TRUE(t->GetBytes(&context, "context")); |
| 193 |
| 194 | std::string hash_func; |
| 195 | ASSERT_TRUE(t->GetAttribute(&hash_func, "hash")); |
| 196 | int nid = 0; |
| 197 | bool nonstandard = false; |
| 198 | if (hash_func == "SHA-256") { |
| 199 | nid = NID_sha256; |
| 200 | } else if (hash_func == "SHA-384") { |
| 201 | nid = NID_sha384; |
| 202 | nonstandard = true; |
| 203 | } else { |
| 204 | abort(); |
| 205 | } |
| 206 |
| 207 | if (nonstandard) { |
| 208 | EXPECT_TRUE(SLHDSA_SHA2_128S_prehash_warning_nonstandard_verify( |
| 209 | sig.data(), sig.size(), pub.data(), msg.data(), msg.size(), nid, |
| 210 | context.data(), context.size())); |
| 211 | } else { |
| 212 | EXPECT_TRUE(SLHDSA_SHA2_128S_prehash_verify( |
| 213 | sig.data(), sig.size(), pub.data(), msg.data(), msg.size(), nid, |
| 214 | context.data(), context.size())); |
| 215 | } |
| 216 | } |
| 217 |
| 218 | TEST(SLHDSATest, NISTPrehashSignatureVerification) { |
| 219 | FileTestGTest("crypto/slhdsa/slhdsa_prehash.txt", |
| 220 | NISTPrehashSignatureVerificationFileTest); |
| 221 | } |
| 222 |
| 223 | TEST(SLHDSATest, Self) { boringssl_self_test_slhdsa(); } |
| 224 |
| 225 | TEST(SLHDSATest, NullptrArgumentsToCreate) { |
| 226 | // For FIPS reasons, this should fail rather than crash. |
| 227 | ASSERT_EQ(BCM_slhdsa_sha2_128s_generate_key_fips(nullptr, nullptr), |
| 228 | bcm_status::failure); |
| 229 | ASSERT_EQ(BCM_slhdsa_sha2_128s_generate_key_from_seed_fips(nullptr, nullptr, |
| 230 | nullptr), |
| 231 | bcm_status::failure); |
| 232 | } |
| 233 |
| 234 | } // namespace |
| 235 |