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

[77] Improve test suite for RSAEncrypter/Decrypter #81

Merged
merged 32 commits into from
Jul 30, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
bc7bbdf
[JOSE-80] Add shared user data introduced Xcode 9.3 to gitignore
mohemian-92817281 Apr 11, 2018
9c0c504
Merge branch 'master' into feature/JOSE-80-improve-tests
mohemian-92817281 May 3, 2018
7d00453
[JOSE-80] Add failure test case for decrypting empty data
mohemian-92817281 May 3, 2018
9567ae8
[JOSE-80] Better assertions for failure case
mohemian-92817281 May 3, 2018
2265f17
[JOSE-80] privateKey2048 -> privateKeyAlice2048
mohemian-92817281 May 3, 2018
a87570c
[JOSE-80] publicKey2048 -> publicKeyAlice2048
mohemian-92817281 May 3, 2018
bcec1c5
[JOSE-80] Add Bob’s 2048 bit private key
mohemian-92817281 May 3, 2018
17ffa68
[JOSE-80] Add encryption test for Bob’s key
mohemian-92817281 May 3, 2018
f7350e5
[JOSE-80] keyPair2048 -> keyPairAlice2048
mohemian-92817281 May 3, 2018
9650165
[JOSE-80] Read Bob’s keypair from plist file
mohemian-92817281 May 3, 2018
79582f8
[JOSE-80] cipherTextBase64URL -> cipherTextWithAliceKeyBase64URL
mohemian-92817281 May 3, 2018
c660a0c
[JOSE-80] Load Bob’s key data; update cipher texts; add Bob decryptio…
mohemian-92817281 May 3, 2018
62f222d
[JOSE-80] Add test for encrypting the same data twice
mohemian-92817281 May 3, 2018
40ce19b
[JOSE-80] Add test to check if Alice’s and Bob’s cipher texts differ
mohemian-92817281 May 3, 2018
a7292e5
[JOSE-80] Add equality for decryption error
mohemian-92817281 May 3, 2018
ee5323a
[JOSE-80] Add default decryption error
mohemian-92817281 May 3, 2018
2d4f09f
[JOSE-80] Decrypting with the wrong key should throw an error
mohemian-92817281 May 7, 2018
8c58365
[JOSE-80] Update Bob’s cipher text
mohemian-92817281 May 7, 2018
f36fdb2
[JOSE-80] Fix Base64 variable names
mohemian-92817281 May 14, 2018
eeecbde
[JOSE-80] Use multiline literals for cipher texts
mohemian-92817281 May 14, 2018
9d675a3
Merge branch 'master' into feature/JOSE-80-improve-tests
mohemian-92817281 Jul 18, 2018
03adc40
[JOSE-80] Re-generate Alice’s cypher text with openssl; adjust test
mohemian-92817281 Jul 26, 2018
25fe769
[JOSE-80] Re-generate Bob’s cypher text with openssl; adjust test
mohemian-92817281 Jul 26, 2018
ba2dac1
[JOSE-80] Better comments
mohemian-92817281 Jul 26, 2018
3d374e6
[JOSE-80] Remove nonsensical empty string test
mohemian-92817281 Jul 26, 2018
6a8a3c8
[JOSE-80] Rename TestKey.plist -> TestKeys.plist
mohemian-92817281 Jul 26, 2018
09b1963
[JOSE-80] Add decrypter test for zero-length data
mohemian-92817281 Jul 26, 2018
5a75cf4
[JOSE-80] Add tests for encrypting message of length `mLen` and `mLen…
mohemian-92817281 Jul 30, 2018
b221486
[JOSE-80] Better naming for zero length decryption test
mohemian-92817281 Jul 30, 2018
130d902
[JOSE-80] Add decryption tests for `len=k`, `len=k-1`, `len=k+1`
mohemian-92817281 Jul 30, 2018
f307b16
Merge branch 'master' into feature/JOSE-80-improve-tests
mohemian-92817281 Jul 30, 2018
fdbfc93
[JOSE-80] Use Alice’s key for new tests
mohemian-92817281 Jul 30, 2018
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
43 changes: 1 addition & 42 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,45 +78,4 @@ DerivedData/
/*.gcno


## Playgrounds
timeline.xctimeline
playground.xcworkspace

# Swift Package Manager
#
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
# Package.pins
.build/

# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
Pods/

# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts

Carthage/Build

# fastlane
#
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
# screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/#source-control

fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots
fastlane/test_output
/test_output

.idea/
sonar-reports/
/JOSESwift.xcworkspace/xcshareddata
# End of https://www.gitignore.io/api/xcode,macos,carthage,cocoapods
16 changes: 8 additions & 8 deletions Tests/EncrypterDecrypterInitializationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ class EncrypterDecrypterInitializationTests: CryptoTestCase {
@available(*, deprecated)
func testEncrypterDeprecatedRSAInitialization() {
XCTAssertNotNil(
Encrypter(keyEncryptionAlgorithm: .RSA1_5, keyEncryptionKey: publicKey2048!, contentEncyptionAlgorithm: .A256CBCHS512)
Encrypter(keyEncryptionAlgorithm: .RSA1_5, keyEncryptionKey: publicKeyAlice2048!, contentEncyptionAlgorithm: .A256CBCHS512)
)
}

func testEncrypterNewRSAInitialization() {
XCTAssertNotNil(
Encrypter(keyEncryptionAlgorithm: .RSA1_5, encryptionKey: publicKey2048!, contentEncyptionAlgorithm: .A256CBCHS512)
Encrypter(keyEncryptionAlgorithm: .RSA1_5, encryptionKey: publicKeyAlice2048!, contentEncyptionAlgorithm: .A256CBCHS512)
)
}

func testEncrypterRSAInitializationWrongAlgorithm() {
XCTAssertNil(
Encrypter(keyEncryptionAlgorithm: .direct, encryptionKey: publicKey2048!, contentEncyptionAlgorithm: .A256CBCHS512)
Encrypter(keyEncryptionAlgorithm: .direct, encryptionKey: publicKeyAlice2048!, contentEncyptionAlgorithm: .A256CBCHS512)
)
}

Expand All @@ -53,26 +53,26 @@ class EncrypterDecrypterInitializationTests: CryptoTestCase {

func testEncrypterDirectInitializationWrongKeyType() {
XCTAssertNil(
Encrypter(keyEncryptionAlgorithm: .direct, encryptionKey: publicKey2048!, contentEncyptionAlgorithm: .A256CBCHS512)
Encrypter(keyEncryptionAlgorithm: .direct, encryptionKey: publicKeyAlice2048!, contentEncyptionAlgorithm: .A256CBCHS512)
)
}

@available(*, deprecated)
func testDecrypterDeprecatedRSAInitialization() {
XCTAssertNotNil(
Decrypter(keyDecryptionAlgorithm: .RSA1_5, keyDecryptionKey: privateKey2048!, contentDecryptionAlgorithm: .A256CBCHS512)
Decrypter(keyDecryptionAlgorithm: .RSA1_5, keyDecryptionKey: privateKeyAlice2048!, contentDecryptionAlgorithm: .A256CBCHS512)
)
}

func testDecrypterNewRSAInitialization() {
XCTAssertNotNil(
Decrypter(keyDecryptionAlgorithm: .RSA1_5, decryptionKey: privateKey2048!, contentDecryptionAlgorithm: .A256CBCHS512)
Decrypter(keyDecryptionAlgorithm: .RSA1_5, decryptionKey: privateKeyAlice2048!, contentDecryptionAlgorithm: .A256CBCHS512)
)
}

func testDecrypterRSAInitializationWrongAlgorithm() {
XCTAssertNil(
Decrypter(keyDecryptionAlgorithm: .direct, decryptionKey: privateKey2048!, contentDecryptionAlgorithm: .A256CBCHS512)
Decrypter(keyDecryptionAlgorithm: .direct, decryptionKey: privateKeyAlice2048!, contentDecryptionAlgorithm: .A256CBCHS512)
)
}

Expand All @@ -84,7 +84,7 @@ class EncrypterDecrypterInitializationTests: CryptoTestCase {

func testDecrypterDirectInitializationWrongKeyType() {
XCTAssertNil(
Decrypter(keyDecryptionAlgorithm: .direct, decryptionKey: privateKey2048!, contentDecryptionAlgorithm: .A256CBCHS512)
Decrypter(keyDecryptionAlgorithm: .direct, decryptionKey: privateKeyAlice2048!, contentDecryptionAlgorithm: .A256CBCHS512)
)
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/JWEDirectEncryptionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class JWEDirectEncryptionTests: CryptoTestCase {
}

func testDecryptWithCorrectAlgWrongKeyType() {
let privateKey = privateKey2048!
let privateKey = privateKeyAlice2048!

let jwe = try! JWE(compactSerialization: serializationFromNimbus)

Expand Down Expand Up @@ -102,7 +102,7 @@ class JWEDirectEncryptionTests: CryptoTestCase {
with: "eyJlbmMiOiJBMjU2Q0JDLUhTNTEyIiwiYWxnIjoiUlNBMV81In0"
)

let privateKey = privateKey2048!
let privateKey = privateKeyAlice2048!

let jwe = try! JWE(compactSerialization: serialization)

Expand Down
4 changes: 2 additions & 2 deletions Tests/JWERSATests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class JWETests: CryptoTestCase {
func testJWERoundtrip() {
let header = JWEHeader(algorithm: .RSA1_5, encryptionAlgorithm: .A256CBCHS512)
let payload = Payload(message.data(using: .utf8)!)
let encrypter = Encrypter(keyEncryptionAlgorithm: .RSA1_5, keyEncryptionKey: publicKeyAlice2048!, contentEncyptionAlgorithm: .A256CBCHS512)!
let encrypter = Encrypter(keyEncryptionAlgorithm: .RSA1_5, encryptionKey: publicKeyAlice2048!, contentEncyptionAlgorithm: .A256CBCHS512)!
let jweEnc = try! JWE(header: header, payload: payload, encrypter: encrypter)

let jweDec = try! JWE(compactSerialization: jweEnc.compactSerializedData)
Expand All @@ -57,7 +57,7 @@ class JWETests: CryptoTestCase {
func testDecryptFails() {
let header = JWEHeader(algorithm: .RSA1_5, encryptionAlgorithm: .A256CBCHS512)
let payload = Payload(message.data(using: .utf8)!)
let encrypter = Encrypter(keyEncryptionAlgorithm: .RSA1_5, keyEncryptionKey: publicKeyAlice2048!, contentEncyptionAlgorithm: .A256CBCHS512)!
let encrypter = Encrypter(keyEncryptionAlgorithm: .RSA1_5, encryptionKey: publicKeyAlice2048!, contentEncyptionAlgorithm: .A256CBCHS512)!
let jweEnc = try! JWE(header: header, payload: payload, encrypter: encrypter)

let attributes: [String: Any] = [
Expand Down
54 changes: 53 additions & 1 deletion Tests/RSADecrypterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ sVyhqpFuZQ6hhklG9lJr6OBBuk/+pcJYdHuYEuLnJhPeKqF/9xgMOU0e0xLMtkQW+IfDMlm0oAVavHrx
}
}

func testCipherTextLengthTooShort() {
func testCipherTextLengthZero() {
guard privateKeyAlice2048 != nil else {
XCTFail()
return
Expand All @@ -131,4 +131,56 @@ sVyhqpFuZQ6hhklG9lJr6OBBuk/+pcJYdHuYEuLnJhPeKqF/9xgMOU0e0xLMtkQW+IfDMlm0oAVavHrx
}
}

func testCipherTextLengthExactlyRight() {
guard privateKeyAlice2048 != nil else {
XCTFail()
return
}

// Length checking: If the length of the ciphertext C is not k octets
// (or if k < 11), output "decryption error" and stop.
// See 7.2.2 Decryption operation RSAES-PKCS1-V1_5-DECRYPT (K, C)
// https://tools.ietf.org/html/rfc3447#section-7.2.2
let cipherTextLengthInBytes = SecKeyGetBlockSize(privateKeyAlice2048!)
let testMessage = Data(count: cipherTextLengthInBytes)

let decrypter = RSADecrypter(algorithm: .RSA1_5, privateKey: privateKeyAlice2048!)

XCTAssertThrowsError(try decrypter.decrypt(testMessage)) { (error: Error) in
// Should throw "decryption failed", but
// should _not_ throw cipherTextLenghtNotSatisfied
XCTAssertNotEqual(error as? RSAError, RSAError.cipherTextLenghtNotSatisfied)
Copy link
Contributor

Choose a reason for hiding this comment

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

This test will fail as soon as #96 is done 🤔

Copy link

Choose a reason for hiding this comment

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

Good catch!

}
}

func testCipherTextLengthTooLongByOneByte() {
guard privateKeyAlice2048 != nil else {
XCTFail()
return
}

let cipherTextLengthInBytes = SecKeyGetBlockSize(privateKeyAlice2048!)
let testMessage = Data(count: cipherTextLengthInBytes + 1)

let decrypter = RSADecrypter(algorithm: .RSA1_5, privateKey: privateKeyAlice2048!)
XCTAssertThrowsError(try decrypter.decrypt(testMessage)) { (error: Error) in
XCTAssertEqual(error as? RSAError, RSAError.cipherTextLenghtNotSatisfied)
Copy link
Contributor

Choose a reason for hiding this comment

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

Same #96 (just to give a heads up)

}
}

func testCipherTextLengthTooShortByOneByte() {
guard privateKeyAlice2048 != nil else {
XCTFail()
return
}

let cipherTextLengthInBytes = SecKeyGetBlockSize(privateKeyAlice2048!)
let testMessage = Data(count: cipherTextLengthInBytes - 1)

let decrypter = RSADecrypter(algorithm: .RSA1_5, privateKey: privateKeyAlice2048!)
XCTAssertThrowsError(try decrypter.decrypt(testMessage)) { (error: Error) in
XCTAssertEqual(error as? RSAError, RSAError.cipherTextLenghtNotSatisfied)
}
}

}
30 changes: 30 additions & 0 deletions Tests/RSAEncrypterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,34 @@ class RSAEncrypterTests: CryptoTestCase {
}
}

func testMaximumPlainTextLength() {
guard publicKeyAlice2048 != nil else {
XCTFail()
return
}

// RSAES-PKCS1-v1_5 can operate on messages of length up to k - 11 octets (k = octet length of the RSA modulus)
// See https://tools.ietf.org/html/rfc3447#section-7.2
let maxMessageLengthInBytes = SecKeyGetBlockSize(publicKeyAlice2048!) - 11
let testMessage = Data(count: maxMessageLengthInBytes)

let encrypter = RSAEncrypter(algorithm: .RSA1_5, publicKey: publicKeyAlice2048!)
XCTAssertNoThrow(try encrypter.encrypt(testMessage))
}

func testMaximumPlainTextLengthPlusOne() {
guard publicKeyAlice2048 != nil else {
XCTFail()
return
}

let maxMessageLengthInBytes = SecKeyGetBlockSize(publicKeyAlice2048!) - 11
let testMessage = Data(count: maxMessageLengthInBytes + 1)

let encrypter = RSAEncrypter(algorithm: .RSA1_5, publicKey: publicKeyAlice2048!)
XCTAssertThrowsError(try encrypter.encrypt(testMessage)) { (error: Error) in
XCTAssertEqual(error as? RSAError, RSAError.plainTextLengthNotSatisfied)
}
}

}