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

reconstructKey: Remove ciphertext padding #65

Merged
merged 2 commits into from
Oct 4, 2023
Merged
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
36 changes: 9 additions & 27 deletions Sources/TorusUtils/Extensions/TorusUtils+extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,7 @@ extension TorusUtils {

if thresholdPublicKey?.X != nil && (thresholdNonceData != nil && thresholdNonceData?.pubNonce?.x != "" || verifierParams.extended_verifier_id != nil || isLegacyNetwork()) {
// Code block to execute if all conditions are true
var sharePkcs7 = [String]()
var shareZeroPadding = [String]()
var shares = [String]()
var sessionTokenSigPromises = [String?]()
var sessionTokenPromises = [String?]()
var nodeIndexes = [Int]()
Expand Down Expand Up @@ -449,20 +448,11 @@ extension TorusUtils {
let latestKey = currentShareResponse.keys[0]
nodeIndexes.append(Int(latestKey.nodeIndex))
let data = Data(base64Encoded: latestKey.share, options: [] )!
let binaryString = String(data: data, encoding: .ascii) ?? ""
let paddedBinaryString = binaryString.padding(toLength: 64, withPad: "0", startingAt: 0)
var decryptedShare = try decryptNodeData(eciesData: latestKey.shareMetadata, ciphertextHex: paddedBinaryString, privKey: sessionAuthKey)
sharePkcs7.append(decryptedShare.addLeading0sForLength64())
// temporary workaround on decrypt padding issue
if ( decryptedShare.count < 64 ) {

decryptedShare = try decryptNodeData(eciesData: latestKey.shareMetadata, ciphertextHex: paddedBinaryString, privKey: sessionAuthKey, padding: .zeroPadding).addLeading0sForLength64()
shareZeroPadding.append(decryptedShare)
} else {
shareZeroPadding.append(decryptedShare)
guard let ciphertextHex = String(data: data, encoding: .ascii) else {
throw TorusUtilError.decodingFailed()
}


var decryptedShare = try decryptNodeData(eciesData: latestKey.shareMetadata, ciphertextHex: ciphertextHex, privKey: sessionAuthKey)
shares.append(decryptedShare.addLeading0sForLength64())
} else {
os_log("retrieveShare - 0 keys returned from nodes", log: getTorusLogger(log: TorusUtilsLogger.core, type: .error), type: .error)
throw TorusUtilError.thresholdError
Expand Down Expand Up @@ -508,23 +498,15 @@ extension TorusUtils {
sessionTokenData.append(SessionToken(token: token, signature: signature!, node_pubx: nodePubX, node_puby: nodePubY))
}
}
let decryptedSharesPkcs7 = sharePkcs7.enumerated().reduce(into: [ Int : String ]()) { acc, current in

let sharesWithIndex = shares.enumerated().reduce(into: [ Int : String ]()) { acc, current in
let (index, curr) = current
acc[nodeIndexes[index]] = curr
}




var returnedKey = try reconstructKey(decryptedShares: decryptedSharesPkcs7, thresholdPublicKey: thresholdPublicKey!)

let returnedKey = try reconstructKey(decryptedShares: sharesWithIndex, thresholdPublicKey: thresholdPublicKey!)
if (returnedKey == nil) {
let decryptedSharesZeroPadding = shareZeroPadding.enumerated().reduce(into: [ Int : String ]()) { acc, current in
let (index, curr) = current
acc[nodeIndexes[index]] = curr
}
returnedKey = try reconstructKey(decryptedShares: decryptedSharesZeroPadding, thresholdPublicKey: thresholdPublicKey!)

throw TorusUtilError.privateKeyDeriveFailed
}

guard let oAuthKey = returnedKey else {
Expand Down
17 changes: 14 additions & 3 deletions Tests/TorusUtilsTests/SapphireTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,9 @@ final class SapphireTest: XCTestCase {
}
}

func testAggregrateLogin() async throws {
func testAggregrateLoginWithEmail(email: String) async throws {
let exp1 = XCTestExpectation(description: "Should be able to aggregate login")

let email = generateRandomEmail(of: 6)

print("email", email)
let verifier: String = TORUS_TEST_AGGREGATE_VERIFIER
let verifierID: String = email
Expand Down Expand Up @@ -447,4 +446,16 @@ final class SapphireTest: XCTestCase {
}
}

func testAggregateLoginWithFixedEmail() async throws {
// This fixed email was previously known to trigger an edge case that
// revealed a bug in our share decryption implementation.
let email = "[email protected]"
try await testAggregrateLoginWithEmail(email: email)
}

func testAggregateLoginWithRandomEmail() async throws {
let email = generateRandomEmail(of: 6)
try await testAggregrateLoginWithEmail(email: email)
}

}
Loading