Implementation of the Secure Remote Password protocol (SRP) - RFC 5054 for Swift.
Supported Hash Algorithms:
- SHA-1
- SHA-224
- SHA-256
- SHA-384
- SHA-512
Supported Group Parameters:
- 1024 bits
- 1536 bits
- 2048 bits
- 3072 bits
- 4096 bits
- 6144 bits
- 8192 bits
import Foundation
import SwiftSRP
// Server storage
var salt: Data!
var verifier: Data!
/* Registration */
// Create a SRP client for an identity, with a given hash algorithm and group type
let client = SRPClient( identity: account.identity, hashAlgorithm: .SHA256, groupType: .NG2048 )
// User registers with a password
client.setPassword( string: account.password )
// Client generates a salt
client.salt = SRPRandom.bytes( count: 16 )
// Client -> Server:
// Server receives salt and verifier from Client
// Client can then discard them
salt = client.salt
verifier = client.v.bytes( endianness: .bigEndian )
/* Authentication */
let client = SRPClient( identity: account.identity, hashAlgorithm: .SHA256, groupType: .NG2048 )
let server = SRPServer( identity: account.identity, hashAlgorithm: .SHA256, groupType: .NG2048 )
// Server has stored salt and verifier during registration (see above)
server.v = SRPBigNum( data: verifier, endianness: .bigEndian )
server.salt = salt
// Client -> Server:
// Server receives A from Client
server.A = client.A
// Server -> Client:
// Client receives B and salt from Server
client.B = server.B
client.salt = server.salt
// User inputs a wrong password
client.setPassword( string: "salad" )
// Client and Server will not have matching M1 and M2, meaning the authentication failed
AssertFalse( client.M1 == server.M1 )
AssertFalse( client.M2 == server.M2 )
// User inputs the correct password
client.setPassword( string: account.password )
// With the correct password, Client and Server will have matching M1 and M2, meaning the authentication was successful
AssertTrue( client.M1 == server.M1 )
AssertTrue( client.M2 == server.M2 )
This project requires OpenSSL.
A pre-built version of BoringSSL is provided for macOS in the Submodules/SRPXX/Submodules/BoringSSL/lib
directory.
Project is released under the terms of the MIT License.
Owner: Jean-David Gadina - XS-Labs
Web: www.xs-labs.com
Blog: www.noxeos.com
Twitter: @macmade
GitHub: github.com/macmade
LinkedIn: ch.linkedin.com/in/macmade/
StackOverflow: stackoverflow.com/users/182676/macmade