Skip to content
This repository has been archived by the owner on Aug 30, 2022. It is now read-only.

Commit

Permalink
Throw errors when baseAddress is nil for a buffer pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhyunkim committed Oct 21, 2019
1 parent c9f48ac commit 60a1e63
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
16 changes: 11 additions & 5 deletions EosioSwiftEcc/Recover/EccRecoverKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ public class EccRecoverKey {
EC_KEY_set_group(key, group)

var recoveredPubKeyHex = ""
privateKey.withUnsafeBytes { rawBufferPointer in
try privateKey.withUnsafeBytes { rawBufferPointer in
let bufferPointer = rawBufferPointer.bindMemory(to: UInt8.self)
guard let pkbytes = bufferPointer.baseAddress else { return }
guard let pkbytes = bufferPointer.baseAddress else {
throw EosioError(.keySigningError, reason: "Base address of privateKey is nil.")
}

BN_bin2bn(pkbytes, Int32(privateKey.count), privKeyBN)
EC_KEY_set_private_key(key, privKeyBN)
Expand Down Expand Up @@ -97,7 +99,9 @@ public class EccRecoverKey {
var recoveredPubKeyHex = ""
try signatureDer.withUnsafeBytes { rawBufferPointer -> Void in
let bufferPointer = rawBufferPointer.bindMemory(to: UInt8.self)
guard let derBytes = bufferPointer.baseAddress else { return }
guard let derBytes = bufferPointer.baseAddress else {
throw EosioError(.keySigningError, reason: "Base address of signatureDer is nil.")
}

let recoveredKey = EC_KEY_new_by_curve_name(curveName)
var sig = ECDSA_SIG_new()
Expand All @@ -106,9 +110,11 @@ public class EccRecoverKey {
guard sig != nil else {
throw EosioError(.keySigningError, reason: "Signature \(signatureDer.hex) is not valid" )
}
message.withUnsafeBytes { rawBufferPointer in
try message.withUnsafeBytes { rawBufferPointer in
let bufferPointer = rawBufferPointer.bindMemory(to: UInt8.self)
guard let messageBytes = bufferPointer.baseAddress else { return }
guard let messageBytes = bufferPointer.baseAddress else {
throw EosioError(.keySigningError, reason: "Base address of message is nil.")
}

ECDSA_SIG_recover_key_GFp(recoveredKey, sig, messageBytes, Int32(message.count), Int32(recid), 1)
guard let recoveredPubKey = EC_KEY_get0_public_key(recoveredKey) else { return }
Expand Down
14 changes: 10 additions & 4 deletions EosioSwiftEcc/Sign/EosioEccSign.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ public class EosioEccSign {
let group = EC_GROUP_new_by_curve_name(NID_secp256k1)
EC_KEY_set_group(key, group)

privateKey.withUnsafeBytes { rawBufferPointer in
try privateKey.withUnsafeBytes { rawBufferPointer in
let bufferPointer = rawBufferPointer.bindMemory(to: UInt8.self)
guard let pkbytes = bufferPointer.baseAddress else { return }
guard let pkbytes = bufferPointer.baseAddress else {
throw EosioError(.keySigningError, reason: "Base address of privateKey is nil.")
}

BN_bin2bn(pkbytes, Int32(privateKey.count), privKeyBN)
EC_KEY_set_private_key(key, privKeyBN)
}
Expand All @@ -46,9 +49,12 @@ public class EosioEccSign {
for i in 1...k1SignMaxAttempts {
var der = Data(count: 100)
var numBytes: Int32 = 0
digest.withUnsafeBytes { rawBufferPointer in
try digest.withUnsafeBytes { rawBufferPointer in
let bufferPointer = rawBufferPointer.bindMemory(to: UInt8.self)
guard let digestBytes = bufferPointer.baseAddress else { return }
guard let digestBytes = bufferPointer.baseAddress else {
throw EosioError(.keySigningError, reason: "Base address of digest is nil.")
}

let sig = ECDSA_do_sign(digestBytes, Int32(digest.count), key)
der.withUnsafeMutableBytes { mutableRawBufferPointer in
let mutableBufferPointer = mutableRawBufferPointer.bindMemory(to: UInt8.self)
Expand Down

0 comments on commit 60a1e63

Please sign in to comment.