Trikalabs
  • Home
  • Best online TDD videos
  • Book Suggestions
  • About Me
  • Contact
Trikalabs
No Result
View All Result

How to wrap c code in a swift package

by fragi
February 12, 2022
in Crypto, SPM
Share on FacebookShare on Twitter

Sometimes we need to add c code in a Swift Package so we can use it in swift. One of situation can be when we want to use some security code that has been used and tested for years. This can be a case that  we don’t want to take the risk to write the code from scratch in Swift.

Let’s try it in an example, so let’s assume we want to use the Nacl code to calculate the public key from the private by using elliptic curves. First we need to have access to the files (AND ALWAYS CHECK THE LICENCE) so we follow the steps to download them from here: http://nacl.cr.yp.to/internals.html.

Let’s now create a package. The package can be the following:

Swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import PackageDescription
 
let package = Package(
    name: "NaCl",
    platforms: [
        .macOS(.v10_12), .iOS(.v9)
    ],
    products: [
        .library(
            name: "NaCl",
            targets: ["NaCl"]),
    ],
    targets: [
        .target(
            name: "NaCl",
            dependencies: ["NaClC"]),
        .target(
            name: "NaClC",
            resources: [
                .copy("ge25519_base.data")]),
        .testTarget(
            name: "NaClTests",
            dependencies: ["NaCl"])
    ]
)

As we see we have two main targets and one test target. The NaCLC target is where we have the c code and the Nacl where we provide an easier swift interface by handling the memory management complexity (we talk about this in another post). Also note that we have to declare as resource the .data file.

Let’s now see the NaCLC target:

As we see we place all the headers in an include folder and rest of the c files outside.

In the NaCL target we provide a simplified interface and we handle the memory management so the swift caller to call it as easier as possible.

Swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import Foundation
import NaClC
 
public struct NaCl {
    public init() {}
    
    public func toPublic(_ input: [UInt8]) -> [UInt8] {
        let capacity = 32
        let pk = UnsafeMutablePointer<UInt8>.allocate(capacity: capacity)
        let sk = UnsafeMutablePointer<UInt8>.allocate(capacity: capacity)
        var pkCopy: [UInt8] = Array(repeating: 0, count: capacity)
 
        for i in 0..<capacity {
            (pk + i).initialize(to: 0)
            (sk + i).initialize(to: input[i])
        }
 
        modified_crypto_sign_publickey(pk, sk)
 
        for i in 0..<capacity {
            pkCopy[i] = (pk + i).pointee
        }
 
        pk.deallocate()
        sk.deallocate()
 
        return pkCopy
    }
 
}

You can find the code in the following link:

https://github.com/arnot-project/swift-nacl

fragi

fragi

Related Posts

Blake2b Algorithm
Algorithms

Bech32 Algorithm

March 1, 2022

Bech32 is a checksummed base32 format. It is being used in cryptocurrencies development (bitcoin, cardano,...) https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki, https://cips.cardano.org/cips/cip19/. Let's explore the...

Blake2b Algorithm
Algorithms

Blake2b Algorithm

February 28, 2022

Blake2 is a cryptographic hash function. https://www.blake2.net/ Let's now explore how to implement this algorithm in Swift. The first option...

BIP39
Algorithms

BIP39

February 12, 2022

In this article we will talk about the BIP39 Algorithm that is being used in many crypto. wallets. So what...

Next Post
How to handle UnsafeMutablePointer in Swift

How to handle UnsafeMutablePointer in Swift

Performance testing in Xcode

Performance testing in Xcode

Memoization

Memoization

  • Advertise
  • Privacy & Policy
  • Contact

© 2019 Trikalabs Ltd. All rights reserved.

No Result
View All Result
  • Home
  • About Me
  • A curated list with the best free online TDD videos
  • Book Suggestions
  • Pinner Code Club

© 2019 Trikalabs Ltd. All rights reserved.