Keyblob.h
/*
* File: Keyblob.h
*
* Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
* See included license file for license details.
*/
#if !defined(_Keyblob_h_)
#define _Keyblob_h_
#include "smart_ptr.h"
#include "OptionContext.h"
#include "DataTarget.h"
#include "AESKey.h"
#include "AESCounter.h"
#define kKeyblobOptionNameStart "start"
#define kKeyblobOptionNameEnd "end"
#define kKeyblobOptionNameKey "key"
#define kKeyblobOptionNameCounter "counter"
namespace elftosb
{
class Keyblob
{
public:
typedef std::vector<OptionContext *> option_vector_t;
enum _encryption_constants
{
k_encryptBlockSize = 16,
k_qspiAlignlength =
512
};
public:
Keyblob()
: m_id(0)
, m_options(0)
{
}
Keyblob(uint32_t identifier)
: m_id(identifier)
, m_options(0)
{
}
virtual ~Keyblob() {}
static uint32_t getKekSizeBytes() { return kKekSizeBytes; }
void setIdentifier(uint32_t identifier) { m_id = identifier; }
uint32_t getIdentifier() const { return m_id; }
inline void addOptions(OptionContext *context) { m_options.push_back(context); }
inline option_vector_t *getOptions() { return &m_options; }
uint8_t *createWrappedKeyblobData(uint8_t *kek, uint32_t *byteCount);
bool encryptMatchingRange(uint32_t start, uint32_t len, uint8_t *data);
protected:
enum _keywrap_constants
{
kKeySizeBits = 128,
kExpandedKekSizeInts = 64,
kNumKeyBlobs = 4,
kKeyBlobSizeBytes = 64,
kOutputLineSize = 16,
kRegionAddrMask = (0x400 - 1),
kFlagMask = 0x07,
kKeyFlags = 0x03,
kAesKeySizeBytes = 16,
kCtrSizeBytes = 8,
kKekSizeBytes = 16,
kNumKeyChars = 32,
kNumCtrChars = 16,
kCrc32SizeBytes = 32
};
typedef struct KeyBlob
{
unsigned char key[kAesKeySizeBytes]; // 16 bytes, 128-bits, KEY[A15...A00]
unsigned char ctr[kCtrSizeBytes]; // 8 bytes, 64-bits, CTR[C7...C0]
unsigned int srtaddr; // region start, STRADDR[31 - 10]
unsigned int endaddr; // region end, ENDADDR[31 - 10]
// end of 32-byte area covered by CRC
unsigned int zero_fill; // zeros
unsigned int key_blob_crc32; // crc32 over 1st 32-bytes
// end of 40 byte (5*64-bit) key blob data
unsigned char expanded_wrap_data[8]; // 8 bytes, used for wrap expanded data
// end of 48 byte (6*64-bit) wrap data
unsigned char unused_filler[16]; // unused fill to 64 bytes
} keyblob_t;
void populateKeyBlob(keyblob_t *blob, uint32_t start, uint32_t end, const char *keyHex, const char *counterHex);
void encrypt(uint32_t length, uint8_t *data, AESKey<128> *key, AESCounter<128> *counter);
bool getOptionValues(OptionContext &opt, const char **key, const char **ctr, uint32_t *start, uint32_t *end);
uint32_t m_id;
option_vector_t m_options;
};
}; // namespace elftosb
#endif // _Keyblob_h_
elftosb::Keyblob::kExpandedKekSizeInts
Expanded KeK size.
Definition: Keyblob.h:79
elftosb::Keyblob::encryptMatchingRange
bool encryptMatchingRange(uint32_t start, uint32_t len, uint8_t *data)
Encrypt for OTFAD if range is within a keyblob entry range.
Definition: Keyblob.cpp:135
elftosb::Keyblob::kKekSizeBytes
Number of bytes in KeK.
Definition: Keyblob.h:88
elftosb::Keyblob::kNumCtrChars
Number of characters in ctr string.
Definition: Keyblob.h:90
elftosb::Keyblob::kCtrSizeBytes
Number of bytes in Ctr.
Definition: Keyblob.h:87
elftosb::Keyblob::kKeySizeBits
AES-128.
Definition: Keyblob.h:78
elftosb::Keyblob::_keywrap_constants
_keywrap_constants
Keywrap constants.
Definition: Keyblob.h:76
elftosb::Keyblob::k_encryptBlockSize
Block size for AES-128 encryption.
Definition: Keyblob.h:35
elftosb::Keyblob::option_vector_t
std::vector< OptionContext * > option_vector_t
List of options entries.
Definition: Keyblob.h:30
keyblob_t
Key Blob format.
Definition: Keyblob.h:95
elftosb::Keyblob::kFlagMask
Key flags mask.
Definition: Keyblob.h:84
elftosb
Definition: BootImage.h:13
elftosb::Keyblob::kNumKeyBlobs
Number of key blobs.
Definition: Keyblob.h:80
elftosb::Keyblob::k_qspiAlignlength
Definition: Keyblob.h:36
elftosb::Keyblob::kNumKeyChars
Number of characters in key string.
Definition: Keyblob.h:89
elftosb::Keyblob::addOptions
void addOptions(OptionContext *context)
Add options.
Definition: Keyblob.h:57
elftosb::OptionContext
Pure abstract interface class to a table of options.
Definition: apps/elftosb/common/OptionContext.h:18
AESCounter< 128 >
elftosb::Keyblob::kKeyFlags
Default flags: RO=0, ADE=1, VLD=1.
Definition: Keyblob.h:85
elftosb::Keyblob
Keyblob specification.
Definition: Keyblob.h:27
elftosb::Keyblob::kCrc32SizeBytes
Number of bytes covered by CRC32.
Definition: Keyblob.h:91
AESKey< 128 >
elftosb::Keyblob::kOutputLineSize
Number of bytes per output line.
Definition: Keyblob.h:82
elftosb::Keyblob::kRegionAddrMask
Region addresses are modulo 1024.
Definition: Keyblob.h:83
elftosb::Keyblob::kAesKeySizeBytes
Number of bytes in AES-128 key.
Definition: Keyblob.h:86
elftosb::Keyblob::createWrappedKeyblobData
uint8_t * createWrappedKeyblobData(uint8_t *kek, uint32_t *byteCount)
Create and wrap keyblob data based on options.
Definition: Keyblob.cpp:225
elftosb::Keyblob::m_options
option_vector_t m_options
List of option entries.
Definition: Keyblob.h:115
elftosb::Keyblob::kKeyBlobSizeBytes
Key blob size in bytes.
Definition: Keyblob.h:81
elftosb::Keyblob::getOptions
option_vector_t * getOptions()
Return options vector.
Definition: Keyblob.h:59
elftosb::Keyblob::m_id
uint32_t m_id
Unique identifier.
Definition: Keyblob.h:114
elftosb::Keyblob::_encryption_constants
_encryption_constants
Constants for encryption.
Definition: Keyblob.h:33