### Example Output Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/encryptdecrypt The output of the encryption/decryption example, showing the decrypted message. ```text The quick brown fox jumps over the lazy dog! ``` -------------------------------- ### Create prime characteristic domain with Domain.instance Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/createdomain Example 3.5 from [GUIDE] creates a prime characteristic domain named "EC29" with p=29, a=4, b=20, gx=1, gy=5, order=37, and cofactor=1. It then performs point addition and scalar multiplication, printing results and the ASN.1 explicit domain parameters. The output shows p1 + p2 = Point(13, 6) and p1 * 2 = Point(14, 6). ```swift import SwiftECC import BigInt // Create the domain let domain = try Domain.instance(name: "EC29", p: BInt(29), a: BInt(4), b: BInt(20), gx: BInt(1), gy: BInt(5), order: BInt(37), cofactor: 1) let p1 = Point(BInt(5), BInt(22)) let p2 = Point(BInt(16), BInt(27)) print("p1 + p2 =", try domain.addPoints(p1, p2)) print("p1 * 2 =", try domain.multiplyPoint(p1, BInt(2))) // Inspect the domain - please refer [SEC 1] appendix C.2 print(domain.asn1Explicit()) ``` ```text p1 + p2 = Point(13, 6) p1 * 2 = Point(14, 6) Sequence (6): Integer: 1 Sequence (2): Object Identifier: 1.2.840.10045.1.1 Integer: 29 Sequence (2): Octet String (1): 04 Octet String (1): 14 Octet String (3): 04 01 05 Integer: 37 Integer: 1 ``` -------------------------------- ### Create characteristic 2 domain with Domain.instance and RP Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/createdomain Example 3.6 from [GUIDE] creates a characteristic 2 domain named "EC4" using reduction polynomial RP(4, 1) for x^4 + x^1 + 1, with a=8, b=9, gx=1, gy=1, order=22, and cofactor=2. It performs point addition and scalar multiplication, printing results and the ASN.1 explicit domain parameters. The output shows p1 + p2 = Point(1, 1) and p1 * 2 = Point(11, 2). ```swift import SwiftECC import BigInt // Reduction polynomial for x^4 + x^1 + 1 let rp = RP(4, 1) // Create the domain let domain = try Domain.instance(name: "EC4", rp: rp, a: BInt(8), b: BInt(9), gx: BInt(1), gy: BInt(1), order: BInt(22), cofactor: 2) let p1 = Point(BInt(2), BInt(15)) let p2 = Point(BInt(12), BInt(12)) print("p1 + p2 =", try domain.addPoints(p1, p2)) print("p1 * 2 =", try domain.multiplyPoint(p1, BInt(2))) // Inspect the domain - please refer [SEC 1] appendix C.2 print(domain.asn1Explicit()) ``` ```text p1 + p2 = Point(1, 1) p1 * 2 = Point(11, 2) Sequence (6): Integer: 1 Sequence (2): Object Identifier: 1.2.840.10045.1.2 Sequence (2): Integer: 4 Integer: 1 Sequence (2): Octet String (1): 08 Octet String (1): 09 Octet String (3): 04 01 01 Integer: 22 Integer: 2 ``` -------------------------------- ### AEAD Encrypt and Decrypt Example Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/aeadencryptdecrypt Demonstrates AEAD encryption and decryption using SwiftECC with both ChaCha20/Poly1305 and AES-128/GCM. The example creates a key pair on the EC256k1 domain, encrypts a plaintext message with associated data, then decrypts and prints the results. Requires the CryptoKit framework and hardware support for AES/GCM. ```swift import SwiftECC let plainText = "Hi, there!" let aaData = "This is the additional authenticated data" let (pub, priv) = Domain.instance(curve: .EC256k1).makeKeyPair() let cipherText1 = pub.encryptChaCha(msg: Bytes(plainText.utf8), aad: Bytes(aaData.utf8)) let cipherText2 = pub.encryptAESGCM(msg: Bytes(plainText.utf8), cipher: .AES128, aad: Bytes(aaData.utf8)) do { let text1 = try priv.decryptChaCha(msg: cipherText1, aad: Bytes(aaData.utf8)) print(String(bytes: text1, encoding: .utf8)!) let text2 = try priv.decryptAESGCM(msg: cipherText2, cipher: .AES128, aad: Bytes(aaData.utf8)) print(String(bytes: text2, encoding: .utf8)!) } catch { print("Exception: \(error)") } ``` ```text Hi, there! Hi, there! ``` -------------------------------- ### var description: String { get } Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/rp/description A textual description of the RP instance. This property is read-only and returns a string representation. ```APIDOC ## var description: String { get } ### Description A textual description of the RP instance. ### Declaration ```swift var description: String { get } ``` ### Properties - `let m: Int` - The `m` exponent - `let k3: Int` - The `k3` exponent - `let k2: Int` - The `k2` exponent - `let k1: Int` - The `k1` exponent - `let p: BInt` - The reduction polynomial as a BInt ``` -------------------------------- ### Get a predefined domain by curve Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc Obtains a predefined NIST elliptic curve domain by specifying the curve. The example uses the EC521r1 curve. ```swift let domain = Domain.instance(curve: .EC521r1) ``` -------------------------------- ### var description: String { get } Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/ecpublickey/description The `description` property provides a textual representation of the ASN1 encoding of the public key. It is a read-only computed property that returns a String. ```APIDOC ## description ### Description A textual representation of the ASN1 encoding of `self`. ### Declaration ```swift var description: String { get } ``` ### See Also - `let domain: Domain` - The domain the key belongs to - `let w: Point` - The public value - a curve point - `var asn1: ASN1` - The ASN1 encoding of `self` - `var der: Bytes` - The DER encoding of `self` - `var pem: String` - The PEM encoding of `self` ``` -------------------------------- ### Sign and verify with ECDSA using SwiftECC Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/signverify SwiftECC example that creates a key pair for the brainpool BP160r1 domain, signs a message with the private key, and verifies the signature with the public key. The output shows the ASN.1 representations of the keys and the verification result "Signature is good". ```swift import SwiftECC // Get a predefined domain - for example brainpool BP160r1 let domain = Domain.instance(curve: .BP160r1) // Create your own keys let (pubKey, privKey) = domain.makeKeyPair() // See how they look print(pubKey.asn1) print(privKey.asn1) // Store them in PEM format for future use let pubPEM = pubKey.pem let privPEM = privKey.pem let message = "The quick brown fox jumps over the lazy dog!".data(using: .utf8)! let sig = privKey.sign(msg: message) let ok = pubKey.verify(signature: sig, msg: message) print("Signature is", ok ? "good" : "wrong") ``` ```text Sequence (2): Sequence (2): Object Identifier: 1.2.840.10045.2.1 Object Identifier: 1.3.36.3.3.2.8.1.1.1 Bit String (328): 00000100 00000011 00000111 00110011 01010100 00000001 10111100 01101111 10100001 01001000 11101000 01111100 10001111 00000110 00010010 11100111 11111010 10010001 00100100 01001000 11000110 01110001 00110100 01001000 10011110 01011110 11000000 10010001 01000110 01011010 01001110 01110000 00011011 01010111 10101011 01101010 00011011 01101100 01100100 01000100 01111101 Sequence (4): Integer: 1 Octet String (20): 32 96 e0 c4 d7 f5 cb 03 0c 95 63 b1 a2 c1 2f 64 4c dc d6 4c [0]: Object Identifier: 1.3.36.3.3.2.8.1.1.1 [1]: Bit String (328): 00000100 00000011 00000111 00110011 01010100 00000001 10111100 01101111 10100001 01001000 11101000 01111100 10001111 00000110 00010010 11100111 11111010 10010001 00100100 01001000 11000110 01110001 00110100 01001000 10011110 01011110 11000000 10010001 01000110 01011010 01001110 01110000 00011011 01010111 10101011 01101010 00011011 01101100 01100100 01000100 01111101 Signature is good ``` -------------------------------- ### Get a predefined domain by OID Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc Obtains a predefined NIST elliptic curve domain by its object identifier (OID). The call throws and must be handled with try. ```swift let domain = try Domain.instance(oid: EC521r1.oid) ``` -------------------------------- ### ECIES Encryption and Decryption Example Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/encryptdecrypt Shows how to encrypt a message with a public key and decrypt with the corresponding private key using AES-128. Requires a PEM-encoded EC key pair from the EC163k1 domain. Prints the decrypted message or an error. ```swift import SwiftECC // You need a public key to encrypt a message and the corresponding private key to decrypt it, // for example from the EC163k1 domain let pemPublic163k1 = """ -----BEGIN PUBLIC KEY----- MEAwEwYHKoZIzj0CAQYFK4EEAAEDLAAEA6txn7CCae0d9AiGj3Rk5m9XflTCB81oe1fKZi4F4oip SF2u79k8TD5J -----END PUBLIC KEY----- """ let pemPrivate163k1 = """ -----BEGIN EC PRIVATE KEY----- MFICAQEEFNfflqz2oOd9WpxuMZ9wJTFO1sjgoAcGBSuBBAABoS4DLAAEA6txn7CCae0d9AiGj3Rk 5m9XflTCB81oe1fKZi4F4oipSF2u79k8TD5J -----END EC PRIVATE KEY----- """ let text = "The quick brown fox jumps over the lazy dog!" do { let pubKey = try ECPublicKey(pem: pemPublic163k1) let privKey = try ECPrivateKey(pem: pemPrivate163k1) let encryptedData = pubKey.encrypt(msg: text.data(using: .utf8)!, cipher: .AES128) let decryptedData = try privKey.decrypt(msg: encryptedData, cipher: .AES128) print(String(data: decryptedData, encoding: .utf8)!) } catch { print("\(error)") } ``` -------------------------------- ### Basic Diffie-Hellman Key Agreement with SwiftECC Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/keyagrement Basic Diffie-Hellman key agreement using SwiftECC. Both parties generate the same shared secret byte array from their own private key and the other's public key. The example uses the EC256r1 domain and prints the resulting secrets. ```swift import SwiftECC do { let domain = Domain.instance(curve: .EC256r1) // Party A's keys let (pubA, privA) = domain.makeKeyPair() // Party B's keys let (pubB, privB) = domain.makeKeyPair() let secretA = try privA.sharedSecret(pubKey: pubB) let secretB = try privB.sharedSecret(pubKey: pubA) print(secretA) print(secretB) } catch { print("Exception: \(error)") } ``` ```text [44, 218, 188, 109, 139, 24, 227, 22, 116, 197, 147, 194, 138, 107, 105, 11, 236, 67, 236, 110, 42, 26, 250, 151, 111, 236, 60, 98, 210, 121, 243, 44] [44, 218, 188, 109, 139, 24, 227, 22, 116, 197, 147, 194, 138, 107, 105, 11, 236, 67, 236, 110, 42, 26, 250, 151, 111, 236, 60, 98, 210, 121, 243, 44] ``` -------------------------------- ### X9.63 Key Agreement with SwiftECC Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/keyagrement X9.63 key agreement (SEC 1 section 3.6.1) using SwiftECC. Both parties must agree on the domain, message digest, and shared information (possibly none) for the agreement to work. The example uses EC256r1, SHA2_256, and shared info [1,2,3] to derive a 16-byte secret. ```swift import SwiftECC do { let domain = Domain.instance(curve: .EC256r1) // Party A's keys let (pubA, privA) = domain.makeKeyPair() // Party B's keys let (pubB, privB) = domain.makeKeyPair() let info: Bytes = [1, 2, 3] let secretA = try privA.x963KeyAgreement(pubKey: pubB, length: 16, md: .SHA2_256, sharedInfo: info) let secretB = try privB.x963KeyAgreement(pubKey: pubA, length: 16, md: .SHA2_256, sharedInfo: info) print(secretA) print(secretB) } catch { print("Exception: \(error)") } ``` ```text [92, 161, 137, 44, 47, 30, 6, 26, 43, 183, 199, 130, 19, 254, 232, 106] [92, 161, 137, 44, 47, 30, 6, 26, 43, 183, 199, 130, 19, 254, 232, 106] ``` -------------------------------- ### Load encrypted private key from PEM Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/encryptedkeys Loads an encrypted private key from its PEM encoding using ECPrivateKey(pem:password:). The example uses a key created by OpenSSL with AES-256 in CBC mode and password 'abcd'. The output shows the parsed key structure. SwiftECC can read encrypted keys using AES-128, AES-192, or AES-256 in CBC mode. ```swift let encryptedPem = """ -----BEGIN ENCRYPTED PRIVATE KEY----- MIHeMEkGCSqGSIb3DQEFDTA8MBsGCSqGSIb3DQEFDDAOBAg7pgGVDlE/xgICCAAwHQYJYIZIAWUD BAEqBBCFF4KWxWqhOB5Q8dOwdcPkBIGQbuj2TvlhtpMZ3ZhLBBBxkJfY1l09yNcJNEcvS8RX4/ST XZkt5gMBgtY2DvGAKI0wkpbim+kXSjM6/hmNxY5bjhQapm8l8jbVGkETtYfseZXpvIT5lnBy9KtO 8o3OmlRTV3xXu3KeDZakDoimfQ8GN7SldmFRcz171yMoIQ17ZU95uneZoogsRuMVMVUJXEh7 -----END ENCRYPTED PRIVATE KEY----- """ let privKey = try ECPrivateKey(pem: encryptedPem, password: Bytes("abcd".utf8)) print(privKey) ``` ```text 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 ``` -------------------------------- ### HKDF Key Agreement with SwiftECC Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/keyagrement HKDF key agreement (RFC 5869) using SwiftECC. Both parties must agree on the domain, message digest, shared information (possibly none), and salt (possibly none) for the agreement to work. The example uses EC256r1, SHA2_256, shared info [1,2,3], and salt [4,5,6] to derive a 16-byte secret. ```swift import SwiftECC do { let domain = Domain.instance(curve: .EC256r1) // Party A's keys let (pubA, privA) = domain.makeKeyPair() // Party B's keys let (pubB, privB) = domain.makeKeyPair() let info: Bytes = [1, 2, 3] let salt: Bytes = [4, 5, 6] let secretA = try privA.hkdfKeyAgreement(pubKey: pubB, length: 16, md: .SHA2_256, sharedInfo: info, salt: salt) let secretB = try privB.hkdfKeyAgreement(pubKey: pubA, length: 16, md: .SHA2_256, sharedInfo: info, salt: salt) print(secretA) print(secretB) } catch { print("Exception: \(error)") } ``` ```text [202, 36, 31, 96, 207, 220, 135, 77, 130, 41, 214, 139, 214, 30, 106, 180] [202, 36, 31, 96, 207, 220, 135, 77, 130, 41, 214, 139, 214, 30, 106, 180] ``` -------------------------------- ### init(domain:r:s:) Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/ecsignature/init%28domain%3Ar%3As%3A%29 Creates a signature from its components. This initializer takes the domain, r, and s components of the signature. ```APIDOC ## init(domain:r:s:) ### Description Creates a signature from its components. ### Signature ``` init( domain: Domain, r: Bytes, s: Bytes ) ``` ### Parameters - **domain** (Domain) - The domain of the signature - **r** (Bytes) - The r component - **s** (Bytes) - The s component ### See Also - `convenience init(asn1: ASN1, domain: Domain) throws` - Creates a signature from its ASN1 representation ``` -------------------------------- ### init(_:_:) Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/point/init%28_%3A_%3A%29 Initializer for creating a Point instance from x and y coordinates. ```APIDOC ## init(_:_:) ### Description Creates a Point from its x- and y-coordinates. ### Method Initializer ### Parameters - **x** (BInt) - The x coordinate - **y** (BInt) - The y coordinate ### Example ```swift let point = Point(x, y) ``` ``` -------------------------------- ### init(domain:w:) Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/ecpublickey/init%28domain%3Aw%3A%29 Initializer for creating an ECPublicKey from a domain and curve point. ```APIDOC ## init(domain:w:) ### Description Creates a public key from its domain and curve point. ### Method Initializer ### Signature ``` init( domain: Domain, w: Point ) throws ``` ### Parameters - **domain** (Domain) - Required - The domain - **w** (Point) - Required - The curve point ### Throws A `publicKeyParameter` exception if `w` is not on the curve or is infinity ### Discussion Creates a public key from its domain and curve point. ``` -------------------------------- ### init(privateKey:) Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/ecpublickey/init%28privatekey%3A%29 Creates a public key from the corresponding private key. This convenience initializer takes an ECPrivateKey and returns an ECPublicKey instance. ```APIDOC ## init(privateKey:) ### Description Creates a public key from the corresponding private key. ### Signature ``` convenience init(privateKey: ECPrivateKey) ``` ### Parameters - **privateKey** (ECPrivateKey) - The corresponding private key. ### See Also - `init(domain: Domain, w: Point) throws` - Creates a public key from its domain and curve point. - `convenience init(der: Bytes) throws` - Creates a public key from its DER encoding. - `convenience init(pem: String) throws` - Creates a public key from its PEM encoding. ``` -------------------------------- ### instance(pem:) Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/domain/instance%28pem%3A%29 Documentation for the static method instance(pem:) that constructs a domain from its PEM encoding. ```APIDOC ## [STATIC METHOD] instance(pem:) ### Description Constructs a domain from its PEM encoding. ### Method static func ### Signature static func instance(pem: String) throws -> Domain ### Parameters - **pem** (String) - Required - The PEM encoding of the domain ### Return Value The corresponding domain ### Discussion No additional discussion provided. ### See Also - `static func instance(curve: ECCurve) -> Domain` - Returns a predefined domain given its curve - `static func instance(oid: ASN1ObjectIdentifier) throws -> Domain` - Returns a predefined domain given its OID - `static func instance(name: String, p: BInt, a: BInt, b: BInt, gx: BInt, gy: BInt, order: BInt, cofactor: Int, oid: ASN1ObjectIdentifier?) throws -> Domain` - Constructs an odd prime characteristic domain based on the curve y^2 = x^3 + a * x + b - `static func instance(name: String, rp: RP, a: BInt, b: BInt, gx: BInt, gy: BInt, order: BInt, cofactor: Int, oid: ASN1ObjectIdentifier?) throws -> Domain` - Constructs a characteristic 2 domain based on the curve y^2 + x * y = x^3 + a * x^2 + b ``` -------------------------------- ### Load keys from PEM and print ASN.1 structures Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/keymanagement Loads existing public and private keys from PEM encodings (EC384r1 domain) and prints their ASN.1 structures. The private key is in PKCS#8 format. Both initializers are throwing. ```swift // Public key encoding - EC384r1 domain let pubKeyPem = """ -----BEGIN PUBLIC KEY----- MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEQW/MahMwMTFjwY95uOEdfBVC7HrQhTGGTwxiPlgDiARq C6y6EQ1Ajkuhe4A02WOltRYQRXKytzspOR25UfgtagURAwxVFYzR9cmi6FRmvvq/Tsigd/dAi4FN jniR7/Pg -----END PUBLIC KEY----- """ let pubKey = try ECPublicKey(pem: pubKeyPem) // Private key encoding in PKCS#8 format - EC384r1 domain let privKeyPem = """ -----BEGIN PRIVATE KEY----- MIG/AgEAMBAGByqGSM49AgEGBSuBBAAiBIGnMIGkAgEBBDBmpNziSYmGoWwl7apJM9ZdDBxkJqmx MScHGXG45ZQXSv7fIuJlsSwxK76nUiiO7gigBwYFK4EEACKhZANiAARBb8xqEzAxMWPBj3m44R18 FULsetCFMYZPDGI+WAOIBGoLrLoRDUCOS6F7gDTZY6W1FhBFcrK3Oyk5HblR+C1qBREDDFUVjNH1 yaLoVGa++r9OyKB390CLgU2OeJHv8+A= -----END PRIVATE KEY----- """ let privKey = try ECPrivateKey(pem: privKeyPem) // See the key ASN1 structures print(pubKey) print(privKey) ``` -------------------------------- ### var der: Bytes { get } Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/ecprivatekey/der The `der` property returns the DER encoding of the EC private key as a `Bytes` value. ```APIDOC ## `var der: Bytes { get }` ### Description Returns the DER encoding of the private key. ### Declaration ```swift var der: Bytes { get } ``` ### Return Value A `Bytes` value representing the DER encoding of the key. ``` -------------------------------- ### init(pem:) Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/ecpublickey/init%28pem%3A%29 Creates a public key from its PEM encoding. This initializer is a convenience initializer that throws an error if the PEM string is invalid. ```APIDOC ## init(pem:) ### Description Creates a public key from its PEM encoding. ### Method convenience init(pem: String) throws ### Parameters - **pem** (String) - The PEM encoding of the key. ### Discussion This initializer parses the provided PEM string and creates a public key. It throws an error if the PEM encoding is invalid. ### See Also - `init(domain: Domain, w: Point) throws` - Creates a public key from its domain and curve point. - `convenience init(der: Bytes) throws` - Creates a public key from its DER encoding. - `convenience init(privateKey: ECPrivateKey)` - Creates a public key from the corresponding private key. ``` -------------------------------- ### var asn1: ASN1 { get } Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/ecsignature/asn1 The asn1 property returns the ASN1 encoding of the signature. It is a read-only instance property of the ECSignature class. ```APIDOC ## var asn1: ASN1 { get } ### Description The ASN1 encoding of the signature. ### Declaration ```swift var asn1: ASN1 { get } ``` ### See Also - `let domain: Domain` - The `domain` component of the signature - `let r: Bytes` - The `r` component of the signature - `let s: Bytes` - The `s` component of the signature - `var description: String` - Textual description of `self` ``` -------------------------------- ### init(der:pkcs8:) Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/ecprivatekey/init%28der%3Apkcs8%3A%29 Creates a private key from its DER encoding. The pkcs8 parameter indicates whether the encoding is in PKCS#8 format. ```APIDOC ## init(der:pkcs8:) ### Description Creates a private key from its DER encoding. ### Method Initializer ### Signature ```swift convenience init( der: Bytes, pkcs8: Bool = false ) throws ``` ### Parameters - **der** (Bytes) - Required - The DER encoding of the key - **pkcs8** (Bool) - Optional - `true` if the encoding is in PKCS#8 format, else `false` ### Discussion This initializer parses the provided DER-encoded data to create a private key. The `pkcs8` parameter indicates whether the encoding is in PKCS#8 format. ### See Also - `init(domain: Domain, s: BInt) throws` - Creates a private key from its domain and secret value - `convenience init(pem: String) throws` - Creates a private key from its PEM encoding - `convenience init(der: Bytes, password: Bytes) throws` - Creates a private key from its encrypted DER encoding - `convenience init(pem: String, password: Bytes) throws` - Creates a private key from its encrypted PEM encoding ``` -------------------------------- ### Create public key from domain and point with ECPublicKey(domain:w:) Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/keymanagement Creates a public key from a given domain and curve point. The initializer is throwing and requires a valid domain and point. ```swift let pubKey = try ECPublicKey(domain: dom, w: pt) ``` -------------------------------- ### var pemPkcs8: String { get } Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/ecprivatekey/pempkcs8 Returns the PEM encoding of the EC private key in PKCS#8 format. This property is read-only and provides a string representation suitable for storage or transmission. ```APIDOC ## var pemPkcs8: String { get } ### Description Returns the PEM encoding of the EC private key in PKCS#8 format. ### Declaration ```swift var pemPkcs8: String { get } ``` ### Parameters None. ### Return Value A String containing the PEM encoding of the key in PKCS#8 format. ### Discussion This property provides a standard textual representation of the private key, commonly used for exchanging keys in a human-readable format. ### See Also - `var asn1: ASN1` - The ASN1 encoding of `self` - `var der: Bytes` - The DER encoding of `self` - `var pem: String` - The PEM encoding of `self` - `var derPkcs8: Bytes` - The DER encoding of `self` in PKCS#8 format ``` -------------------------------- ### init(der:password:) Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/ecprivatekey/init%28der%3Apassword%3A%29 Creates a private key from its encrypted DER encoding. The key must have been encrypted with AES-128, AES-192, or AES-256 in CBC mode. ```APIDOC ## init(der:password:) ### Description Creates a private key from its encrypted DER encoding. ### Method Initializer ### Signature ```swift convenience init( der: Bytes, password: Bytes ) throws ``` ### Parameters - **der** (Bytes) - Required - The DER encoding of the encrypted key - **password** (Bytes) - Required - The password ### Discussion The key must have been encrypted with one of the ciphers AES-128, AES-192 or AES-256 in CBC mode. ### See Also - `init(domain: Domain, s: BInt) throws` - Creates a private key from its domain and secret value - `convenience init(der: Bytes, pkcs8: Bool) throws` - Creates a private key from its DER encoding - `convenience init(pem: String) throws` - Creates a private key from its PEM encoding. - `convenience init(pem: String, password: Bytes) throws` - Creates a private key from its encrypted PEM encoding. ``` -------------------------------- ### init(pem:password:) Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/ecprivatekey/init%28pem%3Apassword%3A%29 Creates a private key from its encrypted PEM encoding. The key must have been encrypted with AES-128, AES-192, or AES-256 in CBC mode, and the PEM type must be 'ENCRYPTED PRIVATE KEY'. ```APIDOC ## init(pem:password:) ### Description Creates a private key from its encrypted PEM encoding. ### Method Initializer ### Signature convenience init( pem: String, password: Bytes ) throws ### Parameters - **pem** (String) - The PEM encoding of the encrypted key - **password** (Bytes) - The password ### Discussion The key must have been encrypted with one of the ciphers AES-128, AES-192 or AES-256 in CBC mode. The PEM type is `ENCRYPTED PRIVATE KEY` ### See Also - `init(domain: Domain, s: BInt) throws` - Creates a private key from its domain and secret value - `convenience init(der: Bytes, pkcs8: Bool) throws` - Creates a private key from its DER encoding - `convenience init(pem: String) throws` - Creates a private key from its PEM encoding. - `convenience init(der: Bytes, password: Bytes) throws` - Creates a private key from its encrypted DER encoding. ``` -------------------------------- ### Convert SwiftECC Keys to CryptoKit Keys Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/keyagrement Convert SwiftECC public and private keys to CryptoKit keys using PEM representations. The example uses the P256 curve, corresponding to the EC256r1 domain. ```swift let ckPubKey = try P256.KeyAgreement.PublicKey(pemRepresentation: eccPubKey.pem) let ckPrivKey = try P256.KeyAgreement.PrivateKey(pemRepresentation: eccPrivKey.pem) ``` -------------------------------- ### instance(curve:) Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/domain/instance%28curve%3A%29 Documentation for the static method instance(curve:) that returns a predefined domain given its curve. ```APIDOC ## instance(curve:) ### Description Returns a predefined domain given its curve. ### Method static func instance(curve: ECCurve) -> Domain ### Parameters - **curve** (ECCurve) - The domain curve. ### Return Value The corresponding domain. ### See Also - `static func instance(oid: ASN1ObjectIdentifier) throws -> Domain` - Returns a predefined domain given its OID - `static func instance(pem: String) throws -> Domain` - Constructs a domain from its PEM encoding - `static func instance(name: String, p: BInt, a: BInt, b: BInt, gx: BInt, gy: BInt, order: BInt, cofactor: Int, oid: ASN1ObjectIdentifier?) throws -> Domain` - Constructs an odd prime characteristic domain based on the curve y^2 = x^3 + a * x + b - `static func instance(name: String, rp: RP, a: BInt, b: BInt, gx: BInt, gy: BInt, order: BInt, cofactor: Int, oid: ASN1ObjectIdentifier?) throws -> Domain` - Constructs a characteristic 2 domain based on the curve y^2 + x * y = x^3 + a * x^2 + b ``` -------------------------------- ### Swift method signature for instance(name:rp:a:b:gx:gy:order:cofactor:oid:) Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/domain/instance%28name%3Arp%3Aa%3Ab%3Agx%3Agy%3Aorder%3Acofactor%3Aoid%3A%29 Declares a static method that constructs a characteristic 2 domain. Parameters include name, reduction polynomial (rp), coefficients a and b, generator point coordinates (gx, gy), curve order, cofactor, and an optional OID. Throws a domainParameter exception if b = 0 or the generator point is not on the curve. ```swift static func instance( name: String, rp: RP, a: BInt, b: BInt, gx: BInt, gy: BInt, order: BInt, cofactor: Int, oid: ASN1ObjectIdentifier? = nil ) throws -> Domain ``` -------------------------------- ### Generate a key pair with Domain.generateKeyPair() Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/keymanagement Generates a new public/private key pair for the EC384r1 domain. The private key is a random positive integer less than the domain order; the public key is the domain generator point multiplied by the private key. ```swift let domain = Domain.instance(curve: .EC384r1) let (pubKey, privKey) = domain.generateKeyPair() ``` -------------------------------- ### let k2: Int Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/rp/k2 The k2 exponent property. ```APIDOC ## let k2: Int ### Description The `k2` exponent. ### Declaration ```swift let k2: Int ``` ### See Also - `let m: Int` - The `m` exponent - `let k3: Int` - The `k3` exponent - `let k1: Int` - The `k1` exponent - `let p: BInt` - The reduction polynomial as a BInt - `var description: String` - Textual description of `self` ``` -------------------------------- ### static func instance(name:p:a:b:gx:gy:order:cofactor:oid:) Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/domain/instance%28name%3Ap%3Aa%3Ab%3Agx%3Agy%3Aorder%3Acofactor%3Aoid%3A%29 Constructs an odd prime characteristic domain based on the curve y^2 = x^3 + a * x + b. This method takes the domain name, field order, coefficients, generator point, order, cofactor, and an optional OID, and returns a Domain instance. It throws a domainParameter exception if the curve parameters are invalid. ```APIDOC ## static func instance(name:p:a:b:gx:gy:order:cofactor:oid:) ### Description Constructs an odd prime characteristic domain based on the curve y^2 = x^3 + a * x + b. ### Method static func ### Signature ```swift static func instance( name: String, p: BInt, a: BInt, b: BInt, gx: BInt, gy: BInt, order: BInt, cofactor: Int, oid: ASN1ObjectIdentifier? = nil ) throws -> Domain ``` ### Parameters - **name** (String) - Required - The domain name - **p** (BInt) - Required - The field order - a prime greater than 3 - **a** (BInt) - Required - The a coefficient - **b** (BInt) - Required - The b coefficient - **gx** (BInt) - Required - The generator point x-coordinate - **gy** (BInt) - Required - The generator point y-coordinate - **order** (BInt) - Required - The curve order - **cofactor** (Int) - Required - The cofactor - **oid** (ASN1ObjectIdentifier?) - Optional - An optional domain OID ### Return Value The domain ### Throws A `domainParameter` exception if 4 * a^3 + 27 * b^2 = 0 or the generator point is not on the curve ### Discussion Throws a `domainParameter` exception if the curve parameters are invalid. ### See Also - `static func instance(curve: ECCurve) -> Domain` - Returns a predefined domain given its curve - `static func instance(oid: ASN1ObjectIdentifier) throws -> Domain` - Returns a predefined domain given its OID - `static func instance(pem: String) throws -> Domain` - Constructs a domain from its PEM encoding - `static func instance(name: String, rp: RP, a: BInt, b: BInt, gx: BInt, gy: BInt, order: BInt, cofactor: Int, oid: ASN1ObjectIdentifier?) throws -> Domain` - Constructs a characteristic 2 domain based on the curve y^2 + x * y = x^3 + a * x^2 + b ``` -------------------------------- ### pem Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/domain/pem Documentation for the pem property of the SwiftECC Domain class. ```APIDOC ## [GET] pem ### Description Returns the PEM encoding of the domain object. This property provides a string representation of the domain in PEM format. ### Method GET ### Endpoint pem ### Response #### Success Response (200) - **pem** (String) - The PEM encoding of the domain. #### Response Example ``` -----BEGIN EC PARAMETERS----- ... -----END EC PARAMETERS----- ``` ``` -------------------------------- ### ECPublicKey.der Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/ecpublickey/der Documentation for the `der` instance property of the `ECPublicKey` type, which returns the DER encoding of the public key. ```APIDOC ## `var der: Bytes { get }` ### Description Returns the DER encoding of the public key. ### Declaration ```swift var der: Bytes { get } ``` ### Return Value A `Bytes` value containing the DER encoding of the public key. ### See Also - `let domain: Domain` – The domain the key belongs to. - `let w: Point` – The public value, a curve point. - `var asn1: ASN1` – The ASN1 encoding of the key. - `var pem: String` – The PEM encoding of the key. - `var description: String` – A textual representation of the ASN1 encoding. ``` -------------------------------- ### let order: BInt Source: https://leif-ibsen.github.io/SwiftECC/documentation/swiftecc/domain/order The curve order property. This read-only property returns the order of the elliptic curve as a BInt (big integer) value. It is a fundamental parameter of the domain. ```APIDOC ## let order: BInt ### Description The curve order. This property provides the order of the elliptic curve domain, which is a fundamental parameter used in cryptographic operations. It is of type BInt, representing a big integer. ### Declaration ```swift let order: BInt ``` ### Parameters None. ### Return Value - **order** (BInt) - The curve order. ### See Also - `let name: String` - The domain name - `let p: BInt` - The modulus - `let a: BInt` - The curve `a` coefficient - `let b: BInt` - The curve `b` coefficient - `let g: Point` - The generator point - `let cofactor: Int` - The cofactor - `let oid: ASN1ObjectIdentifier?` - An optional domain OID - `let characteristic2: Bool` - Is `true` if `self` has characteristic 2, `false` if it has an odd prime characteristic - `var asn1: ASN1` - The ASN1 encoding of `self` - `var pem: String` - The PEM encoding of `self` - `var description: String` - A textual representation of the ASN1 encoding of `self` ```