Skip to content

Added support for RS / PS signatures #71

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "Carthage/Checkouts/CryptoSwift"]
path = Carthage/Checkouts/CryptoSwift
url = https://github.com/krzyzanowskim/CryptoSwift.git
[submodule "Carthage/Checkouts/SwCrypt"]
path = Carthage/Checkouts/SwCrypt
url = https://github.com/jossgb/SwCrypt.git
1 change: 1 addition & 0 deletions Cartfile
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
github "krzyzanowskim/CryptoSwift" ~> 0.6.1
github "jossgb/SwCrypt"
3 changes: 2 additions & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
github "krzyzanowskim/CryptoSwift" "0.6.1"
github "jossgb/SwCrypt" "4.0.0"
github "krzyzanowskim/CryptoSwift" "0.6.9"
2 changes: 1 addition & 1 deletion Carthage/Checkouts/CryptoSwift
Submodule CryptoSwift updated 85 files
+1 −1 .swift-version
+40 −0 CHANGELOG
+11 −14 CryptoSwift.playground/Contents.swift
+1 −1 CryptoSwift.playground/contents.xcplayground
+3 −3 CryptoSwift.podspec
+26 −7 CryptoSwift.xcodeproj/project.pbxproj
+22 −0 ...roj/xcshareddata/xcbaselines/754BE45F19693E190098E6F3.xcbaseline/2774350F-3E36-4FB9-835D-90E1E9EF7CE0.plist
+22 −0 ...roj/xcshareddata/xcbaselines/754BE45F19693E190098E6F3.xcbaseline/7797B693-C86A-4026-B2CE-05813EFA26F4.plist
+22 −0 ...roj/xcshareddata/xcbaselines/754BE45F19693E190098E6F3.xcbaseline/BD46E9D4-C65B-4C11-9BB5-B8B05CCE976F.plist
+1 −1 CryptoSwift.xcodeproj/xcshareddata/xcschemes/CryptoSwift.xcscheme
+64 −42 README.md
+157 −137 Sources/CryptoSwift/AES.swift
+46 −13 Sources/CryptoSwift/Array+Extension.swift
+52 −0 Sources/CryptoSwift/BatchedCollection.swift
+1 −0 Sources/CryptoSwift/Bit.swift
+1 −1 Sources/CryptoSwift/BlockCipher.swift
+1 −1 Sources/CryptoSwift/BlockMode/BlockModeOptions.swift
+2 −2 Sources/CryptoSwift/BlockMode/BlockModeWorker.swift
+7 −7 Sources/CryptoSwift/BlockMode/CBC.swift
+5 −5 Sources/CryptoSwift/BlockMode/CFB.swift
+5 −5 Sources/CryptoSwift/BlockMode/CTR.swift
+4 −4 Sources/CryptoSwift/BlockMode/ECB.swift
+4 −4 Sources/CryptoSwift/BlockMode/OFB.swift
+5 −5 Sources/CryptoSwift/BlockMode/PCBC.swift
+511 −0 Sources/CryptoSwift/Blowfish.swift
+0 −25 Sources/CryptoSwift/BytesSequence.swift
+16 −9 Sources/CryptoSwift/CSArrayType+Extensions.swift
+217 −135 Sources/CryptoSwift/ChaCha20.swift
+76 −77 Sources/CryptoSwift/Checksum.swift
+2 −2 Sources/CryptoSwift/Cipher.swift
+21 −18 Sources/CryptoSwift/Collection+Extension.swift
+4 −3 Sources/CryptoSwift/Cryptors.swift
+14 −6 Sources/CryptoSwift/Digest.swift
+3 −1 Sources/CryptoSwift/DigestType.swift
+3 −2 Sources/CryptoSwift/Foundation/AES+Foundation.swift
+20 −0 Sources/CryptoSwift/Foundation/Blowfish+Foundation.swift
+1 −0 Sources/CryptoSwift/Foundation/CSArrayType+Foundation.swift
+2 −1 Sources/CryptoSwift/Foundation/ChaCha20+Foundation.swift
+7 −9 Sources/CryptoSwift/Foundation/Data+Extension.swift
+3 −2 Sources/CryptoSwift/Foundation/HMAC+Foundation.swift
+5 −4 Sources/CryptoSwift/Foundation/Rabbit+Foundation.swift
+0 −1 Sources/CryptoSwift/Foundation/String+FoundationExtension.swift
+7 −7 Sources/CryptoSwift/Foundation/Utils+Foundation.swift
+13 −95 Sources/CryptoSwift/Generics.swift
+13 −14 Sources/CryptoSwift/HMAC.swift
+1 −1 Sources/CryptoSwift/Info.plist
+5 −4 Sources/CryptoSwift/Int+Extension.swift
+15 −10 Sources/CryptoSwift/IntegerConvertible.swift
+60 −56 Sources/CryptoSwift/MD5.swift
+6 −6 Sources/CryptoSwift/NoPadding.swift
+12 −12 Sources/CryptoSwift/Operators.swift
+6 −6 Sources/CryptoSwift/PKCS5/PBKDF1.swift
+22 −24 Sources/CryptoSwift/PKCS5/PBKDF2.swift
+1 −2 Sources/CryptoSwift/PKCS5/PKCS5.swift
+7 −8 Sources/CryptoSwift/PKCS7.swift
+2 −2 Sources/CryptoSwift/Padding.swift
+111 −251 Sources/CryptoSwift/Poly1305.swift
+45 −44 Sources/CryptoSwift/Rabbit.swift
+53 −51 Sources/CryptoSwift/SHA1.swift
+116 −106 Sources/CryptoSwift/SHA2.swift
+61 −56 Sources/CryptoSwift/SHA3.swift
+33 −3 Sources/CryptoSwift/SecureBytes.swift
+12 −6 Sources/CryptoSwift/String+Extension.swift
+1 −0 Sources/CryptoSwift/UInt16+Extension.swift
+2 −2 Sources/CryptoSwift/UInt32+Extension.swift
+8 −2 Sources/CryptoSwift/UInt64+Extension.swift
+12 −14 Sources/CryptoSwift/UInt8+Extension.swift
+18 −18 Sources/CryptoSwift/Updatable.swift
+36 −26 Sources/CryptoSwift/Utils.swift
+5 −5 Sources/CryptoSwift/ZeroPadding.swift
+193 −149 Tests/CryptoSwiftTests/AESTests.swift
+60 −39 Tests/CryptoSwiftTests/Access.swift
+216 −0 Tests/CryptoSwiftTests/BlowfishTests.swift
+64 −55 Tests/CryptoSwiftTests/ChaCha20Tests.swift
+122 −94 Tests/CryptoSwiftTests/DigestTests.swift
+5 −4 Tests/CryptoSwiftTests/Error+Extension.swift
+46 −18 Tests/CryptoSwiftTests/ExtensionsTest.swift
+21 −22 Tests/CryptoSwiftTests/HMACTests.swift
+30 −11 Tests/CryptoSwiftTests/PBKDF.swift
+14 −13 Tests/CryptoSwiftTests/PaddingTests.swift
+20 −13 Tests/CryptoSwiftTests/Poly1305Tests.swift
+23 −20 Tests/CryptoSwiftTests/RabbitTests.swift
+3 −2 Tests/CryptoSwiftTests/RandomBytesSequenceTests.swift
+3 −2 Tests/LinuxMain.swift
+4 −3 scripts/CI.sh
25 changes: 25 additions & 0 deletions Carthage/Checkouts/SwCrypt/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## Build generated
build/
DerivedData
test_output

## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata

## Other
*.xccheckout
*.moved-aside
*.xcuserstate
*.xcscmblueprint

## Obj-C/Swift specific
*.hmap
*.ipa
1 change: 1 addition & 0 deletions Carthage/Checkouts/SwCrypt/Carthage/Build
137 changes: 137 additions & 0 deletions Carthage/Checkouts/SwCrypt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)

SwCrypt
=========

### Create public and private RSA keys in DER format
```
let (privateKey, publicKey) = try! CC.RSA.generateKeyPair(2048)
```
### Convert them to PEM format
```
let privateKeyPEM = try SwKeyConvert.PrivateKey.derToPKCS1PEM(privateKey)
let publicKeyPEM = SwKeyConvert.PublicKey.derToPKCS8PEM(publicKey)
```
### Or read them from strings with PEM data
```
let privateKeyDER = SwKeyConvert.PrivateKey.pemToPKCS1DER(privateKeyPEM)
let publicKeyDER = SwKeyConvert.PublicKey.pemToPKCS1DER(publicKeyPEM)
```
### Or encrypt, decrypt the private key (OpenSSL compatible)
```
try SwKeyConvert.PrivateKey.encryptPEM(privateKeyPEM, passphrase: "longpassword", mode: .aes256CBC)
try SwKeyConvert.PrivateKey.decryptPEM(privEncrypted, passphrase: "longpassword")
```
### Encrypt, decrypt data with RSA
```
try CC.RSA.encrypt(data, derKey: publicKey, tag: tag, padding: .oaep, digest: .sha1)
try CC.RSA.decrypt(data, derKey: privateKey, tag: tag, padding: .oaep, digest: .sha1)
```
### Sign, verify data with RSA
```
let sign = try? CC.RSA.sign(testMessage, derKey: privKey, padding: .pss,
digest: .sha256, saltLen: 16)
let verified = try? CC.RSA.verify(testMessage, derKey: pubKey, padding: .pss,
digest: .sha256, saltLen: 16, signedData: sign!)
```
### Elliptic curve functions
```
let keys = try? CC.EC.generateKeyPair(384)
let signed = try? CC.EC.signHash(keys!.0, hash: hash)
let verified = try? CC.EC.verifyHash(keys!.1, hash: hash, signedData: signed!)

let shared = try? CC.EC.computeSharedSecret(keys!.0, publicKey: partnerPubKey)
```
### Diffie-Hellman functions
```
let dh = try CC.DH.DH(dhParam: .rfc3526Group5)
let myPubKey = try dh.generateKey()
let commonKey = try dh.computeKey(partnerPubKey!)
```
### Encrypt, decrypt data with symmetric ciphers
```
try CC.crypt(.encrypt, blockMode: .cbc, algorithm: .aes, padding: .pkcs7Padding, data: data, key: aesKey, iv: iv)
try CC.crypt(.decrypt, blockMode: .cfb, algorithm: .aes, padding: .pkcs7Padding, data: data, key: aesKey, iv: iv)
```
### Encrypt, decrypt data with symmetric authenticating ciphers
```
try CC.cryptAuth(.encrypt, blockMode: .gcm, algorithm: .aes, data: data, aData: aData, key: aesKey, iv: iv, tagLength: tagLength)
try CC.cryptAuth(.decrypt, blockMode: .ccm, algorithm: .aes, data: data, aData: aData, key: aesKey, iv: iv, tagLength: tagLength)
```
### Digest functions
```
CC.digest(data, alg: .md5)
CC.digest(data, alg: .sha256)
CC.digest(data, alg: .sha512)
```
### HMAC function
```
CC.HMAC(data, alg: .sha512, key: key)
```
### CMAC function
```
CC.CMAC.AESCMAC(input, key: key)
```
### CRC function
```
let output = try? CC.CRC.crc(input, mode: .crc32)
```
### KeyDerivation
```
CC.KeyDerivation.PBKDF2(password, salt: salt, prf: .sha256, rounds: 4096)
```
### Symmetric Key Wrapping
```
try CC.KeyWrap.SymmetricKeyWrap(CC.KeyWrap.rfc3394IV, kek: kek, rawKey: rawKey)
try CC.KeyWrap.SymmetricKeyUnwrap(CC.KeyWrap.rfc3394IV, kek: kek, wrappedKey: wrappedKey)
```
### Upsert, get, delete keys from KeyStore
```
try SwKeyStore.upsertKey(privateKeyPEM, keyTag: "priv", options: [kSecAttrAccessible:kSecAttrAccessibleWhenUnlockedThisDeviceOnly])
try SwKeyStore.getKey("priv")
try SwKeyStore.delKey("priv")
```
-----


