Skip to content

Commit

Permalink
iOS: added more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdapioneer committed Nov 23, 2023
1 parent 673e549 commit 4c1c3a8
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 29 deletions.
27 changes: 27 additions & 0 deletions ios/RainbowSloth/Tests/RainbowSlothTests/HkdfTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ final class HkdfTests: XCTestCase {
XCTAssertEqual(s, s2)
}

/// "Basic test case with SHA-256"
func testRfc5869TestCase1() throws {
let ikm = Data(hex: "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b")
let salt = Data(hex: "000102030405060708090a0b0c")
Expand All @@ -21,4 +22,30 @@ final class HkdfTests: XCTestCase {

XCTAssertEqual(actual, expected)
}

/// "Test with SHA-256 and longer inputs/outputs"
func testRfc5869TestCase2() throws {
let ikm = Data(hex: "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f")
let salt = Data(hex: "606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeaf")
let info = Data(hex: "b0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff")
let l = 82

let actual = HkdfSha256.derive(salt: salt, ikm: ikm, info: info, outputLength: l)
let expected = Data(hex: "b11e398dc80327a1c8e7f78c596a49344f012eda2d4efad8a050cc4c19afa97c59045a99cac7827271cb41c65e590e09da3275600c2f09b8367793a9aca3db71cc30c58179ec3e87c14c01d5c1f3434f1d87")

XCTAssertEqual(actual, expected)
}

/// "Test with SHA-256 and zero-length salt/info"
func testRfc5869TestCase3() throws {
let ikm = Data(hex: "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b")
let salt = Data(hex: "")
let info = Data(hex: "")
let l = 42

let actual = HkdfSha256.derive(salt: salt, ikm: ikm, info: info, outputLength: l)
let expected = Data(hex: "8da4e775a563c18f715f802a063c5a31b8a11f5c5ee1879ec3454e5f3c738d2d9d201395faa4b61a96c8")

XCTAssertEqual(actual, expected)
}
}
12 changes: 8 additions & 4 deletions ios/RainbowSloth/Tests/RainbowSlothTests/ReHashToEcTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@ import XCTest

final class ReHashToEcTests: XCTestCase {

func testRehash() throws {
let seed = "1337"
func testRehashSameSeedSameOutput() throws {
let seed = "sloth"

// from same seed results in same public keys
let a1 = ReHashToEc.rehashToP256(seed: seed.data(using: .ascii)!)
let a2 = ReHashToEc.rehashToP256(seed: seed.data(using: .ascii)!)

XCTAssertEqual(a1, a2)
}

// from different seed results in different seed
func testRehashDifferentSeedDifferentOutput() throws {
let seed = "sloth"
let differentSeed = "penguin"

let b1 = ReHashToEc.rehashToP256(seed: seed.data(using: .ascii)!)
let b2 = ReHashToEc.rehashToP256(seed: differentSeed.data(using: .ascii)!)

XCTAssertNotEqual(b1, b2)
}
}
78 changes: 53 additions & 25 deletions ios/Sloth/SlothTests/RainbowSlothTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,8 @@ import XCTest
/// These tests must live in a target of an app to get the right entitlements to access the key chain / SecureEnclave.
final class RainbowSlothTests: XCTestCase {

func testWorksWithLargeN() throws {
let sloth = RainbowSloth(withN: 10)

let (_, kOriginal) = try sloth.keygen(
pw: "test",
handle: "default",
outputLength: 32
)
XCTAssertEqual(kOriginal.count, 32)
}

func testDeriveGeneratesSameKeyAsKeyGen() throws {
let sloth = RainbowSloth(withN: 1)
let sloth = RainbowSloth(withN: 10)

let (storageState, kOriginal) = try sloth.keygen(
pw: "test",
Expand All @@ -34,7 +23,7 @@ final class RainbowSlothTests: XCTestCase {
}

func testDifferentPasswortResultsInDifferentKey() throws {
let sloth = RainbowSloth(withN: 1)
let sloth = RainbowSloth(withN: 10)

let (storageState, kOriginal) = try sloth.keygen(
pw: "test",
Expand All @@ -51,8 +40,8 @@ final class RainbowSlothTests: XCTestCase {
XCTAssertNotEqual(kOriginal, kDerived)
}

func testAnotherKeyGenResultsInNewKey() throws {
let sloth = RainbowSloth(withN: 1)
func testAnotherKeyGenUnderSameHandleResultsInKeyChange() throws {
let sloth = RainbowSloth(withN: 10)

let (storageState, kOriginal) = try sloth.keygen(
pw: "test",
Expand All @@ -61,6 +50,8 @@ final class RainbowSlothTests: XCTestCase {
)
XCTAssertEqual(kOriginal.count, 32)

// note that we do not update the storage, but
// using the same handle will change the key inside the SE
let _ = try sloth.keygen(
pw: "test",
handle: "default",
Expand All @@ -76,27 +67,64 @@ final class RainbowSlothTests: XCTestCase {
}

func testAnotherKeyGenWithDifferentHandleIsIndependent() throws {
let sloth = RainbowSloth(withN: 1)
let sloth = RainbowSloth(withN: 10)

let (storageState, kOriginal) = try sloth.keygen(
pw: "test",
handle: "default",
let (storageStateOriginal, kOriginal) = try sloth.keygen(
pw: "sloth",
handle: "original",
outputLength: 32
)
XCTAssertEqual(kOriginal.count, 32)

let (_, kOther) = try sloth.keygen(
pw: "test",
handle: "differentHandle",
let (storageStateOther, kOther) = try sloth.keygen(
pw: "penguin",
handle: "other",
outputLength: 32
)
XCTAssertNotEqual(kOriginal, kOther)

let kDerived = try sloth.derive(
storageState: storageState,
let kDerivedOriginal = try sloth.derive(
storageState: storageStateOriginal,
pw: "sloth",
outputLength: 32
)
XCTAssertEqual(kOriginal, kDerivedOriginal)

let kDerivedOther = try sloth.derive(
storageState: storageStateOther,
pw: "penguin",
outputLength: 32
)
XCTAssertEqual(kOther, kDerivedOther)
}

func testWorksWithVeryLargeN() throws {
let sloth = RainbowSloth(withN: 1000)

let (_, kOriginal) = try sloth.keygen(
pw: "test",
handle: "default",
outputLength: 32
)
XCTAssertEqual(kOriginal, kDerived)
XCTAssertEqual(kOriginal.count, 32)
}

func testUsingLargerParameterResultsInLongerRuntime() throws {
let slothFast = RainbowSloth(withN: 10)
let slothSlow = RainbowSloth(withN: 100)
let iterations = 10

let fastTimings = try RainbowSlothEvaluationWrapper.runEval(sloth: slothFast, iterations: iterations)
let slowTimings = try RainbowSlothEvaluationWrapper.runEval(sloth: slothSlow, iterations: iterations)

let fastAverage = fastTimings.reduce(0.0, +) / 5
let slowAverage = slowTimings.reduce(0.0, +) / 5
XCTAssertGreaterThan(slowAverage, fastAverage)

// from observations the slowDown factor is higher and more consistent on real devices
// compared to simulators
let slowDown = slowAverage / fastAverage
XCTAssertGreaterThan(slowDown, 5)
XCTAssertLessThan(slowDown, 15)
}
}

0 comments on commit 4c1c3a8

Please sign in to comment.