Improve code quality and add decrypt status return value

This commit is contained in:
Jack Hadrill
2022-02-05 22:55:31 +00:00
parent 338cb017f2
commit 5a042757dd
10 changed files with 184 additions and 75 deletions

View File

@@ -1,21 +1,44 @@
import { decrypt } from '../src/decrypt'
test('Test buffers are supported by decrypt function.', () => {
test('Test nonce parsing by public decrypt function.', () => {
// Given
const ciphertext = Buffer.from([
// Nonce
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
// Ciphertext
225, 53, 3, 212, 22, 112, 246, 194, 61, 171, 230, 187, 157, 102, 32, 76, 62, 65,
25, 202, 255, 201, 206, 49, 60, 58, 82, 216, 72, 116, 106, 129, 162, 142, 69, 40,
167, 88, 94, 195, 174, 217, 242, 149, 224, 125, 196, 237, 172, 165, 116, 119, 128
])
const associatedData = Buffer.from('Some associated data.')
const nonce = Buffer.from('\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f')
const key = Buffer.from('\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f')
// When
const result = decrypt(ciphertext, associatedData, nonce, key)
const result = decrypt(ciphertext, associatedData, key)
// Then
const expectedResult = Buffer.from('Hello, World! This is a test message.')
expect(result).toMatchObject(expectedResult)
expect(result.success).toBe(true)
expect(result.plaintext).toMatchObject(expectedResult)
})
test('Test decryption with an invalid key.', () => {
// Given
const ciphertext = Buffer.from([
// Nonce
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
// Ciphertext
225, 53, 3, 212, 22, 112, 246, 194, 61, 171, 230, 187, 157, 102, 32, 76, 62, 65,
25, 202, 255, 201, 206, 49, 60, 58, 82, 216, 72, 116, 106, 129, 162, 142, 69, 40,
167, 88, 94, 195, 174, 217, 242, 149, 224, 125, 196, 237, 172, 165, 116, 119, 128
])
const associatedData = Buffer.from('Some associated data.')
const key = Buffer.from('\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x00')
// When
const result = decrypt(ciphertext, associatedData, key)
// Then
expect(result.success).toBe(false)
expect(result.plaintext).toMatchObject(Buffer.alloc(0))
})

View File

@@ -1,20 +1,17 @@
import { decrypt } from '../src/decrypt'
import { encrypt } from '../src/encrypt'
test('Test buffers are supported by encrypt function.', () => {
test('Test nonce generation by public encrypt function.', () => {
// Given
const message = Buffer.from('Hello, World! This is a test message.')
const associatedData = Buffer.from('Some associated data.')
const nonce = Buffer.from('\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f')
const key = Buffer.from('\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f')
// When
const result = encrypt(message, associatedData, nonce, key)
const ciphertext = encrypt(message, associatedData, key)
const plaintext = decrypt(ciphertext, associatedData, key)
// Then
const expectedResult = Buffer.from([
225, 53, 3, 212, 22, 112, 246, 194, 61, 171, 230, 187, 157, 102, 32, 76, 62, 65,
25, 202, 255, 201, 206, 49, 60, 58, 82, 216, 72, 116, 106, 129, 162, 142, 69, 40,
167, 88, 94, 195, 174, 217, 242, 149, 224, 125, 196, 237, 172, 165, 116, 119, 128
])
expect(result).toMatchObject(expectedResult)
expect(plaintext.success).toBe(true)
expect(plaintext.plaintext).toMatchObject(message)
})

View File

@@ -1,5 +1,5 @@
import { referenceTests } from './resources/reference-tests'
import { cryptoAeadDecrypt, cryptoAeadEncrypt } from '../src/romulus-m'
import { cryptoAeadDecrypt, cryptoAeadEncrypt, DecryptResult } from '../src/romulus-m'
function parseHexString (string: string): number[] {
const ret = []
@@ -24,6 +24,9 @@ test.each(referenceTests)('Perform decryption using reference test %#.', (key, n
const result = cryptoAeadDecrypt(parseHexString(ciphertext), parseHexString(associatedData), parseHexString(nonce), parseHexString(key))
// Then
const expectedResult = parseHexString(plaintext)
const expectedResult: DecryptResult = {
success: true,
plaintext: parseHexString(plaintext)
}
expect(result).toMatchObject(expectedResult)
})

View File

@@ -59,7 +59,8 @@ test('Decrypt a message with no associated data.', () => {
// Then
const expectedResult = stringToArray('Hello, World! This is a test message.')
expect(result).toMatchObject(expectedResult)
expect(result.success).toBe(true)
expect(result.plaintext).toMatchObject(expectedResult)
})
test('Decrypt a message with associated data.', () => {
@@ -78,5 +79,6 @@ test('Decrypt a message with associated data.', () => {
// Then
const expectedResult = stringToArray('Hello, World! This is a test message.')
expect(result).toMatchObject(expectedResult)
expect(result.success).toBe(true)
expect(result.plaintext).toMatchObject(expectedResult)
})