Skip to content

Commit

Permalink
fix final
Browse files Browse the repository at this point in the history
  • Loading branch information
toyobayashi committed Aug 24, 2023
1 parent 532a966 commit ca8e0c5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
32 changes: 16 additions & 16 deletions wasm/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,24 @@ Cipher.prototype.update = function (data) {
};

Cipher.prototype.final = function () {
if (this.remain.length === 0) {
return new Uint8Array([
159, 59, 117, 4, 146, 111,
139, 211, 110, 49, 24, 233,
3, 164, 205, 74
]);
}

if (!this.autoPadding && ((this.remain.length % 16) !== 0)) {
throw new Error('wrong final block length')
var data;
if (!this.autoPadding) {
if ((this.remain.length % 16) !== 0) {
throw new Error('wrong final block length');
}
if (this.remain.length === 0) {
return new Uint8Array(0);
}
data = this.remain;
} else {
data = new Uint8Array(16);
data.set(this.remain);
var pad = 16 - this.remain.length;
for (var i = this.remain.length; i < 16; ++i) {
data[i] = pad;
}
}

var data = new Uint8Array(16);
data.set(this.remain);
var pad = 16 - this.remain.length;
for (var i = this.remain.length; i < 16; ++i) {
data[i] = pad;
}
var source = Module._malloc(16);
if (!source) throw new Error('malloc failed');
Module.HEAPU8.set(data, source);
Expand Down
30 changes: 21 additions & 9 deletions wasm/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,38 @@ function testAutoPadding (input, key) {
const expected = new Uint8Array(nodeAes.update(input))
assert.deepStrictEqual(actual, expected);

assert.throws(() => {
aes.final()
}, /wrong final block length/)

assert.throws(() => {
nodeAes.final()
}, /wrong final block length/)
if (input.length % 16 === 0) {
const actualFinal = aes.final()
const expectedFinal = new Uint8Array(nodeAes.final())
assert.deepStrictEqual(actualFinal, expectedFinal)
} else {
assert.throws(() => {
aes.final()
})

assert.throws(() => {
nodeAes.final()
})
}

aes.destroy()
nodeAes.destroy()
}

wzwasm.default({}).then(() => {
test(new Uint8Array(Array.from({ length: 0 }, (_, k) => k)), key)
test(new Uint8Array(Array.from({ length: 7 }, (_, k) => k)), key)
test(new Uint8Array(Array.from({ length: 16 }, (_, k) => k)), key)
test(new Uint8Array(Array.from({ length: 31 }, (_, k) => k)), key)
test(new Uint8Array(Array.from({ length: 32 }, (_, k) => k)), key)
test(new Uint8Array(Array.from({ length: 126 }, (_, k) => k)), key)
test(new Uint8Array(Array.from({ length: 256 }, (_, k) => k)), key)
test(new Uint8Array(Array.from({ length: 65536 }, (_, k) => k)), key)
test(new Uint8Array(Array.from({ length: 999999 }, (_, k) => k)), key)
testAutoPadding(new Uint8Array(Array.from({ length: 0 }, (_, k) => k)), key)
testAutoPadding(new Uint8Array(Array.from({ length: 7 }, (_, k) => k)), key)
testAutoPadding(new Uint8Array(Array.from({ length: 16 }, (_, k) => k)), key)
testAutoPadding(new Uint8Array(Array.from({ length: 31 }, (_, k) => k)), key)
testAutoPadding(new Uint8Array(Array.from({ length: 32 }, (_, k) => k)), key)
testAutoPadding(new Uint8Array(Array.from({ length: 65536 }, (_, k) => k)), key)
testAutoPadding(new Uint8Array(Array.from({ length: 999999 }, (_, k) => k)), key)
console.log('all test passed')
})

0 comments on commit ca8e0c5

Please sign in to comment.