Skip to content

Commit 21d65d0

Browse files
seebeeskarlw00t
authored andcommitted
fix: encrypt/decrypt browser
add test for browsers encrypt/decrypt
1 parent bf88871 commit 21d65d0

File tree

3 files changed

+131
-12
lines changed

3 files changed

+131
-12
lines changed

modules/decrypt-browser/test/decrypt.test.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@
1818
import { expect } from 'chai'
1919
import 'mocha'
2020
import { decrypt } from '../src/index'
21+
import { AlgorithmSuiteIdentifier } from '@aws-crypto/material-management-browser'
22+
import * as fixtures from './fixtures'
2123

2224
describe('decrypt', () => {
23-
/* This test is mostly to insure that I include all files.
24-
* So I can not lie on code coverage.
25-
*/
26-
it('is a function', () => {
27-
expect(decrypt).to.be.a('function')
25+
it('buffer', async () => {
26+
const { clearMessage: test, messageHeader } = await decrypt(
27+
fixtures.decryptKeyring(),
28+
fixtures.ciphertextAlgAes256GcmIv12Tag16HkdfSha384EcdsaP384()
29+
)
30+
31+
expect(messageHeader.suiteId).to.equal(AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384)
32+
expect(messageHeader.encryptionContext).to.deep.equal(fixtures.encryptionContext())
33+
expect(test).to.deep.equal(fixtures.plaintext())
2834
})
2935
})
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 {
19+
WebCryptoDecryptionMaterial, // eslint-disable-line no-unused-vars
20+
WebCryptoEncryptionMaterial, // eslint-disable-line no-unused-vars
21+
KeyringWebCrypto,
22+
KeyringTraceFlag,
23+
importForWebCryptoDecryptionMaterial
24+
} from '@aws-crypto/material-management-browser'
25+
26+
export function ciphertextAlgAes256GcmIv12Tag16HkdfSha384EcdsaP384 () {
27+
return new Uint8Array([1, 128, 3, 120, 152, 39, 185, 111, 47, 165, 9, 162, 229, 10, 42, 28, 134, 86, 134, 57, 0, 112, 0, 2, 0, 21, 97, 119, 115, 45, 99, 114, 121, 112, 116, 111, 45, 112, 117, 98, 108, 105, 99, 45, 107, 101, 121, 0, 68, 65, 110, 89, 116, 70, 69, 101, 119, 90, 109, 43, 50, 56, 75, 104, 73, 71, 112, 120, 56, 82, 104, 107, 97, 85, 97, 76, 99, 99, 74, 112, 121, 102, 49, 110, 119, 73, 86, 81, 70, 71, 109, 121, 112, 103, 122, 104, 72, 50, 88, 100, 83, 73, 66, 74, 52, 115, 75, 105, 83, 72, 51, 99, 107, 122, 119, 61, 61, 0, 6, 115, 105, 109, 112, 108, 101, 0, 7, 99, 111, 110, 116, 101, 120, 116, 0, 1, 0, 1, 107, 0, 1, 107, 0, 3, 0, 0, 0, 2, 0, 0, 0, 0, 12, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26, 145, 102, 170, 98, 142, 150, 6, 52, 206, 143, 212, 191, 251, 240, 5, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 34, 13, 100, 212, 135, 138, 87, 224, 143, 81, 80, 103, 165, 73, 32, 201, 85, 20, 102, 94, 240, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 57, 122, 63, 111, 60, 86, 216, 227, 41, 135, 153, 3, 185, 146, 121, 226, 96, 140, 133, 7, 108, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 187, 57, 143, 182, 231, 34, 100, 85, 193, 111, 48, 197, 231, 237, 92, 237, 200, 232, 110, 33, 10, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 27, 168, 206, 28, 12, 247, 55, 12, 177, 129, 72, 230, 229, 113, 77, 48, 28, 126, 216, 32, 175, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 132, 109, 27, 21, 79, 80, 166, 225, 107, 189, 218, 203, 249, 24, 174, 153, 124, 14, 184, 164, 218, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 232, 179, 33, 243, 63, 125, 105, 244, 71, 78, 127, 127, 157, 73, 174, 96, 25, 244, 86, 245, 237, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 178, 233, 102, 5, 30, 5, 64, 198, 211, 147, 237, 52, 143, 145, 90, 254, 48, 251, 223, 245, 9, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 194, 160, 61, 149, 147, 116, 50, 179, 116, 248, 87, 64, 55, 167, 34, 116, 145, 91, 247, 142, 46, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 29, 125, 44, 141, 164, 22, 36, 245, 118, 197, 133, 254, 131, 82, 122, 246, 105, 204, 57, 148, 46, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 156, 154, 104, 126, 189, 84, 195, 3, 242, 255, 230, 170, 190, 35, 19, 172, 117, 224, 58, 19, 60, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 217, 111, 181, 109, 101, 204, 15, 81, 25, 72, 231, 32, 102, 157, 148, 72, 75, 197, 16, 191, 20, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 170, 36, 22, 144, 17, 217, 164, 57, 69, 251, 184, 41, 34, 220, 104, 117, 237, 137, 80, 83, 115, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 16, 173, 250, 100, 79, 85, 46, 34, 35, 217, 126, 116, 55, 189, 212, 28, 69, 45, 155, 64, 91, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 73, 43, 213, 252, 75, 222, 162, 219, 108, 46, 206, 72, 49, 234, 14, 122, 193, 162, 115, 176, 197, 0, 0, 0, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 42, 112, 209, 70, 250, 135, 212, 222, 195, 132, 101, 223, 14, 92, 31, 235, 147, 61, 47, 13, 175, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 220, 167, 108, 88, 159, 207, 242, 17, 124, 104, 232, 80, 117, 3, 247, 191, 82, 129, 14, 144, 121, 0, 0, 0, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 108, 144, 25, 119, 159, 160, 107, 253, 100, 55, 215, 82, 47, 119, 171, 236, 20, 64, 99, 243, 87, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 3, 47, 251, 249, 33, 143, 243, 250, 205, 188, 21, 108, 213, 163, 142, 175, 40, 216, 206, 17, 234, 0, 0, 0, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19, 203, 91, 156, 248, 199, 16, 124, 194, 67, 240, 106, 192, 37, 70, 138, 151, 36, 219, 94, 1, 96, 0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 30, 146, 135, 99, 232, 226, 135, 173, 5, 76, 38, 212, 0, 79, 244, 95, 94, 213, 62, 244, 24, 0, 0, 0, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 166, 218, 131, 160, 233, 168, 4, 234, 60, 230, 222, 32, 217, 213, 186, 204, 73, 49, 194, 56, 111, 0, 0, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22, 177, 210, 27, 126, 42, 249, 13, 169, 158, 221, 76, 100, 174, 46, 120, 55, 99, 251, 174, 169, 250, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 168, 143, 35, 86, 22, 225, 246, 115, 200, 191, 247, 231, 150, 71, 4, 226, 198, 221, 83, 223, 212, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 249, 42, 55, 121, 79, 8, 174, 233, 186, 91, 226, 143, 163, 106, 205, 11, 69, 18, 69, 31, 174, 0, 0, 0, 25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 77, 254, 49, 137, 67, 173, 29, 230, 102, 139, 239, 52, 51, 115, 154, 203, 142, 118, 71, 250, 201, 0, 0, 0, 26, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26, 159, 132, 79, 66, 216, 196, 229, 248, 36, 66, 252, 128, 155, 157, 85, 83, 214, 234, 162, 47, 220, 0, 0, 0, 27, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 27, 147, 173, 224, 79, 27, 2, 118, 109, 251, 113, 67, 162, 163, 193, 233, 31, 182, 30, 47, 90, 219, 0, 0, 0, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 116, 186, 5, 86, 52, 126, 203, 30, 13, 86, 141, 65, 75, 27, 252, 46, 92, 154, 247, 65, 5, 0, 0, 0, 29, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 193, 106, 16, 130, 246, 194, 198, 10, 223, 162, 144, 31, 34, 106, 188, 148, 226, 23, 254, 167, 149, 0, 0, 0, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 30, 123, 65, 52, 255, 81, 84, 113, 6, 231, 49, 126, 20, 17, 97, 225, 20, 210, 101, 30, 23, 23, 0, 0, 0, 31, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 31, 39, 34, 176, 33, 192, 20, 70, 243, 31, 55, 241, 157, 228, 199, 33, 202, 0, 54, 208, 70, 7, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 181, 209, 125, 8, 200, 187, 31, 19, 120, 64, 255, 77, 209, 199, 116, 187, 154, 80, 69, 150, 102, 0, 0, 0, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 34, 213, 43, 31, 82, 140, 194, 92, 93, 38, 9, 63, 237, 28, 223, 51, 86, 244, 178, 88, 94, 0, 0, 0, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 34, 20, 251, 120, 227, 44, 81, 111, 10, 238, 3, 81, 121, 98, 70, 13, 162, 71, 131, 46, 103, 98, 0, 0, 0, 35, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 35, 10, 157, 95, 206, 143, 35, 162, 25, 129, 59, 54, 72, 193, 232, 226, 210, 196, 196, 41, 100, 34, 0, 0, 0, 36, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 9, 24, 75, 131, 234, 128, 79, 5, 117, 60, 138, 234, 90, 104, 210, 69, 204, 65, 255, 204, 167, 0, 0, 0, 37, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 201, 8, 128, 251, 92, 81, 68, 64, 63, 169, 238, 67, 142, 236, 122, 89, 163, 196, 201, 76, 225, 0, 0, 0, 38, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 38, 171, 25, 79, 109, 104, 214, 41, 29, 136, 185, 4, 196, 111, 120, 245, 107, 147, 119, 13, 102, 65, 0, 0, 0, 39, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 39, 164, 10, 7, 8, 95, 108, 125, 255, 124, 241, 247, 205, 157, 13, 119, 202, 81, 4, 170, 1, 20, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 113, 35, 109, 246, 65, 23, 45, 65, 116, 51, 39, 90, 143, 131, 34, 6, 141, 86, 174, 97, 134, 0, 0, 0, 41, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 17, 219, 112, 146, 102, 164, 109, 250, 237, 28, 67, 34, 122, 165, 112, 23, 159, 147, 126, 17, 110, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 42, 166, 13, 213, 152, 15, 244, 21, 15, 216, 172, 198, 144, 147, 155, 130, 132, 250, 62, 135, 41, 211, 0, 0, 0, 43, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 43, 122, 230, 189, 241, 100, 207, 33, 97, 250, 117, 32, 29, 205, 246, 22, 60, 207, 106, 246, 26, 6, 0, 0, 0, 44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 44, 15, 131, 64, 121, 47, 163, 18, 170, 84, 253, 221, 153, 52, 40, 51, 94, 208, 71, 136, 115, 214, 0, 0, 0, 45, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 211, 10, 50, 65, 83, 72, 67, 128, 24, 130, 253, 176, 7, 101, 238, 83, 244, 42, 12, 125, 52, 0, 0, 0, 46, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 46, 142, 66, 230, 141, 29, 134, 213, 171, 121, 31, 148, 7, 204, 168, 63, 7, 108, 205, 32, 127, 166, 0, 0, 0, 47, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 47, 175, 180, 23, 150, 138, 210, 61, 241, 170, 248, 43, 178, 57, 164, 206, 222, 235, 101, 133, 16, 64, 0, 0, 0, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48, 217, 50, 135, 140, 18, 81, 49, 56, 168, 19, 77, 173, 63, 128, 170, 129, 62, 36, 64, 168, 94, 0, 0, 0, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 49, 110, 77, 80, 143, 224, 170, 140, 47, 160, 174, 228, 250, 110, 88, 193, 177, 0, 249, 97, 48, 85, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 50, 24, 208, 66, 49, 81, 228, 15, 74, 159, 154, 45, 254, 30, 39, 168, 170, 195, 77, 146, 108, 54, 0, 0, 0, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 51, 182, 72, 29, 209, 253, 26, 238, 146, 20, 49, 239, 248, 241, 154, 53, 162, 46, 73, 231, 158, 61, 0, 0, 0, 52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 52, 193, 124, 164, 17, 25, 250, 8, 66, 12, 132, 48, 174, 46, 21, 1, 153, 89, 250, 124, 43, 187, 0, 0, 0, 53, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 53, 254, 204, 223, 255, 248, 36, 147, 203, 139, 233, 166, 144, 167, 156, 217, 10, 230, 186, 154, 221, 138, 0, 0, 0, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 54, 157, 1, 82, 216, 240, 244, 171, 31, 43, 72, 139, 78, 109, 130, 199, 180, 29, 220, 152, 86, 41, 0, 0, 0, 55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 55, 118, 62, 180, 234, 142, 50, 253, 134, 112, 238, 1, 135, 172, 62, 137, 172, 101, 144, 80, 181, 118, 0, 0, 0, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 56, 248, 25, 23, 244, 216, 85, 10, 32, 5, 245, 76, 71, 163, 248, 171, 150, 22, 157, 42, 51, 83, 0, 0, 0, 57, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 57, 120, 215, 253, 150, 149, 111, 192, 171, 225, 21, 37, 33, 213, 31, 199, 168, 148, 200, 59, 178, 210, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 58, 33, 215, 218, 101, 231, 100, 1, 25, 227, 155, 64, 152, 197, 8, 65, 219, 201, 99, 174, 40, 9, 0, 0, 0, 59, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 59, 147, 209, 159, 187, 1, 87, 223, 93, 64, 154, 44, 158, 152, 252, 15, 46, 181, 109, 69, 191, 253, 0, 0, 0, 60, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 60, 36, 153, 72, 151, 36, 144, 73, 224, 171, 192, 77, 113, 30, 99, 86, 246, 112, 229, 79, 12, 234, 255, 255, 255, 255, 0, 0, 0, 61, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 61, 0, 0, 0, 0, 64, 100, 56, 207, 172, 88, 237, 127, 243, 73, 172, 162, 140, 43, 101, 189, 0, 104, 48, 102, 2, 49, 0, 222, 33, 224, 165, 39, 217, 81, 217, 135, 23, 127, 61, 9, 89, 74, 102, 176, 204, 189, 109, 198, 10, 59, 246, 251, 25, 141, 88, 85, 215, 156, 106, 200, 198, 75, 45, 202, 77, 184, 217, 138, 222, 19, 71, 3, 205, 239, 248, 2, 49, 0, 253, 18, 129, 175, 193, 214, 6, 110, 200, 105, 144, 43, 103, 18, 156, 55, 81, 32, 190, 155, 152, 12, 243, 121, 56, 140, 114, 52, 104, 39, 162, 171, 97, 22, 203, 194, 15, 38, 206, 103, 80, 46, 7, 111, 74, 131, 237, 22])
28+
}
29+
30+
export function plaintext () {
31+
return new Uint8Array([221, 135, 180, 69, 84, 200, 141, 138, 125, 98, 248, 190, 243, 80, 243, 171, 216, 125, 128, 5, 5, 233, 169, 12, 214, 106, 147, 108, 242, 140, 134, 108, 32, 197, 53, 174, 143, 165, 165, 127, 197, 5, 80, 193, 187, 204, 2, 207, 14, 147, 123, 236, 73, 94, 49, 45, 221, 93, 67, 134, 150, 191, 231, 106, 89, 176, 94, 64, 108, 120, 74, 62, 67, 16, 137, 129, 234, 58, 88, 203, 129, 219, 20, 7, 102, 162, 83, 251, 72, 37, 51, 121, 156, 151, 97, 86, 194, 36, 3, 103, 209, 188, 225, 214, 14, 91, 82, 146, 13, 248, 178, 33, 39, 189, 173, 58, 128, 70, 93, 31, 152, 73, 195, 214, 128, 122, 206, 83, 248, 113, 136, 59, 248, 48, 211, 196, 227, 175, 195, 148, 103, 118, 136, 142, 5, 159, 166, 45, 182, 83, 246, 241, 211, 176, 153, 22, 7, 113, 224, 20, 237, 58, 116, 80, 45, 193, 40, 35, 80, 1, 26, 53, 99, 35, 147, 78, 47, 33, 63, 189, 72, 27, 234, 151, 167, 14, 147, 225, 139, 125, 71, 100, 31, 125, 220, 132, 217, 54, 219, 34, 143, 23, 101, 16, 235, 233, 111, 91, 70, 42, 24, 224, 200, 125, 48, 80, 95, 42, 79, 186, 226, 152, 92, 157, 211, 231, 205, 216, 197, 232, 138, 163, 27, 177, 100, 90, 85, 54, 72, 55, 210, 190, 92, 96, 243, 152, 186, 82, 13, 203, 230, 43, 89, 2, 27, 52, 84, 79, 245, 153, 52, 8, 217, 0, 39, 188, 236, 32, 200, 80, 240, 133, 199, 251, 49, 181, 109, 9, 176, 135, 84, 67, 25, 32, 31, 102, 25, 172, 71, 94, 59, 38, 200, 186, 170, 220, 189, 21, 239, 78])
32+
}
33+
34+
export function encryptionContext () {
35+
return {
36+
'aws-crypto-public-key': 'AnYtFEewZm+28KhIGpx8RhkaUaLccJpyf1nwIVQFGmypgzhH2XdSIBJ4sKiSH3ckzw==',
37+
simple: 'context'
38+
}
39+
}
40+
41+
class TestKeyring extends KeyringWebCrypto {
42+
async _onEncrypt () : Promise<WebCryptoEncryptionMaterial> {
43+
throw new Error('I should never see this error')
44+
}
45+
async _onDecrypt (material: WebCryptoDecryptionMaterial) {
46+
const unencryptedDataKey = new Uint8Array(material.suite.keyLengthBytes).fill(0)
47+
const trace = { keyNamespace: 'k', keyName: 'k', flags: KeyringTraceFlag.WRAPPING_KEY_DECRYPTED_DATA_KEY }
48+
return importForWebCryptoDecryptionMaterial(material.setUnencryptedDataKey(unencryptedDataKey, trace))
49+
}
50+
}
51+
52+
export function decryptKeyring () {
53+
return new TestKeyring()
54+
}

modules/encrypt-browser/test/encrypt.test.ts

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,74 @@
1515

1616
/* eslint-env mocha */
1717

18-
import { expect } from 'chai'
18+
import * as chai from 'chai'
19+
import chaiAsPromised from 'chai-as-promised'
1920
import 'mocha'
21+
import {
22+
WebCryptoDecryptionMaterial, // eslint-disable-line no-unused-vars
23+
WebCryptoEncryptionMaterial, // eslint-disable-line no-unused-vars
24+
KeyringWebCrypto, EncryptedDataKey,
25+
KeyringTraceFlag, WebCryptoAlgorithmSuite,
26+
importForWebCryptoEncryptionMaterial
27+
} from '@aws-crypto/material-management-browser'
28+
import {
29+
deserializeFactory
30+
} from '@aws-crypto/serialize'
2031
import { encrypt } from '../src/index'
32+
import { toUtf8, fromUtf8 } from '@aws-sdk/util-utf8-browser'
2133

22-
describe('encrypt', () => {
23-
/* This test is mostly to insure that I include all files.
24-
* So I can not lie on code coverage.
25-
*/
26-
it('is a function', () => {
27-
expect(encrypt).to.be.a('function')
34+
chai.use(chaiAsPromised)
35+
const { expect } = chai
36+
37+
const { deserializeMessageHeader } = deserializeFactory(toUtf8, WebCryptoAlgorithmSuite)
38+
39+
/* These tests only check structure.
40+
* see decrypt-node for actual cryptographic tests
41+
* see integration-node for exhaustive compatibility tests
42+
*/
43+
describe('encrypt structural testing', () => {
44+
const edk = new EncryptedDataKey({
45+
providerId: 'k',
46+
providerInfo: 'k',
47+
encryptedDataKey: new Uint8Array(3),
48+
/* rawInfo added because it will always be there when deserialized.
49+
* This way deep equal will pass nicely.
50+
* 107 is 'k' in ASCII
51+
*/
52+
rawInfo: new Uint8Array([107])
53+
})
54+
class TestKeyring extends KeyringWebCrypto {
55+
async _onEncrypt (material: WebCryptoEncryptionMaterial) {
56+
const unencryptedDataKey = new Uint8Array(material.suite.keyLengthBytes).fill(0)
57+
const trace = { keyNamespace: 'k', keyName: 'k', flags: KeyringTraceFlag.WRAPPING_KEY_GENERATED_DATA_KEY }
58+
material
59+
.setUnencryptedDataKey(unencryptedDataKey, trace)
60+
.addEncryptedDataKey(edk, KeyringTraceFlag.WRAPPING_KEY_ENCRYPTED_DATA_KEY)
61+
return importForWebCryptoEncryptionMaterial(material)
62+
}
63+
async _onDecrypt (): Promise<WebCryptoDecryptionMaterial> {
64+
throw new Error('I should never see this error')
65+
}
66+
}
67+
68+
const keyRing = new TestKeyring()
69+
70+
it('encrypt an ArrayBuffer', async () => {
71+
const encryptionContext = { simple: 'context' }
72+
73+
const plaintext = fromUtf8('asdf')
74+
const { cipherMessage, messageHeader } = await encrypt(keyRing, plaintext, { encryptionContext })
75+
76+
/* The default algorithm suite will add a signature key to the context.
77+
* So I only check that the passed context elements exist.
78+
*/
79+
expect(messageHeader.encryptionContext).to.haveOwnProperty('simple').and.to.equal('context')
80+
expect(messageHeader.encryptedDataKeys).lengthOf(1)
81+
expect(messageHeader.encryptedDataKeys[0]).to.deep.equal(edk)
82+
83+
const messageInfo = deserializeMessageHeader(cipherMessage)
84+
if (!messageInfo) throw new Error('I should never see this error')
85+
86+
expect(messageHeader).to.deep.equal(messageInfo.messageHeader)
2887
})
2988
})

0 commit comments

Comments
 (0)