0

I have developed a simple iOS application that performs encryption and decryption to a randomly generated message. When deploying the application on an iPhone 8, the runtime sometimes varies. In most cases it is consistent. I am trying to understand why could the runtime sometimes vary. I make sure to have no other applications open at the time of running to ensure that no other apps are utilizing the iPhone's processors.

These are my action buttons that call the encrypt and decrypt functions, and this is how I measure the elapsed time:

@IBAction func enc(_ sender: Any) {
    let info = ProcessInfo.processInfo
    let begin = info.systemUptime

    enc0.text = "Enc-c0: \(e0)"
    enc1.text = "Enc-c1: \(e1)"

    let time = (info.systemUptime - begin)

    time1.text = "Enc Time: \(time)"
}

@IBAction func dec(_ sender: Any) {
    let info = ProcessInfo.processInfo
    let begin = info.systemUptime

    let decryption = dDec(encryption0:e0,encryption1:e1)

    dec.text = "Dec: \(decryption)"

    let time = (info.systemUptime - begin)

    time2.text = "Dec Time: \(time)"
}

These are the two functions being called:

func dEnc() -> (BigUInt,BigUInt){

    let r:BigUInt = BigUInt.randomInteger(lessThan: (p-2))

    let c0:BigUInt = x.power(_:r,modulus:p)

    let a:BigUInt = m.power(_:1, modulus: p)
    let b:BigUInt = (pub_m.multiplied(by: pub_r)).power(_:r, modulus: p)
    let c1:BigUInt = a.multiplied(by: b).power(1, modulus: p)

    return (c0, c1)
}


func dDec(encryption0:BigUInt, encryption1:BigUInt) -> BigUInt{

    let a = encryption1
    var b = encryption0.power(priv_m, modulus: p)
    var c = encryption0.power(priv_r, modulus: p)

    b = b.inverse(p)!
    c = c.inverse(p)!

    let d = ((a.multiplied(by: b)).multiplied(by: c)).power(1, modulus: p)
    return (d)
}

The applications works fine, but the runtime sometimes varies. Is there something I need to handle to make sure that the runtime is consistent?

  • 1
    System uptime is not a reliable way to measure this. Your application's process is only one of many that's fighting for time on the CPU. Over the course of the regular multitasking that's going on, you'll get fluctuations in your apparent time. – Alexander May 21 '19 at 02:58
  • 1
    Further more, I would *not* worry about runtime in your circumstance. Given that you've implemented your own crypto functions, you likely have much bigger vulnerabilities to worry about than time analysis. For one, the `BigUInt` type you're using is probably optimized for performance, not constant-time operation, so leak all kind of information in all kinds of side channels (time, memory, CPU usage, etc.) – Alexander May 21 '19 at 03:00
  • @Alexander Do you have any recommendations of how to measure just my applications performance? – user1122000 May 21 '19 at 03:00
  • I would investigate something like https://stackoverflow.com/q/9081094/3141234 But honestly, that's like trying to reinforce the door of a cardboard house. I wouldn't trust any crypto implementation that isn't mass deployed, and vetted by the security community. – Alexander May 21 '19 at 03:02
  • Is there any good source to understand multitasking on the phone's CPU and what keeps it busy even with all applications closed. – user1122000 May 21 '19 at 03:07
  • iOS is just a BSD based operating system, any book on Unix/Linux will cover the meat of the sort of preemptive multitasking that happens on practically all modern devices – Alexander May 21 '19 at 03:57

0 Answers0