Skip to content

Commit

Permalink
lib: avoid using the node.js buffer pool.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed May 12, 2020
1 parent 5a3853b commit e84fde4
Show file tree
Hide file tree
Showing 43 changed files with 119 additions and 126 deletions.
4 changes: 2 additions & 2 deletions lib/cshake.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class CSHAKE extends Keccak {
if (n === 0)
n = 1;

const buf = Buffer.allocUnsafe(n + 1);
const buf = Buffer.alloc(n + 1);

for (let i = 1; i <= n; i++)
buf[i] = x >>> (8 * (n - i));
Expand All @@ -156,7 +156,7 @@ class CSHAKE extends Keccak {
if (n === 0)
n = 1;

const buf = Buffer.allocUnsafe(n + 1);
const buf = Buffer.alloc(n + 1);

for (let i = 1; i <= n; i++)
buf[i - 1] = x >>> (8 * (n - i));
Expand Down
5 changes: 3 additions & 2 deletions lib/encoding/asn1.js
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ class Integer extends Node {
fromNumber(num) {
assert(Number.isSafeInteger(num));

const buf = Buffer.allocUnsafe(8);
const buf = Buffer.alloc(8);

let neg = false;

Expand Down Expand Up @@ -1051,7 +1051,8 @@ class BitString extends Node {
if (shift === 8 || data.length === 0)
return data;

const out = Buffer.allocUnsafe(data.length);
const out = Buffer.alloc(data.length);

out[0] = data[0] >>> shift;

for (let i = 1; i < data.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion lib/encoding/pem.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ function crc24(data) {

crc &= 0xffffff;

const buf = Buffer.allocUnsafe(3);
const buf = Buffer.alloc(3);

buf[2] = crc;
crc >>>= 8;
Expand Down
9 changes: 3 additions & 6 deletions lib/encoding/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,9 @@ function padLeft(data, size) {
if (data.length > size)
throw new RangeError(`Buffer expected to be ${size} bytes in size.`);

const out = Buffer.allocUnsafe(size);
const left = size - data.length;
const out = Buffer.alloc(size, 0x00);

out.fill(0x00, 0, left);
data.copy(out, left);
data.copy(out, size - data.length);

return out;
}
Expand All @@ -179,10 +177,9 @@ function padRight(data, size) {
if (data.length > size)
throw new RangeError(`Buffer expected to be ${size} bytes in size.`);

const out = Buffer.allocUnsafe(size);
const out = Buffer.alloc(size, 0x00);

data.copy(out, 0);
out.fill(0x00, data.length, size);

return out;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/hmac.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class HMAC {
}

// Pad key
const pad = Buffer.allocUnsafe(this.size);
const pad = Buffer.alloc(this.size);

for (let i = 0; i < key.length; i++)
pad[i] = key[i] ^ 0x36;
Expand Down
20 changes: 10 additions & 10 deletions lib/js/aead.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ const assert = require('../internal/assert');
const ChaCha20 = require('./chacha20');
const Poly1305 = require('./poly1305');

/*
* Constants
*/

const zero16 = Buffer.alloc(16, 0x00);

/**
* AEAD
*/
Expand Down Expand Up @@ -43,7 +49,7 @@ class AEAD {
assert(Buffer.isBuffer(key));
assert(Buffer.isBuffer(iv));

this.key.fill(0x00);
this.key.fill(0);
this.chacha.init(key, iv, 0);
this.chacha.encrypt(this.key);
this.poly.init(this.key);
Expand Down Expand Up @@ -161,7 +167,7 @@ class AEAD {
if (this.mode === -1)
throw new Error('Context is not initialized.');

const len = Buffer.allocUnsafe(16);
const len = Buffer.alloc(16);

writeU64(len, this.aadLen, 0);
writeU64(len, this.cipherLen, 8);
Expand Down Expand Up @@ -226,14 +232,8 @@ class AEAD {
_pad16(size) {
const pos = size & 15;

if (pos === 0)
return;

const pad = Buffer.allocUnsafe(16 - pos);

pad.fill(0x00);

this.poly.update(pad);
if (pos > 0)
this.poly.update(zero16.slice(0, 16 - pos));
}

/**
Expand Down
10 changes: 4 additions & 6 deletions lib/js/base58.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ const TABLE = [
55, 56, 57, -1, -1, -1, -1, -1
];

const pool = Buffer.alloc(128);

/**
* Encode a base58 string.
* @param {Buffer} data
Expand All @@ -62,9 +64,7 @@ function encode(data) {
}

const size = (((data.length - zeroes) * 138 / 100) | 0) + 1;
const b58 = Buffer.allocUnsafe(size);

b58.fill(0);
const b58 = size <= 128 ? pool.fill(0) : Buffer.alloc(size);

let length = 0;

Expand Down Expand Up @@ -123,9 +123,7 @@ function decode(str) {
}

const size = ((str.length * 733) / 1000 | 0) + 1;
const b256 = Buffer.allocUnsafe(size);

b256.fill(0);
const b256 = size <= 128 ? pool.fill(0) : Buffer.alloc(size);

let length = 0;

Expand Down
4 changes: 2 additions & 2 deletions lib/js/blake2b.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class BLAKE2b {
this.state = new Uint32Array(16);
this.V = new Uint32Array(32);
this.M = new Uint32Array(32);
this.block = Buffer.allocUnsafe(128);
this.block = Buffer.alloc(128);
this.size = 32;
this.count = 0;
this.pos = FINALIZED;
Expand Down Expand Up @@ -153,7 +153,7 @@ class BLAKE2b {
this._compress(this.block, 0, true);
this.pos = FINALIZED;

const out = Buffer.allocUnsafe(this.size);
const out = Buffer.alloc(this.size);

for (let i = 0; i < this.size; i++)
out[i] = this.state[i >>> 2] >>> (8 * (i & 3));
Expand Down
4 changes: 2 additions & 2 deletions lib/js/blake2s.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class BLAKE2s {
this.state = new Uint32Array(8);
this.V = new Uint32Array(16);
this.M = new Uint32Array(16);
this.block = Buffer.allocUnsafe(64);
this.block = Buffer.alloc(64);
this.size = 32;
this.count = 0;
this.pos = FINALIZED;
Expand Down Expand Up @@ -147,7 +147,7 @@ class BLAKE2s {
this._compress(this.block, 0, true);
this.pos = FINALIZED;

const out = Buffer.allocUnsafe(this.size);
const out = Buffer.alloc(this.size);

for (let i = 0; i < this.size; i++)
out[i] = this.state[i >>> 2] >>> (8 * (i & 3));
Expand Down
8 changes: 4 additions & 4 deletions lib/js/ctr-drbg.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class CtrDRBG {
seed[i] ^= pers[i];
}

this.slab.fill(0x00);
this.slab.fill(0);
this.ctr.init(this.K, this.V);
this.update(seed);
this.rounds = 1;
Expand Down Expand Up @@ -143,7 +143,7 @@ class CtrDRBG {
}

const blocks = Math.ceil(len / this.blkSize);
const out = Buffer.allocUnsafeSlow(blocks * this.blkSize);
const out = Buffer.alloc(blocks * this.blkSize);

for (let i = 0; i < blocks; i++)
this.ctr.encrypt(out, i * this.blkSize);
Expand Down Expand Up @@ -171,7 +171,7 @@ class CtrDRBG {
if (seed.length > this.entSize)
throw new Error('Seed is too long.');

this.slab.fill(0x00);
this.slab.fill(0);

for (let i = 0; i < this.entSize; i += this.blkSize)
this.ctr.encrypt(this.slab, i);
Expand Down Expand Up @@ -232,7 +232,7 @@ class CtrDRBG {
const ctx = new AES(this.bits).init(K);

for (let i = 0; i < blocks; i++) {
chain.fill(0x00);
chain.fill(0);

writeU32(S, i, 0);

Expand Down
4 changes: 2 additions & 2 deletions lib/js/elliptic.js
Original file line number Diff line number Diff line change
Expand Up @@ -4318,7 +4318,7 @@ class ShortPoint extends Point {

// Compressed form (0x02 = even, 0x03 = odd).
if (compact) {
const p = Buffer.allocUnsafe(1 + fieldSize);
const p = Buffer.alloc(1 + fieldSize);
const x = this.curve.encodeField(this.getX());

p[0] = 0x02 | this.y.redIsOdd();
Expand All @@ -4328,7 +4328,7 @@ class ShortPoint extends Point {
}

// Uncompressed form (0x04).
const p = Buffer.allocUnsafe(1 + fieldSize * 2);
const p = Buffer.alloc(1 + fieldSize * 2);
const x = this.curve.encodeField(this.getX());
const y = this.curve.encodeField(this.getY());

Expand Down
33 changes: 17 additions & 16 deletions lib/js/gost94.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ const S_S2015 = [
class GOST94 {
constructor() {
this.S = S_CRYPTOPRO;
this.state = Buffer.allocUnsafe(32);
this.sigma = Buffer.allocUnsafe(32);
this.block = Buffer.allocUnsafe(32);
this.state = Buffer.alloc(32);
this.sigma = Buffer.alloc(32);
this.block = Buffer.alloc(32);
this.size = FINALIZED;
}

Expand All @@ -87,8 +87,8 @@ class GOST94 {
assert(Array.isArray(box) && box.length === 8);

this.S = box;
this.state.fill(0x00);
this.sigma.fill(0x00);
this.state.fill(0);
this.sigma.fill(0);
this.size = 0;

return this;
Expand All @@ -101,7 +101,7 @@ class GOST94 {
}

final() {
return this._final(Buffer.allocUnsafe(32));
return this._final(Buffer.alloc(32));
}

_update(data, len) {
Expand Down Expand Up @@ -160,10 +160,11 @@ class GOST94 {

this.state.copy(out, 0);

this.state.fill(0x00);
this.sigma.fill(0x00);
this.block.fill(0x00);
DESC.fill(0x00, 0, 8);
this.state.fill(0);
this.sigma.fill(0);
this.block.fill(0);

DESC.fill(0, 0, 8);

this.size = FINALIZED;

Expand All @@ -178,7 +179,7 @@ class GOST94 {
}

_shuffle(m, s) {
const res = Buffer.allocUnsafe(32);
const res = Buffer.alloc(32);
s.copy(res, 0);

for (let i = 0; i < 12; i++)
Expand All @@ -193,7 +194,7 @@ class GOST94 {
}

_f(m) {
const s = Buffer.allocUnsafe(32);
const s = Buffer.alloc(32);

this.state.copy(s, 0);

Expand Down Expand Up @@ -331,7 +332,7 @@ function encrypt(msg, pos, key, sbox) {
}

function X(a, b) {
const out = Buffer.allocUnsafe(32);
const out = Buffer.alloc(32);

for (let i = 0; i < 32; i++)
out[i] = a[i] ^ b[i];
Expand All @@ -345,7 +346,7 @@ function XM(a, b) {
}

function A(x) {
const out = Buffer.allocUnsafe(32);
const out = Buffer.alloc(32);

x.copy(out, 0, 8, 32);

Expand All @@ -356,7 +357,7 @@ function A(x) {
}

function P(y) {
const out = Buffer.allocUnsafe(32);
const out = Buffer.alloc(32);

for (let i = 0; i < 4; i++) {
for (let k = 0; k < 8; k++)
Expand All @@ -367,7 +368,7 @@ function P(y) {
}

function psi(block) {
const out = Buffer.allocUnsafe(32);
const out = Buffer.alloc(32);

block.copy(out, 0, 2, 32);
block.copy(out, 30, 0, 2);
Expand Down
12 changes: 6 additions & 6 deletions lib/js/hash-drbg.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ class HashDRBG {
this.minEntropy = hash.id === 'SHA1' ? 10 : 24;
this.seedLen = hash.size <= 32 ? 55 : 111;

this.V = Buffer.allocUnsafe(this.seedLen);
this.C = Buffer.allocUnsafe(this.seedLen);
this.len = Buffer.allocUnsafe(8);
this.V = Buffer.alloc(this.seedLen);
this.C = Buffer.alloc(this.seedLen);
this.len = Buffer.alloc(8);
this.rounds = 0;

if (entropy)
Expand Down Expand Up @@ -106,7 +106,7 @@ class HashDRBG {

const data = Buffer.from(this.V);
const blocks = Math.ceil(len / this.hash.size);
const out = Buffer.allocUnsafeSlow(blocks * this.hash.size);
const out = Buffer.alloc(blocks * this.hash.size);

for (let i = 0; i < blocks; i++) {
this.hash.digest(data).copy(out, i * this.hash.size);
Expand Down Expand Up @@ -152,7 +152,7 @@ class HashDRBG {
assert((prepend & 0xff) === prepend);

const pre = (prepend !== 0xff) | 0;
const data = Buffer.allocUnsafe(5 + pre + input.length);
const data = Buffer.alloc(5 + pre + input.length);

data[0] = 0x01;
data[1] = len >>> 21;
Expand All @@ -166,7 +166,7 @@ class HashDRBG {
input.copy(data, 5 + pre);

const blocks = Math.ceil(len / this.hash.size);
const out = Buffer.allocUnsafeSlow(blocks * this.hash.size);
const out = Buffer.alloc(blocks * this.hash.size);

for (let i = 0; i < blocks; i++) {
this.hash.digest(data).copy(out, i * this.hash.size);
Expand Down
Loading

0 comments on commit e84fde4

Please sign in to comment.