|
| 1 | +/* |
| 2 | + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use |
| 5 | + * this file except in compliance with the License. A copy of the License is |
| 6 | + * located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0/ |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed on an |
| 11 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 | + * implied. See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +/* eslint-env mocha */ |
| 17 | + |
| 18 | +import * as chai from 'chai' |
| 19 | +import chaiAsPromised from 'chai-as-promised' |
| 20 | +import 'mocha' |
| 21 | +import { RawAesKeyringNode, RawAesWrappingSuiteIdentifier } from '../src/index' |
| 22 | +import { |
| 23 | + NodeEncryptionMaterial, |
| 24 | + NodeAlgorithmSuite, |
| 25 | + AlgorithmSuiteIdentifier, |
| 26 | + EncryptedDataKey, // eslint-disable-line no-unused-vars |
| 27 | + NodeDecryptionMaterial |
| 28 | +} from '@aws-crypto/material-management-node' |
| 29 | + |
| 30 | +chai.use(chaiAsPromised) |
| 31 | +const { expect } = chai |
| 32 | + |
| 33 | +describe('RawAesKeyringNode::constructor', () => { |
| 34 | + const wrappingSuite = RawAesWrappingSuiteIdentifier.AES128_GCM_IV12_TAG16_NO_PADDING |
| 35 | + const unencryptedMasterKey = new Uint8Array(128 / 8) |
| 36 | + const keyNamespace = 'keyNamespace' |
| 37 | + const keyName = 'keyName' |
| 38 | + |
| 39 | + it('constructor decorates', async () => { |
| 40 | + const test = new RawAesKeyringNode({ keyName, keyNamespace, unencryptedMasterKey, wrappingSuite }) |
| 41 | + expect(test.keyName).to.equal(keyName) |
| 42 | + expect(test.keyNamespace).to.equal(keyNamespace) |
| 43 | + expect(test._wrapKey).to.be.a('function') |
| 44 | + expect(test._unwrapKey).to.be.a('function') |
| 45 | + }) |
| 46 | + |
| 47 | + it('Precondition: AesKeyringNode needs identifying information for encrypt and decrypt.', async () => { |
| 48 | + // @ts-ignore doing something typescript protects us from doing... |
| 49 | + expect(() => new RawAesKeyringNode({ keyNamespace, unencryptedMasterKey, wrappingSuite })).to.throw() |
| 50 | + // @ts-ignore doing something typescript protects us from doing... |
| 51 | + expect(() => new RawAesKeyringNode({ keyName, unencryptedMasterKey, wrappingSuite })).to.throw() |
| 52 | + }) |
| 53 | + |
| 54 | + it('Precondition: wrappingSuite must be a valid RawAesWrappingSuite.', async () => { |
| 55 | + expect(() => new RawAesKeyringNode({ |
| 56 | + keyName, |
| 57 | + keyNamespace, |
| 58 | + unencryptedMasterKey, |
| 59 | + wrappingSuite: 111 as any |
| 60 | + })).to.throw() |
| 61 | + }) |
| 62 | + |
| 63 | + it('Precondition: unencryptedMasterKey must correspond to the algorithm suite specification.', async () => { |
| 64 | + expect(() => new RawAesKeyringNode({ |
| 65 | + keyName, |
| 66 | + keyNamespace, |
| 67 | + unencryptedMasterKey, |
| 68 | + wrappingSuite: RawAesWrappingSuiteIdentifier.AES192_GCM_IV12_TAG16_NO_PADDING |
| 69 | + })).to.throw() |
| 70 | + }) |
| 71 | +}) |
| 72 | + |
| 73 | +describe('RawAesKeyringNode::_filter', () => { |
| 74 | + const wrappingSuite = RawAesWrappingSuiteIdentifier.AES128_GCM_IV12_TAG16_NO_PADDING |
| 75 | + const unencryptedMasterKey = new Uint8Array(128 / 8) |
| 76 | + const keyNamespace = 'keyNamespace' |
| 77 | + const keyName = 'keyName' |
| 78 | + const keyring = new RawAesKeyringNode({ keyName, keyNamespace, unencryptedMasterKey, wrappingSuite }) |
| 79 | + |
| 80 | + it('true', async () => { |
| 81 | + const test = keyring._filter({ |
| 82 | + providerId: keyNamespace, |
| 83 | + providerInfo: keyName |
| 84 | + } as any) |
| 85 | + expect(test).to.equal(true) |
| 86 | + }) |
| 87 | + |
| 88 | + it('true', async () => { |
| 89 | + const test = keyring._filter({ |
| 90 | + providerId: keyNamespace, |
| 91 | + providerInfo: keyName + 'other stuff' |
| 92 | + } as any) |
| 93 | + expect(test).to.equal(true) |
| 94 | + }) |
| 95 | + |
| 96 | + it('false', async () => { |
| 97 | + expect(keyring._filter({ |
| 98 | + providerId: 'not: keyNamespace', |
| 99 | + providerInfo: keyName + 'other stuff' |
| 100 | + } as any)).to.equal(false) |
| 101 | + |
| 102 | + expect(keyring._filter({ |
| 103 | + providerId: keyNamespace, |
| 104 | + providerInfo: 'not: keyName' |
| 105 | + } as any)).to.equal(false) |
| 106 | + }) |
| 107 | +}) |
| 108 | + |
| 109 | +describe('RawAesKeyringNode encrypt/decrypt', () => { |
| 110 | + const wrappingSuite = RawAesWrappingSuiteIdentifier.AES128_GCM_IV12_TAG16_NO_PADDING |
| 111 | + const unencryptedMasterKey = new Uint8Array(128 / 8) |
| 112 | + const keyNamespace = 'keyNamespace' |
| 113 | + const keyName = 'keyName' |
| 114 | + const keyring = new RawAesKeyringNode({ keyName, keyNamespace, unencryptedMasterKey, wrappingSuite }) |
| 115 | + let encryptedDataKey: EncryptedDataKey |
| 116 | + |
| 117 | + it('can encrypt and create unencrypted data key', async () => { |
| 118 | + const suite = new NodeAlgorithmSuite(AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA256) |
| 119 | + const material = new NodeEncryptionMaterial(suite) |
| 120 | + const test = await keyring.onEncrypt(material) |
| 121 | + expect(test.hasValidKey()).to.equal(true) |
| 122 | + const udk = test.getUnencryptedDataKey() |
| 123 | + expect(udk).to.have.lengthOf(suite.keyLengthBytes) |
| 124 | + expect(test.encryptedDataKeys).to.have.lengthOf(1) |
| 125 | + const [edk] = test.encryptedDataKeys |
| 126 | + expect(edk.providerId).to.equal(keyNamespace) |
| 127 | + encryptedDataKey = edk |
| 128 | + }) |
| 129 | + |
| 130 | + it('can decrypt an EncryptedDataKey', async () => { |
| 131 | + const suite = new NodeAlgorithmSuite(AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA256) |
| 132 | + const material = new NodeDecryptionMaterial(suite) |
| 133 | + const test = await keyring.onDecrypt(material, [encryptedDataKey]) |
| 134 | + expect(test.hasValidKey()).to.equal(true) |
| 135 | + }) |
| 136 | +}) |
0 commit comments