|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftCrypto open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2021-2024 Apple Inc. and the SwiftCrypto project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftCrypto project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | +import Crypto |
| 15 | +#if canImport(Darwin) || swift(>=5.9.1) |
| 16 | +import Foundation |
| 17 | +#else |
| 18 | +@preconcurrency import Foundation |
| 19 | +#endif |
| 20 | + |
| 21 | +#if canImport(CommonCrypto) |
| 22 | +fileprivate typealias BackingPBKDF2 = CommonCryptoPBKDF2 |
| 23 | +#else |
| 24 | +fileprivate typealias BackingPBKDF2 = BoringSSLPBKDF2 |
| 25 | +#endif |
| 26 | + |
| 27 | +extension KDF.Insecure { |
| 28 | + /// An implementation of PBKDF2 key derivation function. |
| 29 | + public struct PBKDF2: Sendable { |
| 30 | + /// Derives a symmetric key using the PBKDF2 algorithm. |
| 31 | + /// |
| 32 | + /// - Parameters: |
| 33 | + /// - password: The passphrase, which should be used as a basis for the key. This can be any type that conforms to `DataProtocol`, like `Data` or an array of `UInt8` instances. |
| 34 | + /// - salt: The salt to use for key derivation. |
| 35 | + /// - hashFunction: The hash function to use for key derivation. |
| 36 | + /// - outputByteCount: The length in bytes of resulting symmetric key. |
| 37 | + /// - rounds: The number of rounds which should be used to perform key derivation. The minimum allowed number of rounds is 210,000. |
| 38 | + /// - Throws: An error if the number of rounds is less than 210,000 |
| 39 | + /// - Note: The correct choice of rounds depends on a number of factors such as the hash function used, the speed of the target machine, and the intended use of the derived key. A good rule of thumb is to use rounds in the hundered of thousands or millions. For more information see OWASP's [Password Storage Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html). |
| 40 | + /// - Returns: The derived symmetric key. |
| 41 | + public static func deriveKey<Passphrase: DataProtocol, Salt: DataProtocol>(from password: Passphrase, salt: Salt, using hashFunction: HashFunction, outputByteCount: Int, rounds: Int) throws -> SymmetricKey { |
| 42 | + guard rounds >= 210_000 else { |
| 43 | + throw CryptoKitError.incorrectParameterSize |
| 44 | + } |
| 45 | + return try PBKDF2.deriveKey(from: password, salt: salt, using: hashFunction, outputByteCount: outputByteCount, unsafeUncheckedRounds: rounds) |
| 46 | + } |
| 47 | + |
| 48 | + /// Derives a symmetric key using the PBKDF2 algorithm. |
| 49 | + /// |
| 50 | + /// - Parameters: |
| 51 | + /// - password: The passphrase, which should be used as a basis for the key. This can be any type that conforms to `DataProtocol`, like `Data` or an array of `UInt8` instances. |
| 52 | + /// - salt: The salt to use for key derivation. |
| 53 | + /// - hashFunction: The hash function to use for key derivation. |
| 54 | + /// - outputByteCount: The length in bytes of resulting symmetric key. |
| 55 | + /// - unsafeUncheckedRounds: The number of rounds which should be used to perform key derivation. |
| 56 | + /// - Warning: This method allows the use of parameters which may result in insecure keys. It is important to ensure that the used parameters do not compromise the security of the application. |
| 57 | + /// - Returns: The derived symmetric key. |
| 58 | + public static func deriveKey<Passphrase: DataProtocol, Salt: DataProtocol>(from password: Passphrase, salt: Salt, using hashFunction: HashFunction, outputByteCount: Int, unsafeUncheckedRounds: Int) throws -> SymmetricKey { |
| 59 | + return try BackingPBKDF2.deriveKey(from: password, salt: salt, using: hashFunction, outputByteCount: outputByteCount, rounds: unsafeUncheckedRounds) |
| 60 | + } |
| 61 | + |
| 62 | + public struct HashFunction: Equatable, Hashable, Sendable { |
| 63 | + let rawValue: String |
| 64 | + |
| 65 | + public static let insecureMD5 = HashFunction(rawValue: "insecure_md5") |
| 66 | + public static let insecureSHA1 = HashFunction(rawValue: "insecure_sha1") |
| 67 | + public static let insecureSHA224 = HashFunction(rawValue: "insecure_sha224") |
| 68 | + public static let sha256 = HashFunction(rawValue: "sha256") |
| 69 | + public static let sha384 = HashFunction(rawValue: "sha384") |
| 70 | + public static let sha512 = HashFunction(rawValue: "sha512") |
| 71 | + |
| 72 | + init(rawValue: String) { |
| 73 | + self.rawValue = rawValue |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments