2022-08-26 15:44:59 +00:00
|
|
|
// Copyright 2016 Google LLC. All rights reserved.
|
2016-04-06 00:19:16 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
|
|
|
#ifndef PACKAGER_MEDIA_BASE_AES_CRYPTOR_H_
|
|
|
|
#define PACKAGER_MEDIA_BASE_AES_CRYPTOR_H_
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
#include <memory>
|
2016-04-06 00:19:16 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2023-10-09 23:21:41 +00:00
|
|
|
#include <mbedtls/cipher.h>
|
2022-11-02 15:34:06 +00:00
|
|
|
#include "packager/macros.h"
|
2016-04-08 18:41:17 +00:00
|
|
|
#include "packager/media/base/fourccs.h"
|
2016-04-06 00:19:16 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2016-04-06 00:19:16 +00:00
|
|
|
namespace media {
|
|
|
|
|
|
|
|
// AES cryptor interface. Inherited by various AES encryptor and decryptor
|
|
|
|
// implementations.
|
|
|
|
class AesCryptor {
|
|
|
|
public:
|
2016-04-13 17:52:41 +00:00
|
|
|
enum ConstantIvFlag {
|
|
|
|
kUseConstantIv,
|
|
|
|
kDontUseConstantIv,
|
|
|
|
};
|
|
|
|
|
|
|
|
/// @param constant_iv_flag indicates whether a constant iv is used,
|
|
|
|
/// kUseConstantIv means that the same iv is used for all Crypt calls
|
|
|
|
/// until iv is changed via SetIv; otherwise, iv can be incremented
|
|
|
|
/// (for counter mode) or chained (for cipher block chaining mode)
|
|
|
|
/// internally inside Crypt call, i.e. iv will be updated across Crypt
|
|
|
|
/// calls.
|
|
|
|
explicit AesCryptor(ConstantIvFlag constant_iv_flag);
|
2016-04-06 00:19:16 +00:00
|
|
|
virtual ~AesCryptor();
|
|
|
|
|
|
|
|
/// Initialize the cryptor with specified key and IV.
|
|
|
|
/// @return true on successful initialization, false otherwise.
|
|
|
|
virtual bool InitializeWithIv(const std::vector<uint8_t>& key,
|
|
|
|
const std::vector<uint8_t>& iv) = 0;
|
|
|
|
|
2023-08-31 23:59:46 +00:00
|
|
|
virtual size_t RequiredOutputSize(size_t plaintext_size) {
|
2023-07-14 15:19:01 +00:00
|
|
|
return plaintext_size;
|
|
|
|
}
|
|
|
|
|
2016-04-06 00:19:16 +00:00
|
|
|
/// @name Various forms of crypt (Encrypt/Decrypt) calls.
|
|
|
|
/// It is an Encrypt function for encryptor and a Decrypt function for
|
|
|
|
/// decryptor. The text and crypt_text pointers can be the same address for
|
|
|
|
/// in place encryption/decryption.
|
|
|
|
/// @{
|
|
|
|
bool Crypt(const std::vector<uint8_t>& text,
|
|
|
|
std::vector<uint8_t>* crypt_text);
|
|
|
|
bool Crypt(const std::string& text, std::string* crypt_text);
|
|
|
|
/// @param crypt_text should have at least @a text_size bytes.
|
|
|
|
bool Crypt(const uint8_t* text, size_t text_size, uint8_t* crypt_text) {
|
|
|
|
size_t crypt_text_size = text_size;
|
2016-04-13 17:52:41 +00:00
|
|
|
return Crypt(text, text_size, crypt_text, &crypt_text_size);
|
|
|
|
}
|
|
|
|
bool Crypt(const uint8_t* text,
|
|
|
|
size_t text_size,
|
|
|
|
uint8_t* crypt_text,
|
|
|
|
size_t* crypt_text_size) {
|
|
|
|
if (constant_iv_flag_ == kUseConstantIv)
|
|
|
|
SetIvInternal();
|
|
|
|
else
|
|
|
|
num_crypt_bytes_ += text_size;
|
|
|
|
return CryptInternal(text, text_size, crypt_text, crypt_text_size);
|
2016-04-06 00:19:16 +00:00
|
|
|
}
|
|
|
|
/// @}
|
|
|
|
|
2018-01-21 20:21:01 +00:00
|
|
|
/// Set IV. SetIv() implementation guarantees that the iv passed to SetIv()
|
|
|
|
/// is set to iv() and then calls SetIvInternal().
|
2016-04-06 00:19:16 +00:00
|
|
|
/// @return true if successful, false if the input is invalid.
|
2016-04-13 17:52:41 +00:00
|
|
|
bool SetIv(const std::vector<uint8_t>& iv);
|
2016-04-06 00:19:16 +00:00
|
|
|
|
|
|
|
/// Update IV for next sample. As recommended in ISO/IEC 23001-7: IV need to
|
|
|
|
/// be updated per sample for CENC.
|
2016-04-13 17:52:41 +00:00
|
|
|
/// This is used by encryptors only. It is a NOP if using kUseConstantIv.
|
|
|
|
void UpdateIv();
|
2016-04-06 00:19:16 +00:00
|
|
|
|
|
|
|
/// @return The current iv.
|
|
|
|
const std::vector<uint8_t>& iv() const { return iv_; }
|
|
|
|
|
2016-05-26 22:44:37 +00:00
|
|
|
/// @return true if constant iv is used, false otherwise.
|
|
|
|
bool use_constant_iv() const { return constant_iv_flag_ == kUseConstantIv; }
|
|
|
|
|
2016-04-08 18:41:17 +00:00
|
|
|
/// @param protection_scheme specifies the protection scheme: 'cenc', 'cens',
|
|
|
|
/// 'cbc1', 'cbcs', which is useful to determine the random iv size.
|
|
|
|
/// @param iv points to generated initialization vector.
|
|
|
|
/// @return true on success, false otherwise.
|
|
|
|
static bool GenerateRandomIv(FourCC protection_scheme,
|
|
|
|
std::vector<uint8_t>* iv);
|
|
|
|
|
2016-04-06 00:19:16 +00:00
|
|
|
protected:
|
2022-11-02 15:34:06 +00:00
|
|
|
enum CipherMode {
|
|
|
|
kCtrMode,
|
|
|
|
kCbcMode,
|
|
|
|
};
|
|
|
|
|
|
|
|
// mbedTLS cipher context.
|
|
|
|
mbedtls_cipher_context_t cipher_ctx_;
|
|
|
|
|
|
|
|
bool SetupCipher(size_t key_size, CipherMode mode);
|
2016-04-06 00:19:16 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Internal implementation of crypt function.
|
|
|
|
// |text| points to the input text.
|
|
|
|
// |text_size| is the size of input text.
|
|
|
|
// |crypt_text| points to the output encrypted or decrypted text, depends on
|
|
|
|
// whether it is an encryption or decryption. |text| and |crypt_text| can
|
|
|
|
// point to the same address for in place encryption/decryption.
|
|
|
|
// |crypt_text_size| contains the size of |crypt_text| and it will be updated
|
|
|
|
// to contain the actual encrypted/decrypted size for |crypt_text| on success.
|
|
|
|
// Return false if the input |crypt_text_size| is not large enough to hold the
|
|
|
|
// output |crypt_text| or if there is any error in encryption/decryption.
|
|
|
|
virtual bool CryptInternal(const uint8_t* text,
|
|
|
|
size_t text_size,
|
|
|
|
uint8_t* crypt_text,
|
|
|
|
size_t* crypt_text_size) = 0;
|
|
|
|
|
2016-04-13 17:52:41 +00:00
|
|
|
// Internal implementation of SetIv, which setup internal iv.
|
|
|
|
virtual void SetIvInternal() = 0;
|
|
|
|
|
2016-04-06 00:19:16 +00:00
|
|
|
// |size| specifies the input text size.
|
|
|
|
// Return the number of padding bytes needed.
|
|
|
|
// Note: No paddings should be needed except for pkcs5-cbc encryptor.
|
|
|
|
virtual size_t NumPaddingBytes(size_t size) const;
|
|
|
|
|
2016-04-13 17:52:41 +00:00
|
|
|
// Indicates whether a constant iv is used. Internal iv will be reset to
|
|
|
|
// |iv_| before calling Crypt if that is the case.
|
|
|
|
const ConstantIvFlag constant_iv_flag_;
|
|
|
|
// Initialization vector from by SetIv or InitializeWithIv, with size 8 or 16
|
|
|
|
// bytes.
|
|
|
|
std::vector<uint8_t> iv_;
|
|
|
|
// Tracks number of crypt bytes. It is used to calculate how many blocks
|
|
|
|
// should iv advance in UpdateIv(). It will be reset to 0 after iv is updated.
|
|
|
|
size_t num_crypt_bytes_;
|
|
|
|
|
2016-04-06 00:19:16 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(AesCryptor);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|
2016-04-06 00:19:16 +00:00
|
|
|
|
|
|
|
#endif // PACKAGER_MEDIA_BASE_AES_CRYPTOR_H_
|