xref: /aosp_15_r20/external/tink/cc/hybrid_encrypt.h (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1*e7b1675dSTing-Kang Chang // Copyright 2017 Google Inc.
2*e7b1675dSTing-Kang Chang //
3*e7b1675dSTing-Kang Chang // Licensed under the Apache License, Version 2.0 (the "License");
4*e7b1675dSTing-Kang Chang // you may not use this file except in compliance with the License.
5*e7b1675dSTing-Kang Chang // You may obtain a copy of the License at
6*e7b1675dSTing-Kang Chang //
7*e7b1675dSTing-Kang Chang //     http://www.apache.org/licenses/LICENSE-2.0
8*e7b1675dSTing-Kang Chang //
9*e7b1675dSTing-Kang Chang // Unless required by applicable law or agreed to in writing, software
10*e7b1675dSTing-Kang Chang // distributed under the License is distributed on an "AS IS" BASIS,
11*e7b1675dSTing-Kang Chang // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*e7b1675dSTing-Kang Chang // See the License for the specific language governing permissions and
13*e7b1675dSTing-Kang Chang // limitations under the License.
14*e7b1675dSTing-Kang Chang //
15*e7b1675dSTing-Kang Chang ///////////////////////////////////////////////////////////////////////////////
16*e7b1675dSTing-Kang Chang 
17*e7b1675dSTing-Kang Chang #ifndef TINK_HYBRID_ENCRYPT_H_
18*e7b1675dSTing-Kang Chang #define TINK_HYBRID_ENCRYPT_H_
19*e7b1675dSTing-Kang Chang 
20*e7b1675dSTing-Kang Chang #include <string>
21*e7b1675dSTing-Kang Chang 
22*e7b1675dSTing-Kang Chang #include "absl/strings/string_view.h"
23*e7b1675dSTing-Kang Chang #include "tink/util/statusor.h"
24*e7b1675dSTing-Kang Chang 
25*e7b1675dSTing-Kang Chang namespace crypto {
26*e7b1675dSTing-Kang Chang namespace tink {
27*e7b1675dSTing-Kang Chang 
28*e7b1675dSTing-Kang Chang ///////////////////////////////////////////////////////////////////////////////
29*e7b1675dSTing-Kang Chang // The interface for hybrid encryption.
30*e7b1675dSTing-Kang Chang //
31*e7b1675dSTing-Kang Chang // Implementations of this interface are secure against adaptive
32*e7b1675dSTing-Kang Chang // chosen ciphertext attacks.  In addition to 'plaintext' the
33*e7b1675dSTing-Kang Chang // encryption takes an extra parameter 'context_info', which usually
34*e7b1675dSTing-Kang Chang // is public data implicit from the context, but should be bound to
35*e7b1675dSTing-Kang Chang // the resulting ciphertext: upon decryption the ciphertext allows for
36*e7b1675dSTing-Kang Chang // checking the integrity of 'context_info' (but there are no
37*e7b1675dSTing-Kang Chang // guarantees wrt. to secrecy or authenticity of 'context_info').
38*e7b1675dSTing-Kang Chang //
39*e7b1675dSTing-Kang Chang // WARNING: hybrid encryption does not provide authenticity, that is the
40*e7b1675dSTing-Kang Chang // recipient of an encrypted message does not know the identity of the sender.
41*e7b1675dSTing-Kang Chang // Similar to general public-key encryption schemes the security goal of
42*e7b1675dSTing-Kang Chang // hybrid encryption is to provide privacy only. In other words, hybrid
43*e7b1675dSTing-Kang Chang // encryption is secure if and only if the recipient can accept anonymous
44*e7b1675dSTing-Kang Chang // messages or can rely on other mechanisms to authenticate the sender.
45*e7b1675dSTing-Kang Chang //
46*e7b1675dSTing-Kang Chang // 'context_info' can be empty or null, but to ensure the correct
47*e7b1675dSTing-Kang Chang // decryption of the resulting ciphertext the same value must be
48*e7b1675dSTing-Kang Chang // provided for decryption operation (cf. HybridDecrypt-interface).
49*e7b1675dSTing-Kang Chang //
50*e7b1675dSTing-Kang Chang // A concrete instantiation of this interface can implement the
51*e7b1675dSTing-Kang Chang // binding of 'context_info' to the ciphertext in various ways, for
52*e7b1675dSTing-Kang Chang // example:
53*e7b1675dSTing-Kang Chang //
54*e7b1675dSTing-Kang Chang // - use 'context_info' as "associated data"-input for the employed
55*e7b1675dSTing-Kang Chang //   AEAD symmetric encryption (cf. https://tools.ietf.org/html/rfc5116).
56*e7b1675dSTing-Kang Chang // - use 'context_info' as "CtxInfo"-input for HKDF (if the implementation uses
57*e7b1675dSTing-Kang Chang //   HKDF as key derivation function, cf. https://tools.ietf.org/html/rfc5869).
58*e7b1675dSTing-Kang Chang class HybridEncrypt {
59*e7b1675dSTing-Kang Chang  public:
60*e7b1675dSTing-Kang Chang   // Encrypts 'plaintext' binding 'context_info' to the resulting ciphertext.
61*e7b1675dSTing-Kang Chang   virtual crypto::tink::util::StatusOr<std::string> Encrypt(
62*e7b1675dSTing-Kang Chang       absl::string_view plaintext, absl::string_view context_info) const = 0;
63*e7b1675dSTing-Kang Chang 
64*e7b1675dSTing-Kang Chang   virtual ~HybridEncrypt() = default;
65*e7b1675dSTing-Kang Chang };
66*e7b1675dSTing-Kang Chang 
67*e7b1675dSTing-Kang Chang }  // namespace tink
68*e7b1675dSTing-Kang Chang }  // namespace crypto
69*e7b1675dSTing-Kang Chang 
70*e7b1675dSTing-Kang Chang #endif  // TINK_HYBRID_ENCRYPT_H_
71