[Pkg-javascript-commits] [node-browserify-aes] 25/43: aes: isolate cryptBlock from this
Bastien Roucariès
rouca at moszumanska.debian.org
Thu Sep 7 14:41:59 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to branch master
in repository node-browserify-aes.
commit 266d6af75ce9c5eaa0f733e17b154b28d74ec234
Author: Daniel Cousens <github at dcousens.com>
Date: Fri May 19 13:21:53 2017 +1000
aes: isolate cryptBlock from this
---
aes.js | 100 ++++++++++++++++++++++++++++++++---------------------------------
1 file changed, 49 insertions(+), 51 deletions(-)
diff --git a/aes.js b/aes.js
index 5e7e77f..2f805de 100644
--- a/aes.js
+++ b/aes.js
@@ -1,9 +1,10 @@
// based on the aes implimentation in triple sec
// https://github.com/keybase/triplesec
-
// which is in turn based on the one from crypto-js
// https://code.google.com/p/crypto-js/
+var Buffer = require('safe-buffer').Buffer
+
function asUInt32Array (buf) {
if (!Buffer.isBuffer(buf)) buf = Buffer.from(buf)
@@ -23,6 +24,39 @@ function scrubVec (v) {
}
}
+function cryptBlock (M, keySchedule, SUB_MIX, SBOX, nRounds) {
+ var s0 = M[0] ^ keySchedule[0]
+ var s1 = M[1] ^ keySchedule[1]
+ var s2 = M[2] ^ keySchedule[2]
+ var s3 = M[3] ^ keySchedule[3]
+ var ksRow = 4
+
+ var t0, t1, t2, t3
+ for (var round = 1; round < nRounds; round++) {
+ t0 = SUB_MIX[0][s0 >>> 24] ^ SUB_MIX[1][(s1 >>> 16) & 0xff] ^ SUB_MIX[2][(s2 >>> 8) & 0xff] ^ SUB_MIX[3][s3 & 0xff] ^ keySchedule[ksRow++]
+ t1 = SUB_MIX[0][s1 >>> 24] ^ SUB_MIX[1][(s2 >>> 16) & 0xff] ^ SUB_MIX[2][(s3 >>> 8) & 0xff] ^ SUB_MIX[3][s0 & 0xff] ^ keySchedule[ksRow++]
+ t2 = SUB_MIX[0][s2 >>> 24] ^ SUB_MIX[1][(s3 >>> 16) & 0xff] ^ SUB_MIX[2][(s0 >>> 8) & 0xff] ^ SUB_MIX[3][s1 & 0xff] ^ keySchedule[ksRow++]
+ t3 = SUB_MIX[0][s3 >>> 24] ^ SUB_MIX[1][(s0 >>> 16) & 0xff] ^ SUB_MIX[2][(s1 >>> 8) & 0xff] ^ SUB_MIX[3][s2 & 0xff] ^ keySchedule[ksRow++]
+ s0 = t0
+ s1 = t1
+ s2 = t2
+ s3 = t3
+ }
+
+ t0 = ((SBOX[s0 >>> 24] << 24) | (SBOX[(s1 >>> 16) & 0xff] << 16) | (SBOX[(s2 >>> 8) & 0xff] << 8) | SBOX[s3 & 0xff]) ^ keySchedule[ksRow++]
+ t1 = ((SBOX[s1 >>> 24] << 24) | (SBOX[(s2 >>> 16) & 0xff] << 16) | (SBOX[(s3 >>> 8) & 0xff] << 8) | SBOX[s0 & 0xff]) ^ keySchedule[ksRow++]
+ t2 = ((SBOX[s2 >>> 24] << 24) | (SBOX[(s3 >>> 16) & 0xff] << 16) | (SBOX[(s0 >>> 8) & 0xff] << 8) | SBOX[s1 & 0xff]) ^ keySchedule[ksRow++]
+ t3 = ((SBOX[s3 >>> 24] << 24) | (SBOX[(s0 >>> 16) & 0xff] << 16) | (SBOX[(s1 >>> 8) & 0xff] << 8) | SBOX[s2 & 0xff]) ^ keySchedule[ksRow++]
+
+ return [
+ t0 >>> 0,
+ t1 >>> 0,
+ t2 >>> 0,
+ t3 >>> 0
+ ]
+}
+
+// AES constants
var RCON = [0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36]
var G = (function () {
// Compute double table
@@ -85,25 +119,20 @@ var G = (function () {
}
})()
-AES.blockSize = 4 * 4
-
-AES.prototype.blockSize = AES.blockSize
-
-AES.keySize = 256 / 8
-
-AES.prototype.keySize = AES.keySize
-
function AES (key) {
this._key = asUInt32Array(key)
- this._doReset()
+ this._reset()
}
-AES.prototype._doReset = function () {
- var keySize, keyWords, ksRows
- keyWords = this._key
- keySize = keyWords.length
- this._nRounds = keySize + 6
- ksRows = (this._nRounds + 1) * 4
+AES.blockSize = 4 * 4
+AES.keySize = 256 / 8
+AES.prototype.blockSize = AES.blockSize
+AES.prototype.keySize = AES.keySize
+AES.prototype._reset = function () {
+ var keyWords = this._key
+ var keySize = keyWords.length
+ var nRounds = keySize + 6
+ var ksRows = (nRounds + 1) * 4
var keySchedule = []
for (var k = 0; k < keySize; k++) {
@@ -149,14 +178,14 @@ AES.prototype._doReset = function () {
}
}
+ this._nRounds = nRounds
this._keySchedule = keySchedule
this._invKeySchedule = invKeySchedule
- return true
}
AES.prototype.encryptBlock = function (M) {
M = asUInt32Array(M)
- var out = this._doCryptBlock(M, this._keySchedule, G.SUB_MIX, G.SBOX)
+ var out = cryptBlock(M, this._keySchedule, G.SUB_MIX, G.SBOX, this._nRounds)
var buf = Buffer.allocUnsafe(16)
buf.writeUInt32BE(out[0], 0)
buf.writeUInt32BE(out[1], 4)
@@ -173,7 +202,7 @@ AES.prototype.decryptBlock = function (M) {
M[1] = M[3]
M[3] = m1
- var out = this._doCryptBlock(M, this._invKeySchedule, G.INV_SUB_MIX, G.INV_SBOX)
+ var out = cryptBlock(M, this._invKeySchedule, G.INV_SUB_MIX, G.INV_SBOX, this._nRounds)
var buf = Buffer.allocUnsafe(16)
buf.writeUInt32BE(out[0], 0)
buf.writeUInt32BE(out[3], 4)
@@ -188,35 +217,4 @@ AES.prototype.scrub = function () {
scrubVec(this._key)
}
-AES.prototype._doCryptBlock = function (M, keySchedule, SUB_MIX, SBOX) {
- var ksRow, s0, s1, s2, s3, t0, t1, t2, t3
-
- s0 = M[0] ^ keySchedule[0]
- s1 = M[1] ^ keySchedule[1]
- s2 = M[2] ^ keySchedule[2]
- s3 = M[3] ^ keySchedule[3]
- ksRow = 4
- for (var round = 1; round < this._nRounds; round++) {
- t0 = SUB_MIX[0][s0 >>> 24] ^ SUB_MIX[1][(s1 >>> 16) & 0xff] ^ SUB_MIX[2][(s2 >>> 8) & 0xff] ^ SUB_MIX[3][s3 & 0xff] ^ keySchedule[ksRow++]
- t1 = SUB_MIX[0][s1 >>> 24] ^ SUB_MIX[1][(s2 >>> 16) & 0xff] ^ SUB_MIX[2][(s3 >>> 8) & 0xff] ^ SUB_MIX[3][s0 & 0xff] ^ keySchedule[ksRow++]
- t2 = SUB_MIX[0][s2 >>> 24] ^ SUB_MIX[1][(s3 >>> 16) & 0xff] ^ SUB_MIX[2][(s0 >>> 8) & 0xff] ^ SUB_MIX[3][s1 & 0xff] ^ keySchedule[ksRow++]
- t3 = SUB_MIX[0][s3 >>> 24] ^ SUB_MIX[1][(s0 >>> 16) & 0xff] ^ SUB_MIX[2][(s1 >>> 8) & 0xff] ^ SUB_MIX[3][s2 & 0xff] ^ keySchedule[ksRow++]
- s0 = t0
- s1 = t1
- s2 = t2
- s3 = t3
- }
- t0 = ((SBOX[s0 >>> 24] << 24) | (SBOX[(s1 >>> 16) & 0xff] << 16) | (SBOX[(s2 >>> 8) & 0xff] << 8) | SBOX[s3 & 0xff]) ^ keySchedule[ksRow++]
- t1 = ((SBOX[s1 >>> 24] << 24) | (SBOX[(s2 >>> 16) & 0xff] << 16) | (SBOX[(s3 >>> 8) & 0xff] << 8) | SBOX[s0 & 0xff]) ^ keySchedule[ksRow++]
- t2 = ((SBOX[s2 >>> 24] << 24) | (SBOX[(s3 >>> 16) & 0xff] << 16) | (SBOX[(s0 >>> 8) & 0xff] << 8) | SBOX[s1 & 0xff]) ^ keySchedule[ksRow++]
- t3 = ((SBOX[s3 >>> 24] << 24) | (SBOX[(s0 >>> 16) & 0xff] << 16) | (SBOX[(s1 >>> 8) & 0xff] << 8) | SBOX[s2 & 0xff]) ^ keySchedule[ksRow++]
-
- return [
- t0 >>> 0,
- t1 >>> 0,
- t2 >>> 0,
- t3 >>> 0
- ]
-}
-
-exports.AES = AES
+module.exports.AES = AES
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-browserify-aes.git
More information about the Pkg-javascript-commits
mailing list