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

Make NIST PublicKeys conform to Equatable #174

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 24 additions & 0 deletions Sources/Crypto/Key Agreement/ECDH.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ extension P256 {
let pemDocument = ASN1.PEMDocument(type: "PUBLIC KEY", derBytes: self.derRepresentation)
return pemDocument.pemString
}

public static func ==(lhs: Self, rhs: Self) -> Bool {
lhs.rawRepresentation == rhs.rawRepresentation
}
}

public struct PrivateKey: NISTECPrivateKey {
Expand Down Expand Up @@ -222,6 +226,10 @@ extension P256 {
let pemDocument = ASN1.PEMDocument(type: "PUBLIC KEY", derBytes: self.derRepresentation)
return pemDocument.pemString
}

public static func ==(lhs: Self, rhs: Self) -> Bool {
lhs.rawRepresentation == rhs.rawRepresentation
}
}

public struct PrivateKey: NISTECPrivateKey {
Expand Down Expand Up @@ -356,6 +364,10 @@ extension P384 {
let pemDocument = ASN1.PEMDocument(type: "PUBLIC KEY", derBytes: self.derRepresentation)
return pemDocument.pemString
}

public static func ==(lhs: Self, rhs: Self) -> Bool {
lhs.rawRepresentation == rhs.rawRepresentation
}
}

public struct PrivateKey: NISTECPrivateKey {
Expand Down Expand Up @@ -490,6 +502,10 @@ extension P384 {
let pemDocument = ASN1.PEMDocument(type: "PUBLIC KEY", derBytes: self.derRepresentation)
return pemDocument.pemString
}

public static func ==(lhs: Self, rhs: Self) -> Bool {
lhs.rawRepresentation == rhs.rawRepresentation
}
}

public struct PrivateKey: NISTECPrivateKey {
Expand Down Expand Up @@ -624,6 +640,10 @@ extension P521 {
let pemDocument = ASN1.PEMDocument(type: "PUBLIC KEY", derBytes: self.derRepresentation)
return pemDocument.pemString
}

public static func ==(lhs: Self, rhs: Self) -> Bool {
lhs.rawRepresentation == rhs.rawRepresentation
}
}

public struct PrivateKey: NISTECPrivateKey {
Expand Down Expand Up @@ -758,6 +778,10 @@ extension P521 {
let pemDocument = ASN1.PEMDocument(type: "PUBLIC KEY", derBytes: self.derRepresentation)
return pemDocument.pemString
}

public static func ==(lhs: Self, rhs: Self) -> Bool {
lhs.rawRepresentation == rhs.rawRepresentation
}
}

public struct PrivateKey: NISTECPrivateKey {
Expand Down
4 changes: 4 additions & 0 deletions Sources/Crypto/Key Agreement/ECDH.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ extension ${CURVE} {
let pemDocument = ASN1.PEMDocument(type: "PUBLIC KEY", derBytes: self.derRepresentation)
return pemDocument.pemString
}

public static func ==(lhs: Self, rhs: Self) -> Bool {
lhs.rawRepresentation == rhs.rawRepresentation
}
}

public struct PrivateKey: NISTECPrivateKey {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Crypto/Keys/EC/NISTCurvesKeys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protocol ECPrivateKey {
var publicKey: PublicKey { get }
}

protocol NISTECPublicKey: ECPublicKey {
protocol NISTECPublicKey: ECPublicKey, Equatable {
init<Bytes: ContiguousBytes>(compactRepresentation: Bytes) throws
init<Bytes: ContiguousBytes>(compressedRepresentation: Bytes) throws
init<Bytes: ContiguousBytes>(x963Representation: Bytes) throws
Expand Down
85 changes: 84 additions & 1 deletion Tests/CryptoTests/Signatures/ECDSA/ECDSASignatureTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,89 @@ class SignatureTests: XCTestCase {
let compressedX963Positive = Data(base64Encoded: "A+QHCXtGd5WWSQgp37FBPXMy+nnSwFK79QQD0ZeNMv7L")!
XCTAssertThrowsError(try P256.Signing.PublicKey(x963Representation: compressedX963Positive))
}


func testP256SigningPublicKeyEquatable() throws {
// Equality
let publicKey = P256.Signing.PrivateKey().publicKey
XCTAssertEqual(publicKey, publicKey)

// Inequality
for _ in 0..<32 {
XCTAssertNotEqual(
publicKey,
P256.Signing.PrivateKey().publicKey
)
}
}

func testP256KeyAgreementPublicKeyEquatable() throws {
// Equality
let publicKey = P256.KeyAgreement.PrivateKey().publicKey
XCTAssertEqual(publicKey, publicKey)

// Inequality
for _ in 0..<32 {
XCTAssertNotEqual(
publicKey,
P256.KeyAgreement.PrivateKey().publicKey
)
}
}

func testP384SigningPublicKeyEquatable() throws {
// Equality
let publicKey = P384.Signing.PrivateKey().publicKey
XCTAssertEqual(publicKey, publicKey)

// Inequality
for _ in 0..<32 {
XCTAssertNotEqual(
publicKey,
P384.Signing.PrivateKey().publicKey
)
}
}

func testP384KeyAgreementPublicKeyEquatable() throws {
// Equality
let publicKey = P384.KeyAgreement.PrivateKey().publicKey
XCTAssertEqual(publicKey, publicKey)

// Inequality
for _ in 0..<32 {
XCTAssertNotEqual(
publicKey,
P384.KeyAgreement.PrivateKey().publicKey
)
}
}

func testP521SigningPublicKeyEquatable() throws {
// Equality
let publicKey = P521.Signing.PrivateKey().publicKey
XCTAssertEqual(publicKey, publicKey)

// Inequality
for _ in 0..<32 {
XCTAssertNotEqual(
publicKey,
P521.Signing.PrivateKey().publicKey
)
}
}

func testP521KeyAgreementPublicKeyEquatable() throws {
// Equality
let publicKey = P521.KeyAgreement.PrivateKey().publicKey
XCTAssertEqual(publicKey, publicKey)

// Inequality
for _ in 0..<32 {
XCTAssertNotEqual(
publicKey,
P521.KeyAgreement.PrivateKey().publicKey
)
}
}
}
#endif // (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) && CRYPTO_IN_SWIFTPM