2016-09-13 13:23:11 +02:00
|
|
|
/* eslint max-nested-callbacks: ["error", 8] */
|
|
|
|
/* eslint-env mocha */
|
|
|
|
'use strict'
|
|
|
|
|
2017-03-21 15:05:22 +00:00
|
|
|
const chai = require('chai')
|
|
|
|
const dirtyChai = require('dirty-chai')
|
|
|
|
const expect = chai.expect
|
|
|
|
chai.use(dirtyChai)
|
2019-07-22 06:16:02 -04:00
|
|
|
const { expectErrCode } = require('../util')
|
2016-11-10 12:55:49 +01:00
|
|
|
|
2017-07-22 10:57:27 -07:00
|
|
|
const crypto = require('../../src')
|
|
|
|
const fixtures = require('./../fixtures/aes')
|
|
|
|
const goFixtures = require('./../fixtures/go-aes')
|
2016-09-13 13:23:11 +02:00
|
|
|
|
|
|
|
const bytes = {
|
|
|
|
16: 'AES-128',
|
|
|
|
32: 'AES-256'
|
|
|
|
}
|
|
|
|
|
2016-11-10 17:19:45 +01:00
|
|
|
describe('AES-CTR', () => {
|
2016-09-13 13:23:11 +02:00
|
|
|
Object.keys(bytes).forEach((byte) => {
|
2019-07-10 17:15:26 +01:00
|
|
|
it(`${bytes[byte]} - encrypt and decrypt`, async () => {
|
2017-01-16 05:17:50 +01:00
|
|
|
const key = Buffer.alloc(parseInt(byte, 10))
|
2016-09-13 13:23:11 +02:00
|
|
|
key.fill(5)
|
|
|
|
|
2017-01-16 05:17:50 +01:00
|
|
|
const iv = Buffer.alloc(16)
|
2016-09-13 13:23:11 +02:00
|
|
|
iv.fill(1)
|
|
|
|
|
2019-07-10 17:15:26 +01:00
|
|
|
const cipher = await crypto.aes.create(key, iv)
|
|
|
|
|
|
|
|
await encryptAndDecrypt(cipher)
|
|
|
|
await encryptAndDecrypt(cipher)
|
|
|
|
await encryptAndDecrypt(cipher)
|
|
|
|
await encryptAndDecrypt(cipher)
|
|
|
|
await encryptAndDecrypt(cipher)
|
2016-11-10 12:55:49 +01:00
|
|
|
})
|
|
|
|
})
|
2017-07-22 10:57:27 -07:00
|
|
|
|
2016-11-10 12:55:49 +01:00
|
|
|
Object.keys(bytes).forEach((byte) => {
|
2019-07-10 17:15:26 +01:00
|
|
|
it(`${bytes[byte]} - fixed - encrypt and decrypt`, async () => {
|
2017-01-16 05:17:50 +01:00
|
|
|
const key = Buffer.alloc(parseInt(byte, 10))
|
2016-11-10 12:55:49 +01:00
|
|
|
key.fill(5)
|
|
|
|
|
2017-01-16 05:17:50 +01:00
|
|
|
const iv = Buffer.alloc(16)
|
2016-11-10 12:55:49 +01:00
|
|
|
iv.fill(1)
|
|
|
|
|
2019-07-10 17:15:26 +01:00
|
|
|
const cipher = await crypto.aes.create(key, iv)
|
|
|
|
|
|
|
|
for (let i = 0; i < fixtures[byte].inputs.length; i++) {
|
|
|
|
const rawIn = fixtures[byte].inputs[i]
|
|
|
|
const input = Buffer.from(rawIn)
|
|
|
|
const output = Buffer.from(fixtures[byte].outputs[i])
|
|
|
|
const encrypted = await cipher.encrypt(input)
|
|
|
|
expect(encrypted).to.have.length(output.length)
|
|
|
|
expect(encrypted).to.eql(output)
|
|
|
|
const decrypted = await cipher.decrypt(encrypted)
|
|
|
|
expect(decrypted).to.eql(input)
|
|
|
|
}
|
2016-11-10 12:55:49 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Object.keys(bytes).forEach((byte) => {
|
|
|
|
if (!goFixtures[byte]) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-10 17:15:26 +01:00
|
|
|
it(`${bytes[byte]} - go interop - encrypt and decrypt`, async () => {
|
2017-01-16 05:17:50 +01:00
|
|
|
const key = Buffer.alloc(parseInt(byte, 10))
|
2016-11-10 12:55:49 +01:00
|
|
|
key.fill(5)
|
|
|
|
|
2017-01-16 05:17:50 +01:00
|
|
|
const iv = Buffer.alloc(16)
|
2016-11-10 12:55:49 +01:00
|
|
|
iv.fill(1)
|
|
|
|
|
2019-07-10 17:15:26 +01:00
|
|
|
const cipher = await crypto.aes.create(key, iv)
|
|
|
|
|
|
|
|
for (let i = 0; i < goFixtures[byte].inputs.length; i++) {
|
|
|
|
const rawIn = goFixtures[byte].inputs[i]
|
|
|
|
const input = Buffer.from(rawIn)
|
|
|
|
const output = Buffer.from(goFixtures[byte].outputs[i])
|
|
|
|
const encrypted = await cipher.encrypt(input)
|
|
|
|
expect(encrypted).to.have.length(output.length)
|
|
|
|
expect(encrypted).to.eql(output)
|
|
|
|
const decrypted = await cipher.decrypt(encrypted)
|
|
|
|
expect(decrypted).to.eql(input)
|
|
|
|
}
|
2016-09-13 13:23:11 +02:00
|
|
|
})
|
|
|
|
})
|
2019-07-22 06:16:02 -04:00
|
|
|
|
|
|
|
it('checks key length', () => {
|
|
|
|
const key = Buffer.alloc(5)
|
|
|
|
const iv = Buffer.alloc(16)
|
|
|
|
return expectErrCode(crypto.aes.create(key, iv), 'ERR_INVALID_KEY_LENGTH')
|
|
|
|
})
|
2016-09-13 13:23:11 +02:00
|
|
|
})
|
2016-11-10 12:55:49 +01:00
|
|
|
|
2019-07-10 17:15:26 +01:00
|
|
|
async function encryptAndDecrypt (cipher) {
|
2017-01-16 05:17:50 +01:00
|
|
|
const data = Buffer.alloc(100)
|
2016-11-10 12:55:49 +01:00
|
|
|
data.fill(Math.ceil(Math.random() * 100))
|
2019-07-10 17:15:26 +01:00
|
|
|
|
|
|
|
const encrypted = await cipher.encrypt(data)
|
|
|
|
const decrypted = await cipher.decrypt(encrypted)
|
|
|
|
|
|
|
|
expect(decrypted).to.be.eql(data)
|
2016-11-10 12:55:49 +01:00
|
|
|
}
|