From 12968b4a5d6344df8a6f39604b6454e1cc24218d Mon Sep 17 00:00:00 2001 From: seebees Date: Mon, 8 Jul 2019 16:19:29 -0700 Subject: [PATCH] fix: Add tests for sha512 Better testing for caching_materials_manager_browser. Pull out the injected hash function and write some simple tests. --- .../src/caching_materials_manager_browser.ts | 14 +------ .../src/sha512.ts | 28 +++++++++++++ .../test/sha512.test.ts | 42 +++++++++++++++++++ 3 files changed, 72 insertions(+), 12 deletions(-) create mode 100644 modules/caching-materials-manager-browser/src/sha512.ts create mode 100644 modules/caching-materials-manager-browser/test/sha512.test.ts diff --git a/modules/caching-materials-manager-browser/src/caching_materials_manager_browser.ts b/modules/caching-materials-manager-browser/src/caching_materials_manager_browser.ts index 6184d41f9..d41184d9e 100644 --- a/modules/caching-materials-manager-browser/src/caching_materials_manager_browser.ts +++ b/modules/caching-materials-manager-browser/src/caching_materials_manager_browser.ts @@ -32,18 +32,8 @@ import { WebCryptoGetDecryptMaterials // eslint-disable-line no-unused-vars } from '@aws-crypto/material-management-browser' import { fromUtf8, toUtf8 } from '@aws-sdk/util-utf8-browser' -import { getWebCryptoBackend, getNonZeroByteBackend, synchronousRandomValues } from '@aws-crypto/web-crypto-backend' -import { concatBuffers } from '@aws-crypto/serialize' - -const sha512 = async (...inputs: (Uint8Array|string)[]) => { - // Normalize to Uint8Array and squash into a single value. - const data = concatBuffers(...inputs.map(u => typeof u === 'string' ? fromUtf8(u) : u)) - // Prefer the non-zero byte because this will always be the native implementation. - const backend = getNonZeroByteBackend(await getWebCryptoBackend()) - // Do the hash - const ab = await backend.digest('SHA-512', data) - return new Uint8Array(ab) -} +import { synchronousRandomValues } from '@aws-crypto/web-crypto-backend' +import { sha512 } from './sha512' const cacheKeyHelpers = buildCryptographicMaterialsCacheKeyHelpers(fromUtf8, toUtf8, sha512) diff --git a/modules/caching-materials-manager-browser/src/sha512.ts b/modules/caching-materials-manager-browser/src/sha512.ts new file mode 100644 index 000000000..0c86a4931 --- /dev/null +++ b/modules/caching-materials-manager-browser/src/sha512.ts @@ -0,0 +1,28 @@ +/* + * 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 { fromUtf8 } from '@aws-sdk/util-utf8-browser' +import { getWebCryptoBackend, getNonZeroByteBackend } from '@aws-crypto/web-crypto-backend' +import { concatBuffers } from '@aws-crypto/serialize' + +export const sha512 = async (...inputs: (Uint8Array|string)[]) => { + // Normalize to Uint8Array and squash into a single value. + const data = concatBuffers(...inputs.map(u => typeof u === 'string' ? fromUtf8(u) : u)) + // Prefer the non-zero byte because this will always be the native implementation. + const backend = getNonZeroByteBackend(await getWebCryptoBackend()) + // Do the hash + const ab = await backend.digest('SHA-512', data) + return new Uint8Array(ab) +} diff --git a/modules/caching-materials-manager-browser/test/sha512.test.ts b/modules/caching-materials-manager-browser/test/sha512.test.ts new file mode 100644 index 000000000..4af381f2f --- /dev/null +++ b/modules/caching-materials-manager-browser/test/sha512.test.ts @@ -0,0 +1,42 @@ +/* + * 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. + */ + +/* eslint-env mocha */ + +import { expect } from 'chai' +import 'mocha' +import { sha512 } from '../src/sha512' + +// sha512('asdf') +const fixture = new Uint8Array([64, 27, 9, 234, 179, 192, 19, 212, 202, 84, 146, 43, 184, 2, 190, 200, 253, 83, 24, 25, 43, 10, 117, 242, 1, 216, 179, 114, 116, 41, 8, 15, 179, 55, 89, 26, 189, 62, 68, 69, 59, 149, 69, 85, 183, 160, 129, 46, 16, 129, 195, 155, 116, 2, 147, 247, 101, 234, 231, 49, 245, 166, 94, 209]) + +describe('WebCryptoCachingMaterialsManager', () => { + it('can hash a string', async () => { + const test = await sha512('asdf') + expect(test).to.deep.equal(fixture) + }) + + it('can hash a Uint8Array', async () => { + // the string 'asdf' as utf-8 encoded bytes + const test = await sha512(new Uint8Array([ 97, 115, 100, 102 ])) + expect(test).to.deep.equal(fixture) + }) + + it('can hash a mix of arguments', async () => { + // the string 'asdf' as a mix of strings and binary + const test = await sha512('a', new Uint8Array([115]), 'd', new Uint8Array([102])) + expect(test).to.deep.equal(fixture) + }) +})