Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const material = await cmm.getEncryptionMaterials(encryptionRequest)
const { kdfGetSubtleEncrypt, subtleSign, dispose } = await getEncryptHelper(material)
const messageId = await backend.randomValues(MESSAGE_ID_LENGTH)
const { id, ivLength } = material.suite
const messageHeader: MessageHeader = {
version: SerializationVersion.V1,
type: ObjectType.CUSTOMER_AE_DATA,
suiteId: id,
messageId,
encryptionContext: material.encryptionContext,
encryptedDataKeys: material.encryptedDataKeys,
contentType: ContentType.FRAMED_DATA,
headerIvLength: ivLength,
frameLength
}
const header = serialize.serializeMessageHeader(messageHeader)
const info = kdfInfo(id, messageId)
const getSubtleEncrypt = kdfGetSubtleEncrypt(info)
const headerAuthIv = serialize.headerAuthIv(ivLength)
const headerAuthTag = await getSubtleEncrypt(headerAuthIv, header)(new Uint8Array(0))
const numberOfFrames = Math.ceil(plaintextLength / frameLength)
/* The final frame has a variable length.
* The value needs to be known, but should only be calculated once.
* So I calculate how much of a frame I should have at the end.
*/
export function getEncryptionInfo (material : NodeEncryptionMaterial, frameLength: number) {
const { kdfGetCipher } = getEncryptHelper(material)
const { encryptionContext } = material
const messageId = randomBytes(MESSAGE_ID_LENGTH)
const { id, ivLength } = material.suite
const messageHeader: MessageHeader = Object.freeze({
version: SerializationVersion.V1,
type: ObjectType.CUSTOMER_AE_DATA,
suiteId: id,
messageId,
encryptionContext,
encryptedDataKeys: Object.freeze(material.encryptedDataKeys), // freeze me please
contentType: ContentType.FRAMED_DATA,
headerIvLength: ivLength,
frameLength
})
const { buffer, byteOffset, byteLength } = serializeMessageHeader(messageHeader)
const headerBuffer = Buffer.from(buffer, byteOffset, byteLength)
const info = kdfInfo(messageHeader.suiteId, messageHeader.messageId)
const getCipher = kdfGetCipher(info)
const headerIv = headerAuthIv(ivLength)
const validateHeader = getCipher(headerIv)
validateHeader.setAAD(headerBuffer)
validateHeader.update(Buffer.alloc(0))
validateHeader.final()
const headerAuth = validateHeader.getAuthTag()
return {