2021-06-27 09:21:53 +01:00
|
|
|
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-12-03 02:04:48 +00:00
|
|
|
package controlbase
|
2021-06-27 09:21:53 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/cipher"
|
2021-10-26 01:24:32 +01:00
|
|
|
"encoding/binary"
|
2021-10-26 00:45:38 +01:00
|
|
|
"errors"
|
2021-06-27 09:21:53 +01:00
|
|
|
"fmt"
|
|
|
|
"hash"
|
|
|
|
"io"
|
|
|
|
"net"
|
2021-07-29 19:59:40 +01:00
|
|
|
"strconv"
|
2021-06-27 09:21:53 +01:00
|
|
|
"time"
|
|
|
|
|
2021-10-26 00:41:30 +01:00
|
|
|
"go4.org/mem"
|
2021-06-27 09:21:53 +01:00
|
|
|
"golang.org/x/crypto/blake2s"
|
|
|
|
chp "golang.org/x/crypto/chacha20poly1305"
|
|
|
|
"golang.org/x/crypto/curve25519"
|
|
|
|
"golang.org/x/crypto/hkdf"
|
|
|
|
"tailscale.com/types/key"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-10-26 00:41:30 +01:00
|
|
|
// protocolName is the name of the specific instantiation of Noise
|
|
|
|
// that the control protocol uses. This string's value is fixed by
|
|
|
|
// the Noise spec, and shouldn't be changed unless we're updating
|
|
|
|
// the control protocol to use a different Noise instance.
|
2021-06-27 09:21:53 +01:00
|
|
|
protocolName = "Noise_IK_25519_ChaChaPoly_BLAKE2s"
|
2021-10-26 00:41:30 +01:00
|
|
|
// protocolVersion is the version of the control protocol that
|
|
|
|
// Client will use when initiating a handshake.
|
2021-07-30 19:38:10 +01:00
|
|
|
protocolVersion uint16 = 1
|
2021-07-29 19:59:40 +01:00
|
|
|
// protocolVersionPrefix is the name portion of the protocol
|
2021-10-26 00:41:30 +01:00
|
|
|
// name+version string that gets mixed into the handshake as a
|
|
|
|
// prologue.
|
2021-07-29 19:59:40 +01:00
|
|
|
//
|
2021-10-26 00:41:30 +01:00
|
|
|
// This mixing verifies that both clients agree that they're
|
|
|
|
// executing the control protocol at a specific version that
|
|
|
|
// matches the advertised version in the cleartext packet header.
|
2021-07-29 19:59:40 +01:00
|
|
|
protocolVersionPrefix = "Tailscale Control Protocol v"
|
|
|
|
invalidNonce = ^uint64(0)
|
2021-06-27 09:21:53 +01:00
|
|
|
)
|
|
|
|
|
2021-07-30 19:38:10 +01:00
|
|
|
func protocolVersionPrologue(version uint16) []byte {
|
2021-07-29 19:59:40 +01:00
|
|
|
ret := make([]byte, 0, len(protocolVersionPrefix)+5) // 5 bytes is enough to encode all possible version numbers.
|
|
|
|
ret = append(ret, protocolVersionPrefix...)
|
|
|
|
return strconv.AppendUint(ret, uint64(version), 10)
|
|
|
|
}
|
|
|
|
|
2021-10-26 00:41:30 +01:00
|
|
|
// Client initiates a control client handshake, returning the resulting
|
|
|
|
// control connection.
|
2021-06-27 09:21:53 +01:00
|
|
|
//
|
|
|
|
// The context deadline, if any, covers the entire handshaking
|
2021-07-30 19:38:10 +01:00
|
|
|
// process. Any preexisting Conn deadline is removed.
|
2021-10-26 00:41:30 +01:00
|
|
|
func Client(ctx context.Context, conn net.Conn, machineKey key.MachinePrivate, controlKey key.MachinePublic) (*Conn, error) {
|
2021-06-27 09:21:53 +01:00
|
|
|
if deadline, ok := ctx.Deadline(); ok {
|
|
|
|
if err := conn.SetDeadline(deadline); err != nil {
|
|
|
|
return nil, fmt.Errorf("setting conn deadline: %w", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
conn.SetDeadline(time.Time{})
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
var s symmetricState
|
|
|
|
s.Initialize()
|
|
|
|
|
2021-07-29 19:59:40 +01:00
|
|
|
// prologue
|
|
|
|
s.MixHash(protocolVersionPrologue(protocolVersion))
|
|
|
|
|
2021-06-27 09:21:53 +01:00
|
|
|
// <- s
|
|
|
|
// ...
|
2021-10-26 00:41:30 +01:00
|
|
|
s.MixHash(controlKey.UntypedBytes())
|
2021-06-27 09:21:53 +01:00
|
|
|
|
|
|
|
// -> e, es, s, ss
|
2021-07-29 19:59:40 +01:00
|
|
|
init := mkInitiationMessage()
|
2021-10-26 00:41:30 +01:00
|
|
|
machineEphemeral := key.NewMachine()
|
2021-06-27 09:21:53 +01:00
|
|
|
machineEphemeralPub := machineEphemeral.Public()
|
2021-10-26 00:41:30 +01:00
|
|
|
copy(init.EphemeralPub(), machineEphemeralPub.UntypedBytes())
|
|
|
|
s.MixHash(machineEphemeralPub.UntypedBytes())
|
2021-10-25 23:50:15 +01:00
|
|
|
cipher, err := s.MixDH(machineEphemeral, controlKey)
|
|
|
|
if err != nil {
|
2021-06-27 09:21:53 +01:00
|
|
|
return nil, fmt.Errorf("computing es: %w", err)
|
|
|
|
}
|
|
|
|
machineKeyPub := machineKey.Public()
|
2021-10-26 00:41:30 +01:00
|
|
|
s.EncryptAndHash(cipher, init.MachinePub(), machineKeyPub.UntypedBytes())
|
2021-10-25 23:50:15 +01:00
|
|
|
cipher, err = s.MixDH(machineKey, controlKey)
|
|
|
|
if err != nil {
|
2021-06-27 09:21:53 +01:00
|
|
|
return nil, fmt.Errorf("computing ss: %w", err)
|
|
|
|
}
|
2021-10-25 23:50:15 +01:00
|
|
|
s.EncryptAndHash(cipher, init.Tag(), nil) // empty message payload
|
2021-06-27 09:21:53 +01:00
|
|
|
|
|
|
|
if _, err := conn.Write(init[:]); err != nil {
|
|
|
|
return nil, fmt.Errorf("writing initiation: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-07-29 19:59:40 +01:00
|
|
|
// Read in the payload and look for errors/protocol violations from the server.
|
2021-06-27 09:21:53 +01:00
|
|
|
var resp responseMessage
|
2021-07-29 19:59:40 +01:00
|
|
|
if _, err := io.ReadFull(conn, resp.Header()); err != nil {
|
|
|
|
return nil, fmt.Errorf("reading response header: %w", err)
|
|
|
|
}
|
|
|
|
if resp.Type() != msgTypeResponse {
|
|
|
|
if resp.Type() != msgTypeError {
|
|
|
|
return nil, fmt.Errorf("unexpected response message type %d", resp.Type())
|
|
|
|
}
|
|
|
|
msg := make([]byte, resp.Length())
|
|
|
|
if _, err := io.ReadFull(conn, msg); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-30 19:38:10 +01:00
|
|
|
return nil, fmt.Errorf("server error: %q", msg)
|
2021-07-29 19:59:40 +01:00
|
|
|
}
|
|
|
|
if resp.Length() != len(resp.Payload()) {
|
|
|
|
return nil, fmt.Errorf("wrong length %d received for handshake response", resp.Length())
|
|
|
|
}
|
|
|
|
if _, err := io.ReadFull(conn, resp.Payload()); err != nil {
|
|
|
|
return nil, err
|
2021-06-27 09:21:53 +01:00
|
|
|
}
|
|
|
|
|
2021-07-29 19:59:40 +01:00
|
|
|
// <- e, ee, se
|
2021-10-26 00:41:30 +01:00
|
|
|
controlEphemeralPub := key.MachinePublicFromRaw32(mem.B(resp.EphemeralPub()))
|
|
|
|
s.MixHash(controlEphemeralPub.UntypedBytes())
|
2021-10-25 23:50:15 +01:00
|
|
|
if _, err = s.MixDH(machineEphemeral, controlEphemeralPub); err != nil {
|
2021-06-27 09:21:53 +01:00
|
|
|
return nil, fmt.Errorf("computing ee: %w", err)
|
|
|
|
}
|
2021-10-25 23:50:15 +01:00
|
|
|
cipher, err = s.MixDH(machineKey, controlEphemeralPub)
|
|
|
|
if err != nil {
|
2021-06-27 09:21:53 +01:00
|
|
|
return nil, fmt.Errorf("computing se: %w", err)
|
|
|
|
}
|
2021-10-25 23:50:15 +01:00
|
|
|
if err := s.DecryptAndHash(cipher, nil, resp.Tag()); err != nil {
|
2021-06-27 09:21:53 +01:00
|
|
|
return nil, fmt.Errorf("decrypting payload: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c1, c2, err := s.Split()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("finalizing handshake: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-07-30 19:38:10 +01:00
|
|
|
c := &Conn{
|
2021-06-27 09:21:53 +01:00
|
|
|
conn: conn,
|
2021-07-29 19:59:40 +01:00
|
|
|
version: protocolVersion,
|
2021-06-27 09:21:53 +01:00
|
|
|
peer: controlKey,
|
|
|
|
handshakeHash: s.h,
|
|
|
|
tx: txState{
|
|
|
|
cipher: c1,
|
|
|
|
},
|
|
|
|
rx: rxState{
|
|
|
|
cipher: c2,
|
|
|
|
},
|
2021-07-30 19:38:10 +01:00
|
|
|
}
|
|
|
|
return c, nil
|
2021-06-27 09:21:53 +01:00
|
|
|
}
|
|
|
|
|
2021-10-26 00:41:30 +01:00
|
|
|
// Server initiates a control server handshake, returning the resulting
|
|
|
|
// control connection.
|
2021-06-27 09:21:53 +01:00
|
|
|
//
|
|
|
|
// The context deadline, if any, covers the entire handshaking
|
|
|
|
// process.
|
2021-10-26 00:41:30 +01:00
|
|
|
func Server(ctx context.Context, conn net.Conn, controlKey key.MachinePrivate) (*Conn, error) {
|
2021-06-27 09:21:53 +01:00
|
|
|
if deadline, ok := ctx.Deadline(); ok {
|
|
|
|
if err := conn.SetDeadline(deadline); err != nil {
|
|
|
|
return nil, fmt.Errorf("setting conn deadline: %w", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
conn.SetDeadline(time.Time{})
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2021-07-29 19:59:40 +01:00
|
|
|
// Deliberately does not support formatting, so that we don't echo
|
|
|
|
// attacker-controlled input back to them.
|
|
|
|
sendErr := func(msg string) error {
|
|
|
|
if len(msg) >= 1<<16 {
|
|
|
|
msg = msg[:1<<16]
|
|
|
|
}
|
|
|
|
var hdr [headerLen]byte
|
2021-10-26 01:24:32 +01:00
|
|
|
hdr[0] = msgTypeError
|
|
|
|
binary.BigEndian.PutUint16(hdr[1:3], uint16(len(msg)))
|
2021-07-29 19:59:40 +01:00
|
|
|
if _, err := conn.Write(hdr[:]); err != nil {
|
|
|
|
return fmt.Errorf("sending %q error to client: %w", msg, err)
|
|
|
|
}
|
2021-07-30 19:38:10 +01:00
|
|
|
if _, err := io.WriteString(conn, msg); err != nil {
|
2021-07-29 19:59:40 +01:00
|
|
|
return fmt.Errorf("sending %q error to client: %w", msg, err)
|
|
|
|
}
|
2021-07-30 19:38:10 +01:00
|
|
|
return fmt.Errorf("refused client handshake: %q", msg)
|
2021-07-29 19:59:40 +01:00
|
|
|
}
|
|
|
|
|
2021-06-27 09:21:53 +01:00
|
|
|
var s symmetricState
|
|
|
|
s.Initialize()
|
|
|
|
|
2021-07-29 19:59:40 +01:00
|
|
|
var init initiationMessage
|
|
|
|
if _, err := io.ReadFull(conn, init.Header()); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if init.Version() != protocolVersion {
|
|
|
|
return nil, sendErr("unsupported protocol version")
|
|
|
|
}
|
|
|
|
if init.Type() != msgTypeInitiation {
|
|
|
|
return nil, sendErr("unexpected handshake message type")
|
|
|
|
}
|
|
|
|
if init.Length() != len(init.Payload()) {
|
|
|
|
return nil, sendErr("wrong handshake initiation length")
|
|
|
|
}
|
|
|
|
if _, err := io.ReadFull(conn, init.Payload()); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// prologue. Can only do this once we at least think the client is
|
|
|
|
// handshaking using a supported version.
|
|
|
|
s.MixHash(protocolVersionPrologue(protocolVersion))
|
|
|
|
|
2021-06-27 09:21:53 +01:00
|
|
|
// <- s
|
|
|
|
// ...
|
|
|
|
controlKeyPub := controlKey.Public()
|
2021-10-26 00:41:30 +01:00
|
|
|
s.MixHash(controlKeyPub.UntypedBytes())
|
2021-06-27 09:21:53 +01:00
|
|
|
|
|
|
|
// -> e, es, s, ss
|
2021-10-26 00:41:30 +01:00
|
|
|
machineEphemeralPub := key.MachinePublicFromRaw32(mem.B(init.EphemeralPub()))
|
|
|
|
s.MixHash(machineEphemeralPub.UntypedBytes())
|
2021-10-25 23:50:15 +01:00
|
|
|
cipher, err := s.MixDH(controlKey, machineEphemeralPub)
|
|
|
|
if err != nil {
|
2021-06-27 09:21:53 +01:00
|
|
|
return nil, fmt.Errorf("computing es: %w", err)
|
|
|
|
}
|
2021-10-26 00:41:30 +01:00
|
|
|
var machineKeyBytes [32]byte
|
|
|
|
if err := s.DecryptAndHash(cipher, machineKeyBytes[:], init.MachinePub()); err != nil {
|
2021-06-27 09:21:53 +01:00
|
|
|
return nil, fmt.Errorf("decrypting machine key: %w", err)
|
|
|
|
}
|
2021-10-26 00:41:30 +01:00
|
|
|
machineKey := key.MachinePublicFromRaw32(mem.B(machineKeyBytes[:]))
|
2021-10-25 23:50:15 +01:00
|
|
|
cipher, err = s.MixDH(controlKey, machineKey)
|
|
|
|
if err != nil {
|
2021-06-27 09:21:53 +01:00
|
|
|
return nil, fmt.Errorf("computing ss: %w", err)
|
|
|
|
}
|
2021-10-25 23:50:15 +01:00
|
|
|
if err := s.DecryptAndHash(cipher, nil, init.Tag()); err != nil {
|
2021-06-27 09:21:53 +01:00
|
|
|
return nil, fmt.Errorf("decrypting initiation tag: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// <- e, ee, se
|
2021-07-29 19:59:40 +01:00
|
|
|
resp := mkResponseMessage()
|
2021-10-26 00:41:30 +01:00
|
|
|
controlEphemeral := key.NewMachine()
|
2021-06-27 09:21:53 +01:00
|
|
|
controlEphemeralPub := controlEphemeral.Public()
|
2021-10-26 00:41:30 +01:00
|
|
|
copy(resp.EphemeralPub(), controlEphemeralPub.UntypedBytes())
|
|
|
|
s.MixHash(controlEphemeralPub.UntypedBytes())
|
2021-10-25 23:50:15 +01:00
|
|
|
if _, err := s.MixDH(controlEphemeral, machineEphemeralPub); err != nil {
|
2021-06-27 09:21:53 +01:00
|
|
|
return nil, fmt.Errorf("computing ee: %w", err)
|
|
|
|
}
|
2021-10-25 23:50:15 +01:00
|
|
|
cipher, err = s.MixDH(controlEphemeral, machineKey)
|
|
|
|
if err != nil {
|
2021-06-27 09:21:53 +01:00
|
|
|
return nil, fmt.Errorf("computing se: %w", err)
|
|
|
|
}
|
2021-10-25 23:50:15 +01:00
|
|
|
s.EncryptAndHash(cipher, resp.Tag(), nil) // empty message payload
|
2021-06-27 09:21:53 +01:00
|
|
|
|
|
|
|
c1, c2, err := s.Split()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("finalizing handshake: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := conn.Write(resp[:]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-30 19:38:10 +01:00
|
|
|
c := &Conn{
|
2021-06-27 09:21:53 +01:00
|
|
|
conn: conn,
|
2021-07-29 19:59:40 +01:00
|
|
|
version: protocolVersion,
|
2021-06-27 09:21:53 +01:00
|
|
|
peer: machineKey,
|
|
|
|
handshakeHash: s.h,
|
|
|
|
tx: txState{
|
|
|
|
cipher: c2,
|
|
|
|
},
|
|
|
|
rx: rxState{
|
|
|
|
cipher: c1,
|
|
|
|
},
|
2021-07-30 19:38:10 +01:00
|
|
|
}
|
|
|
|
return c, nil
|
2021-06-27 09:21:53 +01:00
|
|
|
}
|
|
|
|
|
2021-10-26 00:41:30 +01:00
|
|
|
// symmetricState contains the state of an in-flight handshake.
|
2021-06-27 09:21:53 +01:00
|
|
|
type symmetricState struct {
|
2021-07-30 19:38:10 +01:00
|
|
|
finished bool
|
|
|
|
|
2021-10-26 00:41:30 +01:00
|
|
|
h [blake2s.Size]byte // hash of currently-processed handshake state
|
|
|
|
ck [blake2s.Size]byte // chaining key used to construct session keys at the end of the handshake
|
2021-06-27 09:21:53 +01:00
|
|
|
}
|
|
|
|
|
2021-10-26 01:24:32 +01:00
|
|
|
func (s *symmetricState) checkFinished() {
|
2021-07-30 19:38:10 +01:00
|
|
|
if s.finished {
|
|
|
|
panic("attempted to use symmetricState after Split was called")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-27 09:21:53 +01:00
|
|
|
// Initialize sets s to the initial handshake state, prior to
|
2021-10-25 23:59:37 +01:00
|
|
|
// processing any handshake messages.
|
2021-06-27 09:21:53 +01:00
|
|
|
func (s *symmetricState) Initialize() {
|
2021-07-30 19:38:10 +01:00
|
|
|
s.checkFinished()
|
2021-06-27 09:21:53 +01:00
|
|
|
s.h = blake2s.Sum256([]byte(protocolName))
|
|
|
|
s.ck = s.h
|
|
|
|
}
|
|
|
|
|
|
|
|
// MixHash updates s.h to be BLAKE2s(s.h || data), where || is
|
|
|
|
// concatenation.
|
|
|
|
func (s *symmetricState) MixHash(data []byte) {
|
2021-07-30 19:38:10 +01:00
|
|
|
s.checkFinished()
|
2021-10-25 23:59:37 +01:00
|
|
|
h := newBLAKE2s()
|
|
|
|
h.Write(s.h[:])
|
|
|
|
h.Write(data)
|
|
|
|
h.Sum(s.h[:0])
|
2021-06-27 09:21:53 +01:00
|
|
|
}
|
|
|
|
|
2021-10-25 23:50:15 +01:00
|
|
|
// MixDH updates s.ck with the result of X25519(priv, pub) and returns
|
|
|
|
// a singleUseCHP that can be used to encrypt or decrypt handshake
|
|
|
|
// data.
|
2021-06-27 09:21:53 +01:00
|
|
|
//
|
|
|
|
// MixDH corresponds to MixKey(X25519(...))) in the spec. Implementing
|
|
|
|
// it as a single function allows for strongly-typed arguments that
|
|
|
|
// reduce the risk of error in the caller (e.g. invoking X25519 with
|
|
|
|
// two private keys, or two public keys), and thus producing the wrong
|
|
|
|
// calculation.
|
2021-10-26 00:41:30 +01:00
|
|
|
func (s *symmetricState) MixDH(priv key.MachinePrivate, pub key.MachinePublic) (*singleUseCHP, error) {
|
2021-07-30 19:38:10 +01:00
|
|
|
s.checkFinished()
|
2021-10-26 00:41:30 +01:00
|
|
|
keyData, err := curve25519.X25519(priv.UntypedBytes(), pub.UntypedBytes())
|
2021-06-27 09:21:53 +01:00
|
|
|
if err != nil {
|
2021-10-25 23:50:15 +01:00
|
|
|
return nil, fmt.Errorf("computing X25519: %w", err)
|
2021-06-27 09:21:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
r := hkdf.New(newBLAKE2s, keyData, s.ck[:], nil)
|
|
|
|
if _, err := io.ReadFull(r, s.ck[:]); err != nil {
|
2021-10-25 23:50:15 +01:00
|
|
|
return nil, fmt.Errorf("extracting ck: %w", err)
|
2021-06-27 09:21:53 +01:00
|
|
|
}
|
2021-10-25 23:50:15 +01:00
|
|
|
var k [chp.KeySize]byte
|
|
|
|
if _, err := io.ReadFull(r, k[:]); err != nil {
|
|
|
|
return nil, fmt.Errorf("extracting k: %w", err)
|
2021-06-27 09:21:53 +01:00
|
|
|
}
|
2021-10-25 23:50:15 +01:00
|
|
|
return newSingleUseCHP(k), nil
|
2021-06-27 09:21:53 +01:00
|
|
|
}
|
|
|
|
|
2021-07-02 00:15:32 +01:00
|
|
|
// EncryptAndHash encrypts plaintext into ciphertext (which must be
|
2021-10-25 23:50:15 +01:00
|
|
|
// the correct size to hold the encrypted plaintext) using cipher,
|
|
|
|
// mixes the ciphertext into s.h, and returns the ciphertext.
|
|
|
|
func (s *symmetricState) EncryptAndHash(cipher *singleUseCHP, ciphertext, plaintext []byte) {
|
2021-07-30 19:38:10 +01:00
|
|
|
s.checkFinished()
|
2021-10-26 00:44:22 +01:00
|
|
|
if len(ciphertext) != len(plaintext)+chp.Overhead {
|
2021-07-02 00:15:32 +01:00
|
|
|
panic("ciphertext is wrong size for given plaintext")
|
|
|
|
}
|
2021-10-25 23:50:15 +01:00
|
|
|
ret := cipher.Seal(ciphertext[:0], plaintext, s.h[:])
|
2021-06-27 09:21:53 +01:00
|
|
|
s.MixHash(ret)
|
|
|
|
}
|
|
|
|
|
2021-07-02 00:15:32 +01:00
|
|
|
// DecryptAndHash decrypts the given ciphertext into plaintext (which
|
|
|
|
// must be the correct size to hold the decrypted ciphertext) using
|
2021-10-25 23:50:15 +01:00
|
|
|
// cipher. If decryption is successful, it mixes the ciphertext into
|
|
|
|
// s.h.
|
|
|
|
func (s *symmetricState) DecryptAndHash(cipher *singleUseCHP, plaintext, ciphertext []byte) error {
|
2021-07-30 19:38:10 +01:00
|
|
|
s.checkFinished()
|
2021-10-26 00:44:22 +01:00
|
|
|
if len(ciphertext) != len(plaintext)+chp.Overhead {
|
2021-10-26 00:45:38 +01:00
|
|
|
return errors.New("plaintext is wrong size for given ciphertext")
|
2021-07-02 00:15:32 +01:00
|
|
|
}
|
2021-10-25 23:50:15 +01:00
|
|
|
if _, err := cipher.Open(plaintext[:0], ciphertext, s.h[:]); err != nil {
|
2021-07-02 00:15:32 +01:00
|
|
|
return err
|
2021-06-27 09:21:53 +01:00
|
|
|
}
|
|
|
|
s.MixHash(ciphertext)
|
2021-07-02 00:15:32 +01:00
|
|
|
return nil
|
2021-06-27 09:21:53 +01:00
|
|
|
}
|
|
|
|
|
2021-07-02 00:18:04 +01:00
|
|
|
// Split returns two ChaCha20Poly1305 ciphers with keys derived from
|
2021-07-30 19:38:10 +01:00
|
|
|
// the current handshake state. Methods on s cannot be used again
|
|
|
|
// after calling Split.
|
2021-06-27 09:21:53 +01:00
|
|
|
func (s *symmetricState) Split() (c1, c2 cipher.AEAD, err error) {
|
2021-07-30 19:38:10 +01:00
|
|
|
s.finished = true
|
|
|
|
|
2021-06-27 09:21:53 +01:00
|
|
|
var k1, k2 [chp.KeySize]byte
|
|
|
|
r := hkdf.New(newBLAKE2s, nil, s.ck[:], nil)
|
|
|
|
if _, err := io.ReadFull(r, k1[:]); err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("extracting k1: %w", err)
|
|
|
|
}
|
|
|
|
if _, err := io.ReadFull(r, k2[:]); err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("extracting k2: %w", err)
|
|
|
|
}
|
|
|
|
c1, err = chp.New(k1[:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("constructing AEAD c1: %w", err)
|
|
|
|
}
|
|
|
|
c2, err = chp.New(k2[:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("constructing AEAD c2: %w", err)
|
|
|
|
}
|
|
|
|
return c1, c2, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newBLAKE2s returns a hash.Hash implementing BLAKE2s, or panics on
|
|
|
|
// error.
|
|
|
|
func newBLAKE2s() hash.Hash {
|
|
|
|
h, err := blake2s.New256(nil)
|
|
|
|
if err != nil {
|
|
|
|
// Should never happen, errors only happen when using BLAKE2s
|
|
|
|
// in MAC mode with a key.
|
2021-07-30 19:38:10 +01:00
|
|
|
panic(err)
|
2021-06-27 09:21:53 +01:00
|
|
|
}
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
// newCHP returns a cipher.AEAD implementing ChaCha20Poly1305, or
|
|
|
|
// panics on error.
|
|
|
|
func newCHP(key [chp.KeySize]byte) cipher.AEAD {
|
|
|
|
aead, err := chp.New(key[:])
|
|
|
|
if err != nil {
|
|
|
|
// Can only happen if we passed a key of the wrong length. The
|
|
|
|
// function signature prevents that.
|
2021-07-30 19:38:10 +01:00
|
|
|
panic(err)
|
2021-06-27 09:21:53 +01:00
|
|
|
}
|
|
|
|
return aead
|
|
|
|
}
|
2021-10-25 23:50:15 +01:00
|
|
|
|
|
|
|
// singleUseCHP is an instance of ChaCha20Poly1305 that can be used
|
|
|
|
// only once, either for encrypting or decrypting, but not both. The
|
|
|
|
// chosen operation is always executed with an all-zeros
|
|
|
|
// nonce. Subsequent calls to either Seal or Open panic.
|
|
|
|
type singleUseCHP struct {
|
|
|
|
c cipher.AEAD
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSingleUseCHP(key [chp.KeySize]byte) *singleUseCHP {
|
|
|
|
return &singleUseCHP{newCHP(key)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *singleUseCHP) Seal(dst, plaintext, additionalData []byte) []byte {
|
|
|
|
if c.c == nil {
|
|
|
|
panic("Attempted reuse of singleUseAEAD")
|
|
|
|
}
|
|
|
|
cipher := c.c
|
|
|
|
c.c = nil
|
|
|
|
var nonce [chp.NonceSize]byte
|
|
|
|
return cipher.Seal(dst, nonce[:], plaintext, additionalData)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *singleUseCHP) Open(dst, ciphertext, additionalData []byte) ([]byte, error) {
|
|
|
|
if c.c == nil {
|
|
|
|
panic("Attempted reuse of singleUseAEAD")
|
|
|
|
}
|
|
|
|
cipher := c.c
|
|
|
|
c.c = nil
|
|
|
|
var nonce [chp.NonceSize]byte
|
|
|
|
return cipher.Open(dst, nonce[:], ciphertext, additionalData)
|
|
|
|
}
|