blob: e3ff770b28f87b6b440c9a733aa246d78010d4fa [file] [log] [blame]
Leonid Baraz06b4bed2021-03-24 06:19:441// Copyright 2021 The Chromium Authors. All rights reserved.
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>
11
David Dorwin3f503b82022-04-20 04:07:0312#include "base/check_op.h"
Leonid Baraz06b4bed2021-03-24 06:19:4413#include "crypto/aead.h"
14#include "crypto/openssl_util.h"
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
19#include "base/strings/string_piece.h"
20
21namespace reporting {
22
23static_assert(X25519_PRIVATE_KEY_LEN == kKeySize, "X25519 mismatch");
24static_assert(X25519_PUBLIC_VALUE_LEN == kKeySize, "X25519 mismatch");
25static_assert(X25519_SHARED_KEY_LEN == kKeySize, "X25519 mismatch");
26static_assert(ED25519_PRIVATE_KEY_LEN == kSignKeySize, "ED25519 mismatch");
27static_assert(ED25519_PUBLIC_KEY_LEN == kKeySize, "ED25519 mismatch");
28static_assert(ED25519_SIGNATURE_LEN == kSignatureSize, "ED25519 mismatch");
29
30bool ComputeSharedSecret(const uint8_t peer_public_value[kKeySize],
31 uint8_t shared_secret[kKeySize],
32 uint8_t generated_public_value[kKeySize]) {
33 // Make sure OpenSSL is initialized, in order to avoid data races later.
34 crypto::EnsureOpenSSLInit();
35
36 // Generate new pair of private key and public value.
37 uint8_t out_private_key[kKeySize];
38 X25519_keypair(generated_public_value, out_private_key);
39
40 // Compute shared secret.
41 if (1 != X25519(shared_secret, out_private_key, peer_public_value)) {
42 return false;
43 }
44
45 // Success.
46 return true;
47}
48
49bool ProduceSymmetricKey(const uint8_t shared_secret[kKeySize],
50 uint8_t symmetric_key[kKeySize]) {
51 // Make sure OpenSSL is initialized, in order to avoid data races later.
52 crypto::EnsureOpenSSLInit();
53
54 // Produce symmetric key from shared secret using HKDF.
55 // Since the original keys were only used once, no salt and context is needed.
56 // Since the keys above are only used once, no salt and context is provided.
57 if (1 != HKDF(symmetric_key, kKeySize, /*digest=*/EVP_sha256(), shared_secret,
58 kKeySize,
59 /*salt=*/nullptr, /*salt_len=*/0,
60 /*info=*/nullptr, /*info_len=*/0)) {
61 return false;
62 }
63
64 // Success.
65 return true;
66}
67
68bool PerformSymmetricEncryption(const uint8_t symmetric_key[kKeySize],
69 base::StringPiece input_data,
70 std::string* output_data) {
71 // Make sure OpenSSL is initialized, in order to avoid data races later.
72 crypto::EnsureOpenSSLInit();
73
74 // Encrypt the data with symmetric key using AEAD interface.
75 crypto::Aead aead(crypto::Aead::CHACHA20_POLY1305);
76 DCHECK_EQ(aead.KeyLength(), kKeySize);
77
78 // Use the symmetric key for data encryption.
79 aead.Init(base::make_span(symmetric_key, kKeySize));
80
81 // Set nonce to 0s, since a symmetric key is only used once.
82 // Note: if we ever start reusing the same symmetric key, we will need
83 // to generate new nonce for every record and transfer it to the peer.
84 DCHECK_EQ(aead.NonceLength(), kNonceSize);
85 std::string nonce(kNonceSize, 0);
86
87 // Encrypt the whole record.
88 if (1 != aead.Seal(input_data, nonce, std::string(), output_data)) {
89 return false;
90 }
91
92 // Success. Attach nonce at the head, for compatibility with Tink.
93 output_data->insert(0, nonce);
94 return true;
95}
96
97bool VerifySignature(const uint8_t verification_key[kKeySize],
98 base::StringPiece message,
99 const uint8_t signature[kSignatureSize]) {
100 // Make sure OpenSSL is initialized, in order to avoid data races later.
101 crypto::EnsureOpenSSLInit();
102
103 // Verify message
104 if (1 != ED25519_verify(reinterpret_cast<const uint8_t*>(message.data()),
105 message.size(), signature, verification_key)) {
106 return false;
107 }
108
109 // Success.
110 return true;
111}
112
113} // namespace reporting