|
| 1 | +#include "common.h" |
| 2 | +#include <mls_vectors/mls_vectors.h> |
| 3 | + |
| 4 | +namespace mls_vectors { |
| 5 | + |
| 6 | +using namespace mls; |
| 7 | + |
| 8 | +CryptoBasicsTestVector::RefHash::RefHash(CipherSuite suite, |
| 9 | + PseudoRandom::Generator&& prg) |
| 10 | + : label("RefHash") |
| 11 | + , value(prg.secret("value")) |
| 12 | + , out(suite.raw_ref(from_ascii(label), value)) |
| 13 | +{ |
| 14 | +} |
| 15 | + |
| 16 | +std::optional<std::string> |
| 17 | +CryptoBasicsTestVector::RefHash::verify(CipherSuite suite) const |
| 18 | +{ |
| 19 | + VERIFY_EQUAL("ref hash", out, suite.raw_ref(from_ascii(label), value)); |
| 20 | + return std::nullopt; |
| 21 | +} |
| 22 | + |
| 23 | +CryptoBasicsTestVector::ExpandWithLabel::ExpandWithLabel( |
| 24 | + CipherSuite suite, |
| 25 | + PseudoRandom::Generator&& prg) |
| 26 | + : secret(prg.secret("secret")) |
| 27 | + , label("ExpandWithLabel") |
| 28 | + , context(prg.secret("context")) |
| 29 | + , length(static_cast<uint16_t>(prg.output_length())) |
| 30 | + , out(suite.expand_with_label(secret, label, context, length)) |
| 31 | +{ |
| 32 | +} |
| 33 | + |
| 34 | +std::optional<std::string> |
| 35 | +CryptoBasicsTestVector::ExpandWithLabel::verify(CipherSuite suite) const |
| 36 | +{ |
| 37 | + VERIFY_EQUAL("expand with label", |
| 38 | + out, |
| 39 | + suite.expand_with_label(secret, label, context, length)); |
| 40 | + return std::nullopt; |
| 41 | +} |
| 42 | + |
| 43 | +CryptoBasicsTestVector::DeriveSecret::DeriveSecret( |
| 44 | + CipherSuite suite, |
| 45 | + PseudoRandom::Generator&& prg) |
| 46 | + : secret(prg.secret("secret")) |
| 47 | + , label("DeriveSecret") |
| 48 | + , out(suite.derive_secret(secret, label)) |
| 49 | +{ |
| 50 | +} |
| 51 | + |
| 52 | +std::optional<std::string> |
| 53 | +CryptoBasicsTestVector::DeriveSecret::verify(CipherSuite suite) const |
| 54 | +{ |
| 55 | + VERIFY_EQUAL("derive secret", out, suite.derive_secret(secret, label)); |
| 56 | + return std::nullopt; |
| 57 | +} |
| 58 | + |
| 59 | +CryptoBasicsTestVector::DeriveTreeSecret::DeriveTreeSecret( |
| 60 | + CipherSuite suite, |
| 61 | + PseudoRandom::Generator&& prg) |
| 62 | + : secret(prg.secret("secret")) |
| 63 | + , label("DeriveTreeSecret") |
| 64 | + , generation(prg.uint32("generation")) |
| 65 | + , length(static_cast<uint16_t>(prg.output_length())) |
| 66 | + , out(suite.derive_tree_secret(secret, label, generation, length)) |
| 67 | +{ |
| 68 | +} |
| 69 | + |
| 70 | +std::optional<std::string> |
| 71 | +CryptoBasicsTestVector::DeriveTreeSecret::verify(CipherSuite suite) const |
| 72 | +{ |
| 73 | + VERIFY_EQUAL("derive tree secret", |
| 74 | + out, |
| 75 | + suite.derive_tree_secret(secret, label, generation, length)); |
| 76 | + return std::nullopt; |
| 77 | +} |
| 78 | + |
| 79 | +CryptoBasicsTestVector::SignWithLabel::SignWithLabel( |
| 80 | + CipherSuite suite, |
| 81 | + PseudoRandom::Generator&& prg) |
| 82 | + : priv(prg.signature_key("priv")) |
| 83 | + , pub(priv.public_key) |
| 84 | + , content(prg.secret("content")) |
| 85 | + , label("SignWithLabel") |
| 86 | + , signature(priv.sign(suite, label, content)) |
| 87 | +{ |
| 88 | +} |
| 89 | + |
| 90 | +std::optional<std::string> |
| 91 | +CryptoBasicsTestVector::SignWithLabel::verify(CipherSuite suite) const |
| 92 | +{ |
| 93 | + VERIFY("verify with label", pub.verify(suite, label, content, signature)); |
| 94 | + |
| 95 | + auto new_signature = priv.sign(suite, label, content); |
| 96 | + VERIFY("sign with label", pub.verify(suite, label, content, new_signature)); |
| 97 | + |
| 98 | + return std::nullopt; |
| 99 | +} |
| 100 | + |
| 101 | +CryptoBasicsTestVector::EncryptWithLabel::EncryptWithLabel( |
| 102 | + CipherSuite suite, |
| 103 | + PseudoRandom::Generator&& prg) |
| 104 | + : priv(prg.hpke_key("priv")) |
| 105 | + , pub(priv.public_key) |
| 106 | + , label("EncryptWithLabel") |
| 107 | + , context(prg.secret("context")) |
| 108 | + , plaintext(prg.secret("plaintext")) |
| 109 | +{ |
| 110 | + auto ct = pub.encrypt(suite, label, context, plaintext); |
| 111 | + kem_output = ct.kem_output; |
| 112 | + ciphertext = ct.ciphertext; |
| 113 | +} |
| 114 | + |
| 115 | +std::optional<std::string> |
| 116 | +CryptoBasicsTestVector::EncryptWithLabel::verify(CipherSuite suite) const |
| 117 | +{ |
| 118 | + auto ct = HPKECiphertext{ kem_output, ciphertext }; |
| 119 | + auto pt = priv.decrypt(suite, label, context, ct); |
| 120 | + VERIFY_EQUAL("decrypt with label", pt, plaintext); |
| 121 | + |
| 122 | + auto new_ct = pub.encrypt(suite, label, context, plaintext); |
| 123 | + auto new_pt = priv.decrypt(suite, label, context, new_ct); |
| 124 | + VERIFY_EQUAL("encrypt with label", new_pt, plaintext); |
| 125 | + |
| 126 | + return std::nullopt; |
| 127 | +} |
| 128 | + |
| 129 | +CryptoBasicsTestVector::CryptoBasicsTestVector(CipherSuite suite) |
| 130 | + : PseudoRandom(suite, "crypto-basics") |
| 131 | + , cipher_suite(suite) |
| 132 | + , ref_hash(suite, prg.sub("ref_hash")) |
| 133 | + , expand_with_label(suite, prg.sub("expand_with_label")) |
| 134 | + , derive_secret(suite, prg.sub("derive_secret")) |
| 135 | + , derive_tree_secret(suite, prg.sub("derive_tree_secret")) |
| 136 | + , sign_with_label(suite, prg.sub("sign_with_label")) |
| 137 | + , encrypt_with_label(suite, prg.sub("encrypt_with_label")) |
| 138 | +{ |
| 139 | +} |
| 140 | + |
| 141 | +std::optional<std::string> |
| 142 | +CryptoBasicsTestVector::verify() const |
| 143 | +{ |
| 144 | + auto result = ref_hash.verify(cipher_suite); |
| 145 | + if (result) { |
| 146 | + return result; |
| 147 | + } |
| 148 | + |
| 149 | + result = expand_with_label.verify(cipher_suite); |
| 150 | + if (result) { |
| 151 | + return result; |
| 152 | + } |
| 153 | + |
| 154 | + result = derive_secret.verify(cipher_suite); |
| 155 | + if (result) { |
| 156 | + return result; |
| 157 | + } |
| 158 | + |
| 159 | + result = derive_tree_secret.verify(cipher_suite); |
| 160 | + if (result) { |
| 161 | + return result; |
| 162 | + } |
| 163 | + |
| 164 | + result = sign_with_label.verify(cipher_suite); |
| 165 | + if (result) { |
| 166 | + return result; |
| 167 | + } |
| 168 | + |
| 169 | + result = encrypt_with_label.verify(cipher_suite); |
| 170 | + if (result) { |
| 171 | + return result; |
| 172 | + } |
| 173 | + |
| 174 | + return std::nullopt; |
| 175 | +} |
| 176 | + |
| 177 | +} // namespace mls_vectors |
0 commit comments