-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypt.js
130 lines (112 loc) · 3.25 KB
/
crypt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* Copyright (c) 2016, Markus Peloquin <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED 'AS IS' AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
'use strict';
const Crypt = (() => {
class Crypt {
/** Create a block encrypter
* \param cipher The Cipher object to en/decrypt with
* \param mode One of Crypt.MODE_*
*/
constructor(cipher, mode) {
this._cipher = cipher;
this._mode = mode;
}
/** Encrypt data
* \param plaintext Data to encrypt
* \param sz Size of plaintext in bytes
* \returns The encrypted data (size could be rounded up)
*/
encrypt(plaintext, sz) {
switch (this._mode) {
case Crypt.MODE_CTR:
return _ctr(this._cipher, plaintext, true, sz);
default:
throw 'No such block mode';
}
}
/** Decrypt data
* \param ciphertext Data to decrypt
* \param sz Size of plaintext in bytes
* \returns The decrypted data
*/
decrypt(ciphertext, sz) {
switch (this._mode) {
case Crypt.MODE_CTR:
return _ctr(this._cipher, ciphertext, false, sz);
default:
throw 'No such block mode';
}
}
}
Crypt.MODE_CTR = 0; /**< Counter block mode */
function _ctr(cipher, text, encrypt, sz) {
const szBlock = cipher.blockSize();
const szBlock4 = szBlock >> 2;
let iv;
let pos;
const res = [];
if (encrypt) {
iv = ByteArray.generateRandom(szBlock);
pos = 0;
for (let i = 0; i != szBlock; i += 4)
res.push(iv.get32(i >>> 2));
} else {
iv = text;
pos = szBlock4;
}
const blocks = (sz / szBlock) ^ 0;
const left = sz % szBlock;
const pre = new ByteArray(szBlock);
const tmp = new ByteArray(szBlock);
// copy all but last 4 bytes from 'iv' to 'pre'
for (let i = 0; i < szBlock4 - 1; i++)
pre.set32(i, iv.get32(i));
// copy last 4 bytes to 'ivTail'
const ivTail = iv.get32(szBlock4 - 1);
// encrypt whole blocks
for (let i = 0; i < blocks; i++) {
// in effect, pre = iv XOR counter
pre.set32(szBlock4 - 1, i ^ ivTail);
cipher.encrypt(pre, tmp);
for (let j = 0; j < szBlock4; j++)
res.push(tmp.get32(j) ^ text.get32(pos + j));
pos += szBlock4;
}
// encrypt partial block
if (left) {
pre.set32(szBlock4 - 1, blocks ^ ivTail);
cipher.encrypt(pre, tmp);
let i;
const left4 = left >> 2;
for (i = 0; i < left4; i++)
res.push(tmp.get32(i) ^ text.get32(pos + i));
if (left & 3) {
const pos8 = (pos + i) << 2;
let x = tmp.get(pos8) << 24;
let mask = 0xff000000;
if (left & 3 > 1) {
x |= tmp.get(pos8 + 1) << 16;
mask = 0xffff0000;
}
if (left & 3 > 2) {
x |= tmp.get(pos8 + 2) << 8;
mask = 0xffffff00;
}
res.push((tmp.get32(i) ^ x) & mask);
}
}
return new ByteArray(res, sz + (encrypt ? iv.size() : 0));
}
return Crypt;
})();