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
   53
   54
   55
   56

media / cdm / aes_cbc_crypto.h [blame]

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_CDM_AES_CBC_CRYPTO_H_
#define MEDIA_CDM_AES_CBC_CRYPTO_H_

#include <stdint.h>

#include "base/containers/span.h"
#include "media/base/media_export.h"
#include "third_party/boringssl/src/include/openssl/evp.h"

namespace crypto {
class SymmetricKey;
}

namespace media {

// This class implements AES-CBC-128 decryption as described in the Advanced
// Encryption Standard specified by AES [FIPS-197, https://www.nist.gov]
// using 128-bit keys in Cipher Block Chaining mode, as specified in Block
// Cipher Modes [NIST 800-38A, https://www.nist.gov].

class MEDIA_EXPORT AesCbcCrypto {
 public:
  AesCbcCrypto();

  AesCbcCrypto(const AesCbcCrypto&) = delete;
  AesCbcCrypto& operator=(const AesCbcCrypto&) = delete;

  ~AesCbcCrypto();

  // Initializes the encryptor using |key| and |iv|. Returns false if either
  // the key or the initialization vector cannot be used.
  bool Initialize(base::span<const uint8_t> key, base::span<const uint8_t> iv);

  // Deprecated initializer that takes a crypto::SymmetricKey. Do not add new
  // uses of this - pass the key as a byte span instead.
  bool Initialize(const crypto::SymmetricKey& key,
                  base::span<const uint8_t> iv);

  // Decrypts |encrypted_data| into |decrypted_data|. |encrypted_data| must be
  // a multiple of the blocksize (128 bits), and |decrypted_data| must have
  // enough space for |encrypted_data|.size(). Returns false if the decryption
  // fails.
  bool Decrypt(base::span<const uint8_t> encrypted_data,
               uint8_t* decrypted_data);

 private:
  bssl::ScopedEVP_CIPHER_CTX ctx_;
};

}  // namespace media

#endif  // MEDIA_CDM_AES_CBC_CRYPTO_H_