Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use BlockCipher.blockSize as a result length in randomIV helper #893

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Sources/CryptoSwift/BlockCipher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
16 changes: 6 additions & 10 deletions Sources/CryptoSwift/Cryptors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,12 @@ 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<UInt8>
}

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<UInt8> {
(0..<count).map({ _ in UInt8.random(in: 0...UInt8.max) })
public extension Cryptors where Self: BlockCipher {
/// Generates array of random bytes. `blockSize` is used as length of the result array.
/// Convenience helper that uses `Swift.SystemRandomNumberGenerator`.
static func randomIV() -> [UInt8] {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can it be randomIV(_ count: Int = Self.blockSize) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can, but then it defeats the purpose of these changes. I'd like to make this helper hard to use in a wrong way.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not, really. It keep the backward compatibility, while use the blockSize default now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I've returned randomIV(_ count: Int) helper marking it as deprecated, so existing code won't be broken after upgrade to the new version

(0..<Self.blockSize).map({ _ in UInt8.random(in: 0...UInt8.max) })
}
}
}
6 changes: 6 additions & 0 deletions Tests/CryptoSwiftTests/AESTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,11 @@ final class AESTests: XCTestCase {
}
}

func testAESRandomIV() {
let iv = AES.randomIV()
XCTAssertEqual(iv.count, AES.blockSize)
}

func testAESWithWrongKey() {
let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
let key2: Array<UInt8> = [0x22, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x33]
Expand Down Expand Up @@ -678,6 +683,7 @@ extension AESTests {
("testAESDecryptCTRSeek", testAESDecryptCTRSeek),
("testAESEncryptCTRIrregularLengthIncrementalUpdate", testAESEncryptCTRIrregularLengthIncrementalUpdate),
("testAESEncryptCTRStream", testAESEncryptCTRStream),
("testAESRandomIV", testAESRandomIV),
("testIssue298", testIssue298),
("testIssue394", testIssue394),
("testIssue411", testIssue411),
Expand Down
4 changes: 2 additions & 2 deletions Tests/CryptoSwiftTests/Access.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class Access: XCTestCase {
}

func testRandomIV() {
_ = AES.randomIV(AES.blockSize)
_ = ChaCha20.randomIV(ChaCha20.blockSize)
_ = AES.randomIV()
_ = ChaCha20.randomIV()
}

func testDigest() {
Expand Down
8 changes: 7 additions & 1 deletion Tests/CryptoSwiftTests/ChaCha20Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ final class ChaCha20Tests: XCTestCase {
XCTFail()
}
}

func testChaCha20RandomIV() {
let iv = ChaCha20.randomIV()
XCTAssertEqual(iv.count, ChaCha20.blockSize)
}
}

extension ChaCha20Tests {
Expand All @@ -114,7 +119,8 @@ extension ChaCha20Tests {
("testChaCha20", testChaCha20),
("testCore", testCore),
("testVector1Py", testVector1Py),
("testChaCha20EncryptPartial", testChaCha20EncryptPartial)
("testChaCha20EncryptPartial", testChaCha20EncryptPartial),
("testChaCha20RandomIV", testChaCha20RandomIV)
]

return tests
Expand Down