Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
concatBuffers
} from '@aws-crypto/serialize'
import {
_onEncrypt,
_onDecrypt,
WebCryptoRawAesMaterial,
rawAesEncryptedDataKeyFactory,
rawAesEncryptedPartsFactory,
WrappingSuiteIdentifier, // eslint-disable-line no-unused-vars
WrapKey, // eslint-disable-line no-unused-vars
UnwrapKey // eslint-disable-line no-unused-vars
} from '@aws-crypto/raw-keyring'
import { fromUtf8, toUtf8 } from '@aws-sdk/util-utf8-browser'
import { randomValuesOnly } from '@aws-crypto/random-source-browser'
import { getWebCryptoBackend, getZeroByteSubtle } from '@aws-crypto/web-crypto-backend'
const { serializeEncryptionContext } = serializeFactory(fromUtf8)
const { rawAesEncryptedDataKey } = rawAesEncryptedDataKeyFactory(toUtf8, fromUtf8)
const { rawAesEncryptedParts } = rawAesEncryptedPartsFactory(fromUtf8)
const encryptFlags = KeyringTraceFlag.WRAPPING_KEY_ENCRYPTED_DATA_KEY | KeyringTraceFlag.WRAPPING_KEY_SIGNED_ENC_CTX
const decryptFlags = KeyringTraceFlag.WRAPPING_KEY_DECRYPTED_DATA_KEY | KeyringTraceFlag.WRAPPING_KEY_VERIFIED_ENC_CTX
export type RawAesKeyringWebCryptoInput = {
keyNamespace: string
keyName: string
masterKey: CryptoKey,
wrappingSuite: WrappingSuiteIdentifier
}
export class RawAesKeyringWebCrypto extends KeyringWebCrypto {
public keyNamespace!: string
public keyName!: string
_wrapKey!: WrapKey
import Duplexify from 'duplexify'
import { randomBytes } from 'crypto'
import {
MessageHeader, // eslint-disable-line no-unused-vars
serializeFactory, kdfInfo, ContentType, SerializationVersion, ObjectType,
FRAME_LENGTH,
MESSAGE_ID_LENGTH,
Maximum
} from '@aws-crypto/serialize'
// @ts-ignore
import { pipeline } from 'readable-stream'
import { Duplex } from 'stream' // eslint-disable-line no-unused-vars
const fromUtf8 = (input: string) => Buffer.from(input, 'utf8')
const { serializeMessageHeader, headerAuthIv } = serializeFactory(fromUtf8)
export interface EncryptStreamInput {
suiteId?: AlgorithmSuiteIdentifier
encryptionContext?: EncryptionContext
frameLength?: number
plaintextLength?: number
}
/**
* Takes a NodeDefaultCryptographicMaterialsManager or a KeyringNode that will
* be wrapped in a NodeDefaultCryptographicMaterialsManager and returns a stream.
*
* @param cmm NodeMaterialsManager|KeyringNode
* @param op EncryptStreamInput
*/
export function encryptStream (
kdfInfo,
concatBuffers,
MessageHeader, // eslint-disable-line no-unused-vars
SerializationVersion,
ObjectType,
ContentType,
serializeSignatureInfo,
FRAME_LENGTH,
MESSAGE_ID_LENGTH,
raw2der,
Maximum
} from '@aws-crypto/serialize'
import { fromUtf8 } from '@aws-sdk/util-utf8-browser'
import { getWebCryptoBackend } from '@aws-crypto/web-crypto-backend'
const serialize = serializeFactory(fromUtf8)
const { messageAADContentString, messageAAD } = aadFactory(fromUtf8)
export interface EncryptInput {
suiteId?: AlgorithmSuiteIdentifier
encryptionContext?: EncryptionContext
frameLength?: number
// plaintextLength?: number // Subtle Crypto functions are all one-shot, so frames and length are === plaintext.byteLength
}
export interface EncryptResult {
messageHeader: MessageHeader
result: Uint8Array
}
export async function encrypt (
cmm: KeyringWebCrypto|WebCryptoMaterialsManager,
} from '@aws-crypto/serialize'
import {
_onEncrypt,
_onDecrypt,
NodeRawAesMaterial,
rawAesEncryptedDataKeyFactory,
rawAesEncryptedPartsFactory,
WrappingSuiteIdentifier, // eslint-disable-line no-unused-vars
WrapKey, // eslint-disable-line no-unused-vars
UnwrapKey // eslint-disable-line no-unused-vars
} from '@aws-crypto/raw-keyring'
const fromUtf8 = (input: string) => Buffer.from(input, 'utf8')
const toUtf8 = (input: Uint8Array) => Buffer
.from(input.buffer, input.byteOffset, input.byteLength)
.toString('utf8')
const { serializeEncryptionContext } = serializeFactory(fromUtf8)
const { rawAesEncryptedDataKey } = rawAesEncryptedDataKeyFactory(toUtf8, fromUtf8)
const { rawAesEncryptedParts } = rawAesEncryptedPartsFactory(fromUtf8)
export type RawAesKeyringNodeInput = {
keyNamespace: string
keyName: string
unencryptedMasterKey: Uint8Array,
wrappingSuite: WrappingSuiteIdentifier
}
export class RawAesKeyringNode extends KeyringNode {
public keyNamespace!: string
public keyName!: string
_wrapKey!: WrapKey
_unwrapKey!: UnwrapKey
export function buildCryptographicMaterialsCacheKeyHelpers<s> (
fromUtf8: (input: string) => Uint8Array,
toUtf8: (input: Uint8Array) => string,
sha512: (...data: ((Uint8Array|string))[]) => Promise
): CryptographicMaterialsCacheKeyHelpersInterface<s> {
const {
serializeEncryptionContext,
serializeEncryptedDataKey
} = serializeFactory(fromUtf8)
return {
buildEncryptionMaterialCacheKey,
buildDecryptionMaterialCacheKey,
encryptedDataKeysHash,
encryptionContextHash
}
async function buildEncryptionMaterialCacheKey (
partition: string,
{ suite, encryptionContext }: EncryptionRequest<s>
) {
const algorithmInfo = suite
? [new Uint8Array([1]), uInt16BE(suite.id)]
: [new Uint8Array([0])]
</s></s></s>