-
Notifications
You must be signed in to change notification settings - Fork 23
bindings for EC crypto #356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
a8b1546
6184fa4
d508d2d
1057449
c1edf66
9ffce38
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| import AwsCCal | ||
|
|
||
| import struct Foundation.Data | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0. | ||
| import struct Foundation.Date | ||
| import struct Foundation.TimeInterval | ||
|
|
||
| public class ECKeyPair { | ||
|
|
||
| public enum ECAlgorithm: UInt32 { | ||
| case p256 = 0 | ||
| case p384 = 1 | ||
| } | ||
|
|
||
| public enum ECExportFormat: UInt32 { | ||
| case sec1 = 0 | ||
| case pkcs8 = 1 | ||
| case spki = 2 | ||
| } | ||
|
|
||
| public typealias ECRawSignature = (r: Data, s: Data) | ||
| public typealias ECPublicCoords = (x: Data, y: Data) | ||
|
|
||
| public static let maxExportSize = 512 | ||
|
|
||
| let rawValue: UnsafeMutablePointer<aws_ecc_key_pair> | ||
|
|
||
| private init(rawValue: UnsafeMutablePointer<aws_ecc_key_pair>) { | ||
| self.rawValue = rawValue | ||
| } | ||
|
|
||
| static func generate(algorithm: ECAlgorithm) throws -> ECKeyPair { | ||
| guard | ||
| let rawValue = aws_ecc_key_pair_new_generate_random( | ||
| allocator.rawValue, aws_ecc_curve_name(algorithm.rawValue)) | ||
| else { | ||
| throw CommonRunTimeError.crtError(.makeFromLastError()) | ||
| } | ||
| return ECKeyPair(rawValue: rawValue) | ||
| } | ||
|
|
||
| static func fromDer(data: Data) throws -> ECKeyPair { | ||
| try data.withUnsafeBytes { bufferPointer in | ||
| var byteCursor = aws_byte_cursor_from_array(bufferPointer.baseAddress, data.count) | ||
| guard | ||
| let rawValue = aws_ecc_key_pair_new_from_asn1(allocator.rawValue, &byteCursor) | ||
| else { | ||
| throw CommonRunTimeError.crtError(.makeFromLastError()) | ||
| } | ||
| return ECKeyPair(rawValue: rawValue) | ||
| } | ||
| } | ||
|
|
||
| /// Decodes der ec signature into raw r and s components | ||
| static public func decodeDerEcSignature(signature: Data) throws -> ECKeyPair.ECRawSignature { | ||
| var rCur = aws_byte_cursor() | ||
| var sCur = aws_byte_cursor() | ||
|
|
||
| return try signature.withUnsafeBytes { signaturePointer -> ECKeyPair.ECRawSignature in | ||
| let signatureCursor = aws_byte_cursor_from_array( | ||
| signaturePointer.baseAddress, | ||
| signature.count | ||
| ) | ||
|
|
||
| guard | ||
| aws_ecc_decode_signature_der_to_raw( | ||
| allocator.rawValue, | ||
| signatureCursor, | ||
| &rCur, | ||
| &sCur | ||
| ) == AWS_OP_SUCCESS | ||
| else { | ||
| throw CommonRunTimeError.crtError(.makeFromLastError()) | ||
| } | ||
|
|
||
| let rData = Data(bytes: rCur.ptr, count: rCur.len) | ||
| let sData = Data(bytes: sCur.ptr, count: sCur.len) | ||
| return ECKeyPair.ECRawSignature(r: rData, s: sData) | ||
| } | ||
| } | ||
|
|
||
| /// Encode raw ec signature into der format | ||
| static public func encodeRawECSignature(signature: ECKeyPair.ECRawSignature) throws -> Data { | ||
| return try signature.r.withUnsafeBytes { rPointer in | ||
| let rCursor = aws_byte_cursor_from_array(rPointer.baseAddress, signature.r.count) | ||
| return try signature.s.withUnsafeBytes { sPointer in | ||
| let sCursor = aws_byte_cursor_from_array(sPointer.baseAddress, signature.s.count) | ||
|
|
||
| let bufferSize = signature.r.count + signature.s.count + 32 | ||
| var bufferData = Data(count: bufferSize) | ||
| var newBufferSize = 0 | ||
| try bufferData.withUnsafeMutableBytes { bufferPointer in | ||
| var buffer = aws_byte_buf_from_empty_array(bufferPointer.baseAddress, bufferSize) | ||
| guard | ||
| aws_ecc_encode_signature_raw_to_der( | ||
| allocator.rawValue, | ||
| rCursor, sCursor, &buffer) == AWS_OP_SUCCESS | ||
| else { | ||
| throw CommonRunTimeError.crtError(.makeFromLastError()) | ||
| } | ||
| newBufferSize = buffer.len | ||
| } | ||
| bufferData.count = newBufferSize | ||
| return bufferData | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public func exportKey(format: ECKeyPair.ECExportFormat) throws -> Data { | ||
| var bufferData = Data(count: ECKeyPair.maxExportSize) | ||
| try bufferData.withUnsafeMutableBytes { bufferPointer in | ||
| var buffer = aws_byte_buf_from_empty_array(bufferPointer.baseAddress, ECKeyPair.maxExportSize) | ||
| guard | ||
| aws_ecc_key_pair_export(rawValue, aws_ecc_key_export_format(format.rawValue), &buffer) | ||
| == AWS_OP_SUCCESS | ||
| else { | ||
| throw CommonRunTimeError.crtError(.makeFromLastError()) | ||
| } | ||
| } | ||
| return bufferData | ||
| } | ||
|
|
||
| public func getPublicCoords() throws -> ECKeyPair.ECPublicCoords { | ||
| var xCoord = aws_byte_cursor() | ||
| var yCoord = aws_byte_cursor() | ||
| aws_ecc_key_pair_get_public_key(rawValue, &xCoord, &yCoord) | ||
| let xData = Data(bytes: xCoord.ptr, count: xCoord.len) | ||
| let yData = Data(bytes: yCoord.ptr, count: yCoord.len) | ||
|
|
||
| return ECKeyPair.ECPublicCoords(x: xData, y: yData) | ||
| } | ||
|
|
||
| public func sign(digest: Data) throws -> Data { | ||
| let bufferSize = aws_ecc_key_pair_signature_length(rawValue) | ||
| var bufferData = Data(count: bufferSize) | ||
| var newBufferSize = 0 | ||
| try digest.withUnsafeBytes { bufferPointer in | ||
| var byteCursor = aws_byte_cursor_from_array(bufferPointer.baseAddress, digest.count) | ||
| try bufferData.withUnsafeMutableBytes { bufferPointer in | ||
| var buffer = aws_byte_buf_from_empty_array(bufferPointer.baseAddress, bufferSize) | ||
|
||
| guard aws_ecc_key_pair_sign_message(rawValue, &byteCursor, &buffer) == AWS_OP_SUCCESS else { | ||
| throw CommonRunTimeError.crtError(.makeFromLastError()) | ||
| } | ||
| newBufferSize = buffer.len | ||
| } | ||
| } | ||
| bufferData.count = newBufferSize | ||
| return bufferData | ||
| } | ||
|
|
||
| public func verify(digest: Data, signature: Data) -> Bool { | ||
| return digest.withUnsafeBytes { digestPointer -> Bool in | ||
| var digestCursor = aws_byte_cursor_from_array(digestPointer.baseAddress, digest.count) | ||
|
|
||
| return signature.withUnsafeBytes { signaturePointer -> Bool in | ||
| var signatureCursor = aws_byte_cursor_from_array( | ||
| signaturePointer.baseAddress, signature.count) | ||
| return aws_ecc_key_pair_verify_signature(rawValue, &digestCursor, &signatureCursor) | ||
| == AWS_OP_SUCCESS | ||
| } | ||
| } | ||
| } | ||
|
|
||
| deinit { | ||
| aws_ecc_key_pair_release(rawValue) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0. | ||
| import XCTest | ||
| import AwsCCommon | ||
|
|
||
| @testable import AwsCommonRuntimeKit | ||
|
|
||
| class ECTests: XCBaseTestCase { | ||
|
|
||
| func testP256() throws { | ||
| let input = "Hello" | ||
| let sha256 = try input.data(using: .utf8)!.computeSHA256() | ||
|
|
||
| let ecKey = try ECKeyPair.generate(algorithm: ECKeyPair.ECAlgorithm.p256) | ||
| let signature = try ecKey.sign(digest: sha256) | ||
|
|
||
| let raw = try ECKeyPair.decodeDerEcSignature(signature: signature) | ||
| XCTAssertEqual( | ||
| signature, | ||
| try ECKeyPair.encodeRawECSignature(signature: ECKeyPair.ECRawSignature(r: raw.r, s: raw.s))) | ||
|
|
||
| XCTAssertTrue(ecKey.verify(digest: sha256, signature: signature)) | ||
| } | ||
|
|
||
| func testP384() throws { | ||
| let input = "Hello" | ||
| let sha256 = try input.data(using: .utf8)!.computeSHA256() | ||
|
|
||
| let ecKey = try ECKeyPair.generate(algorithm: ECKeyPair.ECAlgorithm.p384) | ||
| let signature = try ecKey.sign(digest: sha256) | ||
|
|
||
| let raw = try ECKeyPair.decodeDerEcSignature(signature: signature) | ||
| XCTAssertEqual( | ||
| signature, | ||
| try ECKeyPair.encodeRawECSignature(signature: ECKeyPair.ECRawSignature(r: raw.r, s: raw.s))) | ||
|
|
||
| XCTAssertTrue(ecKey.verify(digest: sha256, signature: signature)) | ||
| } | ||
|
|
||
| func testImport() throws { | ||
| let input = "Hello" | ||
| let sha256 = try input.data(using: .utf8)!.computeSHA256() | ||
|
|
||
| let sec1Key = """ | ||
| MHcCAQEEIHjt7c+VnkIkN6RW7QgZPFNLb/9AZEhqSYYMtwrlLb3WoAoGCCqGSM49AwEHoUQDQgAEv2F\ | ||
| jRpMtADMZ4zoZxshV9chEkembgzZnXSUNe+DA8dKqXN/7qTcZjYJHKIi+Rn88zUGqCJo3DWF/X+ufVf\ | ||
| dU2g== | ||
| """ | ||
|
|
||
| guard let keyData = Data(base64Encoded: sec1Key) else { | ||
| XCTFail("Failed to decode base64 string") | ||
| return | ||
| } | ||
|
|
||
| let ecKey = try ECKeyPair.fromDer(data: keyData) | ||
| let signature = try ecKey.sign(digest: sha256) | ||
|
|
||
| let raw = try ECKeyPair.decodeDerEcSignature(signature: signature) | ||
| XCTAssertEqual( | ||
| signature, | ||
| try ECKeyPair.encodeRawECSignature(signature: ECKeyPair.ECRawSignature(r: raw.r, s: raw.s))) | ||
|
|
||
| XCTAssertTrue(ecKey.verify(digest: sha256, signature: signature)) | ||
| } | ||
|
|
||
| } |
Uh oh!
There was an error while loading. Please reload this page.