Skip to content

Commit d240d74

Browse files
authored
Increase salt bytes to 32 from 16 + use platform secure random generator for salt generation (#18)
* Increase salt size from 16 to 32 bytes * Improve salt generation with cryptographically secure random using Security framework * Simplify secure random implementation using direct array reference * Add clean tests for secure random generation * Fix salt tests to be more realistic about random distribution * Remove overly complex salt generation test * Fix: Use MemoryLayout.size instead of stride for accurate byte count * use SystemRandomNumberGenerator for generating random UInt8 * move random(count:) to other extension
1 parent 9392d9d commit d240d74

3 files changed

Lines changed: 39 additions & 12 deletions

File tree

Sources/SRP/Array.swift

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
1-
21
extension Array where Element: FixedWidthInteger {
3-
/// create array of random bytes
4-
static func random(count: Int) -> [Element] {
5-
var array = self.init()
6-
for _ in 0 ..< count {
7-
array.append(.random(in: Element.min ..< Element.max))
8-
}
9-
return array
10-
}
11-
122
/// generate a hexdigest of the array of bytes
133
func hexdigest() -> String {
144
return map {
155
let characters = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"]
166
return "\(characters[Int($0 >> 4)])\(characters[Int($0 & 0xF)])"
177
}.joined()
188
}
9+
10+
/// create array of random bytes using cryptographically secure random number generation
11+
static func random(count: Int) -> [Element] {
12+
var array = [Element]()
13+
array.reserveCapacity(count)
14+
var g = SystemRandomNumberGenerator()
15+
var i = 0
16+
while i < count {
17+
array.append(Element.random(in: Element.min...Element.max, using: &g))
18+
i += 1
19+
}
20+
return array
21+
}
1922
}
2023

2124
extension Array where Element == UInt8 {

Sources/SRP/client.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public struct SRPClient<H: HashFunction> {
165165
/// - password: user password
166166
/// - Returns: tuple containing salt and password verifier
167167
public func generateSaltAndVerifier(username: String, password: String) -> (salt: [UInt8], verifier: SRPKey) {
168-
let salt = [UInt8].random(count: 16)
168+
let salt = [UInt8].random(count: 32)
169169
let verifier = generatePasswordVerifier(username: username, password: password, salt: salt)
170170
return (salt: salt, verifier: SRPKey(verifier, padding: configuration.sizeN))
171171
}

Tests/SRPTests/SRPTests.swift

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@ import Crypto
44
import XCTest
55

66
final class SRPTests: XCTestCase {
7+
func testSecureRandomGeneration() {
8+
// Test that random generation produces arrays of the correct size
9+
let size1 = 16
10+
let randomArray1 = [UInt8].random(count: size1)
11+
XCTAssertEqual(randomArray1.count, size1, "Random array should have the requested size")
12+
13+
let size2 = 32
14+
let randomArray2 = [UInt8].random(count: size2)
15+
XCTAssertEqual(randomArray2.count, size2, "Random array should have the requested size")
16+
17+
// Test that random values are actually random (very unlikely to be the same)
18+
let randomArray3 = [UInt8].random(count: 16)
19+
let randomArray4 = [UInt8].random(count: 16)
20+
XCTAssertNotEqual(randomArray3, randomArray4, "Random arrays should be different")
21+
22+
// Test that all values are within valid UInt8 range
23+
for byte in randomArray1 {
24+
XCTAssertGreaterThanOrEqual(byte, UInt8.min)
25+
XCTAssertLessThanOrEqual(byte, UInt8.max)
26+
}
27+
}
28+
29+
730
func testKeyConversion() {
831
let hex = "00000102030405060708090a0b0c0d0e0f"
932
XCTAssertEqual(SRPKey(hex: hex)?.hex, hex)
@@ -267,7 +290,8 @@ final class SRPTests: XCTestCase {
267290
("testServerSessionProof", testServerSessionProof),
268291
("testRFC5054Appendix", testRFC5054Appendix),
269292
("testMozillaTestVectors", testMozillaTestVectors),
270-
("testConstantTimeEqual", testConstantTimeEqual),
293+
("testSecureRandomGeneration", testSecureRandomGeneration),
294+
("testConstantTimeEqual", testConstantTimeEqual)
271295
]
272296
}
273297

0 commit comments

Comments
 (0)