samueltangz / swift-noise-protocol

Noise protocol implemented in Swift.
https://samueltangz.github.io/swift-noise-protocol/
MIT License
8 stars 4 forks source link

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

Cipher functions

Hash functions

Handshake patterns

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

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.