[ad_1]
Background
I’ve a server which makes use of MSRCrypto to signal my nonce. I have to confirm that nonce in Java. It’s effectively know that MSRCrypto sends it in P1363 format and Apple Cryptokit Library requires it in DER format.
I can not change the server code as I’m the shopper. The server is utilizing SHA256withECDSA
What I would like
- Is there a library which I can use to do these conversion with out writing it myself ?
What I’m conscious of
From this matter: Changing P1363 format to ASN.1/DER format utilizing Java
I translated the answer in Swift to transform P256 ECDSA Signature solely.
However the code required to import this BigInt Library:
https://github.com/attaswift/BigInt
Right here is the code:
import BigInt
class CryptoKitConverter {
/**
Convert ECDSASignature 256 Signature from IEEE P1363 format to DER ASN.1
This algorithm relies on a Java converter declared right here : https://stackoverflow.com/a/61873962/8166854
The converter have been translated into Swift model as a result of, truly (27/04/2022) there's no different resolution I discovered to transform P1363 signature to DER / ASN.1 format that's required on IOS utilizing Apple Cryptokit framework
Please test for replace from CryptoKit or every other resolution authorised by an authority
- Authors:
Helie de Bernis
- parameters:
- plainBase64: The signature formated in P1363 encoded to base 64 String
- Vital:
This Swift implementation is definitely solely working for Elliptic Curve 256
/// The NIST P-256 Elliptic Curve.
, and if it was initially developed to just accept
/// The NIST P-384 Elliptic Curve.
,and
/// The NIST P-521 Elliptic Curve.
it has not been developed for different kinds of digital signatures.
*/
public static func convertSignatureP1363ToDerBase64(_ plainBase64 : String) -> String?
{
let plain : [UInt8] = self.base64Decoding(plainBase64);
let n : Int = 32;
let rArraySlice = plain[0..<n]
let sArraySlice = plain[n..<n * 2]
let r = Array(rArraySlice)
let s = Array(sArraySlice)
var x1: [UInt8] = r
var x2: [UInt8] = s
let bigUIntR = BigUInt(Knowledge(plain[0..<n])).bitWidth
let bigUIntS = BigUInt(Knowledge(plain[n..<n * 2])).bitWidth
if bigUIntR == 256
{
x1.insert(0, at: 0)
}
if bigUIntS == 256
{
x2.insert(0, at: 0)
}
let len : Int = x1.rely + x2.rely + (2 + 2);
var idx : Int = len >= 128 ? 3 : 2;
var out : [UInt8] = Array(repeating: 0, rely: idx + len);
out[0] = 48;
if (idx == 3)
{
out[1] = UInt8(129); //0x81
out[2] = UInt8(len);
}
else
{
out[1] = UInt8(len);
}
out[idx] = 2;
out[idx + 1] = UInt8(x1.rely);
let idxX1Index = idx+2
let sizeX1Range = idxX1Index + x1.rely
out.replaceSubrange(idxX1Index..<sizeX1Range, with: x1)
idx += x1.rely + 2;
out[idx] = 2;
out[idx + 1] = UInt8(x2.rely);
let idxX2Index = idx+2
let sizeX2Range = idxX2Index + x2.rely
out.replaceSubrange(idxX2Index..<sizeX2Range, with: x2)
return self.base64Encoding(&out)
}
static func base64Encoding(_ enter : inout [UInt8]) -> String?
{
let information = Knowledge(enter)
return information.base64EncodedString()
}
static func base64Decoding(_ enter : String) -> [UInt8]
{
let information = Knowledge(base64Encoded: enter, choices: .ignoreUnknownCharacters)
guard let information = information else { return [UInt8]() }
let array = information.bytes
return array
}
}
extension Knowledge {
init<T>(fromArray values: [T]) {
self = values.withUnsafeBytes { Knowledge($0) }
}
func toArray<T>(sort: T.Kind) -> [T] the place T: ExpressibleByIntegerLiteral {
var array = Array<T>(repeating: 0, rely: self.rely/MemoryLayout<T>.stride)
_ = array.withUnsafeMutableBytes { copyBytes(to: $0) }
return array
}
var bytes: [UInt8] {
return [UInt8](self)
}
func hexEncodedString() -> String {
return map { String(format: "%02hhx", $0) }.joined()
}}
So my query is the subsequent, is there every other native resolution in swift to get the identical end result as this converter ?
Thanks to your assist
[ad_2]
