Skip to content

samueltangz/swift-noise-protocol

Repository files navigation

SwiftNoise

Noise protocol implemented with Swift.

Installation

Swift Package Manager

Add the following lines to Package.swift.

dependencies: [
  ...,
  .package(url: "https://github.com/samueltangz/swift-noise-protocol.git", from: "0.2.1")
  ...
],
targets: (
  ...
  dependencies: [
    ...,
    "SwiftNoise",
    ...
  ],
  ...
)

Import package

import SwiftNoise

Supported features

DH functions, cipher functions, and hash functions

The functions are supported based on section 12 of the specification.

Diffie-Hellman curves

  • Curve25519
  • Curve448

Cipher functions

  • ChaChaPoly
  • AESGCM

Hash functions

  • SHA256
  • SHA512
  • BLAKE2s
  • BLAKE2b

Handshake patterns

The handshake patterns defined in section 7 of the specification will be supported.

  • N
  • K
  • X
  • NN
  • NK
  • NX
  • KN
  • KK
  • KX
  • XN
  • XK
  • XX
  • IN
  • IK
  • IX

Example usage

The following is an example usage for Noise with Noise_X_25519_AESGCM_SHA256.

let responderStaticKeyPair = try! generateKeyPair()
let initiatorEphemeralKeyPair = try! generateKeyPair()
let responderEphemeralKeyPair = try! generateKeyPair()

let prologue = Data()

let initiatorState = try! HandshakeState(
  pattern: .N,
  initiator: true,
  prologue: prologue,
  e: initiatorEphemeralKeyPair,
  rs: responderStaticKeyPair.publicKey
)
let responderState = try! HandshakeState(
  pattern: .N,
  initiator: false,
  prologue: prologue,
  s: responderStaticKeyPair,
  e: responderEphemeralKeyPair
)

// -> e, es
let initiatorTx = try! initiatorState.writeMessage(payload: Data())
assert(try! responderState.readMessage(message: initiatorTx) == Data())
assert(responderState.remoteE! == initiatorEphemeralKeyPair.publicKey)

Development

Code Format

We use swift-format for automatic code formatting.

  • Download via homebrew: brew install swift-format
  • Run formatter: swift-format -i --configuration=swift-format.json */**/**/*.swift