Skip to content

Commit

Permalink
Change ipc schema to not pass argon2id parameters
Browse files Browse the repository at this point in the history
The SDK does not support custom parameters, not that it matters because
they will always be the same.

Also, SDK takes string for passphrase, so we can avoid doing conversion
by just passing the passphrase as a string.
  • Loading branch information
paw-hub committed Aug 15, 2024
1 parent 12f2935 commit cbcf99c
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,9 @@ interface NativeCryptoFacade {
key: DataWrapper,
fileUri: String,
): String
suspend fun argon2idHashRaw(
password: DataWrapper,
suspend fun argon2idGeneratePassphraseKey(
passphrase: String,
salt: DataWrapper,
timeCost: Int,
memoryCost: Int,
parallelism: Int,
hashLength: Int,
): DataWrapper
suspend fun generateKyberKeypair(
seed: DataWrapper,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,12 @@ class NativeCryptoFacadeReceiveDispatcher(
)
return json.encodeToString(result)
}
"argon2idHashRaw" -> {
val password: DataWrapper = json.decodeFromString(arg[0])
"argon2idGeneratePassphraseKey" -> {
val passphrase: String = json.decodeFromString(arg[0])
val salt: DataWrapper = json.decodeFromString(arg[1])
val timeCost: Int = json.decodeFromString(arg[2])
val memoryCost: Int = json.decodeFromString(arg[3])
val parallelism: Int = json.decodeFromString(arg[4])
val hashLength: Int = json.decodeFromString(arg[5])
val result: DataWrapper = this.facade.argon2idHashRaw(
password,
val result: DataWrapper = this.facade.argon2idGeneratePassphraseKey(
passphrase,
salt,
timeCost,
memoryCost,
parallelism,
hashLength,
)
return json.encodeToString(result)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,9 @@ public protocol NativeCryptoFacade {
_ key: DataWrapper,
_ fileUri: String
) async throws -> String
func argon2idHashRaw(
_ password: DataWrapper,
_ salt: DataWrapper,
_ timeCost: Int,
_ memoryCost: Int,
_ parallelism: Int,
_ hashLength: Int
func argon2idGeneratePassphraseKey(
_ passphrase: String,
_ salt: DataWrapper
) async throws -> DataWrapper
func generateKyberKeypair(
_ seed: DataWrapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,12 @@ public class NativeCryptoFacadeReceiveDispatcher {
fileUri
)
return toJson(result)
case "argon2idHashRaw":
let password = try! JSONDecoder().decode(DataWrapper.self, from: arg[0].data(using: .utf8)!)
case "argon2idGeneratePassphraseKey":
let passphrase = try! JSONDecoder().decode(String.self, from: arg[0].data(using: .utf8)!)
let salt = try! JSONDecoder().decode(DataWrapper.self, from: arg[1].data(using: .utf8)!)
let timeCost = try! JSONDecoder().decode(Int.self, from: arg[2].data(using: .utf8)!)
let memoryCost = try! JSONDecoder().decode(Int.self, from: arg[3].data(using: .utf8)!)
let parallelism = try! JSONDecoder().decode(Int.self, from: arg[4].data(using: .utf8)!)
let hashLength = try! JSONDecoder().decode(Int.self, from: arg[5].data(using: .utf8)!)
let result = try await self.facade.argon2idHashRaw(
password,
salt,
timeCost,
memoryCost,
parallelism,
hashLength
let result = try await self.facade.argon2idGeneratePassphraseKey(
passphrase,
salt
)
return toJson(result)
case "generateKyberKeypair":
Expand Down
16 changes: 2 additions & 14 deletions ipc-schema/facades/NativeCryptoFacade.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,13 @@
],
"ret": "string"
},
"argon2idHashRaw": {
"argon2idGeneratePassphraseKey": {
"arg": [
{
"password": "bytes"
"passphrase": "string"
},
{
"salt": "bytes"
},
{
"timeCost": "number"
},
{
"memoryCost": "number"
},
{
"parallelism": "number"
},
{
"hashLength": "number"
}
],
"ret": "bytes"
Expand Down
20 changes: 2 additions & 18 deletions src/common/api/worker/facades/Argon2idFacade.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
import {
Aes256Key,
ARGON2ID_ITERATIONS,
ARGON2ID_KEY_LENGTH,
ARGON2ID_MEMORY_IN_KiB,
ARGON2ID_PARALLELISM,
Argon2IDExports,
generateKeyFromPassphraseArgon2id,
uint8ArrayToBitArray,
} from "@tutao/tutanota-crypto"
import { Aes256Key, Argon2IDExports, generateKeyFromPassphraseArgon2id, uint8ArrayToBitArray } from "@tutao/tutanota-crypto"
import { LazyLoaded, stringToUtf8Uint8Array } from "@tutao/tutanota-utils"
import { NativeCryptoFacade } from "../../../native/common/generatedipc/NativeCryptoFacade.js"
import { assertWorkerOrNode } from "../../common/Env.js"
Expand Down Expand Up @@ -49,14 +40,7 @@ export class NativeArgon2idFacade implements Argon2idFacade {
constructor(private readonly nativeCryptoFacade: NativeCryptoFacade) {}

async generateKeyFromPassphrase(passphrase: string, salt: Uint8Array): Promise<Aes256Key> {
const hash = await this.nativeCryptoFacade.argon2idHashRaw(
stringToUtf8Uint8Array(passphrase),
salt,
ARGON2ID_ITERATIONS,
ARGON2ID_MEMORY_IN_KiB,
ARGON2ID_PARALLELISM,
ARGON2ID_KEY_LENGTH,
)
const hash = await this.nativeCryptoFacade.argon2idGeneratePassphraseKey(passphrase, salt)
return uint8ArrayToBitArray(hash)
}
}
11 changes: 2 additions & 9 deletions src/common/desktop/DesktopNativeCryptoFacade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,8 @@ export class DesktopNativeCryptoFacade implements NativeCryptoFacade {
throw new Error("not implemented for this platform")
}

async argon2idHashRaw(
password: Uint8Array,
salt: Uint8Array,
timeCost: number,
memoryCost: number,
parallelism: number,
hashLength: number,
): Promise<Uint8Array> {
const hash = await generateKeyFromPassphraseArgon2id(await this.argon2, utf8Uint8ArrayToString(password), salt)
async argon2idGeneratePassphraseKey(passphrase: string, salt: Uint8Array): Promise<Uint8Array> {
const hash = await generateKeyFromPassphraseArgon2id(await this.argon2, passphrase, salt)
return bitArrayToUint8Array(hash)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface NativeCryptoFacade {
*/
aesDecryptFile(key: Uint8Array, fileUri: string): Promise<string>

argon2idHashRaw(password: Uint8Array, salt: Uint8Array, timeCost: number, memoryCost: number, parallelism: number, hashLength: number): Promise<Uint8Array>
argon2idGeneratePassphraseKey(passphrase: string, salt: Uint8Array): Promise<Uint8Array>

generateKyberKeypair(seed: Uint8Array): Promise<KyberKeyPair>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,10 @@ export class NativeCryptoFacadeReceiveDispatcher {
const fileUri: string = arg[1]
return this.facade.aesDecryptFile(key, fileUri)
}
case "argon2idHashRaw": {
const password: Uint8Array = arg[0]
case "argon2idGeneratePassphraseKey": {
const passphrase: string = arg[0]
const salt: Uint8Array = arg[1]
const timeCost: number = arg[2]
const memoryCost: number = arg[3]
const parallelism: number = arg[4]
const hashLength: number = arg[5]
return this.facade.argon2idHashRaw(password, salt, timeCost, memoryCost, parallelism, hashLength)
return this.facade.argon2idGeneratePassphraseKey(passphrase, salt)
}
case "generateKyberKeypair": {
const seed: Uint8Array = arg[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export class NativeCryptoFacadeSendDispatcher implements NativeCryptoFacade {
async aesDecryptFile(...args: Parameters<NativeCryptoFacade["aesDecryptFile"]>) {
return this.transport.invokeNative("ipc", ["NativeCryptoFacade", "aesDecryptFile", ...args])
}
async argon2idHashRaw(...args: Parameters<NativeCryptoFacade["argon2idHashRaw"]>) {
return this.transport.invokeNative("ipc", ["NativeCryptoFacade", "argon2idHashRaw", ...args])
async argon2idGeneratePassphraseKey(...args: Parameters<NativeCryptoFacade["argon2idGeneratePassphraseKey"]>) {
return this.transport.invokeNative("ipc", ["NativeCryptoFacade", "argon2idGeneratePassphraseKey", ...args])
}
async generateKyberKeypair(...args: Parameters<NativeCryptoFacade["generateKyberKeypair"]>) {
return this.transport.invokeNative("ipc", ["NativeCryptoFacade", "generateKyberKeypair", ...args])
Expand Down

0 comments on commit cbcf99c

Please sign in to comment.