Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 deletions aes.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,41 +113,43 @@ class Aes {
*
* @private
*/
static shiftRows(s, Nb) {
const t = new Array(4);
for (let r=1; r<4; r++) {
for (let c=0; c<4; c++) t[c] = s[r][(c+r)%Nb]; // shift into temp copy
for (let c=0; c<4; c++) s[r][c] = t[c]; // and copy back
} // note that this will work for Nb=4,5,6, but not 7,8 (always 4 for AES):
return s; // see asmaes.sourceforge.net/rijndael/rijndaelImplementation.pdf
static shiftRows(s, Nb) {
const t = new Array(4);
for (let r = 1; r < 4; r++) {
for (let c = 0; c < 4; c++) t[c] = s[r][(c + r) % Nb];
for (let c = 0; c < 4; c++) s[r][c] = t[c];
}
return s;
}


/**
* Combine bytes of each col of state S [§5.1.3].
*
* @private
*/
static mixColumns(s, Nb) {
for (let c=0; c<Nb; c++) {
const a = new Array(Nb); // 'a' is a copy of the current column from 's'
const b = new Array(Nb); // 'b' is a•{02} in GF(2^8)
for (let r=0; r<4; r++) {
a[r] = s[r][c];
b[r] = s[r][c]&0x80 ? s[r][c]<<1 ^ 0x011b : s[r][c]<<1;
}
// a[n] ^ b[n] is a•{03} in GF(2^8)
s[0][c] = b[0] ^ a[1] ^ b[1] ^ a[2] ^ a[3]; // {02}•a0 + {03}•a1 + a2 + a3
s[1][c] = a[0] ^ b[1] ^ a[2] ^ b[2] ^ a[3]; // a0 • {02}•a1 + {03}•a2 + a3
s[2][c] = a[0] ^ a[1] ^ b[2] ^ a[3] ^ b[3]; // a0 + a1 + {02}•a2 + {03}•a3
s[3][c] = a[0] ^ b[0] ^ a[1] ^ a[2] ^ b[3]; // {03}•a0 + a1 + a2 + {02}•a3
static mixColumns(s, Nb) {
const a = new Array(4);
const b = new Array(4);

for (let c = 0; c < Nb; c++) {
for (let r = 0; r < 4; r++) {
a[r] = s[r][c];
b[r] = s[r][c] & 0x80 ? (s[r][c] << 1) ^ 0x011b : s[r][c] << 1;
}
return s;

s[0][c] = b[0] ^ a[1] ^ b[1] ^ a[2] ^ a[3];
s[1][c] = a[0] ^ b[1] ^ a[2] ^ b[2] ^ a[3];
s[2][c] = a[0] ^ a[1] ^ b[2] ^ a[3] ^ b[3];
s[3][c] = a[0] ^ b[0] ^ a[1] ^ a[2] ^ b[3];
}

return s;
}


/**
* Xor Round Key into state S [§5.1.4].
* Xor Round Key into state S [5.1.4].
*
* @private
*/
Expand Down
15 changes: 7 additions & 8 deletions sha512.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,12 @@ Sha512.Long = class {
return hi + lo;
}

add(that) { // addition modulo 2^64
const lo = this.lo + that.lo;
const hi = this.hi + that.hi + (lo>0x100000000 ? 1 : 0); // carry top bit if lo > 2^32

return new Sha512.Long(hi >>> 0, lo >>> 0);
}

add(that) {
const lo = this.lo + that.lo;
const carry = lo >= 0x100000000 ? 1 : 0;
const hi = this.hi + that.hi + carry;
return new Sha512.Long(hi >>> 0, lo >>> 0);
}
and(that) { // &
return new Sha512.Long(this.hi & that.hi, this.lo & that.lo);
}
Expand All @@ -257,7 +256,7 @@ Sha512.Long = class {
}

shr(n) { // >>>
if (n == 0) return this;
if (n == 0) return this; // no shit needed
if (n == 32) return new Sha512.Long(0, this.hi);
if (n > 32) return new Sha512.Long(0, this.hi >>> n-32);
/* n < 32 */ return new Sha512.Long(this.hi >>> n, this.lo >>> n | this.hi << (32-n));
Expand Down