@stablelib/ctr
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -31,2 +31,22 @@ // Copyright (C) 2016 Dmitry Chestnykh | ||
it("should produce correct stream", () => { | ||
const cipher = new AES(decode(v.key)); | ||
const ctr = new CTR(cipher, decode(v.iv)); | ||
const dst1 = new Uint8Array(decode(v.dst1).length); | ||
const src1 = decode(v.src1); | ||
ctr.stream(dst1); | ||
for (let i = 0; i < dst1.length; i++ ) { | ||
dst1[i] ^= src1[i] | ||
} | ||
expect(encode(dst1)).toBe(v.dst1); | ||
// Continue the same stream. | ||
const dst2 = new Uint8Array(decode(v.dst2).length); | ||
const src2 = decode(v.src2); | ||
ctr.stream(dst2); | ||
for (let i = 0; i < dst2.length; i++ ) { | ||
dst2[i] ^= src2[i] | ||
} | ||
expect(encode(dst2)).toBe(v.dst2); | ||
}); | ||
it("should generate succession when calling multiple times", () => { | ||
@@ -33,0 +53,0 @@ const cipher = new AES(decode(v.key)); |
@@ -83,3 +83,3 @@ // Copyright (C) 2016 Dmitry Chestnykh | ||
for (let i = 0; i < dst.length; i++) { | ||
if (this._bufpos === this._counter.length) { | ||
if (this._bufpos === this._buffer.length) { | ||
this.fillBuffer(); | ||
@@ -86,0 +86,0 @@ } |
@@ -5,13 +5,13 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var aes_1 = require("@stablelib/aes"); | ||
var benchmark_1 = require("@stablelib/benchmark"); | ||
var ctr_1 = require("./ctr"); | ||
var buf8192 = benchmark_1.byteSeq(8192); | ||
var buf1111 = benchmark_1.byteSeq(1111); | ||
var key = benchmark_1.byteSeq(32); | ||
var iv = new Uint8Array(16); | ||
var cipher = new aes_1.AES(key); | ||
var ctr = new ctr_1.CTR(cipher, iv); | ||
benchmark_1.report("AES-CTR 8K", benchmark_1.benchmark(function () { return ctr.streamXOR(buf8192, buf8192); }, buf8192.length)); | ||
benchmark_1.report("AES-CTR 1111", benchmark_1.benchmark(function () { return ctr.streamXOR(buf1111, buf1111); }, buf1111.length)); | ||
const aes_1 = require("@stablelib/aes"); | ||
const benchmark_1 = require("@stablelib/benchmark"); | ||
const ctr_1 = require("./ctr"); | ||
const buf8192 = (0, benchmark_1.byteSeq)(8192); | ||
const buf1111 = (0, benchmark_1.byteSeq)(1111); | ||
const key = (0, benchmark_1.byteSeq)(32); | ||
const iv = new Uint8Array(16); | ||
const cipher = new aes_1.AES(key); | ||
const ctr = new ctr_1.CTR(cipher, iv); | ||
(0, benchmark_1.report)("AES-CTR 8K", (0, benchmark_1.benchmark)(() => ctr.streamXOR(buf8192, buf8192), buf8192.length)); | ||
(0, benchmark_1.report)("AES-CTR 1111", (0, benchmark_1.benchmark)(() => ctr.streamXOR(buf1111, buf1111), buf1111.length)); | ||
//# sourceMappingURL=ctr.bench.js.map |
@@ -5,3 +5,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var wipe_1 = require("@stablelib/wipe"); | ||
exports.CTR = void 0; | ||
const wipe_1 = require("@stablelib/wipe"); | ||
/** | ||
@@ -13,4 +14,4 @@ * CTR implements counter cipher mode. | ||
*/ | ||
var CTR = /** @class */ (function () { | ||
function CTR(cipher, iv) { | ||
class CTR { | ||
constructor(cipher, iv) { | ||
this._bufpos = 0; | ||
@@ -25,3 +26,3 @@ // Allocate space for counter. | ||
} | ||
CTR.prototype.setCipher = function (cipher, iv) { | ||
setCipher(cipher, iv) { | ||
// Reset this._cipher to prevent reusing the existing one, | ||
@@ -41,6 +42,6 @@ // if this method throws. | ||
return this; | ||
}; | ||
CTR.prototype.clean = function () { | ||
wipe_1.wipe(this._buffer); | ||
wipe_1.wipe(this._counter); | ||
} | ||
clean() { | ||
(0, wipe_1.wipe)(this._buffer); | ||
(0, wipe_1.wipe)(this._counter); | ||
this._bufpos = this._buffer.length; | ||
@@ -51,10 +52,10 @@ // Cleaning cipher is caller's responsibility, | ||
return this; | ||
}; | ||
CTR.prototype.fillBuffer = function () { | ||
} | ||
fillBuffer() { | ||
this._cipher.encryptBlock(this._counter, this._buffer); | ||
this._bufpos = 0; | ||
incrementCounter(this._counter); | ||
}; | ||
CTR.prototype.streamXOR = function (src, dst) { | ||
for (var i = 0; i < src.length; i++) { | ||
} | ||
streamXOR(src, dst) { | ||
for (let i = 0; i < src.length; i++) { | ||
if (this._bufpos === this._buffer.length) { | ||
@@ -65,6 +66,6 @@ this.fillBuffer(); | ||
} | ||
}; | ||
CTR.prototype.stream = function (dst) { | ||
for (var i = 0; i < dst.length; i++) { | ||
if (this._bufpos === this._counter.length) { | ||
} | ||
stream(dst) { | ||
for (let i = 0; i < dst.length; i++) { | ||
if (this._bufpos === this._buffer.length) { | ||
this.fillBuffer(); | ||
@@ -74,9 +75,8 @@ } | ||
} | ||
}; | ||
return CTR; | ||
}()); | ||
} | ||
} | ||
exports.CTR = CTR; | ||
function incrementCounter(counter) { | ||
var carry = 1; | ||
for (var i = counter.length - 1; i >= 0; i--) { | ||
let carry = 1; | ||
for (let i = counter.length - 1; i >= 0; i--) { | ||
carry = carry + (counter[i] & 0xff) | 0; | ||
@@ -83,0 +83,0 @@ counter[i] = carry & 0xff; |
@@ -5,7 +5,7 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var aes_1 = require("@stablelib/aes"); | ||
var hex_1 = require("@stablelib/hex"); | ||
var ctr_1 = require("./ctr"); | ||
describe("AES-CTR", function () { | ||
var v = { | ||
const aes_1 = require("@stablelib/aes"); | ||
const hex_1 = require("@stablelib/hex"); | ||
const ctr_1 = require("./ctr"); | ||
describe("AES-CTR", () => { | ||
const v = { | ||
key: "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F", | ||
@@ -18,27 +18,46 @@ iv: "202122232425262728292A2B2C2D2E2F", | ||
}; | ||
it("should correctly encrypt", function () { | ||
var cipher = new aes_1.AES(hex_1.decode(v.key)); | ||
var ctr = new ctr_1.CTR(cipher, hex_1.decode(v.iv)); | ||
var dst1 = new Uint8Array(hex_1.decode(v.dst1).length); | ||
ctr.streamXOR(hex_1.decode(v.src1), dst1); | ||
expect(hex_1.encode(dst1)).toBe(v.dst1); | ||
it("should correctly encrypt", () => { | ||
const cipher = new aes_1.AES((0, hex_1.decode)(v.key)); | ||
const ctr = new ctr_1.CTR(cipher, (0, hex_1.decode)(v.iv)); | ||
const dst1 = new Uint8Array((0, hex_1.decode)(v.dst1).length); | ||
ctr.streamXOR((0, hex_1.decode)(v.src1), dst1); | ||
expect((0, hex_1.encode)(dst1)).toBe(v.dst1); | ||
// Continue the same stream. | ||
var dst2 = new Uint8Array(hex_1.decode(v.dst2).length); | ||
ctr.streamXOR(hex_1.decode(v.src2), dst2); | ||
expect(hex_1.encode(dst2)).toBe(v.dst2); | ||
const dst2 = new Uint8Array((0, hex_1.decode)(v.dst2).length); | ||
ctr.streamXOR((0, hex_1.decode)(v.src2), dst2); | ||
expect((0, hex_1.encode)(dst2)).toBe(v.dst2); | ||
}); | ||
it("should generate succession when calling multiple times", function () { | ||
var cipher = new aes_1.AES(hex_1.decode(v.key)); | ||
var dst1 = new Uint8Array(100); | ||
var dst2 = new Uint8Array(dst1.length); | ||
it("should produce correct stream", () => { | ||
const cipher = new aes_1.AES((0, hex_1.decode)(v.key)); | ||
const ctr = new ctr_1.CTR(cipher, (0, hex_1.decode)(v.iv)); | ||
const dst1 = new Uint8Array((0, hex_1.decode)(v.dst1).length); | ||
const src1 = (0, hex_1.decode)(v.src1); | ||
ctr.stream(dst1); | ||
for (let i = 0; i < dst1.length; i++) { | ||
dst1[i] ^= src1[i]; | ||
} | ||
expect((0, hex_1.encode)(dst1)).toBe(v.dst1); | ||
// Continue the same stream. | ||
const dst2 = new Uint8Array((0, hex_1.decode)(v.dst2).length); | ||
const src2 = (0, hex_1.decode)(v.src2); | ||
ctr.stream(dst2); | ||
for (let i = 0; i < dst2.length; i++) { | ||
dst2[i] ^= src2[i]; | ||
} | ||
expect((0, hex_1.encode)(dst2)).toBe(v.dst2); | ||
}); | ||
it("should generate succession when calling multiple times", () => { | ||
const cipher = new aes_1.AES((0, hex_1.decode)(v.key)); | ||
const dst1 = new Uint8Array(100); | ||
const dst2 = new Uint8Array(dst1.length); | ||
// full-length | ||
var ctr1 = new ctr_1.CTR(cipher, hex_1.decode(v.iv)); | ||
const ctr1 = new ctr_1.CTR(cipher, (0, hex_1.decode)(v.iv)); | ||
ctr1.stream(dst1); | ||
// partial | ||
var ctr2 = new ctr_1.CTR(cipher, hex_1.decode(v.iv)); | ||
const ctr2 = new ctr_1.CTR(cipher, (0, hex_1.decode)(v.iv)); | ||
ctr2.stream(dst2.subarray(0, 50)); | ||
ctr2.stream(dst2.subarray(50)); | ||
expect(hex_1.encode(dst2)).toEqual(hex_1.encode(dst1)); | ||
expect((0, hex_1.encode)(dst2)).toEqual((0, hex_1.encode)(dst1)); | ||
}); | ||
}); | ||
//# sourceMappingURL=ctr.test.js.map |
{ | ||
"name": "@stablelib/ctr", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Counter block cipher mode (CTR)", | ||
@@ -30,3 +30,3 @@ "main": "./lib/ctr.js", | ||
}, | ||
"gitHead": "03dadf27703120d54e6be8436525228ee1c4299b" | ||
"gitHead": "a402dc74f45c6a93a777a0e2840ce50ba68c3010" | ||
} |
{ | ||
"extends": "../../configs/tsconfig.json", | ||
"compilerOptions": { | ||
"target": "es5", | ||
"module": "commonjs", | ||
"strict": true, | ||
"noUnusedParameters": true, | ||
"noImplicitReturns": true, | ||
"noUnusedLocals": true, | ||
"removeComments": false, | ||
"preserveConstEnums": true, | ||
"moduleResolution": "node", | ||
"newLine": "LF", | ||
"sourceMap": true, | ||
"declaration": true, | ||
"outDir": "lib", | ||
"lib": [ | ||
"es5", | ||
"es2015.promise", | ||
"dom", | ||
"scripthost" | ||
] | ||
}, | ||
@@ -23,0 +6,0 @@ "exclude": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21156
347