Check availability
---------------------

SwCrypt uses dlopen and dlsym to load the CommonCrypto's functions, because not all of them are available in public header files. You have to check the availability before using them.

```
let digestAvailable : Bool = CC.digestAvailable()
let ramdomAvailable : Bool = CC.randomAvailable(()
let hmacAvailable : Bool = CC.hmacAvailable()
let cryptorAvailable : Bool = CC.cryptorAvailable
let keyDerivationAvailable : Bool = CC.KeyDerivation.available()
let keyWrapAvailable : Bool = CC.KeyWrap.available()
let rsaAvailable : Bool = CC.RSA.available()
let dhAvailable : Bool = CC.DH.available()
let ecAvailable : Bool = CC.EC.available()
let crcAvailable : Bool = CC.CRC.available()
let cmacAvailable : Bool = CC.CMAC.available()
let gcmAvailable : Bool = CC.GCM.available()
let ccmAvailable : Bool = CC.CCM.available()

or all in one turn:
let ccAvailable : Bool = CC.available()
```

Install
-------
Just copy [SwCrypt.swift](https://github.com/soyersoyer/SwCrypt/blob/master/SwCrypt/SwCrypt.swift) to your project or use the [Carthage](https://github.com/Carthage/Carthage) dependency manager.

Inspired from
-------------

- <http://blog.flirble.org/2011/01/05/rsa-public-key-openssl-ios/>
- <https://github.com/lancy/RSADemo>
- <https://github.com/TakeScoop/SwiftyRSA>
- <https://github.com/henrinormak/Heimdall>
- <https://github.com/btnguyen2k/swift-rsautils>

License
-------

This project is copyrighted under the MIT license.
Loading