File: aes_cbc.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (52 lines) | stat: -rw-r--r-- 2,133 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <vector>

#include "base/containers/span.h"
#include "crypto/crypto_export.h"

#ifndef CRYPTO_AES_CBC_H_
#define CRYPTO_AES_CBC_H_

// These functions implement one-shot block cipher encrypt/decrypt operations
// for AES-CBC. This interface is deliberately not abstracted over cipher type -
// new code should prefer the higher-level AEAD interface instead.
namespace crypto::aes_cbc {

inline constexpr size_t kBlockSize = 16;

// WARNING: In general you should not use these, and should prefer an AEAD mode
// which includes authentication.
//
// * The key must be 16 or 32 bytes long, for AES-128 or AES-256 respectively.
// * Decrypt() can fail if padding is incorrect, in which case it returns
//   nullopt.
//
// Design note:
// It may at first seem appealing to replace these functions with equivalents
// that take out parameters to avoid allocating a new value, but it is fiddly
// for the caller to compute the size of the output buffer correctly and for
// Encrypt() to ensure that junk data is not left in the buffer afterwards. For
// example, one could do:
//   size_t Encrypt(&[u8] key, &[u8] iv, &[u8] plaintext, &mut [u8] ciphertext)
// but then the caller could accidentally discard the size and use the full
// ciphertext buffer, even if not all of it was written. It's simpler to just
// always do a heap allocation here, and let callers that care about avoiding it
// use the BoringSSL APIs directly.
//
// WARNING: Do not call Decrypt() with an unauthenticated ciphertext, because
// you are very likely to accidentally create a padding oracle.
CRYPTO_EXPORT std::vector<uint8_t> Encrypt(
    base::span<const uint8_t> key,
    base::span<const uint8_t, kBlockSize> iv,
    base::span<const uint8_t> plaintext);
CRYPTO_EXPORT std::optional<std::vector<uint8_t>> Decrypt(
    base::span<const uint8_t> key,
    base::span<const uint8_t, kBlockSize> iv,
    base::span<const uint8_t> ciphertext);

}  // namespace crypto::aes_cbc

#endif  // CRYPTO_AES_CBC_H_