diff --git a/README.md b/README.md index 2343660b..0d80dbe8 100644 --- a/README.md +++ b/README.md @@ -389,7 +389,7 @@ let key = try PKCS5.PBKDF2( ).calculate() /* Generate random IV value. IV is public value. Either need to generate, or get it from elsewhere */ -let iv = AES.randomIV(AES.blockSize) +let iv = AES.randomIV() /* AES cryptor instance */ let aes = try AES(key: key, blockMode: CBC(iv: iv), padding: .pkcs7) diff --git a/Sources/CryptoSwift/BlockCipher.swift b/Sources/CryptoSwift/BlockCipher.swift index f7911780..c7f7c250 100644 --- a/Sources/CryptoSwift/BlockCipher.swift +++ b/Sources/CryptoSwift/BlockCipher.swift @@ -13,6 +13,6 @@ // - This notice may not be removed or altered from any source or binary distribution. // -protocol BlockCipher: Cipher { +public protocol BlockCipher: Cipher { static var blockSize: Int { get } } diff --git a/Sources/CryptoSwift/Cryptors.swift b/Sources/CryptoSwift/Cryptors.swift index 2c22dde9..8ce7cdfe 100644 --- a/Sources/CryptoSwift/Cryptors.swift +++ b/Sources/CryptoSwift/Cryptors.swift @@ -29,16 +29,20 @@ public protocol Cryptors: AnyObject { /// Cryptor suitable for decryption func makeDecryptor() throws -> Cryptor & Updatable - - /// Generate array of random bytes. Helper function. - static func randomIV(_ blockSize: Int) -> Array } -extension Cryptors { - /// Generate array of random values. - /// Convenience helper that uses `Swift.RandomNumberGenerator`. - /// - Parameter count: Length of array - public static func randomIV(_ count: Int) -> Array { +public extension Cryptors where Self: BlockCipher { + /// Generates array of random bytes. + /// Convenience helper that uses `Swift.SystemRandomNumberGenerator`. + /// - Parameter count: Length of the result array + @available(*, deprecated, message: "Please use `randomIV()`, which returns number of bytes equal to Self.blockSize.") + static func randomIV(_ count: Int) -> [UInt8] { (0.. [UInt8] { + (0.. = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c] let key2: Array = [0x22, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x33] @@ -678,6 +683,7 @@ extension AESTests { ("testAESDecryptCTRSeek", testAESDecryptCTRSeek), ("testAESEncryptCTRIrregularLengthIncrementalUpdate", testAESEncryptCTRIrregularLengthIncrementalUpdate), ("testAESEncryptCTRStream", testAESEncryptCTRStream), + ("testAESRandomIV", testAESRandomIV), ("testIssue298", testIssue298), ("testIssue394", testIssue394), ("testIssue411", testIssue411), diff --git a/Tests/CryptoSwiftTests/Access.swift b/Tests/CryptoSwiftTests/Access.swift index 1d767f94..d687fb2c 100644 --- a/Tests/CryptoSwiftTests/Access.swift +++ b/Tests/CryptoSwiftTests/Access.swift @@ -29,8 +29,8 @@ class Access: XCTestCase { } func testRandomIV() { - _ = AES.randomIV(AES.blockSize) - _ = ChaCha20.randomIV(ChaCha20.blockSize) + _ = AES.randomIV() + _ = ChaCha20.randomIV() } func testDigest() { diff --git a/Tests/CryptoSwiftTests/ChaCha20Tests.swift b/Tests/CryptoSwiftTests/ChaCha20Tests.swift index aa9a01fd..e574150c 100644 --- a/Tests/CryptoSwiftTests/ChaCha20Tests.swift +++ b/Tests/CryptoSwiftTests/ChaCha20Tests.swift @@ -106,6 +106,11 @@ final class ChaCha20Tests: XCTestCase { XCTFail() } } + + func testChaCha20RandomIV() { + let iv = ChaCha20.randomIV() + XCTAssertEqual(iv.count, ChaCha20.blockSize) + } } extension ChaCha20Tests { @@ -114,7 +119,8 @@ extension ChaCha20Tests { ("testChaCha20", testChaCha20), ("testCore", testCore), ("testVector1Py", testVector1Py), - ("testChaCha20EncryptPartial", testChaCha20EncryptPartial) + ("testChaCha20EncryptPartial", testChaCha20EncryptPartial), + ("testChaCha20RandomIV", testChaCha20RandomIV) ] return tests