@@ -80,11 +80,14 @@ pub trait Aead {
80
80
/// The length of a nonce.
81
81
type NonceSize : ArraySize ;
82
82
83
- /// The maximum length of the tag.
83
+ /// The length of a tag.
84
84
type TagSize : ArraySize ;
85
85
86
86
/// Constant which defines whether AEAD specification appends or prepends tags.
87
87
///
88
+ /// It influences the behavior of the [`Aead::encrypt_to_vec`], [`Aead::decrypt_to_vec`],
89
+ /// [`Aead::encrypt_to_buffer`], and [`Aead::decrypt_to_buffer`] methods.
90
+ ///
88
91
/// If the specification does not explicitly specify tag kind, we default to postfix tags.
89
92
const IS_POSTFIX : bool = true ;
90
93
@@ -158,6 +161,13 @@ pub trait Aead {
158
161
self . detached_decrypt_inout ( nonce, associated_data, buf, tag)
159
162
}
160
163
164
+ /// Encrypt the [`InOutBufReserved`] data, append the authentication tag, and return
165
+ /// the resulting byte slice.
166
+ ///
167
+ /// `buffer` should have at least [`TagSize`][Aead::TagSize] bytes of additional output
168
+ /// capacity; otherwise, the method will return an error.
169
+ ///
170
+ /// The returned byte slice is guaranteed to point to the output of `buffer`.
161
171
#[ inline]
162
172
fn postfix_encrypt_inout < ' out > (
163
173
& self ,
@@ -176,8 +186,13 @@ pub trait Aead {
176
186
Ok ( & mut out_buf[ ..res_len] )
177
187
}
178
188
179
- /// Decrypt the [`InOutBuf`] data, returning an error in the event the provided
180
- /// authentication tag does not match the given ciphertext.
189
+ /// Decrypt the [`InOutBuf`] data, verify the appended authentication tag, and return
190
+ /// the resulting byte slice in case of success.
191
+ ///
192
+ /// Returns an error if the provided authentication tag does not match the given ciphertext
193
+ /// or if the size of `buffer` is smaller than the tag size.
194
+ ///
195
+ /// The returned byte slice is guaranteed to point to the output of `buffer`.
181
196
#[ inline]
182
197
fn postfix_decrypt_inout < ' out > (
183
198
& self ,
@@ -193,6 +208,13 @@ pub trait Aead {
193
208
Ok ( into_out_buf ( buf) )
194
209
}
195
210
211
+ /// Encrypt the plaintext data of length `plaintext_len` residing at the beggining of `buffer`
212
+ /// in-place, append the authentication tag, and return the resulting byte slice.
213
+ ///
214
+ /// `buffer` should have at least [`TagSize`][Aead::TagSize] bytes of additional output
215
+ /// capacity; otherwise, the method will return an error.
216
+ ///
217
+ /// The returned byte slice is guaranteed to point to `buffer`.
196
218
#[ inline]
197
219
fn postfix_encrypt_inplace < ' out > (
198
220
& self ,
@@ -210,8 +232,13 @@ pub trait Aead {
210
232
Ok ( buf)
211
233
}
212
234
213
- /// Encrypt the data in-place, returning an error in the event the provided
214
- /// authentication tag does not match the given ciphertext.
235
+ /// Decrypt the data in `buffer` in-place, verify the appended authentication tag, and return
236
+ /// the resulting byte slice in case of success.
237
+ ///
238
+ /// Returns an error if the provided authentication tag does not match the given ciphertext
239
+ /// or if the size of `buffer` is smaller than the tag size.
240
+ ///
241
+ /// The returned byte slice is guaranteed to point to the output of `buffer`.
215
242
#[ inline]
216
243
fn postfix_decrypt_inplace < ' out > (
217
244
& self ,
@@ -227,6 +254,13 @@ pub trait Aead {
227
254
Ok ( buf)
228
255
}
229
256
257
+ /// Encrypt the data in `plaintext`, write resulting ciphertext to `buffer`, append
258
+ /// the authentication tag, and return the resulting byte slice.
259
+ ///
260
+ /// `buffer` should have at least [`TagSize`][Aead::TagSize] bytes of additional capacity;
261
+ /// otherwise, the method will return an error.
262
+ ///
263
+ /// The returned byte slice is guaranteed to point to the output of `buffer`.
230
264
#[ inline]
231
265
fn postfix_encrypt_to_buf < ' out > (
232
266
& self ,
@@ -245,8 +279,15 @@ pub trait Aead {
245
279
Ok ( buf)
246
280
}
247
281
248
- /// Encrypt the data buffer-to-buffer, returning an error in the event the provided
249
- /// authentication tag does not match the given ciphertext.
282
+ /// Decrypt the data in `ciphertext`, write resulting ciphertext to `buffer`, verify
283
+ /// the appended authentication tag, and return the resulting byte slice in case of success.
284
+ ///
285
+ /// Returns an error if the provided authentication tag does not match the given ciphertext,
286
+ /// if the size of `ciphertext` is smaller than the tag size, or if the size of `buffer` is
287
+ /// too small for resulting plaintext (i.e. it should have capacity of at least
288
+ /// `ciphertext.len() - tag_size`).
289
+ ///
290
+ /// The returned byte slice is guaranteed to point to the output of `buffer`.
250
291
#[ inline]
251
292
fn postfix_decrypt_to_buf < ' out > (
252
293
& self ,
@@ -265,6 +306,9 @@ pub trait Aead {
265
306
Ok ( pt_dst)
266
307
}
267
308
309
+ /// Encrypt the data in `buffer`, and append the authentication tag to it.
310
+ ///
311
+ /// `buffer` is a generic [`Buffer`] type. See the trait docs for more information.
268
312
#[ inline]
269
313
fn postfix_encrypt_buffer (
270
314
& self ,
@@ -276,6 +320,11 @@ pub trait Aead {
276
320
buffer. extend_from_slice ( & tag)
277
321
}
278
322
323
+ /// Decrypt the data in `buffer`, verify the appended authentication tag, and truncate `buffer`
324
+ /// to contain only the resulting plaintext.
325
+ ///
326
+ /// Returns an error if the provided authentication tag does not match the given ciphertext,
327
+ /// or if the length of `buffer` is smaller than the tag size.
279
328
#[ inline]
280
329
fn postfix_decrypt_buffer (
281
330
& self ,
0 commit comments