diff --git a/Xcodes.xcodeproj/project.pbxproj b/Xcodes.xcodeproj/project.pbxproj index b177caf..9dfe09d 100644 --- a/Xcodes.xcodeproj/project.pbxproj +++ b/Xcodes.xcodeproj/project.pbxproj @@ -3,7 +3,7 @@ archiveVersion = 1; classes = { }; - objectVersion = 60; + objectVersion = 54; objects = { /* Begin PBXBuildFile section */ @@ -819,7 +819,6 @@ E84E4F552B335094003F3959 /* XCRemoteSwiftPackageReference "swift-collections" */, E83FDC422CBB649100679C6B /* XCRemoteSwiftPackageReference "Sparkle" */, 33027E282CA8BB5800CB387C /* XCRemoteSwiftPackageReference "LibFido2Swift" */, - 15F5B8912CCF44B700705E2F /* XCLocalSwiftPackageReference "swift-srp-main" */, ); productRefGroup = CAD2E79F2449574E00113D76 /* Products */; projectDirPath = ""; @@ -1488,13 +1487,6 @@ }; /* End XCConfigurationList section */ -/* Begin XCLocalSwiftPackageReference section */ - 15F5B8912CCF44B700705E2F /* XCLocalSwiftPackageReference "swift-srp-main" */ = { - isa = XCLocalSwiftPackageReference; - relativePath = "swift-srp-main"; - }; -/* End XCLocalSwiftPackageReference section */ - /* Begin XCRemoteSwiftPackageReference section */ 33027E282CA8BB5800CB387C /* XCRemoteSwiftPackageReference "LibFido2Swift" */ = { isa = XCRemoteSwiftPackageReference; diff --git a/Xcodes.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Xcodes.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 89a660f..19fa71e 100644 --- a/Xcodes.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Xcodes.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -118,6 +118,15 @@ "version": "1.1.7" } }, + { + "package": "swift-srp", + "repositoryURL": "https://github.com/abiligiri/swift-srp", + "state": { + "branch": null, + "revision": "ca452b0b557021ca8af305bbce504bcd13d32b15", + "version": "1.1.0" + } + }, { "package": "SwiftSoup", "repositoryURL": "https://github.com/scinfu/SwiftSoup", diff --git a/Xcodes/AppleAPI/Package.swift b/Xcodes/AppleAPI/Package.swift index 02f3b5c..2b572ff 100644 --- a/Xcodes/AppleAPI/Package.swift +++ b/Xcodes/AppleAPI/Package.swift @@ -13,7 +13,7 @@ let package = Package( targets: ["AppleAPI"]), ], dependencies: [ - .package(name: "swift-srp", path: "swift-srp-main") + .package(url: "https://github.com/abiligiri/swift-srp", from: "1.1.0") ], targets: [ // Targets are the basic building blocks of a package. A target can define a module or a test suite. diff --git a/swift-srp-main/.github/FUNDING.yml b/swift-srp-main/.github/FUNDING.yml deleted file mode 100644 index 7fc910b..0000000 --- a/swift-srp-main/.github/FUNDING.yml +++ /dev/null @@ -1 +0,0 @@ -github: adam-fowler diff --git a/swift-srp-main/.github/workflows/swift.yml b/swift-srp-main/.github/workflows/swift.yml deleted file mode 100644 index 9a734f6..0000000 --- a/swift-srp-main/.github/workflows/swift.yml +++ /dev/null @@ -1,39 +0,0 @@ -name: CI - -on: - push: - branches: - - main - pull_request: - branches: - - main - release: - types: [published] - workflow_dispatch: - -jobs: - macOS: - runs-on: macOS-latest - steps: - - uses: actions/checkout@v4 - with: - fetch-depth: 1 - - name: Build - run: swift build - - name: Run tests - run: swift test - - linux: - runs-on: ubuntu-latest - strategy: - matrix: - image: ['swift:5.9', 'swift:5.10', 'swift:6.0'] - container: - image: ${{ matrix.image }} - steps: - - name: Checkout - uses: actions/checkout@v4 - with: - fetch-depth: 1 - - name: Test - run: swift test --parallel --enable-code-coverage diff --git a/swift-srp-main/.gitignore b/swift-srp-main/.gitignore deleted file mode 100644 index 4c741c0..0000000 --- a/swift-srp-main/.gitignore +++ /dev/null @@ -1,7 +0,0 @@ -.DS_Store -/.build -/.swiftpm -/Packages -/*.xcodeproj -xcuserdata/ -Package.resolved \ No newline at end of file diff --git a/swift-srp-main/LICENSE b/swift-srp-main/LICENSE deleted file mode 100644 index 261eeb9..0000000 --- a/swift-srp-main/LICENSE +++ /dev/null @@ -1,201 +0,0 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. diff --git a/swift-srp-main/Package.swift b/swift-srp-main/Package.swift deleted file mode 100644 index a427028..0000000 --- a/swift-srp-main/Package.swift +++ /dev/null @@ -1,26 +0,0 @@ -// swift-tools-version:5.1 -// The swift-tools-version declares the minimum version of Swift required to build this package. - -import PackageDescription - -let package = Package( - name: "swift-srp", - platforms: [ - .macOS(.v10_15), - .iOS(.v13), - .watchOS(.v6), - .tvOS(.v13), - ], - products: [ - .library(name: "SRP", targets: ["SRP"]), - ], - dependencies: [ - .package(url: "https://github.com/apple/swift-crypto", from: "1.0.0"), - .package(url: "https://github.com/adam-fowler/big-num", from: "2.0.0"), - ], - targets: [ - .target(name: "SRP", dependencies: ["BigNum", "Crypto"]), - .testTarget( - name: "SRPTests", dependencies: ["SRP"]), - ] -) diff --git a/swift-srp-main/README.md b/swift-srp-main/README.md deleted file mode 100644 index f19c231..0000000 --- a/swift-srp-main/README.md +++ /dev/null @@ -1,94 +0,0 @@ -# Swift SRP - -This library provides a swift implementation of the Secure Remote Password protocol. Secure Remote Password (SRP) provides username and password authentication without needing to provide your password to the server. As the server never sees your password it can never leak it to anyone else. - -The server is provided with a cryptographic verifier that is derived from the password and a salt value that was used in the generation of this verifier. Both client and server generate large private and public keys and with these are able to generate a shared secret. The client then sends a proof they have the secret and if it is verified the server will do the same to verify the server as well. - -The SRP protocol is detailed in [RFC2945](https://tools.ietf.org/html/rfc2945). This library implements version 6a of the protocol which includes the username in the salt to avoid the issue where a malicious server attempting to learn if two users have the same password. I believe it is also compliant with [RFC5054](https://tools.ietf.org/html/rfc5054). - -# Usage - -First you create a configuration object. This will hold the hashing algorithm you are using, the large safe prime number required and a generator value. There is an enum that holds example primes and generators. It is general safer to use these as they are the ones provided in RFC5054 and have been battle tested. The following generates a configuration using SHA256 and a 2048 bit safe prime. You need to be sure both client and server use the same configuration. -```swift -let configuration = SRPConfiguration(.N2048) -``` -When the client wants to create a new user they generate a salt and password verifier for their username and password. -```swift -let client = SRPClient(configuration: configuration) -let (salt, verifier) = client.generateSaltAndVerifier(username: username, password: password) -``` -These are passed to the server who will store them alongside the username in a database. - -When the client wants to authenticate with the server they first need to generate a public/private key pair. These keys should only be used once. If you want to authenticate again you should generate a new pair. -```swift -let client = SRPClient(configuration: configuration) -let clientKeys = client.generateKeys() -let clientPublicKey = clientKeys.public -``` -The contents of the `clientPublicKey` variable is passed to the server alongside the username to initiate authentication. - -The server will then find the username in its database and extract the password verifier and salt that was stored with it. The password verifier is used to generate the servers key pair. -```swift -let server = SRPServer(configuration: configuration) -let serverKeys = server.generateKeys(verifier: values.verifier) -let serverPublicKey = serverKeys.public -``` -The server replies with the `serverPublicKey` and the salt value associated with the user. At this point the server will need to store the `serverKeys` and the public key it received from the client, most likely in a database. - -The client then creates the shared secret using the username, password, salt, its own key pair and the server public key. It then has to generate a proof it has the shared secret. This proof is generated from shared secret plus any of the public data available. -```swift -let clientSharedSecret = try client.calculateSharedSecret( - username: username, - password: password, - salt: salt, - clientKeys: clientKeys, - serverPublicKey: serverPublicKey -) -let clientProof = client.calculateClientProof( - username: username, - salt: salt, - clientPublicKey: clientKeys.public, - serverPublicKey: serverPublicKey, - sharedSecret: clientSharedSecret -) -``` -This `clientProof` is passed to the server. The server then generates its own version of the shared secret and verifies the `clientProof` is valid and if so will respond with it's own proof that it has the shared secret. -```swift -let serverSharedSecret = try server.calculateSharedSecret( - clientPublicKey: clientPublicKey, - serverKeys: serverKeys, - verifier: verifier -) -let serverProof = try server.verifyClientProof( - proof: clientProof, - username: username, - salt: salt, - clientPublicKey: clientPublicKey, - serverPublicKey: serverKeys.public, - sharedSecret: serverSharedSecret -) -``` -And finally the client can verify the server proof is valid -```swift -try client.verifyServerProof( - serverProof: serverProof, - clientProof: clientProof, - clientKeys: clientKeys, - sharedSecret: clientSharedSecret -) -``` -If at any point any of these functions fail the process should be aborted. - -# Compatibility - -The library is compliant with RFC5054 and should work with any server implementing this. The library has been verified against -- example data in RFC5054 -- Mozilla test vectors in https://wiki.mozilla.org/Identity/AttachedServices/KeyServerProtocol#SRP_Verifier -- Python library [srptools](https://github.com/idlesign/srptools) -- Typescript library [tssrp6a](https://github.com/midonet/tssrp6a) - -## Proof of secret - -For generating the proof above I use the method detailed in [RFC2945](https://tools.ietf.org/html/rfc2945#section-3) but not all servers use this method. For this reason I have kept the sharedSecret generation separate from the proof generation, so you can insert your own version. - -I have also supplied a simple proof functions `server.verifySimpleClientProof` and `client.verifySimpleServerProof` which use the proof detailed in the Wikipedia [page](https://en.wikipedia.org/wiki/Secure_Remote_Password_protocol) on Secure Remote Password if you would prefer to use these. diff --git a/swift-srp-main/Sources/SRP/Array.swift b/swift-srp-main/Sources/SRP/Array.swift deleted file mode 100644 index 8f2a181..0000000 --- a/swift-srp-main/Sources/SRP/Array.swift +++ /dev/null @@ -1,29 +0,0 @@ - -extension Array where Element: FixedWidthInteger { - /// create array of random bytes - static func random(count: Int) -> [Element] { - var array = self.init() - for _ in 0.. String { - return self.map({ - let characters = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"] - return "\(characters[Int($0 >> 4)])\(characters[Int($0 & 0xf)])" - }).joined() - } -} - -/// xor together the contents of two byte arrays -func ^ (lhs: [UInt8], rhs: [UInt8]) -> [UInt8] { - precondition(lhs.count == rhs.count, "Arrays are required to be the same size") - var result = lhs - for i in 0.. { - /// configuration. This needs to be the same as the server configuration - public let configuration: SRPConfiguration - - /// Initialise a SRPClient object - /// - Parameter configuration: configuration to use - public init(configuration: SRPConfiguration) { - self.configuration = configuration - } - - /// Initiate the authentication process - /// - Returns: An authentication state. The A value from this state should be sent to the server - public func generateKeys() -> SRPKeyPair { - var a = BigNum() - var A = BigNum() - repeat { - a = BigNum(bytes: SymmetricKey(size: .bits256)) - A = configuration.g.power(a, modulus: configuration.N) - } while A % configuration.N == BigNum(0) - - return SRPKeyPair(public: SRPKey(A), private: SRPKey(a)) - } - - /// return shared secret given the username, password, B value and salt from the server - /// - Parameters: - /// - username: user identifier - /// - password: password - /// - salt: salt - /// - clientKeys: client public/private keys - /// - serverPublicKey: server public key - /// - Throws: `nullServerKey` - /// - Returns: shared secret - public func calculateSharedSecret(username: String, password: String, salt: [UInt8], clientKeys: SRPKeyPair, serverPublicKey: SRPKey) throws -> SRPKey { - let message = [UInt8]("\(username):\(password)".utf8) - let sharedSecret = try calculateSharedSecret(message: message, salt: salt, clientKeys: clientKeys, serverPublicKey: serverPublicKey) - return SRPKey(sharedSecret) - } - - public func calculateSharedSecret(password: Data, salt: [UInt8], clientKeys: SRPKeyPair, serverPublicKey: SRPKey) throws -> SRPKey { - let message = [UInt8](Data([0x3a]) + password) - let sharedSecret = try calculateSharedSecret(message: message, salt: salt, clientKeys: clientKeys, serverPublicKey: serverPublicKey) - return SRPKey(sharedSecret) - } - - - /// calculate proof of shared secret to send to server - /// - Parameters: - /// - clientPublicKey: client public key - /// - serverPublicKey: server public key - /// - sharedSecret: shared secret - /// - Returns: The client verification code which should be passed to the server - public func calculateSimpleClientProof(clientPublicKey: SRPKey, serverPublicKey: SRPKey, sharedSecret: SRPKey) -> [UInt8] { - // get verification code - return SRP.calculateSimpleClientProof(clientPublicKey: clientPublicKey, serverPublicKey: serverPublicKey, sharedSecret: sharedSecret) - } - - /// If the server returns that the client verification code was valiid it will also return a server verification code that the client can use to verify the server is correct - /// - /// - Parameters: - /// - code: Verification code returned by server - /// - state: Authentication state - /// - Throws: `requiresVerificationKey`, `invalidServerCode` - public func verifySimpleServerProof(serverProof: [UInt8], clientProof: [UInt8], clientKeys: SRPKeyPair, sharedSecret: SRPKey) throws { - // get out version of server proof - let HAMS = SRP.calculateSimpleServerVerification(clientPublicKey: clientKeys.public, clientProof: clientProof, sharedSecret: sharedSecret) - // is it the same - guard serverProof == HAMS else { throw SRPClientError.invalidServerCode } - } - - /// calculate proof of shared secret to send to server - /// - Parameters: - /// - username: username - /// - salt: The salt value associated with the user returned by the server - /// - clientPublicKey: client public key - /// - serverPublicKey: server public key - /// - sharedSecret: shared secret - /// - Returns: The client verification code which should be passed to the server - public func calculateClientProof(username: String, salt: [UInt8], clientPublicKey: SRPKey, serverPublicKey: SRPKey, sharedSecret: SRPKey) -> [UInt8] { - - let hashSharedSecret = [UInt8](H.hash(data: sharedSecret.bytes)) - - // get verification code - return SRP.calculateClientProof(configuration: configuration, username: username, salt: salt, clientPublicKey: clientPublicKey, serverPublicKey: serverPublicKey, hashSharedSecret: hashSharedSecret) - } - - /// If the server returns that the client verification code was valid it will also return a server - /// verification code that the client can use to verify the server is correct. This is the calculation - /// to verify it is correct - /// - /// - Parameters: - /// - clientPublicKey: Client public key - /// - clientProof: Client proof - /// - sharedSecret: Shared secret - public func calculateServerProof(clientPublicKey: SRPKey, clientProof: [UInt8], sharedSecret: SRPKey) -> [UInt8] { - let hashSharedSecret = [UInt8](H.hash(data: sharedSecret.bytes)) - // get out version of server proof - return SRP.calculateServerVerification(clientPublicKey: clientPublicKey, clientProof: clientProof, sharedSecret: hashSharedSecret) - } - - /// If the server returns that the client verification code was valid it will also return a server - /// verification code that the client can use to verify the server is correct - /// - /// - Parameters: - /// - clientProof: Server proof - /// - clientProof: Client proof - /// - clientKeys: Client keys - /// - sharedSecret: Shared secret - /// - Throws: `requiresVerificationKey`, `invalidServerCode` - public func verifyServerProof(serverProof: [UInt8], clientProof: [UInt8], clientKeys: SRPKeyPair, sharedSecret: SRPKey) throws { - // get our version of server proof - let HAMK = calculateServerProof(clientPublicKey: clientKeys.public, clientProof: clientProof, sharedSecret: sharedSecret) - // is it the same - guard serverProof == HAMK else { throw SRPClientError.invalidServerCode } - } - - /// Generate salt and password verifier from username and password. When creating your user instead of - /// passing your password to the server, you pass the salt and password verifier values. In this way the - /// server never knows your password so can never leak it. - /// - /// - Parameters: - /// - username: username - /// - password: user password - /// - Returns: tuple containing salt and password verifier - public func generateSaltAndVerifier(username: String, password: String) -> (salt: [UInt8], verifier: SRPKey) { - let salt = [UInt8].random(count: 16) - let verifier = generatePasswordVerifier(username: username, password: password, salt: salt) - return (salt: salt, verifier: SRPKey(verifier)) - } -} - -extension SRPClient { - /// return shared secret given the username, password, B value and salt from the server - func calculateSharedSecret(message: [UInt8], salt: [UInt8], clientKeys: SRPKeyPair, serverPublicKey: SRPKey) throws -> BigNum { - guard serverPublicKey.number % configuration.N != BigNum(0) else { throw SRPClientError.nullServerKey } - - // calculate u = H(clientPublicKey | serverPublicKey) - let u = SRP.calculateU(clientPublicKey: clientKeys.public.bytes, serverPublicKey: serverPublicKey.bytes, pad: configuration.sizeN) - - guard u != 0 else { throw SRPClientError.nullServerKey } - - let x = BigNum(bytes: [UInt8](H.hash(data: salt + H.hash(data: message)))) - - // calculate S = (B - k*g^x)^(a+u*x) - let S = (serverPublicKey.number - configuration.k * configuration.g.power(x, modulus: configuration.N)).power(clientKeys.private.number + u * x, modulus: configuration.N) - - return S - } - - /// generate password verifier - public func generatePasswordVerifier(username: String, password: String, salt: [UInt8]) -> BigNum { - let message = "\(username):\(password)" - return generatePasswordVerifier(message: [UInt8](message.utf8), salt: salt) - } - - /// generate password verifier - public func generatePasswordVerifier(message: [UInt8], salt: [UInt8]) -> BigNum { - let x = BigNum(bytes: [UInt8](H.hash(data: salt + H.hash(data: message)))) - let verifier = configuration.g.power(x, modulus: configuration.N) - return verifier - } -} diff --git a/swift-srp-main/Sources/SRP/configuration.swift b/swift-srp-main/Sources/SRP/configuration.swift deleted file mode 100644 index a1fe01a..0000000 --- a/swift-srp-main/Sources/SRP/configuration.swift +++ /dev/null @@ -1,201 +0,0 @@ -import BigNum -import Crypto - -/// SRP Configuration. The same configuration hast to be used by both client and server. Contains a large safe prime N ie a prime where the value (N-1)/2 is also a prime, g the multiplicative group generator and the value k = Hash(N | g) -public struct SRPConfiguration { - /// large safe prime - public let N: BigNum - /// multiplicative group generator - public let g: BigNum - /// derived value from N and g. k = H( N | g ) - public let k: BigNum - /// size in bytes of N - public let sizeN: Int - - /// Initialise SRPConfiguration with known safe prime - /// - Parameter prime: enum indicating size of prime - public init(_ prime: Prime) { - self.N = prime.group - self.sizeN = Int(self.N.numBits() + 7) / 8 - self.g = prime.generator - self.k = BigNum(bytes: [UInt8](H.hash(data: self.N.bytes + SRP.pad(self.g.bytes, to: sizeN)))) - } - - /// Initialise SRPConfiguration with your own prime and multiplicative group generator - /// - Parameters: - /// - N: Large prime - /// - g: multiplicative group generator (usually 2) - public init(N: BigNum, g: BigNum) { - self.N = N - self.sizeN = Int(self.N.numBits() + 7) / 8 - self.g = g - self.k = BigNum(bytes: [UInt8](H.hash(data: self.N.bytes + SRP.pad(self.g.bytes, to: sizeN)))) - } - - public enum Prime { - case N1024 - case N1536 - case N2048 - case N3072 - case N4096 - case N6144 - case N8192 - - /// prime numbers and generators taken from RC 5054 https://tools.ietf.org/html/rfc5054 - var group: BigNum { - switch self { - case .N1024: - return BigNum(hex: - "EEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C" + - "9C256576D674DF7496EA81D3383B4813D692C6E0E0D5D8E250B98BE4" + - "8E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B29" + - "7BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9A" + - "FD5138FE8376435B9FC61D2FC0EB06E3")! - - case .N1536: - return BigNum(hex: - "9DEF3CAFB939277AB1F12A8617A47BBBDBA51DF499AC4C80BEEEA961" + - "4B19CC4D5F4F5F556E27CBDE51C6A94BE4607A291558903BA0D0F843" + - "80B655BB9A22E8DCDF028A7CEC67F0D08134B1C8B97989149B609E0B" + - "E3BAB63D47548381DBC5B1FC764E3F4B53DD9DA1158BFD3E2B9C8CF5" + - "6EDF019539349627DB2FD53D24B7C48665772E437D6C7F8CE442734A" + - "F7CCB7AE837C264AE3A9BEB87F8A2FE9B8B5292E5A021FFF5E91479E" + - "8CE7A28C2442C6F315180F93499A234DCF76E3FED135F9BB")! - - case .N2048: - return BigNum(hex: - "AC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC319294" + - "3DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310D" + - "CD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FB" + - "D5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF74" + - "7359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A" + - "436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D" + - "5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E73" + - "03CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB6" + - "94B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F" + - "9E4AFF73")! - - case .N3072: - return BigNum(hex: - "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08" + - "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B" + - "302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9" + - "A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6" + - "49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8" + - "FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D" + - "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C" + - "180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718" + - "3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D" + - "04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D" + - "B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226" + - "1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C" + - "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC" + - "E0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF")! - - case .N4096: - return BigNum(hex: - "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08" + - "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B" + - "302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9" + - "A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6" + - "49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8" + - "FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D" + - "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C" + - "180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718" + - "3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D" + - "04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D" + - "B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226" + - "1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C" + - "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC" + - "E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26" + - "99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB" + - "04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2" + - "233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127" + - "D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199" + - "FFFFFFFFFFFFFFFF")! - - case .N6144: - return BigNum(hex: - "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08" + - "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B" + - "302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9" + - "A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6" + - "49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8" + - "FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D" + - "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C" + - "180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718" + - "3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D" + - "04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D" + - "B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226" + - "1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C" + - "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC" + - "E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26" + - "99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB" + - "04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2" + - "233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127" + - "D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492" + - "36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406" + - "AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918" + - "DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B33205151" + - "2BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03" + - "F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97F" + - "BEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA" + - "CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58B" + - "B7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632" + - "387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E" + - "6DCC4024FFFFFFFFFFFFFFFF")! - - case .N8192: - return BigNum(hex: - "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08" + - "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B" + - "302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9" + - "A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6" + - "49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8" + - "FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D" + - "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C" + - "180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718" + - "3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D" + - "04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D" + - "B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226" + - "1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C" + - "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC" + - "E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26" + - "99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB" + - "04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2" + - "233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127" + - "D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492" + - "36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406" + - "AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918" + - "DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B33205151" + - "2BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03" + - "F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97F" + - "BEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA" + - "CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58B" + - "B7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632" + - "387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E" + - "6DBE115974A3926F12FEE5E438777CB6A932DF8CD8BEC4D073B931BA" + - "3BC832B68D9DD300741FA7BF8AFC47ED2576F6936BA424663AAB639C" + - "5AE4F5683423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9" + - "22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B4BCBC886" + - "2F8385DDFA9D4B7FA2C087E879683303ED5BDD3A062B3CF5B3A278A6" + - "6D2A13F83F44F82DDF310EE074AB6A364597E899A0255DC164F31CC5" + - "0846851DF9AB48195DED7EA1B1D510BD7EE74D73FAF36BC31ECFA268" + - "359046F4EB879F924009438B481C6CD7889A002ED5EE382BC9190DA6" + - "FC026E479558E4475677E9AA9E3050E2765694DFC81F56E880B96E71" + - "60C980DD98EDD3DFFFFFFFFFFFFFFFFF")! - } - } - - var generator: BigNum { - switch self { - case .N1024, .N1536, .N2048: - return BigNum(2) - case .N3072, .N4096, .N6144: - return BigNum(5) - case .N8192: - return BigNum(19) - } - } - } -} diff --git a/swift-srp-main/Sources/SRP/error.swift b/swift-srp-main/Sources/SRP/error.swift deleted file mode 100644 index d082960..0000000 --- a/swift-srp-main/Sources/SRP/error.swift +++ /dev/null @@ -1,21 +0,0 @@ -/// Errors thrown by SRPClient -public enum SRPClientError: Swift.Error { - /// the key returned by server is invalid, in that either it modulo N is zero or the hash(A,B) is zero - case nullServerKey - /// server verification code was wrong - case invalidServerCode - /// you called verifyServerCode without a verification key - case requiresVerificationKey - /// client key is invalid - case invalidClientKey -} - -/// Errors thrown by SRPServer -///Errors thrown by SRPServer -public enum SRPServerError: Swift.Error { - /// the modulus of the client key and N generated a zero - case nullClientKey - /// client proof of the shared secret was invalid or wrong - case invalidClientProof -} - diff --git a/swift-srp-main/Sources/SRP/keys.swift b/swift-srp-main/Sources/SRP/keys.swift deleted file mode 100644 index c7d08a0..0000000 --- a/swift-srp-main/Sources/SRP/keys.swift +++ /dev/null @@ -1,40 +0,0 @@ -import BigNum - -/// Wrapper for keys used by SRP -public struct SRPKey { - public let number: BigNum - public var bytes: [UInt8] { number.bytes } - public var hex: String { number.hex } - - public init(_ bytes: [UInt8]) { - self.number = BigNum(bytes: bytes) - } - - public init(_ number: BigNum) { - self.number = number - } - - public init?(hex: String) { - guard let number = BigNum(hex: hex) else { return nil } - self.number = number - } -} - -extension SRPKey: Equatable { } - -/// Contains a private and a public key -public struct SRPKeyPair { - public let `public`: SRPKey - public let `private`: SRPKey - - - /// Initialise a SRPKeyPair object - /// - Parameters: - /// - public: The public key of the key pair - /// - private: The private key of the key pair - public init(`public`: SRPKey, `private`: SRPKey) { - self.private = `private` - self.public = `public` - } -} - diff --git a/swift-srp-main/Sources/SRP/server.swift b/swift-srp-main/Sources/SRP/server.swift deleted file mode 100644 index 1e1931c..0000000 --- a/swift-srp-main/Sources/SRP/server.swift +++ /dev/null @@ -1,110 +0,0 @@ -import BigNum -import Crypto - -/// Manages the server side of Secure Remote Password. -/// -/// Secure Remote Password (SRP) provides username and password authentication without needing to provide your password to the server. The server -/// has a cryptographic verifier that is derived from the password and a salt that was used to generate this verifier. Both client and server -/// generate a shared secret then the client sends a proof they have the secret and if it is correct the server will do the same to verify the -/// server as well. -/// -/// This version is compliant with SRP version 6a and RFC 5054. -/// -/// Reference reading -/// - https://tools.ietf.org/html/rfc2945 -/// - https://tools.ietf.org/html/rfc5054 -/// -public struct SRPServer { - /// Authentication state. Stores A,B and shared secret - public struct AuthenticationState { - let clientPublicKey: SRPKey - let serverPublicKey: SRPKey - var serverPrivateKey: SRPKey - } - - /// configuration has to be the same as the client configuration - public let configuration: SRPConfiguration - - /// Initialise SRPServer - /// - Parameter configuration: configuration to use - public init(configuration: SRPConfiguration) { - self.configuration = configuration - } - - /// generate public and private keys to be used in srp authentication - /// - Parameter verifier: password verifier used to generate key pair - /// - Returns: return public/private key pair - public func generateKeys(verifier: SRPKey) -> SRPKeyPair { - var b: BigNum - var B: BigNum - repeat { - b = BigNum(bytes: SymmetricKey(size: .bits256)) - B = (configuration.k * verifier.number + configuration.g.power(b, modulus: configuration.N)) % configuration.N - } while B % configuration.N == BigNum(0) - - return SRPKeyPair(public: SRPKey(B), private: SRPKey(b)) - } - - /// calculate the shared secret - /// - Parameters: - /// - clientPublicKey: public key received from client - /// - serverKeys: server key pair - /// - verifier: password verifier - /// - Returns: shared secret - public func calculateSharedSecret(clientPublicKey: SRPKey, serverKeys: SRPKeyPair, verifier: SRPKey) throws -> SRPKey { - guard clientPublicKey.number % configuration.N != BigNum(0) else { throw SRPServerError.nullClientKey } - - // calculate u = H(clientPublicKey | serverPublicKey) - let u = SRP.calculateU(clientPublicKey: clientPublicKey.bytes, serverPublicKey: serverKeys.public.bytes, pad: configuration.sizeN) - - // calculate S - let S = ((clientPublicKey.number * verifier.number.power(u, modulus: configuration.N)).power(serverKeys.private.number, modulus: configuration.N)) - - return SRPKey(S) - } - - /// verify proof that client has shared secret and return a server verification proof. If verification fails a `invalidClientCode` error is thrown - /// - /// - Parameters: - /// - proof: Client proof - /// - clientPublicKey: Client public key - /// - serverPublicKey: Server public key - /// - sharedSecret: Shared secret - /// - Throws: invalidClientCode - /// - Returns: The server verification code - public func verifySimpleClientProof(proof: [UInt8], clientPublicKey: SRPKey, serverPublicKey: SRPKey, sharedSecret: SRPKey) throws -> [UInt8] { - let clientProof = SRP.calculateSimpleClientProof( - clientPublicKey: clientPublicKey, - serverPublicKey: serverPublicKey, - sharedSecret: sharedSecret - ) - guard clientProof == proof else { throw SRPServerError.invalidClientProof } - return SRP.calculateSimpleServerVerification(clientPublicKey: clientPublicKey, clientProof: clientProof, sharedSecret: sharedSecret) - } - - /// verify proof that client has shared secret and return a server verification proof. If verification fails a `invalidClientCode` error is thrown - /// - /// - Parameters: - /// - code: verification code sent by user - /// - username: username - /// - salt: salt stored with user - /// - clientPublicKey: Client public key - /// - serverPublicKey: Server public key - /// - sharedSecret: Shared secret - /// - Throws: invalidClientCode - /// - Returns: The server verification code - public func verifyClientProof(proof: [UInt8], username: String, salt: [UInt8], clientPublicKey: SRPKey, serverPublicKey: SRPKey, sharedSecret: SRPKey) throws -> [UInt8] { - let hashSharedSecret = [UInt8](H.hash(data: sharedSecret.bytes)) - - let clientProof = SRP.calculateClientProof( - configuration: configuration, - username: username, - salt: salt, - clientPublicKey: clientPublicKey, - serverPublicKey: serverPublicKey, - hashSharedSecret: hashSharedSecret - ) - guard clientProof == proof else { throw SRPServerError.invalidClientProof } - return SRP.calculateServerVerification(clientPublicKey: clientPublicKey, clientProof: clientProof, sharedSecret: hashSharedSecret) - } -} diff --git a/swift-srp-main/Sources/SRP/srp.swift b/swift-srp-main/Sources/SRP/srp.swift deleted file mode 100644 index 6e192c3..0000000 --- a/swift-srp-main/Sources/SRP/srp.swift +++ /dev/null @@ -1,66 +0,0 @@ -import BigNum -import Crypto - -/// Contains common code used by both client and server SRP code -public struct SRP { - - /// pad to a certain size by prefixing with zeros - static func pad(_ data: [UInt8], to size: Int) -> [UInt8] { - let padSize = size - data.count - guard padSize > 0 else { return data } - // create prefix and return prefix + data - let prefix: [UInt8] = (1...padSize).reduce([]) { result,_ in return result + [0] } - return prefix + data - } - - /// calculate u = H(clientPublicKey | serverPublicKey) - public static func calculateU(clientPublicKey: [UInt8], serverPublicKey: [UInt8], pad: Int) -> BigNum { - BigNum(bytes: [UInt8].init(H.hash(data: SRP.pad(clientPublicKey, to: pad) + SRP.pad(serverPublicKey, to: pad)))) - } - - /// Calculate a simpler client verification code H(A | B | S) - static func calculateSimpleClientProof( - clientPublicKey: SRPKey, - serverPublicKey: SRPKey, - sharedSecret: SRPKey) -> [UInt8] - { - let HABK = H.hash(data: clientPublicKey.bytes + serverPublicKey.bytes + sharedSecret.bytes) - return [UInt8](HABK) - } - - /// Calculate a simpler client verification code H(A | M1 | S) - static func calculateSimpleServerVerification( - clientPublicKey: SRPKey, - clientProof: [UInt8], - sharedSecret: SRPKey) -> [UInt8] - { - let HABK = H.hash(data: clientPublicKey.bytes + clientProof + sharedSecret.bytes) - return [UInt8](HABK) - } - - /// Calculate client verification code H(H(N)^ H(g)) | H(username) | salt | A | B | H(S)) - static func calculateClientProof( - configuration: SRPConfiguration, - username: String, - salt: [UInt8], - clientPublicKey: SRPKey, - serverPublicKey: SRPKey, - hashSharedSecret: [UInt8]) -> [UInt8] - { - // M = H(H(N)^ H(g)) | H(username) | salt | client key | server key | H(shared secret)) - let hashN = BigNum(bytes: H.hash(data: configuration.N.bytes)) - let hashG = BigNum(bytes: H.hash(data: SRP.pad(configuration.g.bytes, to: configuration.sizeN))) - let N_xor_g = hashN.bytes ^ hashG.bytes - let hashUser = H.hash(data: [UInt8](username.utf8)) - let M1 = [UInt8](N_xor_g) + hashUser + salt - let M2 = clientPublicKey.bytes + serverPublicKey.bytes + hashSharedSecret - let M = H.hash(data: M1 + M2) - return [UInt8](M) - } - - /// Calculate server verification code H(A | M1 | K) - static func calculateServerVerification(clientPublicKey: SRPKey, clientProof: [UInt8], sharedSecret: [UInt8]) -> [UInt8] { - let HAMK = H.hash(data: clientPublicKey.bytes + clientProof + sharedSecret) - return [UInt8](HAMK) - } -} diff --git a/swift-srp-main/Tests/LinuxMain.swift b/swift-srp-main/Tests/LinuxMain.swift deleted file mode 100644 index f87f4e9..0000000 --- a/swift-srp-main/Tests/LinuxMain.swift +++ /dev/null @@ -1,6 +0,0 @@ -import XCTest -import SRPTests - -var tests = [XCTestCaseEntry]() -tests += SRPTests.allTests() -XCTMain(tests) diff --git a/swift-srp-main/Tests/SRPTests/SRPTests.swift b/swift-srp-main/Tests/SRPTests/SRPTests.swift deleted file mode 100644 index ab15098..0000000 --- a/swift-srp-main/Tests/SRPTests/SRPTests.swift +++ /dev/null @@ -1,207 +0,0 @@ -import XCTest -import BigNum -import Crypto -@testable import SRP - -final class SRPTests: XCTestCase { - - func testSRPSharedSecret() { - let username = "adamfowler" - let password = "testpassword" - let configuration = SRPConfiguration(.N2048) - let client = SRPClient(configuration: configuration) - let server = SRPServer(configuration: configuration) - - let (salt, verifier) = client.generateSaltAndVerifier(username: username, password: password) - - let clientKeys = client.generateKeys() - let serverKeys = server.generateKeys(verifier: verifier) - - do { - let sharedSecret = try client.calculateSharedSecret( - username: username, - password: password, - salt: salt, - clientKeys: clientKeys, - serverPublicKey: serverKeys.public) - - let serverSharedSecret = try server.calculateSharedSecret( - clientPublicKey: clientKeys.public, - serverKeys: serverKeys, - verifier: verifier) - - XCTAssertEqual(sharedSecret, serverSharedSecret) - } catch { - XCTFail("\(error)") - } - } - - func testVerifySRP(configuration: SRPConfiguration) { - let username = "adamfowler" - let password = "testpassword" - let client = SRPClient(configuration: configuration) - let server = SRPServer(configuration: configuration) - - let (salt, verifier) = client.generateSaltAndVerifier(username: username, password: password) - - do { - // client initiates authentication - let clientKeys = client.generateKeys() - // provides the server with an A value and username from which it gets the password verifier. - // server initiates authentication - let serverKeys = server.generateKeys(verifier: verifier) - // server passes back B value and a salt which was attached to the user - // client calculates verification code from username, password, current authenticator state, B and salt - let clientSharedSecret = try client.calculateSharedSecret(username: username, password: password, salt: salt, clientKeys: clientKeys, serverPublicKey: serverKeys.public) - let clientProof = client.calculateClientProof(username: username, salt: salt, clientPublicKey: clientKeys.public, serverPublicKey: serverKeys.public, sharedSecret: clientSharedSecret) - // client passes proof key to server - // server validates the key and then returns a server validation key - let serverSharedSecret = try server.calculateSharedSecret(clientPublicKey: clientKeys.public, serverKeys: serverKeys, verifier: verifier) - let serverProof = try server.verifyClientProof(proof: clientProof, username: username, salt: salt, clientPublicKey: clientKeys.public, serverPublicKey: serverKeys.public, sharedSecret: serverSharedSecret) - // client verifies server validation key - try client.verifyServerProof(serverProof: serverProof, clientProof: clientProof, clientKeys: clientKeys, sharedSecret: clientSharedSecret) - } catch { - XCTFail("\(error)") - } - } - - func testVerifySRP() { - testVerifySRP(configuration: SRPConfiguration(.N1024)) - testVerifySRP(configuration: SRPConfiguration(.N1536)) - testVerifySRP(configuration: SRPConfiguration(.N2048)) - testVerifySRP(configuration: SRPConfiguration(.N3072)) - testVerifySRP(configuration: SRPConfiguration(.N4096)) - testVerifySRP(configuration: SRPConfiguration(.N6144)) - testVerifySRP(configuration: SRPConfiguration(.N8192)) - } - - func testVerifySRPCustomConfiguration() { - testVerifySRP(configuration: SRPConfiguration(N: BigNum(37), g: BigNum(3))) - } - - func testClientSessionProof() { - let configuration = SRPConfiguration(.N1024) - let username = "alice" - let salt = "bafa3be2813c9326".bytes(using: .hexadecimal)! - let A = BigNum(hex: "b525e8fe2eac8f5da6b3220e66a0ab6f833a59d5f079fe9ddcdf111a22eaec95850374d9d7597f45497eb429bcde5057a450948de7d48edc034264916a01e6c0690e14b0a527f107d3207fd2214653c9162f5745e7cbeb19a550a072d4600ce8f4ef778f6d6899ba718adf0a462e7d981ed689de93ea1bda773333f23ebb4a9b")! - let B = BigNum(hex: "2bfc8559a022497f1254af3c76786b95cb0dfb449af15501aa51eefe78947d7ef06df4fcc07a899bcaae0e552ca72c7a1f3016f3ec357a86a1428dad9f98cb8a69d405404e57e9aaf01e51a46a73b3fc7bc1d212569e4a882ae6d878599e098c89033838ec069fe368a49461f531e5b4662700d56d8c252d0aea9da6abe9b014")! - let secret = "b6288955afd690a13686d65886b5f82018515df3".bytes(using: .hexadecimal)! - let clientProof = SRP.calculateClientProof(configuration: configuration, username: username, salt: salt, clientPublicKey: SRPKey(A), serverPublicKey: SRPKey(B), hashSharedSecret: secret) - - XCTAssertEqual(clientProof.hexdigest(), "e4c5c2e145ea2de18d0cc1ac9dc2a0d0988706d6") - } - - func testServerSessionProof() { - let A = BigNum(hex: "eade4992a46182e9ffe2e69f3e2639ca5f8c29b2868083c45d0972b72bb6003911b64a7ea6738061d705d368ddbe2bdb251bec63184db09b8990d8a7415dc449fbab720626fc25d6bd33c32234973c1e41c25b18d1824590c807c491221be5493878bd27a5ca507fd3963c849b07a9ec413e13253c6c61e7f3219b247cfa574a")! - let secret = "d89740e18a9fb597aef8f2ecc0e66f4b31c2ae08".bytes(using: .hexadecimal)! - let clientProof = "e1a8629a723039a61be91a173ab6260fc582192f".bytes(using: .hexadecimal)! - - let serverProof = SRP.calculateServerVerification(clientPublicKey: SRPKey(A), clientProof: clientProof, sharedSecret: secret) - - XCTAssertEqual(serverProof.hexdigest(), "8342bd06bdf4d263de2df9a56da8e581fb38c769") - } - - // Test results against RFC5054 Appendix B - func testRFC5054Appendix() throws { - let username = "alice" - let password = "password123" - let salt = "BEB25379D1A8581EB5A727673A2441EE".bytes(using: .hexadecimal)! - let configuration = SRPConfiguration(.N1024) - let client = SRPClient(configuration: configuration) - - XCTAssertEqual(configuration.k.hex, "7556AA045AEF2CDD07ABAF0F665C3E818913186F".lowercased()) - - let verifier = client.generatePasswordVerifier(username: username, password: password, salt: salt) - - XCTAssertEqual(verifier.hex, "7E273DE8696FFC4F4E337D05B4B375BEB0DDE1569E8FA00A9886D8129BADA1F1822223CA1A605B530E379BA4729FDC59F105B4787E5186F5C671085A1447B52A48CF1970B4FB6F8400BBF4CEBFBB168152E08AB5EA53D15C1AFF87B2B9DA6E04E058AD51CC72BFC9033B564E26480D78E955A5E29E7AB245DB2BE315E2099AFB".lowercased()) - - let a = BigNum(hex: "60975527035CF2AD1989806F0407210BC81EDC04E2762A56AFD529DDDA2D4393")! - // copied from client.swift - let A = configuration.g.power(a, modulus: configuration.N) - - XCTAssertEqual(A.hex, "61D5E490F6F1B79547B0704C436F523DD0E560F0C64115BB72557EC44352E8903211C04692272D8B2D1A5358A2CF1B6E0BFCF99F921530EC8E39356179EAE45E42BA92AEACED825171E1E8B9AF6D9C03E1327F44BE087EF06530E69F66615261EEF54073CA11CF5858F0EDFDFE15EFEAB349EF5D76988A3672FAC47B0769447B".lowercased()) - - let b = BigNum(hex: "E487CB59D31AC550471E81F00F6928E01DDA08E974A004F49E61F5D105284D20")! - // copied from server.swift - let B = (configuration.k * verifier + configuration.g.power(b, modulus: configuration.N)) % configuration.N - - XCTAssertEqual(B.hex, "BD0C61512C692C0CB6D041FA01BB152D4916A1E77AF46AE105393011BAF38964DC46A0670DD125B95A981652236F99D9B681CBF87837EC996C6DA04453728610D0C6DDB58B318885D7D82C7F8DEB75CE7BD4FBAA37089E6F9C6059F388838E7A00030B331EB76840910440B1B27AAEAEEB4012B7D7665238A8E3FB004B117B58".lowercased()) - - let u = SRP.calculateU(clientPublicKey: A.bytes, serverPublicKey: B.bytes, pad: configuration.sizeN) - - XCTAssertEqual(u.hex, "CE38B9593487DA98554ED47D70A7AE5F462EF019".lowercased()) - - let sharedSecret = try client.calculateSharedSecret(username: username, password: password, salt: salt, clientKeys: SRPKeyPair(public: SRPKey(A), private: SRPKey(a)), serverPublicKey: SRPKey(B)) - - XCTAssertEqual(sharedSecret.number.hex, "B0DC82BABCF30674AE450C0287745E7990A3381F63B387AAF271A10D233861E359B48220F7C4693C9AE12B0A6F67809F0876E2D013800D6C41BB59B6D5979B5C00A172B4A2A5903A0BDCAF8A709585EB2AFAFA8F3499B200210DCC1F10EB33943CD67FC88A2F39A4BE5BEC4EC0A3212DC346D7E474B29EDE8A469FFECA686E5A".lowercased()) - } - - /// Test library against Mozilla test vectors https://wiki.mozilla.org/Identity/AttachedServices/KeyServerProtocol#SRP_Verifier - func testMozillaTestVectors() throws { - let username = "andré@example.org" - let password = "00f9b71800ab5337d51177d8fbc682a3653fa6dae5b87628eeec43a18af59a9d".bytes(using: .hexadecimal)! - let salt = "00f1000000000000000000000000000000000000000000000000000000000179".bytes(using: .hexadecimal)! - let configuration = SRPConfiguration(.N2048) - let client = SRPClient(configuration: configuration) - - XCTAssertEqual(configuration.k.dec, "2590038599070950300691544216303772122846747035652616593381637186118123578112") - - let message = [UInt8]("\(username):".utf8) + password - let verifier = client.generatePasswordVerifier(message: message, salt: salt) - - XCTAssertEqual(verifier.hex, "173ffa0263e63ccfd6791b8ee2a40f048ec94cd95aa8a3125726f9805e0c8283c658dc0b607fbb25db68e68e93f2658483049c68af7e8214c49fde2712a775b63e545160d64b00189a86708c69657da7a1678eda0cd79f86b8560ebdb1ffc221db360eab901d643a75bf1205070a5791230ae56466b8c3c1eb656e19b794f1ea0d2a077b3a755350208ea0118fec8c4b2ec344a05c66ae1449b32609ca7189451c259d65bd15b34d8729afdb5faff8af1f3437bbdc0c3d0b069a8ab2a959c90c5a43d42082c77490f3afcc10ef5648625c0605cdaace6c6fdc9e9a7e6635d619f50af7734522470502cab26a52a198f5b00a279858916507b0b4e9ef9524d6") - - let b = BigNum(hex: "00f3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f")! - // copied from server.swift - let B = (configuration.k * verifier + configuration.g.power(b, modulus: configuration.N)) % configuration.N - - XCTAssertEqual(B.hex, "22ce5a7b9d81277172caa20b0f1efb4643b3becc53566473959b07b790d3c3f08650d5531c19ad30ebb67bdb481d1d9cf61bf272f8439848fdda58a4e6abc5abb2ac496da5098d5cbf90e29b4b110e4e2c033c70af73925fa37457ee13ea3e8fde4ab516dff1c2ae8e57a6b264fb9db637eeeae9b5e43dfaba9b329d3b8770ce89888709e026270e474eef822436e6397562f284778673a1a7bc12b6883d1c21fbc27ffb3dbeb85efda279a69a19414969113f10451603065f0a012666645651dde44a52f4d8de113e2131321df1bf4369d2585364f9e536c39a4dce33221be57d50ddccb4384e3612bbfd03a268a36e4f7e01de651401e108cc247db50392") - - let a = BigNum(hex: "00f2000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d3d7")! - // copied from client.swift - let A = configuration.g.power(a, modulus: configuration.N) - - XCTAssertEqual(A.hex, "7da76cb7e77af5ab61f334dbd5a958513afcdf0f47ab99271fc5f7860fe2132e5802ca79d2e5c064bb80a38ee08771c98a937696698d878d78571568c98a1c40cc6e7cb101988a2f9ba3d65679027d4d9068cb8aad6ebff0101bab6d52b5fdfa81d2ed48bba119d4ecdb7f3f478bd236d5749f2275e9484f2d0a9259d05e49d78a23dd26c60bfba04fd346e5146469a8c3f010a627be81c58ded1caaef2363635a45f97ca0d895cc92ace1d09a99d6beb6b0dc0829535c857a419e834db12864cd6ee8a843563b0240520ff0195735cd9d316842d5d3f8ef7209a0bb4b54ad7374d73e79be2c3975632de562c596470bb27bad79c3e2fcddf194e1666cb9fc") - - let u = SRP.calculateU(clientPublicKey: A.bytes, serverPublicKey: B.bytes, pad: configuration.sizeN) - - XCTAssertEqual(u.hex, "b284aa1064e8775150da6b5e2147b47ca7df505bed94a6f4bb2ad873332ad732") - - let sharedSecret = try client.calculateSharedSecret(message: message, salt: salt, clientKeys: SRPKeyPair(public: SRPKey(A), private: SRPKey(a)), serverPublicKey: SRPKey(B)) - - XCTAssertEqual(sharedSecret.hex, "92aaf0f527906aa5e8601f5d707907a03137e1b601e04b5a1deb02a981f4be037b39829a27dba50f1b27545ff2e28729c2b79dcbdd32c9d6b20d340affab91a626a8075806c26fe39df91d0ad979f9b2ee8aad1bc783e7097407b63bfe58d9118b9b0b2a7c5c4cdebaf8e9a460f4bf6247b0da34b760a59fac891757ddedcaf08eed823b090586c63009b2d740cc9f5397be89a2c32cdcfe6d6251ce11e44e6ecbdd9b6d93f30e90896d2527564c7eb9ff70aa91acc0bac1740a11cd184ffb989554ab58117c2196b353d70c356160100ef5f4c28d19f6e59ea2508e8e8aac6001497c27f362edbafb25e0f045bfdf9fb02db9c908f10340a639fe84c31b27") - } - - static var allTests = [ - ("testSRPSharedSecret", testSRPSharedSecret), - ("testVerifySRP", testVerifySRP), - ("testVerifySRPCustomConfiguration", testVerifySRPCustomConfiguration), - ("testClientSessionProof", testClientSessionProof), - ("testServerSessionProof", testServerSessionProof), - ("testRFC5054Appendix", testRFC5054Appendix), - ("testMozillaTestVectors", testMozillaTestVectors), - ] -} - -extension String { - enum ExtendedEncoding { - case hexadecimal - } - - func bytes(using encoding:ExtendedEncoding) -> [UInt8]? { - guard self.count % 2 == 0 else { return nil } - - var bytes: [UInt8] = [] - - var indexIsEven = true - for i in self.indices { - if indexIsEven { - let byteRange = i...self.index(after: i) - guard let byte = UInt8(self[byteRange], radix: 16) else { return nil } - bytes.append(byte) - } - indexIsEven.toggle() - } - return bytes - } -} diff --git a/swift-srp-main/Tests/SRPTests/XCTestManifests.swift b/swift-srp-main/Tests/SRPTests/XCTestManifests.swift deleted file mode 100644 index 97c4c9f..0000000 --- a/swift-srp-main/Tests/SRPTests/XCTestManifests.swift +++ /dev/null @@ -1,9 +0,0 @@ -import XCTest - -#if !canImport(ObjectiveC) -public func allTests() -> [XCTestCaseEntry] { - return [ - testCase(SRPTests.allTests), - ] -} -#endif