|
1 | 1 | /* |
2 | | - * Copyright 2019 NEM |
| 2 | + * Copyright 2020 NEM |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 |
|
17 | | -import { WalletAlgorithm } from '../../model/wallet/WalletAlgorithm'; |
18 | 17 | import { Convert as convert } from '../format/Convert'; |
19 | 18 | import { KeyPair } from './KeyPair'; |
20 | 19 | import * as utility from './Utilities'; |
| 20 | + |
21 | 21 | // eslint-disable-next-line @typescript-eslint/no-var-requires |
22 | 22 | const CryptoJS = require('crypto-js'); |
| 23 | + |
23 | 24 | export class Crypto { |
24 | 25 | /** |
25 | | - * Encrypt a private key for mobile apps (AES_PBKF2) |
26 | | - * |
27 | | - * @param {string} password - A wallet password |
28 | | - * @param {string} privateKey - An account private key |
29 | | - * |
30 | | - * @return {object} - The encrypted data |
| 26 | + * Encrypt data |
| 27 | + * @param {string} data |
| 28 | + * @param {string} salt |
| 29 | + * @param {string} password |
31 | 30 | */ |
32 | | - public static toMobileKey = (password: string, privateKey: string): any => { |
33 | | - // Errors |
34 | | - if (!password || !privateKey) { |
35 | | - throw new Error('Missing argument !'); |
36 | | - } |
37 | | - // Processing |
38 | | - const salt = CryptoJS.lib.WordArray.random(256 / 8); |
| 31 | + public static encrypt(data: string, password: string): string { |
| 32 | + const salt = CryptoJS.lib.WordArray.random(16); |
| 33 | + |
| 34 | + // generate password based key |
39 | 35 | const key = CryptoJS.PBKDF2(password, salt, { |
40 | | - keySize: 256 / 32, |
41 | | - iterations: 2000, |
| 36 | + keySize: 8, |
| 37 | + iterations: 1024, |
42 | 38 | }); |
43 | | - const iv = Crypto.randomBytes(16); |
44 | | - const encIv = { |
45 | | - iv: utility.ua2words(iv, 16), |
46 | | - }; |
47 | | - const encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Hex.parse(privateKey), key, encIv); |
48 | | - // Result |
49 | | - return { |
50 | | - encrypted: convert.uint8ToHex(iv) + encrypted.ciphertext, |
51 | | - salt: salt.toString(), |
52 | | - }; |
53 | | - }; |
54 | 39 |
|
55 | | - /** |
56 | | - * Derive a private key from a password using count iterations of SHA3-256 |
57 | | - * |
58 | | - * @param {string} password - A wallet password |
59 | | - * @param {number} count - A number of iterations above 0 |
60 | | - * |
61 | | - * @return {object} - The derived private key |
62 | | - */ |
63 | | - public static derivePassSha = (password: string, count: number): any => { |
64 | | - // Errors |
65 | | - if (!password) { |
66 | | - throw new Error('Missing argument !'); |
67 | | - } |
68 | | - if (!count || count <= 0) { |
69 | | - throw new Error('Please provide a count number above 0'); |
70 | | - } |
71 | | - // Processing |
72 | | - let data = password; |
73 | | - for (let i = 0; i < count; ++i) { |
74 | | - data = CryptoJS.SHA3(data, { |
75 | | - outputLength: 256, |
76 | | - }); |
77 | | - } |
78 | | - // Result |
79 | | - return { |
80 | | - priv: CryptoJS.enc.Hex.stringify(data), |
81 | | - }; |
82 | | - }; |
| 40 | + // encrypt using random IV |
| 41 | + const iv = CryptoJS.lib.WordArray.random(16); |
| 42 | + const encrypted = CryptoJS.AES.encrypt(data, key, { |
| 43 | + iv: iv, |
| 44 | + padding: CryptoJS.pad.Pkcs7, |
| 45 | + mode: CryptoJS.mode.CBC, |
| 46 | + }); |
83 | 47 |
|
84 | | - /** |
85 | | - * Encrypt hex data using a key |
86 | | - * |
87 | | - * @param {string} data - An hex string |
88 | | - * @param {Uint8Array} key - An Uint8Array key |
89 | | - * |
90 | | - * @return {object} - The encrypted data |
91 | | - */ |
92 | | - public static encrypt = (data: string, key: Uint8Array): any => { |
93 | | - // Errors |
94 | | - if (!data || !key) { |
95 | | - throw new Error('Missing argument !'); |
96 | | - } |
97 | | - // Processing |
98 | | - const iv = Crypto.randomBytes(16); |
99 | | - const encKey = utility.ua2words(key, 32); |
100 | | - const encIv = { |
101 | | - iv: utility.ua2words(iv, 16), |
102 | | - }; |
103 | | - const encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Hex.parse(data), encKey, encIv); |
104 | | - // Result |
105 | | - return { |
106 | | - ciphertext: encrypted.ciphertext, |
107 | | - iv, |
108 | | - key, |
109 | | - }; |
110 | | - }; |
| 48 | + // salt (16 bytes) + iv (16 bytes) |
| 49 | + // prepend them to the ciphertext for use in decryption |
| 50 | + return salt.toString() + iv.toString() + encrypted.toString(); |
| 51 | + } |
111 | 52 |
|
112 | 53 | /** |
113 | 54 | * Decrypt data |
114 | | - * |
115 | | - * @param {object} data - An encrypted data object |
116 | | - * |
117 | | - * @return {string} - The decrypted hex string |
| 55 | + * @param {string} data |
| 56 | + * @param {string} salt |
| 57 | + * @param {string} password |
118 | 58 | */ |
119 | | - public static decrypt = (data: any): string => { |
120 | | - // Errors |
121 | | - if (!data) { |
122 | | - throw new Error('Missing argument !'); |
123 | | - } |
124 | | - // Processing |
125 | | - const encKey = utility.ua2words(data.key, 32); |
126 | | - const encIv = { |
127 | | - iv: utility.ua2words(data.iv, 16), |
128 | | - }; |
129 | | - // Result |
130 | | - return CryptoJS.enc.Hex.stringify(CryptoJS.AES.decrypt(data, encKey, encIv)); |
131 | | - }; |
| 59 | + public static decrypt(data: string, password: string): string { |
| 60 | + const salt = CryptoJS.enc.Hex.parse(data.substr(0, 32)); |
| 61 | + const iv = CryptoJS.enc.Hex.parse(data.substr(32, 32)); |
| 62 | + const encrypted = data.substring(64); |
132 | 63 |
|
133 | | - /** |
134 | | - * Reveal the private key of an account or derive it from the wallet password |
135 | | - * |
136 | | - * @param {object} common- An object containing password and privateKey field |
137 | | - * @param {object} walletAccount - A wallet account object |
138 | | - * @param {WalletAlgorithm} algo - A wallet algorithm |
139 | | - * |
140 | | - * @return {object|boolean} - The account private key in and object or false |
141 | | - */ |
142 | | - public static passwordToPrivateKey = (common: any, walletAccount: any, algo: WalletAlgorithm): any => { |
143 | | - // Errors |
144 | | - if (!common || !common.password || !walletAccount || !algo) { |
145 | | - throw new Error('Missing argument !'); |
146 | | - } |
147 | | - // Processing |
148 | | - let r; |
149 | | - if (algo === WalletAlgorithm.Pass_6k) { |
150 | | - // Brain wallets |
151 | | - if (!walletAccount.encrypted && !walletAccount.iv) { |
152 | | - // Account private key is generated simply using a passphrase so it has no encrypted and iv |
153 | | - r = Crypto.derivePassSha(common.password, 6000); |
154 | | - } else if (!walletAccount.encrypted || !walletAccount.iv) { |
155 | | - // Else if one is missing there is a problem |
156 | | - return false; |
157 | | - } else { |
158 | | - // Else child accounts have encrypted and iv so we decrypt |
159 | | - const pass = Crypto.derivePassSha(common.password, 20); |
160 | | - const obj = { |
161 | | - ciphertext: CryptoJS.enc.Hex.parse(walletAccount.encrypted), |
162 | | - iv: convert.hexToUint8(walletAccount.iv), |
163 | | - key: convert.hexToUint8(pass.priv), |
164 | | - }; |
165 | | - const d = Crypto.decrypt(obj); |
166 | | - r = { priv: d }; |
167 | | - } |
168 | | - } else if (algo === WalletAlgorithm.Pass_bip32) { |
169 | | - // Wallets from PRNG |
170 | | - const pass = Crypto.derivePassSha(common.password, 20); |
171 | | - const obj = { |
172 | | - ciphertext: CryptoJS.enc.Hex.parse(walletAccount.encrypted), |
173 | | - iv: convert.hexToUint8(walletAccount.iv), |
174 | | - key: convert.hexToUint8(pass.priv), |
175 | | - }; |
176 | | - const d = Crypto.decrypt(obj); |
177 | | - r = { priv: d }; |
178 | | - } else if (algo === WalletAlgorithm.Pass_enc) { |
179 | | - // Private Key wallets |
180 | | - const pass = Crypto.derivePassSha(common.password, 20); |
181 | | - const obj = { |
182 | | - ciphertext: CryptoJS.enc.Hex.parse(walletAccount.encrypted), |
183 | | - iv: convert.hexToUint8(walletAccount.iv), |
184 | | - key: convert.hexToUint8(pass.priv), |
185 | | - }; |
186 | | - const d = Crypto.decrypt(obj); |
187 | | - r = { priv: d }; |
188 | | - } else if (algo === WalletAlgorithm.Trezor) { |
189 | | - // HW wallet |
190 | | - r = { priv: '' }; |
191 | | - common.isHW = true; |
192 | | - } else { |
193 | | - return false; |
194 | | - } |
195 | | - // Result |
196 | | - common.privateKey = r.priv; |
197 | | - return true; |
198 | | - }; |
| 64 | + // generate password based key |
| 65 | + const key = CryptoJS.PBKDF2(password, salt, { |
| 66 | + keySize: 8, |
| 67 | + iterations: 1024, |
| 68 | + }); |
199 | 69 |
|
200 | | - /** |
201 | | - * Generate a random key |
202 | | - * |
203 | | - * @return {Uint8Array} - A random key |
204 | | - */ |
205 | | - public static randomKey = (): Uint8Array => { |
206 | | - return Crypto.randomBytes(32); |
207 | | - }; |
| 70 | + // decrypt using custom IV |
| 71 | + const decrypted = CryptoJS.AES.decrypt(encrypted, key, { |
| 72 | + iv: iv, |
| 73 | + padding: CryptoJS.pad.Pkcs7, |
| 74 | + mode: CryptoJS.mode.CBC, |
| 75 | + }); |
208 | 76 |
|
209 | | - /** |
210 | | - * Encode a private key using a password |
211 | | - * |
212 | | - * @param {string} privateKey - An hex private key |
213 | | - * @param {string} password - A password |
214 | | - * |
215 | | - * @return {object} - The encoded data |
216 | | - */ |
217 | | - public static encodePrivateKey = (privateKey: string, password: string): any => { |
218 | | - // Errors |
219 | | - if (!privateKey || !password) { |
220 | | - throw new Error('Missing argument !'); |
221 | | - } |
222 | | - // Processing |
223 | | - const pass = Crypto.derivePassSha(password, 20); |
224 | | - const r = Crypto.encrypt(privateKey, convert.hexToUint8(pass.priv)); |
225 | | - // Result |
226 | | - return { |
227 | | - ciphertext: CryptoJS.enc.Hex.stringify(r.ciphertext), |
228 | | - iv: convert.uint8ToHex(r.iv), |
229 | | - }; |
230 | | - }; |
| 77 | + return decrypted.toString(CryptoJS.enc.Utf8); |
| 78 | + } |
231 | 79 |
|
232 | 80 | /*** |
233 | 81 | * Encode a message, separated from encode() to help testing |
|
0 commit comments