Skip to content
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
11 changes: 9 additions & 2 deletions Sources/CryptorECC/ECPrivateKey.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,12 @@ public class ECPrivateKey {
// OBJECT IDENTIFIER
// OBJECT IDENTIFIER
// BIT STRING (This is the `pubKeyBytes` added afterwards)
if self.curve == .prime256v1 {
if self.curve == .prime192v1 {
keyHeader = Data([0x30, 0x49,
0x30, 0x13,
0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01,
0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x01, 0x03, 0x32])
} else if self.curve == .prime256v1 {
keyHeader = Data([0x30, 0x59,
0x30, 0x13,
0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01,
Expand Down Expand Up @@ -281,7 +286,9 @@ public class ECPrivateKey {
#else
let kAsymmetricCryptoManagerKeyType = kSecAttrKeyTypeECSECPrimeRandom
let kAsymmetricCryptoManagerKeySize: Int
if curve == .prime256v1 {
if curve == .prime192v1 {
kAsymmetricCryptoManagerKeySize = 192
} else if curve == .prime256v1 {
kAsymmetricCryptoManagerKeySize = 256
} else if curve == .secp384r1 {
kAsymmetricCryptoManagerKeySize = 384
Expand Down
17 changes: 14 additions & 3 deletions Sources/CryptorECC/EllipticCurve.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ public struct EllipticCurve: Equatable, CustomStringConvertible {

// enum for faster comparisons
private enum InternalRepresentation: String {
case prime256v1, secp384r1, secp521r1
case prime256v1, secp384r1, secp521r1, prime192v1
}

/// A prime 192 curve
public static let prime192v1 = EllipticCurve.p192

/// A prime256v1 curve.
public static let prime256v1 = EllipticCurve.p256

Expand Down Expand Up @@ -84,6 +87,12 @@ public struct EllipticCurve: Equatable, CustomStringConvertible {
nativeCurve: NID_secp521r1,
keySize: 133)
#else
/// Secure Hash Algorithm 2 192-bit
static let p192 = EllipticCurve(internalRepresentation: .prime192v1,
signingAlgorithm: .ecdsaSignatureDigestX962SHA256,
hashEngine: CC_SHA256,
hashLength: CC_LONG(CC_SHA256_DIGEST_LENGTH),
keySize: 49)
/// Secure Hash Algorithm 2 256-bit
static let p256 = EllipticCurve(internalRepresentation: .prime256v1,
signingAlgorithm: .ecdsaSignatureDigestX962SHA256,
Expand All @@ -108,8 +117,10 @@ public struct EllipticCurve: Equatable, CustomStringConvertible {

// Select the ECAlgorithm based on the object identifier (OID) extracted from the EC key.
static func objectToCurve(ObjectIdentifier: Data) throws -> EllipticCurve {

if [UInt8](ObjectIdentifier) == [0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07] {

if [UInt8](ObjectIdentifier) == [0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x01] { // 1.2.840.10045.3.1.1
return .prime192v1
} else if [UInt8](ObjectIdentifier) == [0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07] { //1.2.840.10045.3.1.7
// p-256 (e.g: prime256v1, secp256r1) private key
return .prime256v1
} else if [UInt8](ObjectIdentifier) == [0x2B, 0x81, 0x04, 0x00, 0x22] {
Expand Down
6 changes: 6 additions & 0 deletions Tests/CryptorECCTests/CryptorECCTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,12 @@ Mw==

func test_newPrivatekey() {
do {
let p192PrivateKey = try ECPrivateKey.make(for: .prime192v1)
let p192PubKey = try p192PrivateKey.extractPublicKey()
let p192signature = try "Hello world".sign(with: p192PrivateKey)
let p192verified = p192signature.verify(plaintext: "Hello world", using: p192PubKey)
XCTAssertTrue(p192verified)

let p256PrivateKey = try ECPrivateKey.make(for: .prime256v1)
let p256PubKey = try p256PrivateKey.extractPublicKey()
let signature = try "Hello world".sign(with: p256PrivateKey)
Expand Down