V0.6.9 Arroyo Toad
Added support for P256 Curve and suppressed support for P521 Curve that will be reimplemented later
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
public enum SignType: UInt32 {
|
||||
case P521 = 10
|
||||
case P384 = 11
|
||||
case P256 = 12
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
// The Swift Programming Language
|
||||
// https://docs.swift.org/swift-book
|
||||
|
||||
import Foundation
|
||||
import Crypto
|
||||
|
||||
import Foundation
|
||||
|
||||
public func serializeV1(msg: MessageP) -> Data {
|
||||
|
||||
let MESSAGE_CAPACITY: Int = countBytes(msg: msg)
|
||||
@@ -63,26 +64,37 @@ public func serializeV1(msg: MessageP) -> Data {
|
||||
|
||||
return serializedData
|
||||
}
|
||||
|
||||
public func signMessage(msgData: Data, signType: SignType, key: P521.Signing.PrivateKey) throws -> [UInt8] {
|
||||
let signatureBytes = try signatureBytes(signature: signType)
|
||||
public func signMessage(msgData: Data, signType: SignType, key: P256.Signing.PrivateKey) throws
|
||||
-> [UInt8]
|
||||
{
|
||||
|
||||
// UGLY We are hypothesisying that signType is P521
|
||||
let signature = try signP521(object: msgData, key: key).map { value in
|
||||
return value
|
||||
}
|
||||
switch signType {
|
||||
/* case .P521:
|
||||
|
||||
return signature
|
||||
return try signP521(object: msgData, key: key).map { value in
|
||||
return value
|
||||
} */
|
||||
case .P256:
|
||||
return try signP256(object: msgData, key: key).map { value in
|
||||
return value
|
||||
}
|
||||
|
||||
default:
|
||||
throw CommonError.SIGNATURE_NOT_SUPPORTED
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public func verifyMessageSignature(message: SignedMessage, key: P521.Signing.PublicKey) throws -> Bool {
|
||||
public func verifyMessageSignature(message: SignedMessage, key: P256.Signing.PublicKey) throws
|
||||
-> Bool
|
||||
{
|
||||
// UGLY Assuming P521 Signature
|
||||
|
||||
let msgData = serializeV1(msg: message)
|
||||
return try verifySignatureP521(signature: Data(message.signature), object: msgData, key: key)
|
||||
|
||||
let msgData = serializeV1(msg: message)
|
||||
return try verifySignatureP256(signature: Data(message.signature), object: msgData, key: key)
|
||||
|
||||
}
|
||||
|
||||
public func deserializeV1(serializedData: Data) throws -> SignedMessage {
|
||||
@@ -97,8 +109,6 @@ public func deserializeV1(serializedData: Data) throws -> SignedMessage {
|
||||
let signType: SignType = SignType(rawValue: serializedData[4...7].uint32)!
|
||||
// First 8 bytes
|
||||
|
||||
let signBytes = try signatureBytes(signature: signType)
|
||||
|
||||
let timestamp = serializedData[8...15].double
|
||||
// 8 Bytes
|
||||
|
||||
@@ -160,10 +170,7 @@ public func deserializeV1(serializedData: Data) throws -> SignedMessage {
|
||||
return value
|
||||
}
|
||||
|
||||
// Sanity check signature with signatureType
|
||||
if signature.count != signBytes {
|
||||
throw DeserializationError.UNMATCHING_SIGNATURE_TYPE
|
||||
}
|
||||
// We can't check for bytes a priori, unluckily
|
||||
|
||||
return SignedMessage(
|
||||
version: version,
|
||||
@@ -178,7 +185,6 @@ public func deserializeV1(serializedData: Data) throws -> SignedMessage {
|
||||
signature: signature
|
||||
)
|
||||
}
|
||||
|
||||
public func countBytes(msg: MessageP) -> Int {
|
||||
|
||||
///
|
||||
@@ -200,14 +206,3 @@ public func countBytes(msg: MessageP) -> Int {
|
||||
return INITIAL_CAPACITY_BYTES + fieldReveservedCapacity
|
||||
|
||||
}
|
||||
|
||||
public func signatureBytes(signature: SignType) throws -> Int {
|
||||
switch signature {
|
||||
|
||||
case .P521:
|
||||
return 132
|
||||
|
||||
default:
|
||||
throw CommonError.SIGNATURE_NOT_SUPPORTED
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,11 @@ import Foundation
|
||||
public func signP521(object: Data, key: P521.Signing.PrivateKey)throws -> Data {
|
||||
return try key.signature<Data>(for: object).rawRepresentation
|
||||
|
||||
}
|
||||
|
||||
public func signP256(object: Data, key: P256.Signing.PrivateKey)throws -> Data {
|
||||
return try key.signature<Data>(for: object).rawRepresentation
|
||||
|
||||
}
|
||||
/*
|
||||
public func sign<T>(object: T, key: P521.Signing.PrivateKey) throws -> String {
|
||||
@@ -35,24 +40,59 @@ public func verifySignatureP521(signature: Data, object: Data, key: P521.Signing
|
||||
return key.isValidSignature<Data>(ecdsa, for: object)
|
||||
}
|
||||
|
||||
public func verifySignatureP256(signature: Data, object: Data, key: P256.Signing.PublicKey) throws -> Bool {
|
||||
|
||||
let ecdsa: P256.Signing.ECDSASignature
|
||||
|
||||
do {
|
||||
ecdsa = try P256.Signing.ECDSASignature(rawRepresentation: signature)
|
||||
} catch {
|
||||
throw SecurityError.NotDecodableError
|
||||
}
|
||||
|
||||
return key.isValidSignature<Data>(ecdsa, for: object)
|
||||
}
|
||||
|
||||
|
||||
// ------------------
|
||||
// --- PEM 2 Key ----
|
||||
// ------------------
|
||||
public func pem2key(filePath: String) throws -> P521.Signing.PrivateKey {
|
||||
|
||||
// -------------------
|
||||
// --- PEM 2 Key 521 -
|
||||
// -------------------
|
||||
public func pem2_P521key(filePath: String) throws -> P521.Signing.PrivateKey {
|
||||
|
||||
let pemURL: URL = URL(filePath: filePath)
|
||||
|
||||
return try pem2key(filePem: pemURL)
|
||||
return try pem2_P521key(filePem: pemURL)
|
||||
}
|
||||
|
||||
public func pem2key(filePem: URL) throws -> P521.Signing.PrivateKey {
|
||||
public func pem2_P521key(filePem: URL) throws -> P521.Signing.PrivateKey {
|
||||
|
||||
let fileString: String = try String(contentsOf: filePem, encoding: String.Encoding.utf8)
|
||||
return try pem2key(pemString: fileString)
|
||||
return try pem2_P521key(pemString: fileString)
|
||||
}
|
||||
|
||||
public func pem2key(pemString: String) throws -> P521.Signing.PrivateKey {
|
||||
public func pem2_P521key(pemString: String) throws -> P521.Signing.PrivateKey {
|
||||
return try P521.Signing.PrivateKey(pemRepresentation: pemString)
|
||||
}
|
||||
|
||||
// -------------------
|
||||
// --- PEM 2 Key 256 -
|
||||
// -------------------
|
||||
|
||||
public func pem2_P256key(filePath: String) throws -> P256.Signing.PrivateKey {
|
||||
|
||||
let pemURL: URL = URL(filePath: filePath)
|
||||
|
||||
return try pem2_P256key(filePem: pemURL)
|
||||
}
|
||||
|
||||
public func pem2_P256key(filePem: URL) throws -> P256.Signing.PrivateKey {
|
||||
|
||||
let fileString: String = try String(contentsOf: filePem, encoding: String.Encoding.utf8)
|
||||
return try pem2_P256key(pemString: fileString)
|
||||
}
|
||||
|
||||
public func pem2_P256key(pemString: String) throws -> P256.Signing.PrivateKey {
|
||||
return try P256.Signing.PrivateKey(pemRepresentation: pemString)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import Crypto
|
||||
|
||||
public protocol PublicSignKeyP {
|
||||
|
||||
}
|
||||
|
||||
public protocol PrivateSignKeyP {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user