core

package module
v0.0.0-...-3b2f4c1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2017 License: MIT Imports: 16 Imported by: 0

README

core

Build Status

FleeGrid core library, provides PSK-AEAD encrypted connections.

Security

All commits and emails should be signed with GPG key 0xE160DAF25E30EB52

Contribution

This project is for personal usage only, contact me via unit.2b@yorha.army if you have any questions.

You MUST encrypt your email with the GPG key mentioned above and also provide your own GPG public key, or I will ignore your email.

Credits

  • A project that must not be named

License

MIT License, see LICENSE file.

Documentation

Index

Constants

View Source
const PacketMaxSize = 64 * 1024

PacketMaxSize usually 64k

View Source
const PayloadMaxSize = 0x3FFF // 16*1024 - 1

PayloadMaxSize is the maximum size of payload in bytes.

Variables

View Source
var (
	// SupportedCipherNames names of supported ciphers
	SupportedCipherNames = []string{
		"AEAD_CHACHA20_POLY1305",
		"AEAD_AES_128_GCM",
		"AEAD_AES_192_GCM",
		"AEAD_AES_256_GCM",
		"AEAD_DUMMY",
	}
	// SupportedCiphers array of supported ciphers
	SupportedCiphers = map[string]*CipherDescriptor{
		"AEAD_CHACHA20_POLY1305": {
			KeySize:       32,
			CipherFactory: NewChapoCipher,
		},
		"AEAD_AES_128_GCM": {
			KeySize:       16,
			CipherFactory: NewAESGCMCipher,
		},
		"AEAD_AES_192_GCM": {
			KeySize:       24,
			CipherFactory: NewAESGCMCipher,
		},
		"AEAD_AES_256_GCM": {
			KeySize:       32,
			CipherFactory: NewAESGCMCipher,
		},
		"AEAD_DUMMY": {
			KeySize:       32,
			CipherFactory: NewDummyCipher,
		},
	}
)
View Source
var (
	// ErrBadURL URL is mal-formatted
	ErrBadURL = errors.New("bad url")
	// ErrBadScheme URL scheme is not 'flee'
	ErrBadScheme = errors.New("url scheme is not '" + FleeScheme + "'")
	// ErrBadCipher Cipher is not in SupportedCipherNames
	ErrBadCipher = errors.New("cipher is not supported, only " + strings.Join(SupportedCipherNames, ",") + " are supported")
	// ErrMissingPasswd password is missing from url
	ErrMissingPasswd = errors.New("password is not specified in url")
	// ErrMissingAddress host:port is missing from url
	ErrMissingAddress = errors.New("host:port is not specified in url")
)
View Source
var ErrPacketTooShort = errors.New("short packet")

ErrPacketTooShort packet is too short for a valid encrypted packet

View Source
var FleeScheme = "flee"

FleeScheme URL scheme for FleeGrid

View Source
var HKDFInfo = []byte("ss-subkey")

HKDFInfo pre-defined HKDF info

Functions

func DeriveMasterKey

func DeriveMasterKey(password string, keyLen int) []byte

DeriveMasterKey derive master key from password string

func DeriveSubkey

func DeriveSubkey(master, salt, out []byte)

DeriveSubkey expand password with HKDF_SHA1

func OpenPacket

func OpenPacket(dst, src []byte, ciph Cipher) ([]byte, error)

OpenPacket decrypt packet

func ResolveCipherName

func ResolveCipherName(cipher string) (string, bool)

ResolveCipherName resolve cipher alises and check if cipher is supported

func SealPacket

func SealPacket(dst, plain []byte, ciph Cipher) ([]byte, error)

SealPacket encrypts packet

Types

type AESGCMCipher

type AESGCMCipher struct {
	// contains filtered or unexported fields
}

AESGCMCipher is for AES_XXX_GCM

func (*AESGCMCipher) CreateAEAD

func (c *AESGCMCipher) CreateAEAD(salt []byte) (cipher.AEAD, error)

CreateAEAD for AESGCM

func (*AESGCMCipher) KeySize

func (c *AESGCMCipher) KeySize() int

KeySize for AES-GCM

func (*AESGCMCipher) NonceSize

func (c *AESGCMCipher) NonceSize() int

NonceSize for AES-GCM

func (*AESGCMCipher) SaltSize

func (c *AESGCMCipher) SaltSize() int

SaltSize for AES-GCM

type BadKeyLengthError

type BadKeyLengthError int

BadKeyLengthError error when key length is bad

func (BadKeyLengthError) Error

func (e BadKeyLengthError) Error() string

type ChapoCipher

type ChapoCipher struct {
	// contains filtered or unexported fields
}

ChapoCipher is for ChaCha20-Poly1305

func (*ChapoCipher) CreateAEAD

func (c *ChapoCipher) CreateAEAD(salt []byte) (cipher.AEAD, error)

CreateAEAD for ChaCha20-Poly1305

func (*ChapoCipher) KeySize

func (c *ChapoCipher) KeySize() int

KeySize for ChaCha20-Poly1305

func (*ChapoCipher) NonceSize

func (c *ChapoCipher) NonceSize() int

NonceSize for ChaCha20-Poly1305

func (*ChapoCipher) SaltSize

func (c *ChapoCipher) SaltSize() int

