Skip to content
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

feat(sdk): remove hex encoding for segment hash #397

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions lib/tdf3/src/assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ export function isAssertionConfig(obj: unknown): obj is AssertionConfig {
*/
export async function verify(
thiz: Assertion,
aggregateHash: string,
key: AssertionKey
aggregateHash: Uint8Array,
key: AssertionKey,
isLegacyTDF: boolean
): Promise<void> {
let payload: AssertionPayload;
try {
Expand All @@ -126,14 +127,25 @@ export async function verify(

// Get the hash of the assertion
const hashOfAssertion = await hash(thiz);
const combinedHash = aggregateHash + hashOfAssertion;
const encodedHash = base64.encode(combinedHash);

// check if assertionHash is same as hashOfAssertion
if (hashOfAssertion !== assertionHash) {
throw new IntegrityError('Assertion hash mismatch');
}

let encodedHash: string;
if (isLegacyTDF) {
const aggregateHashAsStr = new TextDecoder('utf-8').decode(aggregateHash);
const combinedHash = aggregateHashAsStr + hashOfAssertion;
encodedHash = base64.encode(combinedHash);
} else {
const combinedHash = concatenateUint8Arrays(
aggregateHash,
new Uint8Array(hex.decodeArrayBuffer(assertionHash))
);
encodedHash = base64.encodeArrayBuffer(combinedHash);
}

// check if assertionSig is same as encodedHash
if (assertionSig !== encodedHash) {
throw new IntegrityError('Failed integrity check on assertion signature');
Expand All @@ -144,7 +156,7 @@ export async function verify(
* Creates an Assertion object with the specified properties.
*/
export async function CreateAssertion(
aggregateHash: string,
aggregateHash: Uint8Array,
assertionConfig: AssertionConfig
): Promise<Assertion> {
if (!assertionConfig.signingKey) {
Expand All @@ -162,8 +174,11 @@ export async function CreateAssertion(
};

const assertionHash = await hash(a);
const combinedHash = aggregateHash + assertionHash;
const encodedHash = base64.encode(combinedHash);
const combinedHash = concatenateUint8Arrays(
aggregateHash,
new Uint8Array(hex.decodeArrayBuffer(assertionHash))
);
const encodedHash = base64.encodeArrayBuffer(combinedHash);

return await sign(a, assertionHash, encodedHash, assertionConfig.signingKey);
}
Expand All @@ -189,3 +204,13 @@ export type AssertionVerificationKeys = {
DefaultKey?: AssertionKey;
Keys: Record<string, AssertionKey>;
};

function concatenateUint8Arrays(array1: Uint8Array, array2: Uint8Array): Uint8Array {
const combinedLength = array1.length + array2.length;
const combinedArray = new Uint8Array(combinedLength);

combinedArray.set(array1, 0);
combinedArray.set(array2, array1.length);

return combinedArray;
}
1 change: 1 addition & 0 deletions lib/tdf3/src/models/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export type Manifest = {
payload: Payload;
encryptionInformation: EncryptionInformation;
assertions: Assertion[];
tdf_spec_version: string;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like this is importing this file which specifies version 0.2.0 of the TDF spec. Is that what we should be doing in java?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this should somehow be the spec version, which AFAIK is 4.3.0

https://github.com/opentdf/spec/blob/3c5adf29fe9c327b2f85b89e8beee09f0b91d8c7/VERSION

};
120 changes: 81 additions & 39 deletions lib/tdf3/src/tdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { DecoratedReadableStream } from './client/DecoratedReadableStream.js';
import { fetchKasPubKey as fetchKasPubKeyV2, fetchWrappedKey } from '../../src/access.js';
import { DecryptParams } from './client/builders.js';
import { AssertionConfig, AssertionKey, AssertionVerificationKeys } from './assertions.js';
import { version } from './version.js';
import { hex } from '../../src/encodings/index.js';
import * as assertions from './assertions.js';

import {
Expand Down Expand Up @@ -269,26 +271,35 @@ async function _generateManifest(
// generate the manifest first, then insert integrity information into it
encryptionInformation: encryptionInformationStr,
assertions: assertions,
tdf_spec_version: version,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
tdf_spec_version: version,
tdf_spec_version: '4.3.0',

};
}

async function getSignature(
unwrappedKeyBinary: Binary,
payloadBinary: Binary,
unwrappedKey: Uint8Array,
content: Uint8Array,
algorithmType: IntegrityAlgorithm,
cryptoService: CryptoService
) {
) : Promise<Uint8Array> {
switch (algorithmType.toUpperCase()) {
case 'GMAC':
// use the auth tag baked into the encrypted payload
return buffToString(Uint8Array.from(payloadBinary.asByteArray()).slice(-16), 'hex');
return content.slice(-16);
case 'HS256':
// simple hmac is the default
return await cryptoService.hmac(
buffToString(new Uint8Array(unwrappedKeyBinary.asArrayBuffer()), 'hex'),
buffToString(new Uint8Array(payloadBinary.asArrayBuffer()), 'utf-8')
const cryptoKey = await crypto.subtle.importKey(
'raw',
unwrappedKey,
{
name: 'HMAC',
hash: { name: 'SHA-256' },
},
true,
['sign', 'verify']
);
default:
const signature = await crypto.subtle.sign('HMAC', cryptoKey, content);
return new Uint8Array(signature);
default:``
throw new ConfigurationError(`Unsupported signature alg [${algorithmType}]`);
}
}
Expand Down Expand Up @@ -321,7 +332,7 @@ export async function writeStream(cfg: EncryptConfiguration): Promise<DecoratedR
let bytesProcessed = 0;
let crcCounter = 0;
let fileByteCount = 0;
let aggregateHash = '';
const segmentHashList: Uint8Array[] = [];

const zipWriter = new ZipWriter();
const manifest = await _generateManifest(
Expand Down Expand Up @@ -414,14 +425,17 @@ export async function writeStream(cfg: EncryptConfiguration): Promise<DecoratedR
fileByteCount = 0;

// hash the concat of all hashes
const payloadSigStr = await getSignature(
cfg.keyForEncryption.unwrappedKeyBinary,
Binary.fromString(aggregateHash),
const aggregateHash = await concatenateUint8Array(segmentHashList);

const payloadSig = await getSignature(
new Uint8Array(cfg.keyForEncryption.unwrappedKeyBinary.asArrayBuffer()),
aggregateHash,
cfg.integrityAlgorithm,
cfg.cryptoService
);
manifest.encryptionInformation.integrityInformation.rootSignature.sig =
base64.encode(payloadSigStr);

const rootSig = base64.encodeArrayBuffer(payloadSig);
manifest.encryptionInformation.integrityInformation.rootSignature.sig = rootSig;
manifest.encryptionInformation.integrityInformation.rootSignature.alg =
cfg.integrityAlgorithm;

Expand Down Expand Up @@ -527,18 +541,17 @@ export async function writeStream(cfg: EncryptConfiguration): Promise<DecoratedR
cfg.keyForEncryption.unwrappedKeyBinary
);
const payloadBuffer = new Uint8Array(encryptedResult.payload.asByteArray());
const payloadSigStr = await getSignature(
cfg.keyForEncryption.unwrappedKeyBinary,
encryptedResult.payload,
const payloadSig = await getSignature(
new Uint8Array(cfg.keyForEncryption.unwrappedKeyBinary.asArrayBuffer()),
new Uint8Array(encryptedResult.payload.asArrayBuffer()),
cfg.segmentIntegrityAlgorithm,
cfg.cryptoService
);

// combined string of all hashes for root signature
aggregateHash += payloadSigStr;

segmentHashList.push(new Uint8Array(payloadSig));

segmentInfos.push({
hash: base64.encode(payloadSigStr),
hash: base64.encodeArrayBuffer(payloadSig),
segmentSize: chunk.length === segmentSizeDefault ? undefined : chunk.length,
encryptedSegmentSize:
payloadBuffer.length === encryptedSegmentSizeDefault ? undefined : payloadBuffer.length,
Expand Down Expand Up @@ -715,17 +728,21 @@ async function decryptChunk(
hash: string,
cipher: SymmetricCipher,
segmentIntegrityAlgorithm: IntegrityAlgorithm,
cryptoService: CryptoService
cryptoService: CryptoService,
isLegacyTDF: boolean
): Promise<DecryptResult> {
if (segmentIntegrityAlgorithm !== 'GMAC' && segmentIntegrityAlgorithm !== 'HS256') {
}
const segmentHashStr = await getSignature(
reconstructedKeyBinary,
Binary.fromArrayBuffer(encryptedChunk.buffer),
const segmentSig = await getSignature(
new Uint8Array(reconstructedKeyBinary.asArrayBuffer()),
encryptedChunk,
segmentIntegrityAlgorithm,
cryptoService
);
if (hash !== btoa(segmentHashStr)) {

const segmentHash = isLegacyTDF ? base64.encode(hex.encodeArrayBuffer(segmentSig)) :base64.encodeArrayBuffer(segmentSig);

if (hash !== segmentHash) {
throw new IntegrityError('Failed integrity check on segment hash');
}
return await cipher.decrypt(encryptedChunk, reconstructedKeyBinary);
Expand All @@ -738,7 +755,8 @@ async function updateChunkQueue(
reconstructedKeyBinary: Binary,
cipher: SymmetricCipher,
segmentIntegrityAlgorithm: IntegrityAlgorithm,
cryptoService: CryptoService
cryptoService: CryptoService,
isLegacyTDF: boolean
) {
const chunksInOneDownload = 500;
let requests = [];
Expand Down Expand Up @@ -779,6 +797,7 @@ async function updateChunkQueue(
slice,
cipher,
segmentIntegrityAlgorithm,
isLegacyTDF,
});
}
})()
Expand All @@ -793,13 +812,15 @@ export async function sliceAndDecrypt({
cipher,
cryptoService,
segmentIntegrityAlgorithm,
isLegacyTDF,
}: {
buffer: Uint8Array;
reconstructedKeyBinary: Binary;
slice: Chunk[];
cipher: SymmetricCipher;
cryptoService: CryptoService;
segmentIntegrityAlgorithm: IntegrityAlgorithm;
isLegacyTDF: boolean;
}) {
for (const index in slice) {
const { encryptedOffset, encryptedSegmentSize, _resolve, _reject } = slice[index];
Expand All @@ -817,7 +838,8 @@ export async function sliceAndDecrypt({
slice[index]['hash'],
cipher,
segmentIntegrityAlgorithm,
cryptoService
cryptoService,
isLegacyTDF
);
slice[index].decryptedChunk = result;
if (_resolve) {
Expand Down Expand Up @@ -864,23 +886,36 @@ export async function readStream(cfg: DecryptConfiguration) {
const keyForDecryption = await cfg.keyMiddleware(reconstructedKeyBinary);
const encryptedSegmentSizeDefault = defaultSegmentSize || DEFAULT_SEGMENT_SIZE;

// check the combined string of hashes
const aggregateHash = segments.map(({ hash }) => base64.decode(hash)).join('');
// check if the TDF is a legacy TDF
const isLegacyTDF = manifest.tdf_spec_version ? false : true;

// Decode each hash and store it in an array of Uint8Array
const segmentHashList = segments.map(
({ hash }) => new Uint8Array(base64.decodeArrayBuffer(hash))
);

// Concatenate all segment hashes into a single Uint8Array
const aggregateHash = await concatenateUint8Array(segmentHashList);

const integrityAlgorithm = rootSignature.alg;
if (integrityAlgorithm !== 'GMAC' && integrityAlgorithm !== 'HS256') {
throw new UnsupportedError(`Unsupported integrity alg [${integrityAlgorithm}]`);
}
const payloadSigStr = await getSignature(
keyForDecryption,
Binary.fromString(aggregateHash),

const payloadForSigCalculation = isLegacyTDF ?
new TextEncoder().encode(hex.encodeArrayBuffer(aggregateHash)) : aggregateHash;
const payloadSig = await getSignature(
new Uint8Array(keyForDecryption.asArrayBuffer()),
payloadForSigCalculation,
integrityAlgorithm,
cfg.cryptoService
);

if (
manifest.encryptionInformation.integrityInformation.rootSignature.sig !==
base64.encode(payloadSigStr)
) {
const rootSig = isLegacyTDF
? base64.encode(hex.encodeArrayBuffer(payloadSig))
: base64.encodeArrayBuffer(payloadSig);

if (manifest.encryptionInformation.integrityInformation.rootSignature.sig !== rootSig) {
throw new IntegrityError('Failed integrity check on root signature');
}

Expand All @@ -898,7 +933,7 @@ export async function readStream(cfg: DecryptConfiguration) {
assertionKey = foundKey;
}
}
await assertions.verify(assertion, aggregateHash, assertionKey);
await assertions.verify(assertion, aggregateHash, assertionKey, isLegacyTDF);
}
}

Expand Down Expand Up @@ -939,7 +974,8 @@ export async function readStream(cfg: DecryptConfiguration) {
keyForDecryption,
cipher,
segmentIntegrityAlg,
cfg.cryptoService
cfg.cryptoService,
isLegacyTDF
);

let progress = 0;
Expand Down Expand Up @@ -972,3 +1008,9 @@ export async function readStream(cfg: DecryptConfiguration) {
outputStream.metadata = metadata;
return outputStream;
}

async function concatenateUint8Array(uint8arrays: Uint8Array[]): Promise<Uint8Array> {
const blob = new Blob(uint8arrays);
const buffer = await blob.arrayBuffer();
return new Uint8Array(buffer);
}
Loading