diff --git a/packages/crypto/src/EncryptionFormats/EncryptionFormats.ts b/packages/crypto/src/EncryptionFormats/EncryptionFormats.ts index f58a073b..fb62a50b 100644 --- a/packages/crypto/src/EncryptionFormats/EncryptionFormats.ts +++ b/packages/crypto/src/EncryptionFormats/EncryptionFormats.ts @@ -1,3 +1,4 @@ +import sodium from 'libsodium-wrappers'; import { InvalidArgument } from '@tanker/errors'; import { EncryptionV1 } from './v1'; @@ -50,8 +51,10 @@ export const extractEncryptionFormat = (encryptedData: Uint8Array) => { const encryption = encryptionFormats[version]; - if (!encryption) - throw new InvalidArgument(`Unhandled format version ${version} used in encryptedData`); + if (!encryption) { + const headerHex = sodium.to_hex(encryptedData.slice(0, 5)); + throw new InvalidArgument(`Unhandled format version ${version} used in encryptedData. Header starts with: 0x${headerHex}`); + } if (encryptedData.length < encryption.overhead) throw new InvalidArgument(`Truncated encrypted data. Length should be at least ${encryption.overhead} with encryption format v${encryption.version}`); diff --git a/packages/crypto/src/__tests__/types.spec.ts b/packages/crypto/src/__tests__/types.spec.ts index 1320944a..435393c8 100644 --- a/packages/crypto/src/__tests__/types.spec.ts +++ b/packages/crypto/src/__tests__/types.spec.ts @@ -52,6 +52,11 @@ describe('Resource', () => { expect(() => extractEncryptionFormat(incorrectVersion)).to.throw(InvalidArgument); }); + it('should return the first 5 bytes as hex when unsupported format version is detected', () => { + const elf = new Uint8Array([0x7F, 0x45, 0x4C, 0x46, 0x2, 0x1, 0x1]); + expect(() => extractEncryptionFormat(elf)).to.throw('Header starts with: 0x7f454c4602'); + }); + configs.forEach(({ version, testVector }) => { it(`should detect a buffer v${version}`, () => { const resource = utils.concatArrays(new Uint8Array([version]), testVector);