Latest 2.7
License MIT
Platforms ios 9.3
Dependencies FFDataWrapper

This is an implementation of SRP 6a compatible with that of BouncyCastle.

For SRP 6a Specification, see:

SRP Design Specification

How to Install

To use with cocoapods, add the following line to your Podfile:

pod 'SwiftySRP', :git => '', :branch => 'master'

Xcode 10 vs 9.4 Support

Starting with version 2.7 SwiftySRP no longer supports building with Xcode 9 (because of the different ways the CommonCrypto library must be imported). If you still need to support XCode 9, you have to stay on version 2.6.

How to Use

Currently the implementation is Swift-only and supports iOS9.0 and higher.
(Objective-C wrappers may be added in a future release).

To use the SRP on the client side you need to create an SRP protocol instance,
where you specify a large safe prime number (see below on how to generate one), a generator, a hashing function, and an HMAC function
(HMAC function is used as an alternative way to generate a shared session key from the shared secret; this way we can generate multiple session keys from the same shared secret by utilizing different HMAC keys). The SRP implementation can use two different big integer implementations: BigInt (written in pure Swift) and imath (written in C). At the moment the C implementation is a lot (about 50 times) faster than the pure Swift one.

// In this example we use the same prime that BouncyCastle tests use.
let N = data(hex: "EEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C"
    + "9C256576D674DF7496EA81D3383B4813D692C6E0E0D5D8E250B98BE4"
    + "8E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B29"
    + "7BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9A"
    + "FD5138FE8376435B9FC61D2FC0EB06E3")

// In this example we use the same prime that BouncyCastle tests use.
let g = data(hex: "02")

// Use SHA256 as the hashing function, and HMAC-SHA256 as the HMAC function.
let srp256 = try SRP.iMath.protocol(N: N, g:g, digest: CryptoAlgorithm.SHA256.digestFunc(), hmac: CryptoAlgorithm.SHA256.hmacFunc())

Afterwards, you should generate the verifier and send it over to the server.
(You need to generate an SRP salt and obtain the user’s name and password to generate the verifier.)

// Normally you would get this from the user:
let userName = "alice".data(using: .utf8)!
// Normally you would get this from the user:
let userPassword = "password123".data(using: .utf8)!

// Just a way to generate the salt... (implemented in a separate category)
let salt = Data.generateRandomBytes(count: 128)

// Generate the verifier
let srpData = try srp256.verifier(s: salt, I: userName, p: password)
let verifier = srpData.v.serialize()

// Now you must send the salt, userName, and the verifier to the server.

At the time of login you must first receive the salt and the public value ‘B’ parameters back from the server.
Then you can obtain the userName and password from the user and generate the client side evidence message:

let srp = try SRP.iMath.protocol(N: N, g:g, digest: CryptoAlgorithm.SHA256.digestFunc(), hmac: CryptoAlgorithm.SHA256.hmacFunc())

// TODO: Obtain the salt and parameter 'B' from the server.
// Also, the server can send a number of HMAC keys to generate shared session keys.
// let salt: Data = ...
// let B:Data = ...
// let hmacKey1: Data = ...

// TODO: Obtain the userName and password from the user.
// let userName: Data = ...
// let password: Data = ...

var srpData = try srp.generateClientCredentials(s:salt, I:userName, p:password)

srpData.serverPublicValue = B

srpData = try srp.clientEvidenceMessage(srpData: srpData)

// Now send the following values to the server:
// - userName
// - srpData.clientM.serialize()
// - srpData.A.serialize()

// The server will verify the client evidence and, if successful, should send back its own evidence message: serverM
srpData.serverEvidenceMessage = serverM

// Validate the server evidence message:
try srp.verifyServerEvidenceMessage(srpData: clientSRPData)

// Now derive the shared session key.
let clientSharedSessionKey: Data = try! srp.calculateClientSharedKey(srpData: clientSRPData, salt: hmacKey1)

Generating Safe Primes

To generate a large safe prime, you can use, for example, openssl in the following way:

openssl dhparam -text 2048

This is going to take a rather long time, but in the end you get the prime printed in hex form.

Example output. You can remove the ‘:’ delimiters and use the resulting string

Diffie-Hellman-Parameters: (2048 bit)
    generator: 2 (0x2)

Latest podspec

    "name": "SwiftySRP",
    "version": "2.7",
    "platforms": {
        "ios": "9.3"
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    "summary": "Swift implementation of SRP",
    "homepage": "",
    "authors": "Sergey Novitsky",
    "source": {
        "git": "",
        "tag": "v2.7"
    "source_files": [
    "exclude_files": "SwiftySRP/BigIntSpecific/*",
    "public_header_files": "SwiftySRP/**/*.h",
    "documentation_url": "",
    "dependencies": {
        "FFDataWrapper": [
            "~> 1.4"
    "swift_version": "4.1",
    "preserve_paths": [
    "xcconfig": {
        "SWIFT_INCLUDE_PATHS[sdk=iphoneos*]": "$(PODS_ROOT)/SwiftySRP/imath $(SRCROOT)/../imath",
        "SWIFT_INCLUDE_PATHS[sdk=iphonesimulator*]": "$(PODS_ROOT)/SwiftySRP/imath $(SRCROOT)/../imath",
        "SWIFT_INCLUDE_PATHS[sdk=appletvos*]": "$(PODS_ROOT)/SwiftySRP/imath $(SRCROOT)/../imath",
        "SWIFT_INCLUDE_PATHS[sdk=appletvsimulator*]": "$(PODS_ROOT)/SwiftySRP/imath $(SRCROOT)/../imath",
        "SWIFT_INCLUDE_PATHS[sdk=watchos*]": "$(PODS_ROOT)/SwiftySRP/imath $(SRCROOT)/../imath",
        "SWIFT_INCLUDE_PATHS[sdk=watchsimulator*]": "$(PODS_ROOT)/SwiftySRP/imath $(SRCROOT)/../imath"

Pin It on Pinterest

Share This