Skip to content

Instantly share code, notes, and snippets.

@landonf
Created July 16, 2015 00:32
Show Gist options
  • Save landonf/174f7844fe7316314e36 to your computer and use it in GitHub Desktop.
Save landonf/174f7844fe7316314e36 to your computer and use it in GitHub Desktop.
/**
* @internal
*
* Create a Codec instance for VDEFileItem records.
*/
static Codec<VDEFileItem> fileItemCodec () {
using namespace codecs;
const auto varLengthBytes = eager(variableSizeBytes(uint32L, identityBytes));
const auto vdeRecordVersion = (
("compat_version" | codecs::uint8 ) &
("feature_version" | codecs::uint8 )
).as<VDERecordVersion>();
const auto vdeSection = (
("offset" | uint64L ) &
("length" | uint64L )
).as<VDESectionRecord>();
const auto vdeHeader = (
("magic" | vdeFileMagicCodec ) >>
("file_version" | vdeRecordVersion ) &
("vde_encrypted_sect" | vdeSection ) &
("vde_session_sect" | vdeSection )
).as<VDEFileHeader>();
const auto pbkdf2Params = (
("iters" | uint32L ) &
("salt" | varLengthBytes )
).as<VDESessionMasterKeyParameters>();
// TODO: Until we generalize Codec::as(), we have to lift the single Codec to a TupledCodec here
const auto hkdfParams = TupledCodec<std::vector<uint8_t>>(
("salt" | varLengthBytes )
).as<VDESessionSubkeyParameters>();
const auto keyParams = (
("master_key_params" | pbkdf2Params ) &
("sub_key_params" | hkdfParams )
).as<VDESessionKeyParameters>();
const auto vdeSession = (
("version" | vdeRecordVersion ) &
("key_params" | keyParams ) &
("dpk" | varLengthBytes )
).as<VDESession>();
return (
("vde_header" | vdeHeader ) >>= [vdeSession](const VDEFileHeader &hdr) {
auto padding1Length = hdr.encryptedSection().offset() - VDE_FILE_HEADER_SIZE;
auto encryptedLength = hdr.encryptedSection().length();
auto padding2Length = hdr.sessionSection().offset() - (hdr.encryptedSection().offset() + encryptedLength);
auto sessionLength = hdr.sessionSection().length();
return (
("padding_1" | ignore(size_t(padding1Length)) ) >>
("encrypted_data" | eager(bytes(size_t(encryptedLength))) ) &
("padding_2" | ignore(size_t(padding2Length)) ) >>
("vde_session" | fixedSizeBytes(size_t(sessionLength), vdeSession) )
);
}
).as<VDEFileItem>();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment