Skip to content

Commit 5da89bb

Browse files
committed
Move errors outside of SRPClient/SRPServer
1 parent 5d85a65 commit 5da89bb

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

Sources/SRP/client.swift

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,6 @@ import Crypto
1515
/// - https://tools.ietf.org/html/rfc5054
1616
///
1717
public struct SRPClient<H: HashFunction> {
18-
/// Errors thrown by SRPClient
19-
public enum Error: Swift.Error {
20-
/// the key returned by server is invalid, in that either it modulo N is zero or the hash(A,B) is zero
21-
case nullServerKey
22-
/// server verification code was wrong
23-
case invalidServerCode
24-
/// you called verifyServerCode without a verification key
25-
case requiresVerificationKey
26-
/// client key is invalid
27-
case invalidClientKey
28-
}
29-
3018
/// configuration. This needs to be the same as the server configuration
3119
public let configuration: SRPConfiguration<H>
3220

@@ -86,7 +74,7 @@ public struct SRPClient<H: HashFunction> {
8674
// get out version of server proof
8775
let HAMS = SRP<H>.calculateSimpleServerVerification(clientPublicKey: clientKeys.public, clientProof: clientProof, sharedSecret: sharedSecret)
8876
// is it the same
89-
guard serverProof == HAMS else { throw Error.invalidServerCode }
77+
guard serverProof == HAMS else { throw SRPClientError.invalidServerCode }
9078
}
9179

9280
/// calculate proof of shared secret to send to server
@@ -116,7 +104,7 @@ public struct SRPClient<H: HashFunction> {
116104
// get out version of server proof
117105
let HAMK = SRP<H>.calculateServerVerification(clientPublicKey: clientKeys.public, clientProof: clientProof, sharedSecret: hashSharedSecret)
118106
// is it the same
119-
guard serverProof == HAMK else { throw Error.invalidServerCode }
107+
guard serverProof == HAMK else { throw SRPClientError.invalidServerCode }
120108
}
121109

122110
/// Generate salt and password verifier from username and password. When creating your user instead of passing your password to the server, you
@@ -136,12 +124,12 @@ public struct SRPClient<H: HashFunction> {
136124
extension SRPClient {
137125
/// return shared secret given the username, password, B value and salt from the server
138126
func calculateSharedSecret(message: [UInt8], salt: [UInt8], clientKeys: SRPKeyPair, serverPublicKey: SRPKey) throws -> BigNum {
139-
guard serverPublicKey.number % configuration.N != BigNum(0) else { throw Error.nullServerKey }
127+
guard serverPublicKey.number % configuration.N != BigNum(0) else { throw SRPClientError.nullServerKey }
140128

141129
// calculate u = H(clientPublicKey | serverPublicKey)
142130
let u = SRP<H>.calculateU(clientPublicKey: clientKeys.public.bytes, serverPublicKey: serverPublicKey.bytes, pad: configuration.sizeN)
143131

144-
guard u != 0 else { throw Error.nullServerKey }
132+
guard u != 0 else { throw SRPClientError.nullServerKey }
145133

146134
let x = BigNum(bytes: [UInt8](H.hash(data: salt + H.hash(data: message))))
147135

Sources/SRP/error.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// Errors thrown by SRPClient
2+
public enum SRPClientError: Swift.Error {
3+
/// the key returned by server is invalid, in that either it modulo N is zero or the hash(A,B) is zero
4+
case nullServerKey
5+
/// server verification code was wrong
6+
case invalidServerCode
7+
/// you called verifyServerCode without a verification key
8+
case requiresVerificationKey
9+
/// client key is invalid
10+
case invalidClientKey
11+
}
12+
13+
/// Errors thrown by SRPServer
14+
///Errors thrown by SRPServer
15+
public enum SRPServerError: Swift.Error {
16+
/// the modulus of the client key and N generated a zero
17+
case nullClientKey
18+
/// client proof of the shared secret was invalid or wrong
19+
case invalidClientProof
20+
}
21+

Sources/SRP/server.swift

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,6 @@ import Crypto
1515
/// - https://tools.ietf.org/html/rfc5054
1616
///
1717
public struct SRPServer<H: HashFunction> {
18-
///Errors thrown by SRPServer
19-
public enum Error: Swift.Error {
20-
/// the modulus of the client key and N generated a zero
21-
case nullClientKey
22-
/// client proof of the shared secret was invalid or wrong
23-
case invalidClientProof
24-
}
25-
2618
/// Authentication state. Stores A,B and shared secret
2719
public struct AuthenticationState {
2820
let clientPublicKey: SRPKey
@@ -60,7 +52,7 @@ public struct SRPServer<H: HashFunction> {
6052
/// - verifier: password verifier
6153
/// - Returns: shared secret
6254
public func calculateSharedSecret(clientPublicKey: SRPKey, serverKeys: SRPKeyPair, verifier: SRPKey) throws -> SRPKey {
63-
guard clientPublicKey.number % configuration.N != BigNum(0) else { throw Error.nullClientKey }
55+
guard clientPublicKey.number % configuration.N != BigNum(0) else { throw SRPServerError.nullClientKey }
6456

6557
// calculate u = H(clientPublicKey | serverPublicKey)
6658
let u = SRP<H>.calculateU(clientPublicKey: clientPublicKey.bytes, serverPublicKey: serverKeys.public.bytes, pad: configuration.sizeN)
@@ -86,7 +78,7 @@ public struct SRPServer<H: HashFunction> {
8678
serverPublicKey: serverPublicKey,
8779
sharedSecret: sharedSecret
8880
)
89-
guard clientProof == proof else { throw Error.invalidClientProof }
81+
guard clientProof == proof else { throw SRPServerError.invalidClientProof }
9082
return SRP<H>.calculateSimpleServerVerification(clientPublicKey: clientPublicKey, clientProof: clientProof, sharedSecret: sharedSecret)
9183
}
9284

@@ -110,7 +102,7 @@ public struct SRPServer<H: HashFunction> {
110102
serverPublicKey: serverPublicKey,
111103
hashSharedSecret: hashSharedSecret
112104
)
113-
guard clientProof == proof else { throw Error.invalidClientProof }
105+
guard clientProof == proof else { throw SRPServerError.invalidClientProof }
114106
return SRP<H>.calculateServerVerification(clientPublicKey: clientPublicKey, clientProof: clientProof, sharedSecret: hashSharedSecret)
115107
}
116108
}

0 commit comments

Comments
 (0)