pixelcort
pixelcort

Reputation: 269

Getting RSA private key's n and d using Swift

I'm trying to implement Yao's Millionaires' Problem algorithm in Swift and am hitting a snag.

To implement this algorithm, I need to generate an RSA private key and get n and d.

So far, I've created the key like this:

import Security
import Foundation

let tag = "com.example.keys.mykey".data(using: .utf8)!
let attributes: [String: Any] = [kSecAttrKeyType as String: kSecAttrKeyTypeRSA,
    kSecAttrKeySizeInBits as String: 1024,
    kSecPrivateKeyAttrs as String:
        [kSecAttrIsPermanent as String: false,
        kSecAttrApplicationTag as String: tag]
]

var error: Unmanaged<CFError>?
guard let privateKey = SecKeyCreateRandomKey(attributes as CFDictionary, &error) else {
    throw error!.takeRetainedValue() as Error
}

privateKey seems to be of type SecKey but I can't figure out how to get n and d out of it.

How can I generate an RSA private key and get the n and d values?

Upvotes: 4

Views: 720

Answers (2)

pixelcort
pixelcort

Reputation: 269

Thanks to the help of Luke's answer, I got it to work as follows. I decided to use BigInt swift package.

import BigInt

var error2: Unmanaged<CFError>?
let privateKeyData = SecKeyCopyExternalRepresentation(privateKey!, &error2) as Data?
let privateKeyDataN = privateKeyData![10...10+128]
let privateKeyDataD = privateKeyData![147...147+127]
let n = BigUInt(privateKeyDataN)
let d = BigUInt(privateKeyDataD)

Upvotes: 2

Luke Joshua Park
Luke Joshua Park

Reputation: 9805

You're looking for the function SecKeyCopyAttributes. This function, when passed a SecKey, will return a CFDictionary that contains a key, v_Data, that contains a DER-encoded sequence of attributes that describe the key:

PrivateKey ::= SEQUENCE {
    version           INTEGER,
    modulus           INTEGER, <- n
    publicExponent    INTEGER,
    privateExponent   INTEGER, <- d
    prime1            INTEGER,
    prime2            INTEGER,
    exponent1         INTEGER,
    exponent2         INTEGER,
    coefficient       INTEGER,
    otherPrimeInfos   OtherPrimeInfos OPTIONAL
 }

Not by any means a straight forward solution, but Apple has never been known for their straight forward crypto.

Now you could implement an ASN.1 parser or try and find one and get it going. But to be honest, if I was you, I'd just shortcut it and pull out just those two values that you need.

Here is a dummy breakdown of a similar ASN.1 structure that is a sequence of integers:

enter image description here

and its hexadecimal representation...

enter image description here

These images came from this excellent ASN.1 decoder here.. The highlighted portion of hex is the second integer down in the list, for visibility.

You can extract the values you want by skipping the SEQUENCE header and calculating the length of each integer (the 0x02 byte literally means "INTEGER"). Doing this, you can pinpoint the locations in the structure that contain your modulus and your private exponent.

The implementation of this I'll leave to you!

Upvotes: 3

Related Questions