Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
feat: WIP decryption
Browse files Browse the repository at this point in the history
  • Loading branch information
kodzonko committed Jun 29, 2023
1 parent f0f3fd1 commit 0f55f92
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 30 deletions.
77 changes: 51 additions & 26 deletions sdex-encrypted-communicator/src/crypto/SdexCrypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import * as Crypto from "expo-crypto";
import { EncryptionError } from "../Errors";
import logger from "../Logger";
import {
bytesToString,
changeTo1IndexedArray,
mergeUint8Arrays,
splitMessageIntoBlocks
changeTo1IndexedArray,
mergeUint8Arrays,
splitMessageIntoBlocks,
} from "../utils/Converters";
import { xorUintArrays } from "../utils/Math";

Expand Down Expand Up @@ -70,9 +69,9 @@ export default class SdexCrypto {
return xorUintArrays(block, hash1, hash2);
}

encryptMessage(messagePlainTextByteArray: Uint8Array): Uint8Array {
calculateMessage(messageByteArray: Uint8Array): Uint8Array {
// Message split into blocks with length equal to hashes to facilitate XOR operations
const messageSplit = splitMessageIntoBlocks(messagePlainTextByteArray, this.HASH_LENGTH);
const messageSplit = splitMessageIntoBlocks(messageByteArray, this.HASH_LENGTH);
// Array changed to 1-based indexing to follow SDEx algorithm more easily
const messageBlocks = changeTo1IndexedArray(messageSplit);

Expand All @@ -82,13 +81,12 @@ export default class SdexCrypto {
const sessionKeyHash = this.blake3Wrapper(this.sessionKey); // aka hash from initialization vector H(IV)

// First block
logger.debug("Encrypting 1st block.");
logger.debug("Calculating k=d1 block.");
// IV++h0
const sessionKeyAndInitializationHash = mergeUint8Arrays(
this.sessionKey,
this.initializationHash,
);

// H(IV++h0)
const hashFromSessionKeyAndInitializationHash = this.blake3Wrapper(
sessionKeyAndInitializationHash,
Expand All @@ -109,7 +107,7 @@ export default class SdexCrypto {
);

// Second block
logger.debug("Encrypting 2nd block.");
logger.debug("Calculating k=2 block.");
// C2
result[2] = SdexCrypto.calculateBlock(
messageBlocks[2] ?? this.ZEROED_BLOCK,
Expand Down Expand Up @@ -143,7 +141,7 @@ export default class SdexCrypto {
),
);
}
logger.debug(`Encrypting k=${k + 1} block.`);
logger.debug(`Calculating k=${k + 1} block.`);
// Odd blocks in 1-based indexing (k=3, 5, 7...)
result[2 * k + 1] = SdexCrypto.calculateBlock(
messageBlocks[2 * k + 1] ?? this.ZEROED_BLOCK,
Expand All @@ -162,20 +160,47 @@ export default class SdexCrypto {
return mergeUint8Arrays(...nonEmptyArray);
}

decryptMessage(messageCipherTextByteArray: Uint8Array): string {
// Message split into blocks with length equal to hashes to facilitate XOR operations
const messageSplit = splitMessageIntoBlocks(messageCipherTextByteArray, this.HASH_LENGTH);
// Array changed to 1-based indexing to follow SDEx algorithm more easily
const messageBlocks = changeTo1IndexedArray(messageSplit);

const result = <Uint8Array[]>[];
const hashIterations = <Uint8Array[]>[]; // h0, h1, ..., hk
hashIterations[0] = this.initializationHash; // h0
const sessionKeyHash = this.blake3Wrapper(this.sessionKey); // aka hash from initialization vector H(IV)

// Remove empty element (at 0 index)
const nonEmptyArray = result.filter((element) => element);
const mergedResult = mergeUint8Arrays(...nonEmptyArray);
return bytesToString(mergedResult);
}
//
// decryptMessage(messageCipherTextByteArray: Uint8Array): string {
// // Message split into blocks with length equal to hashes to facilitate XOR operations
// const messageSplit = splitMessageIntoBlocks(messageCipherTextByteArray, this.HASH_LENGTH);
// // Array changed to 1-based indexing to follow SDEx algorithm more easily
// const messageBlocks = changeTo1IndexedArray(messageSplit);
//
// const result = <Uint8Array[]>[];
// const hashIterations = <Uint8Array[]>[]; // h0, h1, ..., hk
// hashIterations[0] = this.initializationHash; // h0
// const sessionKeyHash = this.blake3Wrapper(this.sessionKey); // aka hash from initialization vector H(IV)
//
// // First block
// logger.debug("Decrypting 1st block.");
// // IV++h0
// const sessionKeyAndInitializationHash = mergeUint8Arrays(
// this.sessionKey,
// this.initializationHash,
// );
// // H(IV++h0)
// const hashFromSessionKeyAndInitializationHash = this.blake3Wrapper(
// sessionKeyAndInitializationHash,
// );
// // M1
// result[1] = SdexCrypto.calculateBlock(
// messageBlocks[1] ?? this.ZEROED_BLOCK,
// sessionKeyHash,
// hashFromSessionKeyAndInitializationHash,
// );
// // h1
// hashIterations[1] = this.blake3Wrapper(
// sessionKeyAndInitializationHash,
// mergeUint8Arrays(
// messageBlocks[0] ?? this.ZEROED_BLOCK,
// messageBlocks[1] ?? this.ZEROED_BLOCK,
// ),
// );
//
// // Remove empty element (at 0 index)
// const nonEmptyArray = result.filter((element) => element);
// const mergedResult = mergeUint8Arrays(...nonEmptyArray);
// return bytesToString(mergedResult);
// }
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { blake3 } from "@noble/hashes/blake3";
import SdexCrypto from "../../../src/crypto/SdexCrypto";
import { EncryptionError } from "../../../src/Errors";
import { stringToBytes } from "../../../src/utils/Converters";
import { bytesToString, stringToBytes } from "../../../src/utils/Converters";

const initializationHash = blake3("test-initialization-hash", { dkLen: 32 });
const hashFromUserPassword = blake3("test-user-password-hash", { dkLen: 32 });
Expand Down Expand Up @@ -43,7 +43,7 @@ test("Calculating a block throws error on mismatching arrays lengths.", () => {

test("Encrypting a message properly.", () => {
const message = stringToBytes("Hello world!");
const result = messageEncryptorDecryptor.encryptMessage(message);
const result = messageEncryptorDecryptor.calculateMessage(message);
const expected = new Uint8Array([
33, 219, 133, 253, 234, 100, 96, 76, 182, 215, 34, 78, 50, 106, 19, 179, 173, 229, 8, 238, 29,
117, 182, 204, 171, 238, 138, 5, 127, 84, 88, 172, 81, 109, 231, 222, 197, 40, 214, 110, 117,
Expand Down Expand Up @@ -72,7 +72,7 @@ test("Decrypting a message properly.", () => {
246, 22, 45, 24, 147, 248, 226, 245, 8, 160, 10, 244, 230, 192, 122, 186, 8, 99, 216, 180, 52,
32, 19, 192, 156,
]);
const result = messageEncryptorDecryptor.decryptMessage(message);
const result = bytesToString(messageEncryptorDecryptor.calculateMessage(message));
const expected = "Hello world!";
expect(result).toEqual(expected);
expect(result).toBe(expected);
});

0 comments on commit 0f55f92

Please sign in to comment.