Skip to content

Commit

Permalink
Fixed salt value hex conversion methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kerimovscreations committed May 6, 2021
1 parent 509955c commit c165fef
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 30 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 3.4.0 - 2021-05-6
### Changes
- Fixed salt hex conversion

## 3.3.0 - 2021-03-18
### Changes
- Updated createSaltedVerificationKey method return type

## 3.2.1 - 2021-03-18
### Changes
- Fixed stability issues Nimbus and Thinbus client implementation
Expand Down
9 changes: 4 additions & 5 deletions Sources/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class Client {
self.algorithm = algorithm

if let privateKey = privateKey {
a = BInt(privateKey.hex, radix: 16)!
a = BInt(privateKey.hexEncodedString(), radix: 16)!

// A = g^a % N
A = BIntMath.mod_exp(group.g, a, group.N)
Expand Down Expand Up @@ -106,7 +106,7 @@ public class Client {
privateKey: Data? = nil)
{
self.init(username: username, group: group, algorithm: algorithm, privateKey: privateKey)
self.precomputedX = BInt(precomputedX.hex, radix: 16)!
self.precomputedX = BInt(precomputedX.hexEncodedString(), radix: 16)!
}

/// Starts authentication. This method is a no-op.
Expand Down Expand Up @@ -166,12 +166,11 @@ public class Client {

x = self.precomputedX ?? calculate_x_thinbus(group: group,
algorithm: algorithm,
salt: salt.hexaData,
salt: salt,
username: username,
password: password!)
}


let v = calculate_v(group: group, x: x)

// shared secret
Expand All @@ -198,7 +197,7 @@ public class Client {
algorithm: algorithm,
A: publicKey,
B: strB.hexaData,
S: Sdata).hex
S: Sdata).hexEncodedString()
case .thinbus:
M = calculate_M_thinbus(group: group,
algorithm: algorithm,
Expand Down
18 changes: 9 additions & 9 deletions Sources/Data+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ func + (lhs: Data, rhs: Data) -> Data {
return result
}

extension Data {
public var hex : String {
var str = ""
enumerateBytes { buffer, index, stop in
for byte in buffer {
str.append(String(format:"%02x",byte))
extension DataProtocol {
func hexEncodedString(uppercase: Bool = false) -> String {
return self.map {
if $0 < 16 {
return "0" + String($0, radix: 16, uppercase: uppercase)
} else {
return String($0, radix: 16, uppercase: uppercase)
}
}
return str
}.joined()
}
}

extension NSData {
public var hex : String {
return (self as Data).hex
return (self as Data).hexEncodedString()
}
}

Expand Down
34 changes: 18 additions & 16 deletions Sources/SRP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,21 @@ public func createSaltedVerificationKey(
salt: Data? = nil,
group: Group = .N2048,
algorithm: Digest.Algorithm = .sha1)
-> (salt: Data, verificationKeyHex: String)
-> (salt: String, verificationKeyHex: String)
{
let salt = salt ?? Data(try! Random.generate(byteCount: 16))
let saltStr = salt.hexEncodedString()

let x: BInt

switch clientType {
case .nimbus:
x = calculate_x_nimbus(algorithm: algorithm, salt: salt, password: password)
case .thinbus:
x = calculate_x_thinbus(group: group, algorithm: algorithm, salt: salt, username: username, password: password)
x = calculate_x_thinbus(group: group, algorithm: algorithm, salt: saltStr, username: username, password: password)
}

return createSaltedVerificationKey(from: x, salt: salt, group: group)
return createSaltedVerificationKey(from: x, salt: saltStr, group: group)
}

/// Creates the salted verification key based on a precomputed SRP x value.
Expand All @@ -56,13 +57,14 @@ public func createSaltedVerificationKey(
/// - Returns: salt (s) and verification key (v)
func createSaltedVerificationKey(
from x: BInt,
salt: Data? = nil,
salt: String? = nil,
group: Group = .N2048)
-> (salt: Data, verificationKeyHex: String)
-> (salt: String, verificationKeyHex: String)
{
let salt = salt ?? Data(try! Random.generate(byteCount: 16))
let saltStr = salt ?? Data(try! Random.generate(byteCount: 16)).hexEncodedString()

let v = calculate_v(group: group, x: x)
return (salt, v.asString(radix: 16))
return (saltStr, v.asString(radix: 16))
}

func pad(_ data: Data, to size: Int) -> Data {
Expand All @@ -74,7 +76,7 @@ func pad(_ data: Data, to size: Int) -> Data {
func calculate_u(group: Group, algorithm: Digest.Algorithm, A: Data, B: Data) -> BInt {
let H = Digest.hasher(algorithm)
let size = group.getNSize()
return BInt(H(pad(A, to: size) + pad(B, to: size)).hex, radix: 16)!
return BInt(H(pad(A, to: size) + pad(B, to: size)).hexEncodedString(), radix: 16)!
}

//u = H(A | B)
Expand All @@ -83,7 +85,7 @@ func calculate_u_thinbus(group: Group, algorithm: Digest.Algorithm, A: String, B
let Adata = A.data(using: .utf8)!
let Bdata = B.data(using: .utf8)!

return BInt(H(Adata + Bdata).hex, radix: 16)!
return BInt(H(Adata + Bdata).hexEncodedString(), radix: 16)!
}

//M1 = H(H(N) XOR H(g) | H(I) | s | A | B | K)
Expand Down Expand Up @@ -132,11 +134,11 @@ func calculate_k(group: Group, algorithm: Digest.Algorithm) -> BInt {
let H = Digest.hasher(algorithm)
let size = group.getNSize()
return BInt(H(Bignum.init(hex: group.N.asString(radix: 16)).data +
pad(Bignum.init(hex: group.g.asString(radix: 16)).data, to: size)).hex, radix: 16)!
pad(Bignum.init(hex: group.g.asString(radix: 16)).data, to: size)).hexEncodedString(), radix: 16)!
}

//x = H(s | H(I | ":" | P))
func calculate_x_thinbus(group: Group, algorithm: Digest.Algorithm, salt: Data, username: String, password: String) -> BInt {
func calculate_x_thinbus(group: Group, algorithm: Digest.Algorithm, salt: String, username: String, password: String) -> BInt {
let H = Digest.hasher(algorithm)

var hash1 = H("\(username):\(password)".data(using: .utf8)!)
Expand All @@ -145,21 +147,21 @@ func calculate_x_thinbus(group: Group, algorithm: Digest.Algorithm, salt: Data,
hash1.remove(at: 0)
}

let hash1S = hash1.hex

var hash = H("\(salt.hex)\(hash1S)".uppercased().data(using: .utf8)!)
let hash1S = hash1.hexEncodedString()
var hash = H("\(salt)\(hash1S)".uppercased().data(using: .utf8)!)

if hash[0] == 0 {
hash.remove(at: 0)
}

return BIntMath.mod_exp(BInt(hash.hex, radix: 16)!, BInt(1), group.N)
return BIntMath.mod_exp(BInt(hash.hexEncodedString(), radix: 16)!, BInt(1), group.N)
}

//x = H(s | H(P))
func calculate_x_nimbus(algorithm: Digest.Algorithm, salt: Data, password: String) -> BInt {
let H = Digest.hasher(algorithm)
return BInt(H(salt + H(password.data(using: .utf8)!)).hex, radix: 16)!
return BInt(H(salt + H(password.data(using: .utf8)!)).hexEncodedString(), radix: 16)!
}

// v = g^x % N
Expand Down

0 comments on commit c165fef

Please sign in to comment.