Move a couple of AESBaseCipher fields from the constructors

Thanks to modern JS class features we can, ever so slightly, shorten some class field definitions.
This commit is contained in:
Jonas Jenwald 2025-03-08 15:56:39 +01:00
parent 461488cb16
commit c65f1b0dae

View File

@ -90,15 +90,7 @@ class NullCipher {
} }
class AESBaseCipher { class AESBaseCipher {
constructor() { _s = new Uint8Array([
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
this.constructor === AESBaseCipher
) {
unreachable("Cannot initialize AESBaseCipher.");
}
this._s = new Uint8Array([
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b,
0xfe, 0xd7, 0xab, 0x76, 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xfe, 0xd7, 0xab, 0x76, 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0,
0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26,
@ -123,7 +115,7 @@ class AESBaseCipher {
0xb0, 0x54, 0xbb, 0x16, 0xb0, 0x54, 0xbb, 0x16,
]); ]);
this._inv_s = new Uint8Array([ _inv_s = new Uint8Array([
0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e,
0x81, 0xf3, 0xd7, 0xfb, 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x81, 0xf3, 0xd7, 0xfb, 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87,
0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, 0x54, 0x7b, 0x94, 0x32, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, 0x54, 0x7b, 0x94, 0x32,
@ -148,7 +140,7 @@ class AESBaseCipher {
0x55, 0x21, 0x0c, 0x7d, 0x55, 0x21, 0x0c, 0x7d,
]); ]);
this._mix = new Uint32Array([ _mix = new Uint32Array([
0x00000000, 0x0e090d0b, 0x1c121a16, 0x121b171d, 0x3824342c, 0x362d3927, 0x00000000, 0x0e090d0b, 0x1c121a16, 0x121b171d, 0x3824342c, 0x362d3927,
0x24362e3a, 0x2a3f2331, 0x70486858, 0x7e416553, 0x6c5a724e, 0x62537f45, 0x24362e3a, 0x2a3f2331, 0x70486858, 0x7e416553, 0x6c5a724e, 0x62537f45,
0x486c5c74, 0x4665517f, 0x547e4662, 0x5a774b69, 0xe090d0b0, 0xee99ddbb, 0x486c5c74, 0x4665517f, 0x547e4662, 0x5a774b69, 0xe090d0b0, 0xee99ddbb,
@ -194,11 +186,17 @@ class AESBaseCipher {
0x9f5d80be, 0x91548db5, 0x834f9aa8, 0x8d4697a3, 0x9f5d80be, 0x91548db5, 0x834f9aa8, 0x8d4697a3,
]); ]);
this._mixCol = new Uint8Array(256); _mixCol = new Uint8Array(256).map((_, i) =>
for (let i = 0; i < 256; i++) { i < 128 ? i << 1 : (i << 1) ^ 0x1b
this._mixCol[i] = i < 128 ? i << 1 : (i << 1) ^ 0x1b; );
}
constructor() {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
this.constructor === AESBaseCipher
) {
unreachable("Cannot initialize AESBaseCipher.");
}
this.buffer = new Uint8Array(16); this.buffer = new Uint8Array(16);
this.bufferPosition = 0; this.bufferPosition = 0;
} }
@ -469,9 +467,7 @@ class AESBaseCipher {
bufferLength = this.bufferPosition; bufferLength = this.bufferPosition;
const result = []; const result = [];
if (!iv) { iv ||= new Uint8Array(16);
iv = new Uint8Array(16);
}
for (let i = 0; i < sourceLength; ++i) { for (let i = 0; i < sourceLength; ++i) {
buffer[bufferLength] = data[i]; buffer[bufferLength] = data[i];
++bufferLength; ++bufferLength;
@ -508,13 +504,7 @@ class AESBaseCipher {
} }
class AES128Cipher extends AESBaseCipher { class AES128Cipher extends AESBaseCipher {
constructor(key) { _rcon = new Uint8Array([
super();
this._cyclesOfRepetition = 10;
this._keySize = 160; // bits
this._rcon = new Uint8Array([
0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c,
0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a,
0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd,
@ -539,6 +529,12 @@ class AES128Cipher extends AESBaseCipher {
0x74, 0xe8, 0xcb, 0x8d, 0x74, 0xe8, 0xcb, 0x8d,
]); ]);
constructor(key) {
super();
this._cyclesOfRepetition = 10;
this._keySize = 160; // bits
this._key = this._expandKey(key); this._key = this._expandKey(key);
} }