| 1 | #include "aead_cipher.h" |
| 2 | #include "openssl_common.h" |
| 3 | |
| 4 | #include <openssl/evp.h> |
| 5 | |
| 6 | #if WITH_BORINGSSL |
| 7 | #include <openssl/aead.h> |
| 8 | #endif |
| 9 | |
| 10 | namespace mlspp::hpke { |
| 11 | |
| 12 | /// |
| 13 | /// ExportOnlyCipher |
| 14 | /// |
| 15 | bytes |
| 16 | ExportOnlyCipher::seal(const bytes& /* key */, |
| 17 | const bytes& /* nonce */, |
| 18 | const bytes& /* aad */, |
| 19 | const bytes& /* pt */) const |
| 20 | { |
| 21 | throw std::runtime_error("seal() on export-only context"); |
| 22 | } |
| 23 | |
| 24 | std::optional<bytes> |
| 25 | ExportOnlyCipher::open(const bytes& /* key */, |
| 26 | const bytes& /* nonce */, |
| 27 | const bytes& /* aad */, |
| 28 | const bytes& /* ct */) const |
| 29 | { |
| 30 | throw std::runtime_error("open() on export-only context"); |
| 31 | } |
| 32 | |
| 33 | ExportOnlyCipher::ExportOnlyCipher() |
| 34 | : AEAD(AEAD::ID::export_only, 0, 0) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | /// |
| 39 | /// AEADCipher |
| 40 | /// |
| 41 | AEADCipher |
| 42 | make_aead(AEAD::ID cipher_in) |
| 43 | { |
| 44 | return { cipher_in }; |
| 45 | } |
| 46 | |
| 47 | template<> |
| 48 | const AEADCipher& |
| 49 | AEADCipher::get<AEAD::ID::AES_128_GCM>() |
| 50 | { |
| 51 | static const auto instance = make_aead(AEAD::ID::AES_128_GCM); |
| 52 | return instance; |
| 53 | } |
| 54 | |
| 55 | template<> |
| 56 | const AEADCipher& |
| 57 | AEADCipher::get<AEAD::ID::AES_256_GCM>() |
| 58 | { |
| 59 | static const auto instance = make_aead(AEAD::ID::AES_256_GCM); |
| 60 | return instance; |
| 61 | } |
| 62 | |
| 63 | template<> |
| 64 | const AEADCipher& |
| 65 | AEADCipher::get<AEAD::ID::CHACHA20_POLY1305>() |
| 66 | { |
| 67 | static const auto instance = make_aead(AEAD::ID::CHACHA20_POLY1305); |
| 68 | return instance; |
| 69 | } |
| 70 | |
| 71 | static size_t |
| 72 | cipher_key_size(AEAD::ID cipher) |
| 73 | { |
| 74 | switch (cipher) { |
| 75 | case AEAD::ID::AES_128_GCM: |
| 76 | return 16; |
| 77 | |
| 78 | case AEAD::ID::AES_256_GCM: |
| 79 | case AEAD::ID::CHACHA20_POLY1305: |
| 80 | return 32; |
| 81 | |
| 82 | default: |
| 83 | throw std::runtime_error("Unsupported algorithm"); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | static size_t |
| 88 | cipher_nonce_size(AEAD::ID cipher) |
| 89 | { |
| 90 | switch (cipher) { |
| 91 | case AEAD::ID::AES_128_GCM: |
| 92 | case AEAD::ID::AES_256_GCM: |
| 93 | case AEAD::ID::CHACHA20_POLY1305: |
| 94 | return 12; |
| 95 | |
| 96 | default: |
| 97 | throw std::runtime_error("Unsupported algorithm"); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | static size_t |
| 102 | cipher_tag_size(AEAD::ID cipher) |
| 103 | { |
| 104 | switch (cipher) { |
| 105 | case AEAD::ID::AES_128_GCM: |
| 106 | case AEAD::ID::AES_256_GCM: |
| 107 | case AEAD::ID::CHACHA20_POLY1305: |
| 108 | return 16; |
| 109 | |
| 110 | default: |
| 111 | throw std::runtime_error("Unsupported algorithm"); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | #if WITH_BORINGSSL |
| 116 | static const EVP_AEAD* |
| 117 | boringssl_cipher(AEAD::ID cipher) |
| 118 | { |
| 119 | switch (cipher) { |
| 120 | case AEAD::ID::AES_128_GCM: |
| 121 | return EVP_aead_aes_128_gcm(); |
| 122 | |
| 123 | case AEAD::ID::AES_256_GCM: |
| 124 | return EVP_aead_aes_256_gcm(); |
| 125 | |
| 126 | case AEAD::ID::CHACHA20_POLY1305: |
| 127 | return EVP_aead_chacha20_poly1305(); |
| 128 | |
| 129 | default: |
| 130 | throw std::runtime_error("Unsupported algorithm"); |
| 131 | } |
| 132 | } |
| 133 | #else |
| 134 | static const EVP_CIPHER* |
| 135 | openssl_cipher(AEAD::ID cipher) |
| 136 | { |
| 137 | switch (cipher) { |
| 138 | case AEAD::ID::AES_128_GCM: |
| 139 | return EVP_aes_128_gcm(); |
| 140 | |
| 141 | case AEAD::ID::AES_256_GCM: |
| 142 | return EVP_aes_256_gcm(); |
| 143 | |
| 144 | case AEAD::ID::CHACHA20_POLY1305: |
| 145 | return EVP_chacha20_poly1305(); |
| 146 | |
| 147 | default: |
| 148 | throw std::runtime_error("Unsupported algorithm"); |
| 149 | } |
| 150 | } |
| 151 | #endif // WITH_BORINGSSL |
| 152 | |
| 153 | AEADCipher::AEADCipher(AEAD::ID id_in) |
| 154 | : AEAD(id_in, cipher_key_size(id_in), cipher_nonce_size(id_in)) |
| 155 | , tag_size(cipher_tag_size(id)) |
| 156 | { |
| 157 | } |
| 158 | |
| 159 | bytes |
| 160 | AEADCipher::seal(const bytes& key, |
| 161 | const bytes& nonce, |
| 162 | const bytes& aad, |
| 163 | const bytes& pt) const |
| 164 | { |
| 165 | #if WITH_BORINGSSL |
| 166 | auto ctx = make_typed_unique( |
| 167 | EVP_AEAD_CTX_new(boringssl_cipher(id), key.data(), key.size(), tag_size)); |
| 168 | if (ctx == nullptr) { |
| 169 | throw openssl_error(); |
| 170 | } |
| 171 | |
| 172 | auto ct = bytes(pt.size() + tag_size); |
| 173 | auto out_len = ct.size(); |
| 174 | if (1 != EVP_AEAD_CTX_seal(ctx.get(), |
| 175 | ct.data(), |
| 176 | &out_len, |
| 177 | ct.size(), |
| 178 | nonce.data(), |
| 179 | nonce.size(), |
| 180 | pt.data(), |
| 181 | pt.size(), |
| 182 | aad.data(), |
| 183 | aad.size())) { |
| 184 | throw openssl_error(); |
| 185 | } |
| 186 | |
| 187 | return ct; |
| 188 | #else |
| 189 | auto ctx = make_typed_unique(EVP_CIPHER_CTX_new()); |
| 190 | if (ctx == nullptr) { |
| 191 | throw openssl_error(); |
| 192 | } |
| 193 | |
| 194 | const auto* cipher = openssl_cipher(id); |
| 195 | if (1 != EVP_EncryptInit(ctx.get(), cipher, key.data(), nonce.data())) { |
| 196 | throw openssl_error(); |
| 197 | } |
| 198 | |
| 199 | int outlen = 0; |
| 200 | if (!aad.empty()) { |
| 201 | if (1 != EVP_EncryptUpdate(ctx.get(), |
| 202 | nullptr, |
| 203 | &outlen, |
| 204 | aad.data(), |
| 205 | static_cast<int>(aad.size()))) { |
| 206 | throw openssl_error(); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | bytes ct(pt.size()); |
| 211 | if (1 != EVP_EncryptUpdate(ctx.get(), |
| 212 | ct.data(), |
| 213 | &outlen, |
| 214 | pt.data(), |
| 215 | static_cast<int>(pt.size()))) { |
| 216 | throw openssl_error(); |
| 217 | } |
| 218 | |
| 219 | // Providing nullptr as an argument is safe here because this |
| 220 | // function never writes with GCM; it only computes the tag |
| 221 | if (1 != EVP_EncryptFinal(ctx.get(), nullptr, &outlen)) { |
| 222 | throw openssl_error(); |
| 223 | } |
| 224 | |
| 225 | bytes tag(tag_size); |
| 226 | if (1 != EVP_CIPHER_CTX_ctrl(ctx.get(), |
| 227 | EVP_CTRL_GCM_GET_TAG, |
| 228 | static_cast<int>(tag_size), |
| 229 | tag.data())) { |
| 230 | throw openssl_error(); |
| 231 | } |
| 232 | |
| 233 | ct += tag; |
| 234 | return ct; |
| 235 | #endif // WITH_BORINGSSL |
| 236 | } |
| 237 | |
| 238 | std::optional<bytes> |
| 239 | AEADCipher::open(const bytes& key, |
| 240 | const bytes& nonce, |
| 241 | const bytes& aad, |
| 242 | const bytes& ct) const |
| 243 | { |
| 244 | if (ct.size() < tag_size) { |
| 245 | throw std::runtime_error("AEAD ciphertext smaller than tag size"); |
| 246 | } |
| 247 | |
| 248 | #if WITH_BORINGSSL |
| 249 | auto ctx = make_typed_unique(EVP_AEAD_CTX_new( |
| 250 | boringssl_cipher(id), key.data(), key.size(), cipher_tag_size(id))); |
| 251 | if (ctx == nullptr) { |
| 252 | throw openssl_error(); |
| 253 | } |
| 254 | |
| 255 | auto pt = bytes(ct.size() - tag_size); |
| 256 | auto out_len = pt.size(); |
| 257 | if (1 != EVP_AEAD_CTX_open(ctx.get(), |
| 258 | pt.data(), |
| 259 | &out_len, |
| 260 | pt.size(), |
| 261 | nonce.data(), |
| 262 | nonce.size(), |
| 263 | ct.data(), |
| 264 | ct.size(), |
| 265 | aad.data(), |
| 266 | aad.size())) { |
| 267 | throw openssl_error(); |
| 268 | } |
| 269 | |
| 270 | return pt; |
| 271 | #else |
| 272 | auto ctx = make_typed_unique(EVP_CIPHER_CTX_new()); |
| 273 | if (ctx == nullptr) { |
| 274 | throw openssl_error(); |
| 275 | } |
| 276 | |
| 277 | const auto* cipher = openssl_cipher(id); |
| 278 | if (1 != EVP_DecryptInit(ctx.get(), cipher, key.data(), nonce.data())) { |
| 279 | throw openssl_error(); |
| 280 | } |
| 281 | |
| 282 | auto inner_ct_size = ct.size() - tag_size; |
| 283 | auto tag = ct.slice(inner_ct_size, ct.size()); |
| 284 | if (1 != EVP_CIPHER_CTX_ctrl(ctx.get(), |
| 285 | EVP_CTRL_GCM_SET_TAG, |
| 286 | static_cast<int>(tag_size), |
| 287 | tag.data())) { |
| 288 | throw openssl_error(); |
| 289 | } |
| 290 | |
| 291 | int out_size = 0; |
| 292 | if (!aad.empty()) { |
| 293 | if (1 != EVP_DecryptUpdate(ctx.get(), |
| 294 | nullptr, |
| 295 | &out_size, |
| 296 | aad.data(), |
| 297 | static_cast<int>(aad.size()))) { |
| 298 | throw openssl_error(); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | bytes pt(inner_ct_size); |
| 303 | if (1 != EVP_DecryptUpdate(ctx.get(), |
| 304 | pt.data(), |
| 305 | &out_size, |
| 306 | ct.data(), |
| 307 | static_cast<int>(inner_ct_size))) { |
| 308 | throw openssl_error(); |
| 309 | } |
| 310 | |
| 311 | // Providing nullptr as an argument is safe here because this |
| 312 | // function never writes with GCM; it only verifies the tag |
| 313 | if (1 != EVP_DecryptFinal(ctx.get(), nullptr, &out_size)) { |
| 314 | throw std::runtime_error("AEAD authentication failure"); |
| 315 | } |
| 316 | |
| 317 | return pt; |
| 318 | #endif // WITH_BORINGSSL |
| 319 | } |
| 320 | |
| 321 | } // namespace mlspp::hpke |