SaltSize for ChaCha20-Poly1305

type Cipher

type Cipher interface {
	// size of key
	KeySize() int
	// size of salt
	SaltSize() int
	// size of nonce
	NonceSize() int
	// create a AEAD with given salt
	CreateAEAD(salt []byte) (cipher.AEAD, error)
}

Cipher represents a AEAD cipher

func NewAESGCMCipher

func NewAESGCMCipher(key []byte, size int) (Cipher, error)

NewAESGCMCipher create a new AES_XXX_GCM cipher one of 16, 24, or 32 to select AES-128/196/256-GCM.

func NewChapoCipher

func NewChapoCipher(key []byte, size int) (Cipher, error)

NewChapoCipher create a new ChapoCipher base on a key string

func NewCipher

func NewCipher(name, password string) (Cipher, error)

NewCipher create a new Cipher with name and password

func NewDummyCipher

func NewDummyCipher(b []byte, k int) (Cipher, error)

NewDummyCipher creates a new dummy cipher

type CipherDescriptor

type CipherDescriptor struct {
	KeySize       int
	CipherFactory func([]byte, int) (Cipher, error)
}

CipherDescriptor describes ciphers

type Config

type Config struct {
	// full address of ss protocol, for both server and client
	Address string
	// AEAD cipher, see https://www.iana.org/assignments/aead-parameters/aead-parameters.xhtml for names
	// default to "AEAD_CHACHA20_POLY1305"
	Cipher string
	// password
	Passwd string
}

Config represents a basic configuration with address, cipher and password

func ParseConfigFromURL

func ParseConfigFromURL(urlstr string) (*Config, error)

ParseConfigFromURL decode url string to Config Format:

flee://CIPHER:PASSWORD@ADDRESS:PORT

type DummyCipher

type DummyCipher struct{}

DummyCipher a non-encryption cipher for debug purpose

func (*DummyCipher) CreateAEAD

func (d *DummyCipher) CreateAEAD(salt []byte) (cipher.AEAD, error)

CreateAEAD for DummyCipher

func (*DummyCipher) KeySize

func (d *DummyCipher) KeySize() int

KeySize for DummyCipher

func (*DummyCipher) NonceSize

func (d *DummyCipher) NonceSize() int

NonceSize for DummyCipher

func (*DummyCipher) SaltSize

func (d *DummyCipher) SaltSize() int

SaltSize for DummyCipher

type PacketConn

type PacketConn struct {
	net.PacketConn
	Cipher
	sync.Mutex
	// contains filtered or unexported fields
}

PacketConn wraps a net.PacketConn with Cipher

func NewPacketConn

func NewPacketConn(conn net.PacketConn, c Cipher) (*PacketConn, error)

NewPacketConn wraps a net.PacketConn with Cipher

func (*PacketConn) ReadFrom

func (c *PacketConn) ReadFrom(b []byte) (int, net.Addr, error)

ReadFrom reads from underlaying net.PacketConn and decrypts

func (*PacketConn) WriteTo

func (c *PacketConn) WriteTo(b []byte, addr net.Addr) (int, error)

WriteTo encrypts bytes and writes to underlaying net.PacketConn

type StreamConn

type StreamConn struct {
	net.Conn
	Cipher
	// contains filtered or unexported fields
}

StreamConn wraps a net.Conn with automatically encryption and decryption

func NewStreamConn

func NewStreamConn(conn net.Conn, c Cipher) *StreamConn

NewStreamConn create a new StreamConn

func (*StreamConn) Read

func (c *StreamConn) Read(b []byte) (int, error)

func (*StreamConn) ReadFrom

func (c *StreamConn) ReadFrom(r io.Reader) (int64, error)

ReadFrom see StreamWriter#ReadFrom

func (*StreamConn) Write

func (c *StreamConn) Write(b []byte) (int, error)

func (*StreamConn) WriteTo

func (c *StreamConn) WriteTo(w io.Writer) (int64, error)

WriteTo see StreamReader#WriteTo

type StreamReader

type StreamReader struct {
	io.Reader
	cipher.AEAD
	// contains filtered or unexported fields
}

StreamReader reads a encrypted io.Reader and decrypt

func NewStreamReader

func NewStreamReader(r io.Reader, a cipher.AEAD) *StreamReader

NewStreamReader Create a New StreamReader

func (*StreamReader) Read

func (r *StreamReader) Read(b []byte) (int, error)

Read reads from the embedded io.Reader, decrypts and writes to b.

func (*StreamReader) WriteTo

func (r *StreamReader) WriteTo(w io.Writer) (n int64, err error)

WriteTo reads from underlaying io.Reader, write everything to io.Writer

type StreamWriter

type StreamWriter struct {
	io.Writer
	cipher.AEAD
	// contains filtered or unexported fields
}

StreamWriter encrypt data and write to underlying io.Writer

func NewStreamWriter

func NewStreamWriter(w io.Writer, a cipher.AEAD) *StreamWriter

NewStreamWriter create a StreamWriter

func (*StreamWriter) ReadFrom

func (w *StreamWriter) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom encrypt and write bytes from a io.Reader

func (*StreamWriter) Write

func (w *StreamWriter) Write(b []byte) (int, error)

Write encrypt and write bytes

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL