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 Hashable #176

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

public static func ==(lhs: Self, rhs: Self) -> Bool {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Equatable, Part of #174 but I was unable to create a diff between just this PR and the source branch of that PR.

lhs.rawRepresentation == rhs.rawRepresentation
}

public func hash(into hasher: inout Hasher) {
hasher.combine(rawRepresentation)
}
}

/// A P-256 private key used to create cryptographic signatures.
Expand Down Expand Up @@ -347,6 +355,14 @@ 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 func hash(into hasher: inout Hasher) {
hasher.combine(rawRepresentation)
}
}

/// A P-256 private key used for key agreement.
Expand Down Expand Up @@ -561,6 +577,14 @@ 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 func hash(into hasher: inout Hasher) {
hasher.combine(rawRepresentation)
}
}

/// A P-384 private key used to create cryptographic signatures.
Expand Down Expand Up @@ -776,6 +800,14 @@ 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 func hash(into hasher: inout Hasher) {
hasher.combine(rawRepresentation)
}
}

/// A P-384 private key used for key agreement.
Expand Down Expand Up @@ -990,6 +1022,14 @@ 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 func hash(into hasher: inout Hasher) {
hasher.combine(rawRepresentation)
}
}

/// A P-521 private key used to create cryptographic signatures.
Expand Down Expand Up @@ -1205,6 +1245,14 @@ 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 func hash(into hasher: inout Hasher) {
hasher.combine(rawRepresentation)
}
}

/// A P-521 private key used for key agreement.
Expand Down
8 changes: 8 additions & 0 deletions Sources/Crypto/Key Agreement/ECDH.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ extension ${CURVE} {
let pemDocument = ASN1.PEMDocument(type: "PUBLIC KEY", derBytes: self.derRepresentation)
return pemDocument.pemString
}

public static func ==(lhs: Self, rhs: Self) -> Bool {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Equatable, Part of #174 but I was unable to create a diff between just this PR and the source branch of that PR.

lhs.rawRepresentation == rhs.rawRepresentation
}

public func hash(into hasher: inout Hasher) {
hasher.combine(rawRepresentation)
}
}

% if FUNC == "Signing":
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: PK { get }
}

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


func testP256SigningPublicKeyEquatable() throws {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Equatable, Part of #174 but I was unable to create a diff between just this PR and the source branch of that PR.

// 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
)
}
}

func testP256SigningPublicKeyHashable() throws {
let expectedCount = 64
let set = Set((0..<expectedCount).map { _ in
P256.Signing.PrivateKey().publicKey
})
XCTAssertEqual(set.count, expectedCount)
}

func testP256KeyAgreementPublicKeyHashable() throws {
let expectedCount = 64
let set = Set((0..<expectedCount).map { _ in
P256.KeyAgreement.PrivateKey().publicKey
})
XCTAssertEqual(set.count, expectedCount)
}

func testP384SigningPublicKeyHashable() throws {
let expectedCount = 64
let set = Set((0..<expectedCount).map { _ in
P384.Signing.PrivateKey().publicKey
})
XCTAssertEqual(set.count, expectedCount)
}

func testP384KeyAgreementPublicKeyHashable() throws {
let expectedCount = 64
let set = Set((0..<expectedCount).map { _ in
P384.KeyAgreement.PrivateKey().publicKey
})
XCTAssertEqual(set.count, expectedCount)
}

func testP521SigningPublicKeyHashable() throws {
let expectedCount = 64
let set = Set((0..<expectedCount).map { _ in
P521.Signing.PrivateKey().publicKey
})
XCTAssertEqual(set.count, expectedCount)
}

func testP521KeyAgreementPublicKeyHashable() throws {
let expectedCount = 64
let set = Set((0..<expectedCount).map { _ in
P521.KeyAgreement.PrivateKey().publicKey
})
XCTAssertEqual(set.count, expectedCount)
}

}
#endif // (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) && CRYPTO_IN_SWIFTPM