# `ExCEP.Codec.AuthAsdu`

Binary Authentication ASDU codec per CSG CEP TS2.2 Section 5 (Tables 3, 4, 5).

## Three-Way Handshake
1. Gateway -> Cluster: `AuthRequest` (`0x01`) with gateway IP, 64-byte UTF-8 name, 8-byte `r1`
2. Cluster -> Gateway: `AuthResponse` (`0x02`) with cluster IP, 64-byte UTF-8 name, `r1`, 8-byte `r2`
3. Gateway -> Cluster: `AuthConfirm` (`0x03`) with `r2`

All three frames carry `auth?: true, signed?: true` in the APDU control byte and a 64-byte
SM2 signature covering the 32-byte APDU header and the binary ASDU payload.

# `t`

```elixir
@type t() ::
  ExCEP.Codec.AuthAsdu.Request.t()
  | ExCEP.Codec.AuthAsdu.Response.t()
  | ExCEP.Codec.AuthAsdu.Confirm.t()
```

# `decode`

```elixir
@spec decode(binary()) :: {:ok, t()} | {:error, term()}
```

Decodes a binary authentication ASDU.

Accepts both the standard body-only form (when the 64-byte SM2 signature is carried as the
APDU trailer with `bit0 = 1`) and the embedded-signature form (trailing 64 bytes inside ASDU).

# `encode`

```elixir
@spec encode(t()) :: {:ok, binary()} | {:error, term()}
```

Encodes an authentication ASDU struct into binary (excluding the trailing 64B APDU signature).

# `random_nonce`

```elixir
@spec random_nonce() :: &lt;&lt;_::64&gt;&gt;
```

Generates an 8-byte cryptographically random challenge (`r1` or `r2`).

# `verify_embedded_signature`

```elixir
@spec verify_embedded_signature(ExCEP.Codec.Frame.t(), {module(), term()} | nil) ::
  :ok | {:error, term()}
```

Verifies an embedded 64-byte SM2 signature when a peer places the auth signature inside
the ASDU body (`frame.signed? == false`) instead of as the APDU trailer (`frame.signed? == true`).
If `frame.signed? == true`, verification was already performed by `ExCEP.Codec.Frame`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
