|
| 1 | +import type { BinaryLikeNode } from '../../../../../src/Utils'; |
| 2 | +import { Buffer as CraftzdogBuffer } from '@craftzdog/react-native-buffer'; |
| 3 | +import { Buffer as FerossBuffer } from 'buffer'; |
| 4 | +import crypto from 'react-native-quick-crypto'; |
| 5 | +import { describe, it } from '../../MochaRNAdapter'; |
| 6 | +import { expect } from 'chai'; |
| 7 | + |
| 8 | +// ----------------------------------------------------------------------------- |
| 9 | +// tests |
| 10 | + |
| 11 | +describe('specific issues', () => { |
| 12 | + it('issue 398', () => { |
| 13 | + const publicKeySpkiBase64 = |
| 14 | + 'MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAENlFpbMBNfCY6Lhj9A/clefyxJVIXGJ0y6CcZ/cbbyyebvN6T0aNPvpQyFdUwRtYvFHlYbqIZOM8AoqdPcnSMIA=='; |
| 15 | + |
| 16 | + const publicKey = getPublicKeyInPEMFormat(publicKeySpkiBase64); |
| 17 | + // console.log('\n' + publicKey); |
| 18 | + const encrypted = encrypt({ |
| 19 | + payload: JSON.stringify({ a: 1 }), |
| 20 | + publicKey, |
| 21 | + }); |
| 22 | + // console.log({ encrypted }); |
| 23 | + const { response: decrypted } = decrypt({ |
| 24 | + response: encrypted, |
| 25 | + secretKey: encrypted.secretKey, |
| 26 | + }); |
| 27 | + expect(decrypted).to.equal({ a: 1 }); |
| 28 | + }); |
| 29 | + |
| 30 | + const largeKey = crypto.randomBytes(64); |
| 31 | + it('issue 505 - craftzdog buffer', () => { |
| 32 | + // an instance of CraftzdogBuffer |
| 33 | + testBufferConversion('test', largeKey); |
| 34 | + }); |
| 35 | + it('issue 505 - feross buffer', () => { |
| 36 | + // not an instance of CraftzdogBuffer |
| 37 | + const largeKeyFeross = FerossBuffer.from( |
| 38 | + largeKey.toString('base64'), |
| 39 | + 'base64', |
| 40 | + ); |
| 41 | + testBufferConversion('test', largeKeyFeross); |
| 42 | + }); |
| 43 | +}); |
| 44 | + |
| 45 | +// ----------------------------------------------------------------------------- |
| 46 | +// #398 |
| 47 | +type EncryptRequest = { |
| 48 | + payload: string; |
| 49 | + publicKey: ArrayBuffer; |
| 50 | +}; |
| 51 | +type EncryptResponse = { |
| 52 | + KEY: string; |
| 53 | + IV: string; |
| 54 | + PAYLOAD: string; |
| 55 | + secretKey: BinaryLikeNode; |
| 56 | +}; |
| 57 | + |
| 58 | +const algo = 'aes-128-gcm'; |
| 59 | + |
| 60 | +const encrypt = ({ payload, publicKey }: EncryptRequest): EncryptResponse => { |
| 61 | + const secretKey = crypto.randomBytes(16); |
| 62 | + const iv = crypto.randomBytes(16); |
| 63 | + |
| 64 | + const cipher = crypto.createCipheriv(algo, secretKey, iv); |
| 65 | + |
| 66 | + const encryptedPayload = FerossBuffer.concat([ |
| 67 | + cipher.update(payload, 'utf8'), |
| 68 | + cipher.final(), |
| 69 | + cipher.getAuthTag(), |
| 70 | + ]).toString('base64'); |
| 71 | + |
| 72 | + const encryptedSessionKey = crypto.publicEncrypt( |
| 73 | + { |
| 74 | + key: publicKey, |
| 75 | + padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, |
| 76 | + }, |
| 77 | + secretKey, |
| 78 | + ); |
| 79 | + |
| 80 | + return { |
| 81 | + KEY: encryptedSessionKey.toString('base64'), |
| 82 | + IV: iv.toString('base64'), |
| 83 | + PAYLOAD: encryptedPayload, |
| 84 | + secretKey, |
| 85 | + }; |
| 86 | +}; |
| 87 | + |
| 88 | +const decrypt = ({ |
| 89 | + response, |
| 90 | + secretKey, |
| 91 | +}: { |
| 92 | + response: EncryptResponse; |
| 93 | + secretKey: BinaryLikeNode; |
| 94 | +}) => { |
| 95 | + const { IV, PAYLOAD } = response; |
| 96 | + |
| 97 | + const decipher = crypto.createDecipheriv( |
| 98 | + algo, |
| 99 | + secretKey, |
| 100 | + FerossBuffer.from(IV, 'base64'), |
| 101 | + ); |
| 102 | + |
| 103 | + const encryptedPayload = FerossBuffer.from(PAYLOAD, 'base64'); |
| 104 | + let decrypted = decipher.update( |
| 105 | + FerossBuffer.from( |
| 106 | + encryptedPayload.subarray(0, encryptedPayload.length - 16), |
| 107 | + ), |
| 108 | + ); |
| 109 | + decrypted = FerossBuffer.concat([decrypted, decipher.final()]); |
| 110 | + |
| 111 | + return JSON.parse(decrypted.toString('utf8')); |
| 112 | +}; |
| 113 | + |
| 114 | +const getPublicKeyInPEMFormat = (key: string): ArrayBuffer => { |
| 115 | + return crypto |
| 116 | + .createPublicKey({ |
| 117 | + key: FerossBuffer.from(key, 'base64'), |
| 118 | + format: 'der', |
| 119 | + type: 'spki', |
| 120 | + }) |
| 121 | + .export({ |
| 122 | + type: 'spki', |
| 123 | + format: 'pem', |
| 124 | + }); |
| 125 | +}; |
| 126 | + |
| 127 | +// ----------------------------------------------------------------------------- |
| 128 | +// #505 |
| 129 | +const testBufferConversion = ( |
| 130 | + clearText: string, |
| 131 | + largeKey: CraftzdogBuffer | FerossBuffer, |
| 132 | +) => { |
| 133 | + const key = largeKey.subarray(32); |
| 134 | + const iv = crypto.randomBytes(16); |
| 135 | + const cipher = crypto.createCipheriv('aes-256-cbc', key, iv); |
| 136 | + const enc = CraftzdogBuffer.concat([ |
| 137 | + cipher.update( |
| 138 | + CraftzdogBuffer.from(clearText), |
| 139 | + ) as unknown as CraftzdogBuffer, |
| 140 | + cipher.final() as unknown as CraftzdogBuffer, |
| 141 | + ]); |
| 142 | + const encB64 = enc.toString('base64'); |
| 143 | + const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv); |
| 144 | + const dec = CraftzdogBuffer.concat([ |
| 145 | + decipher.update( |
| 146 | + CraftzdogBuffer.from(encB64, 'base64'), |
| 147 | + ) as unknown as CraftzdogBuffer, |
| 148 | + decipher.final() as unknown as CraftzdogBuffer, |
| 149 | + ]); |
| 150 | + expect(dec.toString()).to.equal(clearText); |
| 151 | +}; |
0 commit comments