-
Notifications
You must be signed in to change notification settings - Fork 63
feat: Encryption tests for integration-browser #159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
70255dc
feat: Encryption tests for integration-browser
seebees a6d34b4
Merge branch 'master' into decrypt-oracle-browser
seebees bb44841
Merge branch 'master' into decrypt-oracle-browser
seebees 7841651
comments
seebees 3429c98
more exposition
seebees File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
modules/integration-browser/src/build_encrypt_fixtures.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use | ||
* this file except in compliance with the License. A copy of the License is | ||
* located at | ||
* | ||
* http://aws.amazon.com/apache2.0/ | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
* implied. See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { | ||
EncryptManifestList, // eslint-disable-line no-unused-vars | ||
KeyList, // eslint-disable-line no-unused-vars | ||
KeyInfoTuple // eslint-disable-line no-unused-vars | ||
} from './types' | ||
import { randomBytes } from 'crypto' | ||
import { | ||
AlgorithmSuiteIdentifier, // eslint-disable-line no-unused-vars | ||
EncryptionContext // eslint-disable-line no-unused-vars | ||
} from '@aws-crypto/client-browser' | ||
import { URL } from 'url' | ||
import { readFileSync, writeFileSync } from 'fs' | ||
import got from 'got' | ||
|
||
/* This function interacts with manifest information | ||
* and produces the fixtures in the `fixtures` | ||
* that the karma server will consume to run tests. | ||
* This gives us 2 useful freedoms. | ||
* 1. The code is not tied to a specific copy of the manifest information | ||
* 2. The tests can be run on a subset of tests for debugging. | ||
*/ | ||
export async function buildEncryptFixtures (fixtures: string, manifestFile: string, keyFile: string, testName: string, slice: string) { | ||
const [start = 0, end = 9999] = (slice || '').split(':').map(n => parseInt(n, 10)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: This smells funny. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very funny. Just me working around some cmd line parsing :( |
||
const { tests, plaintexts }: EncryptManifestList = await getParsedJSON(manifestFile) | ||
const { keys }: KeyList = await getParsedJSON(keyFile) | ||
|
||
const plaintextBytes: {[name: string]: string} = {} | ||
|
||
Object | ||
.keys(plaintexts) | ||
.forEach(name => { | ||
/* Generate random bites as per spec. | ||
* See: https://github.com/awslabs/aws-crypto-tools-test-vector-framework/blob/master/features/0003-awses-message-encryption.md#plaintexts | ||
*/ | ||
plaintextBytes[name] = randomBytes(10).toString('base64') | ||
}) | ||
|
||
const testNames = [] | ||
let count = 0 | ||
|
||
for (const [name, testInfo] of Object.entries(tests)) { | ||
count += 1 | ||
|
||
if (testName) { | ||
if (name !== testName) continue | ||
} | ||
|
||
if (slice) { | ||
if (start >= count) continue | ||
if (count > end) continue | ||
} | ||
|
||
testNames.push(name) | ||
|
||
const { | ||
plaintext, | ||
'master-keys': masterKeys, | ||
algorithm, | ||
'frame-size': frameLength, | ||
'encryption-context': encryptionContext | ||
} = testInfo | ||
|
||
const keysInfo = <KeyInfoTuple[]>masterKeys.map(keyInfo => { | ||
const key = keys[keyInfo.key] | ||
if (!key) throw new Error(`no key for ${name}`) | ||
return [keyInfo, key] | ||
}) | ||
|
||
/* I'm expecting that the encrypt function will throw if this is not a supported AlgorithmSuiteIdentifier */ | ||
const suiteId = <AlgorithmSuiteIdentifier>parseInt(algorithm, 16) | ||
|
||
const test: EncryptTestVectorInfo = { | ||
name, | ||
keysInfo, | ||
plainTextData: plaintextBytes[plaintext], | ||
encryptOp: { suiteId, frameLength, encryptionContext } | ||
} | ||
|
||
writeFileSync(`${fixtures}/${name}.json`, JSON.stringify(test)) | ||
} | ||
|
||
writeFileSync(`${fixtures}/encrypt_tests.json`, JSON.stringify(testNames)) | ||
} | ||
|
||
export interface EncryptTestVectorInfo { | ||
name: string, | ||
keysInfo: KeyInfoTuple[], | ||
plainTextData: string, | ||
encryptOp: { | ||
suiteId: AlgorithmSuiteIdentifier, | ||
frameLength: number, | ||
encryptionContext: EncryptionContext | ||
} | ||
} | ||
|
||
async function getParsedJSON (thing: string) { | ||
try { | ||
const url = new URL(thing) | ||
if (url.protocol === 'file:') { | ||
return jsonAtPath(thing) | ||
} else { | ||
return jsonAtUrl(url) | ||
} | ||
} catch (ex) { | ||
return jsonAtPath(thing) | ||
} | ||
} | ||
async function jsonAtUrl (url: URL) { | ||
const { body } = await got(url) | ||
return JSON.parse(body) | ||
} | ||
|
||
function jsonAtPath (path: string) { | ||
const json = readFileSync(path, { encoding: 'utf-8' }) | ||
return JSON.parse(json) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.