feat: libse
This commit is contained in:
@@ -1,64 +1,55 @@
|
|||||||
// import Swift
|
// import Swift
|
||||||
import Foundation
|
// import Foundation
|
||||||
// import Security
|
// import Security
|
||||||
import CryptoKit
|
import CryptoKit
|
||||||
import LocalAuthentication
|
import LocalAuthentication
|
||||||
|
|
||||||
func is_support_secure_enclave() -> Bool {
|
// reference:
|
||||||
let se = SecureEnclave.isAvailable
|
// https://zenn.dev/iceman/scraps/380f69137c7ea2
|
||||||
print("Supports SE: \(se)")
|
// https://www.andyibanez.com/posts/cryptokit-secure-enclave/
|
||||||
return se
|
@_cdecl("is_support_secure_enclave")
|
||||||
|
func isSupportSecureEnclave() -> Bool {
|
||||||
|
return SecureEnclave.isAvailable
|
||||||
}
|
}
|
||||||
|
|
||||||
enum StringError: Error {
|
enum StringError: Error {
|
||||||
case base64error
|
case base64error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func generateKeyPair() throws {
|
||||||
|
var error: Unmanaged<CFError>? = nil;
|
||||||
|
guard
|
||||||
|
let accessCtrl = SecAccessControlCreateWithFlags(
|
||||||
|
nil,
|
||||||
|
kSecAttrAccessibleWhenUnlockedThisDeviceOnly,
|
||||||
|
[.privateKeyUsage, .biometryCurrentSet],
|
||||||
|
&error
|
||||||
|
) else {
|
||||||
|
throw error!.takeRetainedValue() as Swift.Error
|
||||||
|
}
|
||||||
|
var privateKeyReference = try CryptoKit.SecureEnclave.P256.KeyAgreement.PrivateKey.init(
|
||||||
|
accessControl: accessCtrl
|
||||||
|
);
|
||||||
|
var dataRepresentation = privateKeyReference.dataRepresentation;
|
||||||
|
print("Private key reference: \(privateKeyReference)");
|
||||||
|
print("Private key reference - publicKey: \(privateKeyReference.publicKey)");
|
||||||
|
print("Private key reference - dataRepresentation: \(privateKeyReference.dataRepresentation)");
|
||||||
|
print("Private key reference - dataRepresentation: \(privateKeyReference.dataRepresentation.base64EncodedString())");
|
||||||
|
}
|
||||||
|
|
||||||
// // var error: Unmanaged<CFError>? = nil;
|
func computeEcdh() throws {
|
||||||
// // guard
|
guard let dataRepresentation = Data(base64Encoded: "BIIB2DGCAdQwgfUMAnJrMYHuMAsMA2JpZAQElx-----"
|
||||||
// // let accessCtrl = SecAccessControlCreateWithFlags(
|
) else {
|
||||||
// // nil,
|
throw StringError.base64error
|
||||||
// // kSecAttrAccessibleWhenUnlockedThisDeviceOnly,
|
}
|
||||||
// // [.privateKeyUsage, .biometryCurrentSet],
|
|
||||||
// // &error
|
let context = LAContext();
|
||||||
// // ) else {
|
let p = try SecureEnclave.P256.KeyAgreement.PrivateKey(
|
||||||
// // throw error!.takeRetainedValue() as Swift.Error
|
dataRepresentation: dataRepresentation, authenticationContext: context)
|
||||||
// // }
|
|
||||||
// // var privateKeyReference = try CryptoKit.SecureEnclave.P256.KeyAgreement.PrivateKey.init(
|
let ephemeralSecretKey = P256.KeyAgreement.PrivateKey()
|
||||||
// // accessControl: accessCtrl
|
let sharedSecret = try ephemeralSecretKey.sharedSecretFromKeyAgreement(with: p.publicKey)
|
||||||
// // );
|
let sharedSecret2 = try p.sharedSecretFromKeyAgreement(with: ephemeralSecretKey.publicKey)
|
||||||
// // var dataRepresentation = privateKeyReference.dataRepresentation;
|
print("Shared secret: \(sharedSecret)")
|
||||||
//
|
print("Shared secret: \(sharedSecret2)")
|
||||||
// guard let dataRepresentation = Data(base64Encoded: "BIIB2DGCAdQwgfUMAnJrMYHuMAsMA2JpZAQElxyY8jBIDANwdWIEQQQOrOKfvtBWnEquk6+92hH5yxO6SN5TSIA7/kP0DMdzRlojtNQJ3RUs7uJfG/stjJ/Cza+1i+hISbAGaL0BasDPMAgMA3JrbwIBADAHD-----") else {
|
}
|
||||||
// throw StringError.base64error
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// let context = LAContext();
|
|
||||||
// let p = try SecureEnclave.P256.KeyAgreement.PrivateKey(
|
|
||||||
// dataRepresentation: dataRepresentation, authenticationContext: context)
|
|
||||||
//
|
|
||||||
// let ephemeralSecretKey = P256.KeyAgreement.PrivateKey()
|
|
||||||
// let sharedSecret = try ephemeralSecretKey.sharedSecretFromKeyAgreement(with: p.publicKey)
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// let sharedSecret2 = try p.sharedSecretFromKeyAgreement(with: ephemeralSecretKey.publicKey)
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// // print("Private key reference: \(privateKeyReference)");
|
|
||||||
//
|
|
||||||
// // print("Private key reference - publicKey: \(privateKeyReference.publicKey)");
|
|
||||||
//
|
|
||||||
// // // print("Private key reference - publicKey - ageRecipient: \(privateKeyReference.publicKey.ageRecipient)");
|
|
||||||
//
|
|
||||||
// // print("Private key reference - dataRepresentation: \(privateKeyReference.dataRepresentation)");
|
|
||||||
// // print("Private key reference - dataRepresentation: \(privateKeyReference.dataRepresentation.base64EncodedString())");
|
|
||||||
//
|
|
||||||
// // https://www.andyibanez.com/posts/cryptokit-secure-enclave/
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// print("Shared secret: \(sharedSecret)")
|
|
||||||
// print("Shared secret: \(sharedSecret2)")
|
|
||||||
|
|||||||
Reference in New Issue
Block a user