# Noise Keys

> Interfaces and calls can run over the Noise protocol instead of plain TCP, authenticating both sides by 32-byte Curve25519 static keys.

`import "github.com/unum-cloud/ucall"` 1.0.0-rc.1 · b7577426

Interfaces and calls can run over the Noise protocol instead of plain TCP, authenticating both sides by 32-byte Curve25519 static keys.

```go
serverPrivateKey, serverPublicKey, err := ucall.GenerateNoiseKeypair()
if err != nil {
	return err
}
clientPrivateKey, _, err := ucall.GenerateNoiseKeypair()
if err != nil {
	return err
}
clientPublicKey, err := ucall.DeriveNoisePublicKey(clientPrivateKey)
if err != nil {
	return err
}

sealed := ucall.NewInterface("0.0.0.0:8546")
sealed.Security, sealed.NoisePrivateKey = ucall.SecurityNoiseXX, &serverPrivateKey
sealed.PeerAuthorizer = func(key ucall.NoiseKey) bool { return key == clientPublicKey }
server, err := ucall.NewServer([]ucall.Interface{sealed}, nil)
if err != nil {
	return err
}

client, err := ucall.NewClient(&ucall.ClientOptions{
	Connections:     1,
	NoisePrivateKey: &clientPrivateKey,
	PeerAuthorizer:  func(key ucall.NoiseKey) bool { return key == serverPublicKey },
})
```

`GenerateNoiseKeypair` draws from the operating system’s entropy and fails with `StatusIO` when it is refused. `DeriveNoisePublicKey` recovers the public half of a stored private key, which peers compare in their authorizers. Under Noise XX both sides learn each other’s key during the handshake, so both need an authorizer; Noise IK has the client name the server’s key in advance through `ClientCallOptions.NoisePeerKey`. Noise KK has both sides know each other in advance: the interface admits the one key `Interface.NoisePeerKey` names, and the call presents `ClientCallOptions.NoisePrivateKey` or the client’s own key. Both sides may also hold a 32-byte `NoisePreSharedKey`, and must match their `KeyExchange`, whose zero value is the post-quantum `KeyExchangeX25519MLKEM768`. An `http+noise://` URL implies Noise XX, and a handler reads the client’s authenticated key through `Request.Peer`.

## API

### Types

#### `PeerAuthorizer`

PeerAuthorizer decides whether a peer presenting an authenticated static key may proceed.

[server.go:640 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/server.go#L640)

```go
type PeerAuthorizer func(key NoiseKey) bool
```

It runs on the polling goroutine completing the handshake and must not panic.

#### `Security`

Security is the transport an interface or a call runs over.

[server.go:560 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/server.go#L560)

```go
type Security int32
```

#### Values

| `SecurityNone` | | SecurityNone is plain TCP. |
| - | - | - |
| `SecurityNoiseXX` | | SecurityNoiseXX authenticates both sides during the handshake. |
| `SecurityNoiseIK` | | SecurityNoiseIK has the client know the server's key in advance. |
| `SecurityNoiseKK` | | SecurityNoiseKK has both sides know each other's key in advance. |

#### Enum `KeyExchange`

KeyExchange is the key exchange of a Noise handshake, which both sides must match.2

[types.go:575 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/types.go#L575)

```go
type KeyExchange int32
```

#### Values

| `KeyExchangeX25519MLKEM768` | | KeyExchangeX25519MLKEM768 runs X25519 and ML-KEM-768 together: recorded traffic stays private unless both break. |
| - | - | - |
| `KeyExchangeX25519` | | KeyExchangeX25519 runs X25519 alone, for standard Noise peers: recorded traffic falls to a quantum computer. |

##### `KeyExchangeFromName`

KeyExchangeFromName reads a key exchange back from its stable name.

[types.go:592 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/types.go#L592)

```go
func KeyExchangeFromName(name string) (KeyExchange, error)
```

##### `String`

[types.go:587 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/types.go#L587)

```go
func (exchange KeyExchange) String() string
```

#### Type `NoiseKey`

NoiseKey is a 32-byte Curve25519 key.1

[types.go:605 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/types.go#L605)

```go
type NoiseKey [32]byte
```

##### `DeriveNoisePublicKey`

DeriveNoisePublicKey recovers the public half of a stored private key, which peers compare in their authorizers.

[types.go:624 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/types.go#L624)

```go
func DeriveNoisePublicKey(privateKey NoiseKey) (NoiseKey, error)
```

### Functions

#### `GenerateNoiseKeypair`

GenerateNoiseKeypair draws a fresh private key and its public key from the operating system's entropy.

[ucall.go:609 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/ucall.go#L609)

```go
func GenerateNoiseKeypair() (NoiseKey, NoiseKey, error)
```
