Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors |
Leonid Baraz | 06b4bed | 2021-03-24 06:19:44 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "components/reporting/encryption/primitives.h" |
| 6 | |
| 7 | #include <cstddef> |
| 8 | #include <cstdint> |
| 9 | #include <memory> |
| 10 | #include <string> |
Claudio DeSouza | de4d661 | 2024-05-23 10:45:40 | [diff] [blame] | 11 | #include <string_view> |
Leonid Baraz | 06b4bed | 2021-03-24 06:19:44 | [diff] [blame] | 12 | |
David Dorwin | 3f503b8 | 2022-04-20 04:07:03 | [diff] [blame] | 13 | #include "base/check_op.h" |
Leonid Baraz | 06b4bed | 2021-03-24 06:19:44 | [diff] [blame] | 14 | #include "crypto/aead.h" |
Leonid Baraz | 06b4bed | 2021-03-24 06:19:44 | [diff] [blame] | 15 | #include "third_party/boringssl/src/include/openssl/curve25519.h" |
| 16 | #include "third_party/boringssl/src/include/openssl/digest.h" |
| 17 | #include "third_party/boringssl/src/include/openssl/hkdf.h" |
| 18 | |
Leonid Baraz | 06b4bed | 2021-03-24 06:19:44 | [diff] [blame] | 19 | |
| 20 | namespace reporting { |
| 21 | |
| 22 | static_assert(X25519_PRIVATE_KEY_LEN == kKeySize, "X25519 mismatch"); |
| 23 | static_assert(X25519_PUBLIC_VALUE_LEN == kKeySize, "X25519 mismatch"); |
| 24 | static_assert(X25519_SHARED_KEY_LEN == kKeySize, "X25519 mismatch"); |
| 25 | static_assert(ED25519_PRIVATE_KEY_LEN == kSignKeySize, "ED25519 mismatch"); |
| 26 | static_assert(ED25519_PUBLIC_KEY_LEN == kKeySize, "ED25519 mismatch"); |
| 27 | static_assert(ED25519_SIGNATURE_LEN == kSignatureSize, "ED25519 mismatch"); |
| 28 | |
| 29 | bool ComputeSharedSecret(const uint8_t peer_public_value[kKeySize], |
| 30 | uint8_t shared_secret[kKeySize], |
| 31 | uint8_t generated_public_value[kKeySize]) { |
Leonid Baraz | 06b4bed | 2021-03-24 06:19:44 | [diff] [blame] | 32 | // Generate new pair of private key and public value. |
| 33 | uint8_t out_private_key[kKeySize]; |
| 34 | X25519_keypair(generated_public_value, out_private_key); |
| 35 | |
| 36 | // Compute shared secret. |
| 37 | if (1 != X25519(shared_secret, out_private_key, peer_public_value)) { |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | // Success. |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | bool ProduceSymmetricKey(const uint8_t shared_secret[kKeySize], |
| 46 | uint8_t symmetric_key[kKeySize]) { |
Leonid Baraz | 06b4bed | 2021-03-24 06:19:44 | [diff] [blame] | 47 | // Produce symmetric key from shared secret using HKDF. |
| 48 | // Since the original keys were only used once, no salt and context is needed. |
| 49 | // Since the keys above are only used once, no salt and context is provided. |
| 50 | if (1 != HKDF(symmetric_key, kKeySize, /*digest=*/EVP_sha256(), shared_secret, |
| 51 | kKeySize, |
| 52 | /*salt=*/nullptr, /*salt_len=*/0, |
| 53 | /*info=*/nullptr, /*info_len=*/0)) { |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | // Success. |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | bool PerformSymmetricEncryption(const uint8_t symmetric_key[kKeySize], |
Josh Hilke | 4996b60 | 2023-07-18 17:49:04 | [diff] [blame] | 62 | std::string_view input_data, |
Leonid Baraz | 06b4bed | 2021-03-24 06:19:44 | [diff] [blame] | 63 | std::string* output_data) { |
Leonid Baraz | 06b4bed | 2021-03-24 06:19:44 | [diff] [blame] | 64 | // Encrypt the data with symmetric key using AEAD interface. |
| 65 | crypto::Aead aead(crypto::Aead::CHACHA20_POLY1305); |
Leonid Baraz | 48447f14 | 2023-07-31 19:46:21 | [diff] [blame] | 66 | CHECK_EQ(aead.KeyLength(), kKeySize); |
Leonid Baraz | 06b4bed | 2021-03-24 06:19:44 | [diff] [blame] | 67 | |
| 68 | // Use the symmetric key for data encryption. |
| 69 | aead.Init(base::make_span(symmetric_key, kKeySize)); |
| 70 | |
| 71 | // Set nonce to 0s, since a symmetric key is only used once. |
| 72 | // Note: if we ever start reusing the same symmetric key, we will need |
| 73 | // to generate new nonce for every record and transfer it to the peer. |
Leonid Baraz | 48447f14 | 2023-07-31 19:46:21 | [diff] [blame] | 74 | CHECK_EQ(aead.NonceLength(), kNonceSize); |
Leonid Baraz | 06b4bed | 2021-03-24 06:19:44 | [diff] [blame] | 75 | std::string nonce(kNonceSize, 0); |
| 76 | |
| 77 | // Encrypt the whole record. |
| 78 | if (1 != aead.Seal(input_data, nonce, std::string(), output_data)) { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | // Success. Attach nonce at the head, for compatibility with Tink. |
| 83 | output_data->insert(0, nonce); |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | bool VerifySignature(const uint8_t verification_key[kKeySize], |
Josh Hilke | 4996b60 | 2023-07-18 17:49:04 | [diff] [blame] | 88 | std::string_view message, |
Leonid Baraz | 06b4bed | 2021-03-24 06:19:44 | [diff] [blame] | 89 | const uint8_t signature[kSignatureSize]) { |
Leonid Baraz | 06b4bed | 2021-03-24 06:19:44 | [diff] [blame] | 90 | // Verify message |
| 91 | if (1 != ED25519_verify(reinterpret_cast<const uint8_t*>(message.data()), |
| 92 | message.size(), signature, verification_key)) { |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | // Success. |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | } // namespace reporting |