Asylo
aead_cryptor.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2018 Asylo authors
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef ASYLO_CRYPTO_AEAD_CRYPTOR_H_
20 #define ASYLO_CRYPTO_AEAD_CRYPTOR_H_
21 
22 #include <cstdint>
23 #include <memory>
24 
25 #include "absl/types/span.h"
26 #include "asylo/crypto/aead_key.h"
27 #include "asylo/crypto/algorithms.pb.h"
28 #include "asylo/crypto/nonce_generator_interface.h"
29 #include "asylo/crypto/util/byte_container_view.h"
30 #include "asylo/util/statusor.h"
31 
32 namespace asylo {
33 
34 /// An AEAD cryptor that provides Seal() and Open() functionality. Currently
35 /// supported configurations:
36 /// * AES-GCM-128 and AES-GCM-256 with 96-bit random nonces.
37 /// * AES-GCM-SIV-128 and AES-GCM-SIV-256 with 96-bit random nonces. (For
38 /// information on AES-GCM-SIV see https://cyber.biu.ac.il/aes-gcm-siv/)
39 class AeadCryptor {
40  public:
41  /// Creates a cryptor that uses AES-GCM for Seal() and Open(), and generates
42  /// random 96-bit nonces for use in Seal().
43  ///
44  /// \param key The underlying key used for encryption and decryption.
45  /// \return A pointer to the created cryptor, or a non-OK Status if creation
46  /// failed.
49 
50  /// Creates a cryptor that uses AES-GCM-SIV for Seal() and Open(), and
51  /// generates random 96-bit nonces for use in Seal().
52  ///
53  /// \param key The underlying key used for encryption and decryption.
54  /// \return A pointer to the created cryptor, or a non-OK Status if creation
55  /// failed.
58 
59  /// Gets the maximum size of a message that may be sealed successfully with a
60  /// cryptor that uses `scheme`.
61  ///
62  /// \param scheme The associated AeadScheme.
63  /// \return The maximum message size that may be sealed successfully, or a
64  /// non-OK Status if `scheme` is unsupported.
66 
67  /// Gets the maximum number of messages that may be sealed safely with a
68  /// cryptor that uses `scheme`.
69  ///
70  /// \param scheme The associated AeadScheme.
71  /// \return The maximum number of messages that may be sealed safely, or a
72  /// non-OK Status if `scheme` is unsupported.
74 
75  /// Gets the maximum size of a message that may be sealed successfully.
76  ///
77  /// \return The maximum message size that this cryptor will seal successfully.
78  size_t MaxMessageSize() const;
79 
80  /// Gets the maximum number of messages that may be sealed successfully.
81  ///
82  /// \return The maximum number of messages that this cryptor will seal
83  /// successfully.
84  uint64_t MaxSealedMessages() const;
85 
86  /// Gets the max overhead of Seal().
87  ///
88  /// \return The maximum space overhead of Seal().
89  size_t MaxSealOverhead() const;
90 
91  /// Gets the nonce size.
92  ///
93  /// \return The nonce size.
94  size_t NonceSize() const;
95 
96  /// Implements the AEAD Seal operation.
97  ///
98  /// The nonce used is returned through `nonce` and the authenticated
99  /// ciphertext is written to `ciphertext`. `plaintext.size()` must be less
100  /// than or equal to MaxMessageSize(). `nonce.size()` must be greater than or
101  /// equal to the value returned by NonceSize(). `ciphertext.size()` must be
102  /// greater than or equal to `plaintext.size()` + MaxSealOverhead().
103  /// `ciphertext` is not resized, but its final size is returned through
104  /// `ciphertext_size`. Seal() will succeed at most MaxSealedMessages() times.
105  ///
106  /// \param plaintext The secret that will be sealed.
107  /// \param associated_data The authenticated data for the Seal() operation.
108  /// \param[out] nonce The generated nonce.
109  /// \param[out] ciphertext The sealed ciphertext of `plaintext`.
110  /// \param[out] ciphertext_size The size of `ciphertext`.
111  /// \return The resulting status of the Seal() operation.
115 
116  /// Implements the AEAD Open operation.
117  ///
118  /// `nonce.size()` must be greater than or equal to the value returned by
119  /// NonceSize(). `plaintext` is not resized, but its final size is returned
120  /// through `plaintext_size`. To ascertain that `plaintext` is not smaller
121  /// than is necessary for Open(), `plaintext.size()` should be greater than or
122  /// equal to `ciphertext.size()`.
123  ///
124  /// \param ciphertext The sealed ciphertext.
125  /// \param associated_data The authenticated data for the Open() operation.
126  /// \param nonce The nonce used to seal the ciphertext.
127  /// \param[out] plaintext The unsealed ciphertext.
128  /// \param[out] plaintext_size The size of the plaintext.
129  /// \return The resulting status of the Open() operation.
133 
134  private:
135  AeadCryptor(std::unique_ptr<AeadKey> key, size_t max_message_size,
136  uint64_t max_sealed_messages,
137  std::unique_ptr<NonceGeneratorInterface> nonce_generator);
138 
139  // The AeadKey used for Seal() and Open().
140  const std::unique_ptr<AeadKey> key_;
141 
142  // The maximum size of a message passed in for Seal().
143  const size_t max_message_size_;
144 
145  // The maximum number of messages that may be sealed successfully.
146  const uint64_t max_sealed_messages_;
147 
148  // The nonce generator used to generate nonces for Seal().
149  const std::unique_ptr<NonceGeneratorInterface> nonce_generator_;
150 
151  // The number messages that have been sealed successfully.
152  size_t number_of_sealed_messages_;
153 };
154 
155 namespace experimental {
156 /// \deprecated `AeadCryptor` has been moved to the `asylo` top-level namespace.
157 /// This type alias will be removed in an up-coming release.
158 using AeadCryptor = ::asylo::AeadCryptor;
159 } // namespace experimental
160 
161 } // namespace asylo
162 
163 #endif // ASYLO_CRYPTO_AEAD_CRYPTOR_H_
Status Open(ByteContainerView ciphertext, ByteContainerView associated_data, ByteContainerView nonce, absl::Span< uint8_t > plaintext, size_t *plaintext_size)
Implements the AEAD Open operation.
static StatusOr< uint64_t > MaxSealedMessages(AeadScheme scheme)
Gets the maximum number of messages that may be sealed safely with a cryptor that uses scheme...
An AEAD cryptor that provides Seal() and Open() functionality.
Definition: aead_cryptor.h:39
size_t NonceSize() const
Gets the nonce size.
static StatusOr< std::unique_ptr< AeadCryptor > > CreateAesGcmSivCryptor(ByteContainerView key)
Creates a cryptor that uses AES-GCM-SIV for Seal() and Open(), and generates random 96-bit nonces for...
ABSL_CONST_INIT const char kStatusMoveAssignmentMsg[]
uint64_t MaxSealedMessages() const
Gets the maximum number of messages that may be sealed successfully.
size_t MaxMessageSize() const
Gets the maximum size of a message that may be sealed successfully.
static StatusOr< std::unique_ptr< AeadCryptor > > CreateAesGcmCryptor(ByteContainerView key)
Creates a cryptor that uses AES-GCM for Seal() and Open(), and generates random 96-bit nonces for use...
Status Seal(ByteContainerView plaintext, ByteContainerView associated_data, absl::Span< uint8_t > nonce, absl::Span< uint8_t > ciphertext, size_t *ciphertext_size)
Implements the AEAD Seal operation.
Definition: aead_cryptor.h:155
size_t MaxSealOverhead() const
Gets the max overhead of Seal().
static StatusOr< size_t > MaxMessageSize(AeadScheme scheme)
Gets the maximum size of a message that may be sealed successfully with a cryptor that uses scheme...