-
Notifications
You must be signed in to change notification settings - Fork 196
Add PEM/DER APIs for Curve25519 #419
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7639f5b
Add PEM APIs for Curve25519
ptoffy 350ee4a
Fix space
ptoffy dded9db
Add `derRepresentation` and `init(derRepresentation:)` too
ptoffy c66928b
Merge branch 'main' into ed25519-pem
ptoffy 2a36d2b
Fix oversight
ptoffy c600595
Move `RFC5480AlgorithmIdentifier`s to own extension
ptoffy 903f05a
Update CMake
ptoffy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the SwiftCrypto open source project | ||
| // | ||
| // Copyright (c) 2025 Apple Inc. and the SwiftCrypto project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // See CONTRIBUTORS.txt for the list of SwiftCrypto project authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import Crypto | ||
| import Foundation | ||
| import SwiftASN1 | ||
|
|
||
| @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, *) | ||
| extension Curve25519.Signing.PrivateKey { | ||
| /// A Distinguished Encoding Rules (DER) encoded representation of the private key. | ||
| public var derRepresentation: Data { | ||
| let pkey = ASN1.PKCS8PrivateKey(algorithm: .ed25519, privateKey: Array(self.rawRepresentation)) | ||
| var serializer = DER.Serializer() | ||
|
|
||
| // Serializing this key can't throw | ||
| try! serializer.serialize(pkey) | ||
| return Data(serializer.serializedBytes) | ||
| } | ||
|
|
||
| /// A Privacy-Enhanced Mail (PEM) representation of the private key. | ||
| public var pemRepresentation: String { | ||
| let pemDocument = ASN1.PEMDocument(type: "PRIVATE KEY", derBytes: self.derRepresentation) | ||
| return pemDocument.pemString | ||
| } | ||
|
|
||
| /// Creates a Curve25519 private key for signing from a Privacy-Enhanced Mail | ||
| /// (PEM) representation. | ||
| /// | ||
| /// - Parameters: | ||
| /// - pemRepresentation: A PEM representation of the key. | ||
| public init(pemRepresentation: String) throws { | ||
| let document = try PEMDocument(pemString: pemRepresentation) | ||
| self = try .init(derRepresentation: document.derBytes) | ||
| } | ||
|
|
||
| /// Creates a Curve25519 private key for signing from a Distinguished Encoding | ||
| /// Rules (DER) encoded representation. | ||
| /// | ||
| /// - Parameters: | ||
| /// - derRepresentation: A DER-encoded representation of the key. | ||
| public init<Bytes: RandomAccessCollection>(derRepresentation: Bytes) throws where Bytes.Element == UInt8 { | ||
| let bytes = Array(derRepresentation) | ||
| let key = try ASN1.PKCS8PrivateKey(derEncoded: bytes) | ||
| self = try .init(rawRepresentation: key.privateKey.bytes) | ||
| } | ||
| } | ||
|
|
||
| @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, *) | ||
| extension Curve25519.Signing.PublicKey { | ||
| /// A Distinguished Encoding Rules (DER) encoded representation of the public key. | ||
| public var derRepresentation: Data { | ||
| let spki = SubjectPublicKeyInfo( | ||
| algorithmIdentifier: .init(algorithm: .AlgorithmIdentifier.idEd25519, parameters: nil), | ||
| key: Array(self.rawRepresentation) | ||
| ) | ||
| var serializer = DER.Serializer() | ||
|
|
||
| try! serializer.serialize(spki) | ||
| return Data(serializer.serializedBytes) | ||
| } | ||
|
|
||
| /// A Privacy-Enhanced Mail (PEM) representation of the public key. | ||
| public var pemRepresentation: String { | ||
| let pemDocument = ASN1.PEMDocument(type: "PUBLIC KEY", derBytes: self.derRepresentation) | ||
| return pemDocument.pemString | ||
| } | ||
|
|
||
| /// Creates a Curve25519 public key for signing from a Privacy-Enhanced Mail | ||
| /// (PEM) representation. | ||
| /// | ||
| /// - Parameters: | ||
| /// - pemRepresentation: A PEM representation of the key. | ||
| public init(pemRepresentation: String) throws { | ||
| let document = try PEMDocument(pemString: pemRepresentation) | ||
| self = try .init(derRepresentation: document.derBytes) | ||
| } | ||
|
|
||
| /// Creates a Curve25519 public key for signing from a Distinguished Encoding | ||
| /// Rules (DER) encoded representation. | ||
| /// | ||
| /// - Parameters: | ||
| /// - derRepresentation: A DER-encoded representation of the key. | ||
| public init<Bytes: RandomAccessCollection>(derRepresentation: Bytes) throws where Bytes.Element == UInt8 { | ||
| let bytes = Array(derRepresentation) | ||
| let spki = try SubjectPublicKeyInfo(derEncoded: bytes) | ||
| guard spki.algorithmIdentifier.algorithm == .AlgorithmIdentifier.idEd25519 else { | ||
| throw CryptoKitASN1Error.invalidPEMDocument | ||
| } | ||
| self = try .init(rawRepresentation: spki.key.bytes) | ||
| } | ||
| } | ||
|
|
||
| @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, *) | ||
| extension Curve25519.KeyAgreement.PrivateKey { | ||
| /// A Distinguished Encoding Rules (DER) encoded representation of the private key. | ||
| public var derRepresentation: Data { | ||
| let pkey = ASN1.PKCS8PrivateKey(algorithm: .x25519, privateKey: Array(self.rawRepresentation)) | ||
| var serializer = DER.Serializer() | ||
|
|
||
| // Serializing this key can't throw | ||
| try! serializer.serialize(pkey) | ||
| return Data(serializer.serializedBytes) | ||
| } | ||
|
|
||
| /// A Privacy-Enhanced Mail (PEM) representation of the private key. | ||
| public var pemRepresentation: String { | ||
| ASN1.PEMDocument(type: "PRIVATE KEY", derBytes: self.derRepresentation).pemString | ||
| } | ||
|
|
||
| /// Creates a Curve25519 private key for key agreement from a Privacy-Enhanced Mail | ||
| /// (PEM) representation. | ||
| /// | ||
| /// - Parameters: | ||
| /// - pemRepresentation: A PEM representation of the key. | ||
| public init(pemRepresentation: String) throws { | ||
| let document = try PEMDocument(pemString: pemRepresentation) | ||
| self = try .init(derRepresentation: document.derBytes) | ||
| } | ||
|
|
||
| /// Creates a Curve25519 private key for key agreement from a Distinguished Encoding | ||
| /// Rules (DER) encoded representation. | ||
| /// | ||
| /// - Parameters: | ||
| /// - derRepresentation: A DER-encoded representation of the key. | ||
| public init<Bytes: RandomAccessCollection>(derRepresentation: Bytes) throws where Bytes.Element == UInt8 { | ||
| let bytes = Array(derRepresentation) | ||
| let key = try ASN1.PKCS8PrivateKey(derEncoded: bytes) | ||
| self = try .init(rawRepresentation: key.privateKey.bytes) | ||
| } | ||
| } | ||
|
|
||
| @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, *) | ||
| extension Curve25519.KeyAgreement.PublicKey { | ||
| /// A Distinguished Encoding Rules (DER) encoded representation of the public key. | ||
| public var derRepresentation: Data { | ||
| // KeyAgreement public keys use the X25519 OID. | ||
| let spki = SubjectPublicKeyInfo( | ||
| algorithmIdentifier: .init(algorithm: .AlgorithmIdentifier.idX25519, parameters: nil), | ||
ptoffy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| key: Array(self.rawRepresentation) | ||
| ) | ||
| var serializer = DER.Serializer() | ||
|
|
||
| try! serializer.serialize(spki) | ||
| return Data(serializer.serializedBytes) | ||
| } | ||
|
|
||
| /// A Privacy-Enhanced Mail (PEM) representation of the public key. | ||
| public var pemRepresentation: String { | ||
| let spki = SubjectPublicKeyInfo( | ||
| algorithmIdentifier: .init(algorithm: .AlgorithmIdentifier.idX25519, parameters: nil), | ||
| key: Array(self.rawRepresentation) | ||
| ) | ||
|
|
||
| var serializer = DER.Serializer() | ||
| try! serializer.serialize(spki) | ||
|
|
||
| return PEMDocument(type: "PUBLIC KEY", derBytes: serializer.serializedBytes).pemString | ||
| } | ||
|
|
||
| /// Creates a Curve25519 public key for key agreement from a Privacy-Enhanced Mail | ||
| /// (PEM) representation. | ||
| /// | ||
| /// - Parameters: | ||
| /// - pemRepresentation: A PEM representation of the key. | ||
| public init(pemRepresentation: String) throws { | ||
| let document = try PEMDocument(pemString: pemRepresentation) | ||
| self = try .init(derRepresentation: document.derBytes) | ||
| } | ||
|
|
||
| /// Creates a Curve25519 public key for key agreement from a Distinguished Encoding | ||
| /// Rules (DER) encoded representation. | ||
| /// | ||
| /// - Parameters: | ||
| /// - derRepresentation: A DER-encoded representation of the key. | ||
| public init<Bytes: RandomAccessCollection>(derRepresentation: Bytes) throws where Bytes.Element == UInt8 { | ||
| let bytes = Array(derRepresentation) | ||
| let spki = try SubjectPublicKeyInfo(derEncoded: bytes) | ||
| guard spki.algorithmIdentifier.algorithm == .AlgorithmIdentifier.idX25519 else { | ||
| throw CryptoKitASN1Error.invalidPEMDocument | ||
| } | ||
| self = try .init(rawRepresentation: spki.key.bytes) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the SwiftCrypto open source project | ||
| // | ||
| // Copyright (c) 2025 Apple Inc. and the SwiftCrypto project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // See CONTRIBUTORS.txt for the list of SwiftCrypto project authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import CryptoExtras | ||
| import XCTest | ||
|
|
||
| final class Curve25519DERTests: XCTestCase { | ||
| func testSigningPrivateKeyDERRoundTrip() throws { | ||
| let privateKey = Curve25519.Signing.PrivateKey() | ||
|
|
||
| let der = privateKey.derRepresentation | ||
| let imported = try Curve25519.Signing.PrivateKey(derRepresentation: der) | ||
|
|
||
| XCTAssertEqual(imported.rawRepresentation, privateKey.rawRepresentation) | ||
| } | ||
|
|
||
| func testSigningPublicKeyDERRoundTrip() throws { | ||
| let privateKey = Curve25519.Signing.PrivateKey() | ||
| let publicKey = privateKey.publicKey | ||
|
|
||
| let der = publicKey.derRepresentation | ||
| let imported = try Curve25519.Signing.PublicKey(derRepresentation: der) | ||
|
|
||
| XCTAssertEqual(imported.rawRepresentation, publicKey.rawRepresentation) | ||
| } | ||
|
|
||
| func testKeyAgreementPrivateKeyDERRoundTrip() throws { | ||
| let privateKey = Curve25519.KeyAgreement.PrivateKey() | ||
|
|
||
| let der = privateKey.derRepresentation | ||
| let imported = try Curve25519.KeyAgreement.PrivateKey(derRepresentation: der) | ||
|
|
||
| XCTAssertEqual(imported.rawRepresentation, privateKey.rawRepresentation) | ||
| } | ||
|
|
||
| func testKeyAgreementPublicKeyDERRoundTrip() throws { | ||
| let privateKey = Curve25519.KeyAgreement.PrivateKey() | ||
| let publicKey = privateKey.publicKey | ||
|
|
||
| let der = publicKey.derRepresentation | ||
| let imported = try Curve25519.KeyAgreement.PublicKey(derRepresentation: der) | ||
|
|
||
| XCTAssertEqual(imported.rawRepresentation, publicKey.rawRepresentation) | ||
| } | ||
|
|
||
| func testInvalidDERThrows() throws { | ||
| let invalidDER: [UInt8] = [0x01, 0x02, 0x03] | ||
|
|
||
| XCTAssertThrowsError(try Curve25519.Signing.PrivateKey(derRepresentation: invalidDER)) | ||
| XCTAssertThrowsError(try Curve25519.Signing.PublicKey(derRepresentation: invalidDER)) | ||
| XCTAssertThrowsError(try Curve25519.KeyAgreement.PrivateKey(derRepresentation: invalidDER)) | ||
| XCTAssertThrowsError(try Curve25519.KeyAgreement.PublicKey(derRepresentation: invalidDER)) | ||
| } | ||
|
|
||
| func testImportOpenSSLSigningPrivateKeyDER() throws { | ||
| // DER extracted from an OpenSSL-generated Ed25519 PKCS#8 PEM | ||
| let derBytes: [UInt8] = [ | ||
| 0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, | ||
| 0x04, 0x22, 0x04, 0x20, 0x3f, 0x10, 0x52, 0x03, 0xb4, 0x06, 0x87, 0x0c, | ||
| 0x51, 0x71, 0xfa, 0xdc, 0x8f, 0x96, 0xbd, 0x2a, 0x5f, 0x42, 0xac, 0x5c, | ||
| 0xb9, 0x5b, 0x27, 0x4e, 0xf0, 0x06, 0xe5, 0x61, 0x6a, 0x12, 0x00, 0xa5, | ||
| ] | ||
|
|
||
| let key = try Curve25519.Signing.PrivateKey(derRepresentation: derBytes) | ||
| XCTAssertEqual(key.rawRepresentation.count, 32) | ||
| } | ||
|
|
||
| func testImportOpenSSLKeyAgreementPrivateKeyDER() throws { | ||
| // DER extracted from an OpenSSL-generated X25519 PKCS#8 PEM | ||
| let derBytes: [UInt8] = [ | ||
| 0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e, | ||
| 0x04, 0x22, 0x04, 0x20, 0xb8, 0x38, 0x3f, 0x28, 0xea, 0x8f, 0x1d, 0x71, | ||
| 0x49, 0xa2, 0xa3, 0x91, 0x37, 0x00, 0xa1, 0x0c, 0x7c, 0x9d, 0xa9, 0x59, | ||
| 0x28, 0x2d, 0x14, 0x7e, 0x9b, 0x1e, 0x1b, 0x8c, 0x04, 0xa5, 0xd8, 0x47, | ||
| ] | ||
|
|
||
| let key = try Curve25519.KeyAgreement.PrivateKey(derRepresentation: derBytes) | ||
| XCTAssertEqual(key.rawRepresentation.count, 32) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.