2021-06-27 08:21:53 +00: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.
|
|
|
|
|
|
|
|
package noise
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/cipher"
|
|
|
|
"fmt"
|
|
|
|
"hash"
|
|
|
|
"io"
|
|
|
|
"net"
|
2021-07-29 18:59:40 +00:00
|
|
|
"strconv"
|
2021-06-27 08:21:53 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/blake2s"
|
|
|
|
chp "golang.org/x/crypto/chacha20poly1305"
|
|
|
|
"golang.org/x/crypto/curve25519"
|
|
|
|
"golang.org/x/crypto/hkdf"
|
2021-07-01 23:15:32 +00:00
|
|
|
"golang.org/x/crypto/poly1305"
|
2021-06-27 08:21:53 +00:00
|
|
|
"tailscale.com/types/key"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-07-29 18:59:40 +00:00
|
|
|
// protocolName is the name of the specific instantiation of the
|
|
|
|
// Noise protocol we're using. Each field is defined in the Noise
|
|
|
|
// spec, and shouldn't be changed unless we're switching to a
|
|
|
|
// different Noise protocol instance.
|
2021-06-27 08:21:53 +00:00
|
|
|
protocolName = "Noise_IK_25519_ChaChaPoly_BLAKE2s"
|
2021-07-29 18:59:40 +00:00
|
|
|
// protocolVersion is the version of the Tailscale base
|
|
|
|
// protocol that Client will use when initiating a handshake.
|
2021-07-30 18:38:10 +00:00
|
|
|
protocolVersion uint16 = 1
|
2021-07-29 18:59:40 +00:00
|
|
|
// protocolVersionPrefix is the name portion of the protocol
|
|
|
|
// name+version string that gets mixed into the Noise handshake as
|
|
|
|
// a prologue.
|
|
|
|
//
|
|
|
|
// This mixing verifies that both clients agree that
|
|
|
|
// they're executing the Tailscale control protocol at a specific
|
|
|
|
// version that matches the advertised version in the cleartext
|
|
|
|
// packet header.
|
|
|
|
protocolVersionPrefix = "Tailscale Control Protocol v"
|
|
|
|
invalidNonce = ^uint64(0)
|
2021-06-27 08:21:53 +00:00
|
|
|
)
|
|
|
|
|
2021-07-30 18:38:10 +00:00
|
|
|
func protocolVersionPrologue(version uint16) []byte {
|
2021-07-29 18:59:40 +00: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-06-27 08:21:53 +00:00
|
|
|
// Client initiates a Noise client handshake, returning the resulting
|
|
|
|
// Noise connection.
|
|
|
|
//
|
|
|
|
// The context deadline, if any, covers the entire handshaking
|
2021-07-30 18:38:10 +00:00
|
|
|
// process. Any preexisting Conn deadline is removed.
|
2021-06-27 08:21:53 +00:00
|
|
|
func Client(ctx context.Context, conn net.Conn, machineKey key.Private, controlKey key.Public) (*Conn, error) {
|
|
|
|
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 18:59:40 +00:00
|
|
|
// prologue
|
|
|
|
s.MixHash(protocolVersionPrologue(protocolVersion))
|
|
|
|
|
2021-06-27 08:21:53 +00:00
|
|
|
// <- s
|
|
|
|
// ...
|
|
|
|
s.MixHash(controlKey[:])
|
|
|
|
|
|
|
|
// -> e, es, s, ss
|
2021-07-29 18:59:40 +00:00
|
|
|
init := mkInitiationMessage()
|
2021-06-27 08:21:53 +00:00
|
|
|
machineEphemeral := key.NewPrivate()
|
|
|
|
machineEphemeralPub := machineEphemeral.Public()
|
2021-07-29 18:59:40 +00:00
|
|
|
copy(init.EphemeralPub(), machineEphemeralPub[:])
|
2021-06-27 08:21:53 +00:00
|
|
|
s.MixHash(machineEphemeralPub[:])
|
2021-10-25 22:50:15 +00:00
|
|
|
cipher, err := s.MixDH(machineEphemeral, controlKey)
|
|
|
|
if err != nil {
|
2021-06-27 08:21:53 +00:00
|
|
|
return nil, fmt.Errorf("computing es: %w", err)
|
|
|
|
}
|
|
|
|
machineKeyPub := machineKey.Public()
|
2021-10-25 22:50:15 +00:00
|
|
|
s.EncryptAndHash(cipher, init.MachinePub(), machineKeyPub[:])
|
|
|
|
cipher, err = s.MixDH(machineKey, controlKey)
|
|
|
|
if err != nil {
|
2021-06-27 08:21:53 +00:00
|
|
|
return nil, fmt.Errorf("computing ss: %w", err)
|
|
|
|
}
|
2021-10-25 22:50:15 +00:00
|
|
|
s.EncryptAndHash(cipher, init.Tag(), nil) // empty message payload
|
2021-06-27 08:21:53 +00:00
|
|
|
|
|
|
|
if _, err := conn.Write(init[:]); err != nil {
|
|
|
|
return nil, fmt.Errorf("writing initiation: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-07-29 18:59:40 +00:00
|
|
|
// Read in the payload and look for errors/protocol violations from the server.
|
2021-06-27 08:21:53 +00:00
|
|
|
var resp responseMessage
|
2021-07-29 18:59:40 +00:00
|
|
|
if _, err := io.ReadFull(conn, resp.Header()); err != nil {
|
|
|
|
return nil, fmt.Errorf("reading response header: %w", err)
|
|
|
|
}
|
|
|
|
if resp.Version() != protocolVersion {
|
|
|
|
return nil, fmt.Errorf("unexpected version %d from server, want %d", resp.Version(), protocolVersion)
|
|
|
|
}
|
|
|
|
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 18:38:10 +00:00
|
|
|
return nil, fmt.Errorf("server error: %q", msg)
|
2021-07-29 18:59:40 +00: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 08:21:53 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 18:59:40 +00:00
|
|
|
// <- e, ee, se
|
2021-06-27 08:21:53 +00:00
|
|
|
var controlEphemeralPub key.Public
|
2021-07-29 18:59:40 +00:00
|
|
|
copy(controlEphemeralPub[:], resp.EphemeralPub())
|
2021-06-27 08:21:53 +00:00
|
|
|
s.MixHash(controlEphemeralPub[:])
|
2021-10-25 22:50:15 +00:00
|
|
|
if _, err = s.MixDH(machineEphemeral, controlEphemeralPub); err != nil {
|
2021-06-27 08:21:53 +00:00
|
|
|
return nil, fmt.Errorf("computing ee: %w", err)
|
|
|
|
}
|
2021-10-25 22:50:15 +00:00
|
|
|
cipher, err = s.MixDH(machineKey, controlEphemeralPub)
|
|
|
|
if err != nil {
|
2021-06-27 08:21:53 +00:00
|
|
|
return nil, fmt.Errorf("computing se: %w", err)
|
|
|
|
}
|
2021-10-25 22:50:15 +00:00
|
|
|
if err := s.DecryptAndHash(cipher, nil, resp.Tag()); err != nil {
|
2021-06-27 08:21:53 +00: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 18:38:10 +00:00
|
|
|
c := &Conn{
|
2021-06-27 08:21:53 +00:00
|
|
|
conn: conn,
|
2021-07-29 18:59:40 +00:00
|
|
|
version: protocolVersion,
|
2021-06-27 08:21:53 +00:00
|
|
|
peer: controlKey,
|
|
|
|
handshakeHash: s.h,
|
|
|
|
tx: txState{
|
|
|
|
cipher: c1,
|
|
|
|
},
|
|
|
|
rx: rxState{
|
|
|
|
cipher: c2,
|
|
|
|
},
|
2021-07-30 18:38:10 +00:00
|
|
|
}
|
|
|
|
return c, nil
|
2021-06-27 08:21:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Server initiates a Noise server handshake, returning the resulting
|
|
|
|
// Noise connection.
|
|
|
|
//
|
|
|
|
// The context deadline, if any, covers the entire handshaking
|
|
|
|
// process.
|
|
|
|
func Server(ctx context.Context, conn net.Conn, controlKey key.Private) (*Conn, error) {
|
|
|
|
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 18:59:40 +00: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
|
|
|
|
setHeader(hdr[:], protocolVersion, msgTypeError, len(msg))
|
|
|
|
if _, err := conn.Write(hdr[:]); err != nil {
|
|
|
|
return fmt.Errorf("sending %q error to client: %w", msg, err)
|
|
|
|
}
|
2021-07-30 18:38:10 +00:00
|
|
|
if _, err := io.WriteString(conn, msg); err != nil {
|
2021-07-29 18:59:40 +00:00
|
|
|
return fmt.Errorf("sending %q error to client: %w", msg, err)
|
|
|
|
}
|
2021-07-30 18:38:10 +00:00
|
|
|
return fmt.Errorf("refused client handshake: %q", msg)
|
2021-07-29 18:59:40 +00:00
|
|
|
}
|
|
|
|
|
2021-06-27 08:21:53 +00:00
|
|
|
var s symmetricState
|
|
|
|
s.Initialize()
|
|
|
|
|
2021-07-29 18:59:40 +00: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 08:21:53 +00:00
|
|
|
// <- s
|
|
|
|
// ...
|
|
|
|
controlKeyPub := controlKey.Public()
|
|
|
|
s.MixHash(controlKeyPub[:])
|
|
|
|
|
|
|
|
// -> e, es, s, ss
|
|
|
|
var machineEphemeralPub key.Public
|
2021-07-29 18:59:40 +00:00
|
|
|
copy(machineEphemeralPub[:], init.EphemeralPub())
|
2021-06-27 08:21:53 +00:00
|
|
|
s.MixHash(machineEphemeralPub[:])
|
2021-10-25 22:50:15 +00:00
|
|
|
cipher, err := s.MixDH(controlKey, machineEphemeralPub)
|
|
|
|
if err != nil {
|
2021-06-27 08:21:53 +00:00
|
|
|
return nil, fmt.Errorf("computing es: %w", err)
|
|
|
|
}
|
|
|
|
var machineKey key.Public
|
2021-10-25 22:50:15 +00:00
|
|
|
if err := s.DecryptAndHash(cipher, machineKey[:], init.MachinePub()); err != nil {
|
2021-06-27 08:21:53 +00:00
|
|
|
return nil, fmt.Errorf("decrypting machine key: %w", err)
|
|
|
|
}
|
2021-10-25 22:50:15 +00:00
|
|
|
cipher, err = s.MixDH(controlKey, machineKey)
|
|
|
|
if err != nil {
|
2021-06-27 08:21:53 +00:00
|
|
|
return nil, fmt.Errorf("computing ss: %w", err)
|
|
|
|
}
|
2021-10-25 22:50:15 +00:00
|
|
|
if err := s.DecryptAndHash(cipher, nil, init.Tag()); err != nil {
|
2021-06-27 08:21:53 +00:00
|
|
|
return nil, fmt.Errorf("decrypting initiation tag: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// <- e, ee, se
|
2021-07-29 18:59:40 +00:00
|
|
|
resp := mkResponseMessage()
|
2021-06-27 08:21:53 +00:00
|
|
|
controlEphemeral := key.NewPrivate()
|
|
|
|
controlEphemeralPub := controlEphemeral.Public()
|
2021-07-29 18:59:40 +00:00
|
|
|
copy(resp.EphemeralPub(), controlEphemeralPub[:])
|
2021-06-27 08:21:53 +00:00
|
|
|
s.MixHash(controlEphemeralPub[:])
|
2021-10-25 22:50:15 +00:00
|
|
|
if _, err := s.MixDH(controlEphemeral, machineEphemeralPub); err != nil {
|
2021-06-27 08:21:53 +00:00
|
|
|
return nil, fmt.Errorf("computing ee: %w", err)
|
|
|
|
}
|
2021-10-25 22:50:15 +00:00
|
|
|
cipher, err = s.MixDH(controlEphemeral, machineKey)
|
|
|
|
if err != nil {
|
2021-06-27 08:21:53 +00:00
|
|
|
return nil, fmt.Errorf("computing se: %w", err)
|
|
|
|
}
|
2021-10-25 22:50:15 +00:00
|
|
|
s.EncryptAndHash(cipher, resp.Tag(), nil) // empty message payload
|
2021-06-27 08:21:53 +00: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 18:38:10 +00:00
|
|
|
c := &Conn{
|
2021-06-27 08:21:53 +00:00
|
|
|
conn: conn,
|
2021-07-29 18:59:40 +00:00
|
|
|
version: protocolVersion,
|
2021-06-27 08:21:53 +00:00
|
|
|
peer: machineKey,
|
|
|
|
handshakeHash: s.h,
|
|
|
|
tx: txState{
|
|
|
|
cipher: c2,
|
|
|
|
},
|
|
|
|
rx: rxState{
|
|
|
|
cipher: c1,
|
|
|
|
},
|
2021-07-30 18:38:10 +00:00
|
|
|
}
|
|
|
|
return c, nil
|
2021-06-27 08:21:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// symmetricState is the SymmetricState object from the Noise protocol
|
|
|
|
// spec. It contains all the symmetric cipher state of an in-flight
|
|
|
|
// handshake. Field names match the variable names in the spec.
|
|
|
|
type symmetricState struct {
|
2021-07-30 18:38:10 +00:00
|
|
|
finished bool
|
|
|
|
|
2021-06-27 08:21:53 +00:00
|
|
|
h [blake2s.Size]byte
|
|
|
|
ck [blake2s.Size]byte
|
|
|
|
|
|
|
|
mixer hash.Hash // for updating h
|
|
|
|
}
|
|
|
|
|
2021-07-30 18:38:10 +00:00
|
|
|
func (s *symmetricState) checkFinished() {
|
|
|
|
if s.finished {
|
|
|
|
panic("attempted to use symmetricState after Split was called")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-27 08:21:53 +00:00
|
|
|
// Initialize sets s to the initial handshake state, prior to
|
|
|
|
// processing any Noise messages.
|
|
|
|
func (s *symmetricState) Initialize() {
|
2021-07-30 18:38:10 +00:00
|
|
|
s.checkFinished()
|
2021-06-27 08:21:53 +00:00
|
|
|
if s.mixer != nil {
|
|
|
|
panic("symmetricState cannot be reused")
|
|
|
|
}
|
|
|
|
s.h = blake2s.Sum256([]byte(protocolName))
|
|
|
|
s.ck = s.h
|
|
|
|
s.mixer = newBLAKE2s()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MixHash updates s.h to be BLAKE2s(s.h || data), where || is
|
|
|
|
// concatenation.
|
|
|
|
func (s *symmetricState) MixHash(data []byte) {
|
2021-07-30 18:38:10 +00:00
|
|
|
s.checkFinished()
|
2021-06-27 08:21:53 +00:00
|
|
|
s.mixer.Reset()
|
|
|
|
s.mixer.Write(s.h[:])
|
|
|
|
s.mixer.Write(data)
|
2021-07-30 18:38:10 +00:00
|
|
|
s.mixer.Sum(s.h[:0])
|
2021-06-27 08:21:53 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 22:50:15 +00: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 08:21:53 +00: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-25 22:50:15 +00:00
|
|
|
func (s *symmetricState) MixDH(priv key.Private, pub key.Public) (*singleUseCHP, error) {
|
2021-07-30 18:38:10 +00:00
|
|
|
s.checkFinished()
|
2021-06-27 08:21:53 +00:00
|
|
|
keyData, err := curve25519.X25519(priv[:], pub[:])
|
|
|
|
if err != nil {
|
2021-10-25 22:50:15 +00:00
|
|
|
return nil, fmt.Errorf("computing X25519: %w", err)
|
2021-06-27 08:21:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r := hkdf.New(newBLAKE2s, keyData, s.ck[:], nil)
|
|
|
|
if _, err := io.ReadFull(r, s.ck[:]); err != nil {
|
2021-10-25 22:50:15 +00:00
|
|
|
return nil, fmt.Errorf("extracting ck: %w", err)
|
2021-06-27 08:21:53 +00:00
|
|
|
}
|
2021-10-25 22:50:15 +00: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 08:21:53 +00:00
|
|
|
}
|
2021-10-25 22:50:15 +00:00
|
|
|
return newSingleUseCHP(k), nil
|
2021-06-27 08:21:53 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 23:15:32 +00:00
|
|
|
// EncryptAndHash encrypts plaintext into ciphertext (which must be
|
2021-10-25 22:50:15 +00: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 18:38:10 +00:00
|
|
|
s.checkFinished()
|
2021-07-01 23:15:32 +00:00
|
|
|
if len(ciphertext) != len(plaintext)+poly1305.TagSize {
|
|
|
|
panic("ciphertext is wrong size for given plaintext")
|
|
|
|
}
|
2021-10-25 22:50:15 +00:00
|
|
|
ret := cipher.Seal(ciphertext[:0], plaintext, s.h[:])
|
2021-06-27 08:21:53 +00:00
|
|
|
s.MixHash(ret)
|
|
|
|
}
|
|
|
|
|
2021-07-01 23:15:32 +00:00
|
|
|
// DecryptAndHash decrypts the given ciphertext into plaintext (which
|
|
|
|
// must be the correct size to hold the decrypted ciphertext) using
|
2021-10-25 22:50:15 +00: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 18:38:10 +00:00
|
|
|
s.checkFinished()
|
2021-07-01 23:15:32 +00:00
|
|
|
if len(ciphertext) != len(plaintext)+poly1305.TagSize {
|
|
|
|
panic("plaintext is wrong size for given ciphertext")
|
|
|
|
}
|
2021-10-25 22:50:15 +00:00
|
|
|
if _, err := cipher.Open(plaintext[:0], ciphertext, s.h[:]); err != nil {
|
2021-07-01 23:15:32 +00:00
|
|
|
return err
|
2021-06-27 08:21:53 +00:00
|
|
|
}
|
|
|
|
s.MixHash(ciphertext)
|
2021-07-01 23:15:32 +00:00
|
|
|
return nil
|
2021-06-27 08:21:53 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 23:18:04 +00:00
|
|
|
// Split returns two ChaCha20Poly1305 ciphers with keys derived from
|
2021-07-30 18:38:10 +00:00
|
|
|
// the current handshake state. Methods on s cannot be used again
|
|
|
|
// after calling Split.
|
2021-06-27 08:21:53 +00:00
|
|
|
func (s *symmetricState) Split() (c1, c2 cipher.AEAD, err error) {
|
2021-07-30 18:38:10 +00:00
|
|
|
s.finished = true
|
|
|
|
|
2021-06-27 08:21:53 +00: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 18:38:10 +00:00
|
|
|
panic(err)
|
2021-06-27 08:21:53 +00: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 18:38:10 +00:00
|
|
|
panic(err)
|
2021-06-27 08:21:53 +00:00
|
|
|
}
|
|
|
|
return aead
|
|
|
|
}
|
2021-10-25 22:50:15 +00: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)
|
|
|
|
}
|