# Noise Keys

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

`#include <ucall/types.hpp>` 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.

```cpp
auto [server_keys, server_keys_status] = uc::noise_key_generate();
auto [client_keys, client_keys_status] = uc::noise_key_generate();

auto admit_client = [&client_keys](ucall_noise_key_t const& key) noexcept {
    return uc::noise_keys_equal(key, client_keys.public_key);
};
uc::interface_t const interfaces[] {{.address = "0.0.0.0:8546",
                                     .security = ucall_security_noise_xx_k,
                                     .noise_private_key = &server_keys.private_key,
                                     .peer_authorizer = admit_client}};
auto [server, server_status] = uc::server_t::make(interfaces);

auto admit_server = [&server_keys](ucall_noise_key_t const& key) noexcept {
    return uc::noise_keys_equal(key, server_keys.public_key);
};
auto [client, client_status] = uc::client_t::make(
    {.max_concurrent_connections = 1, .noise_private_key = &client_keys.private_key, .peer_authorizer = admit_server});
```

`noise_key_generate` draws from the operating system’s entropy and fails with `status_code_t::io_k` when it is refused. `noise_key_derive` 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 `client_call_options_t::noise_peer_key`. Noise KK has both sides know each other in advance: the interface admits the one key `interface_t::noise_peer_key` names, and the call presents its own key or the client’s. Both sides may also hold a 32-byte `noise_pre_shared_key`, and must match their `key_exchange`, which defaults to the post-quantum `ucall_key_exchange_x25519_mlkem768_k`. An `http+noise://` URL implies Noise XX, and a handler reads the client’s authenticated key through `request_t::peer`.

## API

### Types

#### `noise_keypair_t`

A static Curve25519 keypair, the private half kept secret.

[types.hpp:368 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/types.hpp#L368)

```cpp
struct noise_keypair_t
```

#### Fields

| `private_key` | `ucall_noise_key_t` | The secret scalar a server interface or a client presents. |
| - | - | - |
| `public_key` | `ucall_noise_key_t` | The public key peers see during the handshake and compare in their authorizers. |

#### `peer_authorizer_t`

A peer authorizer the caller keeps for the server's or the client's lifetime; empty admits nobody under Noise XX, which needs one.

[types.hpp:425 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/types.hpp#L425)

```cpp
class peer_authorizer_t
```

### Functions

#### `noise_key_generate`

Draws a fresh keypair from the operating system's entropy.

[types.hpp:378 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/types.hpp#L378)

```cpp
expected<noise_keypair_t> noise_key_generate() noexcept;
```

#### `noise_key_derive`

Derives the public half of a stored `private_key`, answering both halves.

[types.hpp:390 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/types.hpp#L390)

```cpp
expected<noise_keypair_t> noise_key_derive(
    ucall_noise_key_t const & private_key) noexcept;
```

#### `noise_keys_equal`

Whether two keys hold the same bytes, as an authorizer compares a peer with the one expected.

[types.hpp:403 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/types.hpp#L403)

```cpp
bool noise_keys_equal(
    ucall_noise_key_t const & first,
    ucall_noise_key_t const & second) noexcept;
```
