From cbfab551ac3e47da99dcfb21663fba461debbda9 Mon Sep 17 00:00:00 2001 From: Leif Ibsen Date: Sun, 27 Mar 2022 17:17:40 +0200 Subject: [PATCH] Release 3.2.0 --- README.md | 76 +++++- Sources/SwiftECC/Cipher/HMAC.swift | 5 +- Sources/SwiftECC/Cipher/MD.swift | 8 + Sources/SwiftECC/Cipher/PBE.swift | 49 ++++ Sources/SwiftECC/Cipher/SHA1.swift | 106 ++++++++ Sources/SwiftECC/PrivateKey.swift | 173 ++++++++++++ Tests/SwiftECCTests/HMACTest.swift | 214 +++++++++++++++ Tests/SwiftECCTests/KeysEncryptedTest.swift | 160 +++++++++++ Tests/SwiftECCTests/KeysTest.swift | 6 +- Tests/SwiftECCTests/SHA2Test.swift | 16 ++ docs/Classes.html | 2 +- docs/Classes/Domain.html | 2 +- docs/Classes/ECPrivateKey.html | 257 +++++++++++++++++- docs/Classes/ECPublicKey.html | 2 +- docs/Classes/ECSignature.html | 2 +- docs/Enums.html | 2 +- docs/Enums/AESCipher.html | 2 +- docs/Enums/BlockMode.html | 2 +- docs/Enums/ECCurve.html | 2 +- docs/Enums/ECException.html | 2 +- docs/Enums/MessageDigestAlgorithm.html | 29 +- docs/Structs.html | 2 +- docs/Structs/Base64.html | 2 +- docs/Structs/Point.html | 2 +- docs/Structs/RP.html | 2 +- docs/Typealiases.html | 2 +- .../Contents/Resources/Documents/Classes.html | 2 +- .../Resources/Documents/Classes/Domain.html | 2 +- .../Documents/Classes/ECPrivateKey.html | 257 +++++++++++++++++- .../Documents/Classes/ECPublicKey.html | 2 +- .../Documents/Classes/ECSignature.html | 2 +- .../Contents/Resources/Documents/Enums.html | 2 +- .../Resources/Documents/Enums/AESCipher.html | 2 +- .../Resources/Documents/Enums/BlockMode.html | 2 +- .../Resources/Documents/Enums/ECCurve.html | 2 +- .../Documents/Enums/ECException.html | 2 +- .../Enums/MessageDigestAlgorithm.html | 29 +- .../Contents/Resources/Documents/Structs.html | 2 +- .../Resources/Documents/Structs/Base64.html | 2 +- .../Resources/Documents/Structs/Point.html | 2 +- .../Resources/Documents/Structs/RP.html | 2 +- .../Resources/Documents/Typealiases.html | 2 +- .../Contents/Resources/Documents/index.html | 80 +++++- .../Contents/Resources/Documents/search.json | 2 +- .../.docset/Contents/Resources/docSet.dsidx | Bin 49152 -> 49152 bytes docs/docsets/.tgz | Bin 105221 -> 107782 bytes docs/index.html | 80 +++++- docs/search.json | 2 +- 48 files changed, 1532 insertions(+), 73 deletions(-) create mode 100644 Sources/SwiftECC/Cipher/PBE.swift create mode 100644 Sources/SwiftECC/Cipher/SHA1.swift create mode 100644 Tests/SwiftECCTests/HMACTest.swift create mode 100644 Tests/SwiftECCTests/KeysEncryptedTest.swift diff --git a/README.md b/README.md index 2bafc82..ee59f1e 100755 --- a/README.md +++ b/README.md @@ -6,11 +6,12 @@
  • Key Derivation
  • Performance
  • @@ -32,7 +33,7 @@ SwiftECC requires Swift 5.0. It also requires that the Int and UInt types be 64 In your project Package.swift file add a dependency like
    dependencies: [ - .package(url: "https://github.com/leif-ibsen/SwiftECC", from: "3.1.0"), + .package(url: "https://github.com/leif-ibsen/SwiftECC", from: "3.2.0"), ]

    Basics

    @@ -108,7 +109,59 @@ giving: [1]: Bit String (776): 00000100 01000001 01101111 11001100 01101010 00010011 00110000 00110001 00110001 01100011 11000001 10001111 01111001 10111000 11100001 00011101 01111100 00010101 01000010 11101100 01111010 11010000 10000101 00110001 10000110 01001111 00001100 01100010 00111110 01011000 00000011 10001000 00000100 01101010 00001011 10101100 10111010 00010001 00001101 01000000 10001110 01001011 10100001 01111011 10000000 00110100 11011001 01100011 10100101 10110101 00010110 00010000 01000101 01110010 10110010 10110111 00111011 00101001 00111001 00011101 10111001 01010001 11111000 00101101 01101010 00000101 00010001 00000011 00001100 01010101 00010101 10001100 11010001 11110101 11001001 10100010 11101000 01010100 01100110 10111110 11111010 10111111 01001110 11001000 10100000 01110111 11110111 01000000 10001011 10000001 01001101 10001110 01111000 10010001 11101111 11110011 11100000 -

    Encryption and Decryption

    +

    Encrypted Private Keys

    +Private keys can be encrypted as described in [PKCS#5] using the PBES2 scheme. For example: + + let pw = Bytes("MySecret".utf8) + let domain = Domain.instance(curve: .EC384r1) + let (_, priv) = domain.makeKeyPair() + let encryptedKey = priv.pemEncrypted(password: pw, cipher: .AES256) + print(encryptedKey) + +giving (for example): + + -----BEGIN ENCRYPTED PRIVATE KEY----- + MIIBHjBJBgkqhkiG9w0BBQ0wPDAbBgkqhkiG9w0BBQwwDgQI3id2VFlFxXUCAggA + MB0GCWCGSAFlAwQBKgQQlJJQtcZ23p1Q4fXmvpS6hgSB0DBuxL/sCUc/c9NDhrHK + /R2sbtS7rs5a9zUFwcMNV1nVUCK1SSbaCg8/BxHPfqKlAw4RcnsQtN+YD7hz5pxF + YDcYk4mEZo7ODFkRxhKF7vLsUsRZAl2XYGIJflp03+fAWdsiNisjo/4Y/5xxWvCe + OBzfjRpsDT4HjRgcxTtxrzvInzrJkQwyDBAkPMudIshkPOQ1LEoXhi0gVFl9jGN+ + eSLv5Wba2chf/kQcw7R4B3iiE5787wE2fWvvh4ek3oSYcLCvO/gkwgUhyA2hk3rn + 01k= + -----END ENCRYPTED PRIVATE KEY----- + +The implied encryption parameters are cipher block mode = CBC, iteration count = 2048 and salt = 8 random bytes. +The password is simply a byte array, any possible interpretation of it as a string is unspecified. +The encrypted private key is compatible with, and is readable by OpenSSL. + +Private keys can be created from their PEM encodings in encrypted form. +In the example the encrypted private key was created by OpenSSL using the AES-256 cipher in CBC mode with password 'abcd'. + + let encryptedPem = + """ + -----BEGIN ENCRYPTED PRIVATE KEY----- + MIHeMEkGCSqGSIb3DQEFDTA8MBsGCSqGSIb3DQEFDDAOBAg7pgGVDlE/xgICCAAw + HQYJYIZIAWUDBAEqBBCFF4KWxWqhOB5Q8dOwdcPkBIGQbuj2TvlhtpMZ3ZhLBBBx + kJfY1l09yNcJNEcvS8RX4/STXZkt5gMBgtY2DvGAKI0wkpbim+kXSjM6/hmNxY5b + jhQapm8l8jbVGkETtYfseZXpvIT5lnBy9KtO8o3OmlRTV3xXu3KeDZakDoimfQ8G + N7SldmFRcz171yMoIQ17ZU95uneZoogsRuMVMVUJXEh7 + -----END ENCRYPTED PRIVATE KEY----- + """ + let privKey = try ECPrivateKey(pem: encryptedPem, password: Bytes("abcd".utf8)) + print(privKey) + +giving: + + Sequence (4): + Integer: 1 + Octet String (32): 1e 4d c5 de 0f 47 66 6b 7e 4c b8 ee e5 0f f9 6c 4a d3 4f 6f 2e 07 f7 fc e7 c8 24 dd 17 18 fd fa + [0]: + Object Identifier: 1.2.840.10045.3.1.7 + [1]: + Bit String (520): 00000100 00101110 10100100 10110110 10001111 11111010 00111111 00000111 01011010 01011101 01110000 01100001 10110000 10101110 01011010 10011100 10001111 00110100 11010000 11111101 10010110 11001110 00101011 10001111 11000001 10101001 11000000 00001101 00011101 11011101 11001011 10101110 10011000 11001011 10000101 01110001 10100010 11100000 01100011 01101010 11110100 11011101 00011000 01011101 10010110 01010101 10110011 00101101 01010000 10100010 00110001 10000100 11011001 00111001 00011000 01100100 10001110 11011111 10011100 00010100 10110101 11011010 00111010 10101100 11111100 + +SwiftECC can read encrypted private key files provided they were encrypted with one of the ciphers AES-128, AES-192 or AES-256 in CBC mode. +

    Encryption and Decryption

    Encryption and decryption is done using the ECIES algorithm based on AES block cipher. The algorithm uses one of AES-128, AES-192 or AES-256 ciphers, depending on your choice.
    The following cipher block modes are supported: @@ -176,7 +229,7 @@ giving
    The quick brown fox jumps over the lazy dog! -

    Signing and Verifying

    +

    Signing and Verifying

    Signing data and verifying signatures is performed using the ECDSA algorithm. It is possible to generate deterministic signatures as specificed in [RFC-6979] by setting the deterministic parameter to true in the sign operation. @@ -241,7 +294,7 @@ giving (for example):
    Signature is good -

    Secret Key Agreement

    +

    Secret Key Agreement

    Given your own private key and another party's public key, you can generate a byte array that can be used as a symmetric encryption key. The other party can generate the same byte array by using his own private key and your public key.

    Example

    @@ -286,7 +339,7 @@ To convert a SwiftECC public key - e.g. 'pubKey' - to the corresponding CryptoKi let ckKey = try P256.KeyAgreement.PublicKey(pemRepresentation: pubKey.pem) -

    Creating New Domains

    +

    Creating New Domains

    You can create your own domains as illustrated by the two examples below.

    Example

    @@ -362,7 +415,7 @@ giving
    Integer: 22 Integer: 2 -

    Elliptic Curve Arithmetic

    +

    Elliptic Curve Arithmetic

    SwiftECC implements the common elliptic curve arithmetic operations:
    @@ -559,6 +688,132 @@

    Instance Methods

    @@ -559,6 +688,132 @@

    Instance Methods