|
17 | 17 |
|
18 | 18 | import { expect } from 'chai' |
19 | 19 | import 'mocha' |
20 | | -import { |
21 | | - NodeDecryptionMaterial, // eslint-disable-line no-unused-vars |
22 | | - NodeEncryptionMaterial, // eslint-disable-line no-unused-vars |
23 | | - KeyringNode, EncryptedDataKey, |
24 | | - KeyringTraceFlag, AlgorithmSuiteIdentifier |
25 | | -} from '@aws-crypto/material-management-node' |
26 | | - |
27 | | -// import * as fs from 'fs' |
28 | | - |
29 | | -import { encrypt } from '@aws-crypto/encrypt-node' |
30 | | -import { decrypt } from '../src/decrypt' |
31 | | - |
32 | | -describe('simple', () => { |
33 | | - it('decrypt what I encrypt', async () => { |
34 | | - class TestKeyring extends KeyringNode { |
35 | | - async _onEncrypt (material: NodeEncryptionMaterial) { |
36 | | - const unencryptedDataKey = new Uint8Array(material.suite.keyLengthBytes).fill(1) |
37 | | - const trace = { keyNamespace: 'k', keyName: 'k', flags: KeyringTraceFlag.WRAPPING_KEY_GENERATED_DATA_KEY } |
38 | | - const edk = new EncryptedDataKey({ providerId: 'k', providerInfo: 'k', encryptedDataKey: new Uint8Array(3) }) |
39 | | - return material |
40 | | - .setUnencryptedDataKey(unencryptedDataKey, trace) |
41 | | - .addEncryptedDataKey(edk, KeyringTraceFlag.WRAPPING_KEY_ENCRYPTED_DATA_KEY) |
42 | | - } |
43 | | - async _onDecrypt (material: NodeDecryptionMaterial) { |
44 | | - const unencryptedDataKey = new Uint8Array(material.suite.keyLengthBytes).fill(1) |
45 | | - const trace = { keyNamespace: 'k', keyName: 'k', flags: KeyringTraceFlag.WRAPPING_KEY_DECRYPTED_DATA_KEY } |
46 | | - return material.setUnencryptedDataKey(unencryptedDataKey, trace) |
47 | | - } |
48 | | - } |
49 | | - |
50 | | - const keyRing = new TestKeyring() |
51 | | - const suiteId = AlgorithmSuiteIdentifier.ALG_AES128_GCM_IV12_TAG16 |
52 | | - |
53 | | - const plaintext = 'asdf' |
54 | | - const { ciphertext } = await encrypt(keyRing, plaintext, { suiteId }) |
55 | | - |
56 | | - const { plaintext: test, messageHeader } = await decrypt(keyRing, ciphertext) |
57 | | - |
58 | | - expect(messageHeader.suiteId).to.equal(suiteId) |
59 | | - expect(test.toString()).to.equal(plaintext) |
| 20 | +import { AlgorithmSuiteIdentifier } from '@aws-crypto/material-management-node' |
| 21 | +import { decrypt } from '../src/index' |
| 22 | +import * as fixtures from './fixtures' |
| 23 | +import from from 'from2' |
| 24 | + |
| 25 | +describe('decrypt', () => { |
| 26 | + it('string with encoding', async () => { |
| 27 | + const { plaintext: test, messageHeader } = await decrypt( |
| 28 | + fixtures.decryptKeyring(), |
| 29 | + fixtures.base64CiphertextAlgAes256GcmIv12Tag16HkdfSha384EcdsaP384(), |
| 30 | + { encoding: 'base64' } |
| 31 | + ) |
| 32 | + |
| 33 | + expect(messageHeader.suiteId).to.equal(AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384) |
| 34 | + expect(messageHeader.encryptionContext).to.deep.equal(fixtures.encryptionContext()) |
| 35 | + expect(test.toString('base64')).to.equal(fixtures.base64Plaintext()) |
| 36 | + }) |
| 37 | + |
| 38 | + it('buffer', async () => { |
| 39 | + const { plaintext: test, messageHeader } = await decrypt( |
| 40 | + fixtures.decryptKeyring(), |
| 41 | + Buffer.from(fixtures.base64CiphertextAlgAes256GcmIv12Tag16HkdfSha384EcdsaP384(), 'base64') |
| 42 | + ) |
| 43 | + |
| 44 | + expect(messageHeader.suiteId).to.equal(AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384) |
| 45 | + expect(messageHeader.encryptionContext).to.deep.equal(fixtures.encryptionContext()) |
| 46 | + expect(test.toString('base64')).to.equal(fixtures.base64Plaintext()) |
| 47 | + }) |
| 48 | + |
| 49 | + it('stream', async () => { |
| 50 | + const ciphertext = Buffer.from(fixtures.base64CiphertextAlgAes256GcmIv12Tag16HkdfSha384EcdsaP384(), 'base64') |
| 51 | + const i = ciphertext.values() |
| 52 | + const ciphertextStream = from((_: number, next: Function) => { |
| 53 | + /* Pushing 1 byte at time is the most annoying thing. |
| 54 | + * This is done intentionally to hit _every_ boundary condition. |
| 55 | + */ |
| 56 | + const { value, done } = i.next() |
| 57 | + if (done) return next(null, null) |
| 58 | + next(null, new Uint8Array([value])) |
| 59 | + }) |
| 60 | + |
| 61 | + const { plaintext: test, messageHeader } = await decrypt( |
| 62 | + fixtures.decryptKeyring(), |
| 63 | + ciphertextStream |
| 64 | + ) |
| 65 | + |
| 66 | + expect(messageHeader.suiteId).to.equal(AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384) |
| 67 | + expect(messageHeader.encryptionContext).to.deep.equal(fixtures.encryptionContext()) |
| 68 | + expect(test.toString('base64')).to.equal(fixtures.base64Plaintext()) |
60 | 69 | }) |
61 | 70 | }) |
0 commit comments