cryptography

package
v0.9.5 Latest Latest
Warning

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

Go to latest
Published: May 4, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

SPDX-License-Identifier: Apache-2.0 Copyright (c) 2024-2026 Quad4.io

SPDX-License-Identifier: Apache-2.0 Copyright (c) 2024-2026 Quad4.io

SPDX-License-Identifier: Apache-2.0 Copyright (c) 2024-2026 Quad4.io

Package cryptography is the single integration point for cryptographic primitives used across Reticulum-Go. Application and library code should call the exported functions here (or types derived from them) rather than importing lower-level packages such as crypto/ed25519 or curve25519 directly, so algorithms and test doubles can be changed in one place.

Extension model:

On-wire formats (key sizes, packet layouts, hash truncation) are defined elsewhere and assume the default provider’s behavior. Replacing the provider without updating those formats will break interoperability; treat provider swaps as coordinated protocol changes unless you control all peers.

Hardware signing (HSM, PKCS#11, cloud KMS) can integrate via Ed25519Signer: use NewSoftwareEd25519Signer for in-memory seeds, or NewEd25519SignerFromCryptoSigner to wrap a standard crypto.Signer that performs Ed25519. Identity wiring is in package identity (git.quad4.io/Networks/Reticulum-Go/pkg/identity.NewIdentityWithSigner).

SPDX-License-Identifier: Apache-2.0 Copyright (c) 2024-2026 Quad4.io

SPDX-License-Identifier: Apache-2.0 Copyright (c) 2024-2026 Quad4.io

SPDX-License-Identifier: Apache-2.0 Copyright (c) 2024-2026 Quad4.io

SPDX-License-Identifier: Apache-2.0 Copyright (c) 2024-2026 Quad4.io

SPDX-License-Identifier: Apache-2.0 Copyright (c) 2024-2026 Quad4.io

SPDX-License-Identifier: Apache-2.0 Copyright (c) 2024-2026 Quad4.io

SPDX-License-Identifier: Apache-2.0 Copyright (c) 2024-2026 Quad4.io

SPDX-License-Identifier: Apache-2.0 Copyright (c) 2024-2026 Quad4.io

SPDX-License-Identifier: Apache-2.0 Copyright (c) 2024-2026 Quad4.io

Index

Constants

View Source
const (
	SHA256Size    = 32
	AES256KeySize = 32
	// IdentityKeyMaterialSize is the HKDF output for identity encrypt/decrypt (HMAC key + AES key).
	IdentityKeyMaterialSize = SHA256Size + AES256KeySize
)
View Source
const Ed25519SignatureSize = ed25519.SignatureSize

Ed25519SignatureSize is the byte length of an Ed25519 signature.

Variables

This section is empty.

Functions

func ComputeHMAC

func ComputeHMAC(key, message []byte) []byte

ComputeHMAC returns HMAC-SHA256(key, message).

func DecryptAES256CBC

func DecryptAES256CBC(key, ciphertext []byte) ([]byte, error)

DecryptAES256CBC decrypts data using AES-256 in CBC mode. It assumes the IV is prepended to the ciphertext.

func DeriveIdentityKeyMaterial added in v0.9.0

func DeriveIdentityKeyMaterial(sharedSecret, salt, context []byte) ([]byte, error)

DeriveIdentityKeyMaterial derives 64 bytes of key material for identity-layer encryption using the active CryptoProvider.

func DeriveKey

func DeriveKey(secret, salt, info []byte, length int) ([]byte, error)

DeriveKey performs HKDF-SHA256 expansion (non-RFC 5869 extract; matches legacy use).

func DeriveSharedSecret

func DeriveSharedSecret(privateKey, peerPublicKey []byte) ([]byte, error)

DeriveSharedSecret performs an X25519 scalar multiplication.

func EncryptAES256CBC

func EncryptAES256CBC(key, plaintext []byte) ([]byte, error)

EncryptAES256CBC encrypts data using AES-256 in CBC mode. The IV is prepended to the ciphertext.

func ExpandEncryptWithHMACKeyMaterial added in v0.9.0

func ExpandEncryptWithHMACKeyMaterial(key32 []byte) (hmacKey, aesKey []byte, err error)

ExpandEncryptWithHMACKeyMaterial derives 32-byte HMAC and 32-byte AES keys from a 32-byte input using HKDF-SHA256 (RFC 5869).

func GenerateAES256Key

func GenerateAES256Key() ([]byte, error)

func GenerateHMACKey

func GenerateHMACKey(size int) ([]byte, error)

func GenerateKeyPair

func GenerateKeyPair() (privateKey, publicKey []byte, err error)

GenerateKeyPair creates a random X25519 key pair.

func GenerateSigningKeyPair

func GenerateSigningKeyPair() (ed25519.PublicKey, ed25519.PrivateKey, error)

GenerateSigningKeyPair creates a random Ed25519 key pair.

func GetBasepoint

func GetBasepoint() []byte

GetBasepoint returns the standard Curve25519 basepoint.

func Hash

func Hash(data []byte) []byte

Hash returns the SHA-256 digest of data.

func PublicKeyFromPrivate added in v0.9.0

func PublicKeyFromPrivate(privateKey []byte) ([]byte, error)

PublicKeyFromPrivate derives the X25519 public key from a private key.

func RemovePKCS7Padding added in v0.9.0

func RemovePKCS7Padding(plaintext []byte) ([]byte, error)

RemovePKCS7Padding validates and removes PKCS#7 padding without early exit on the first mismatched byte (reduces padding-oracle surface when used after MAC verify).

func SetProvider added in v0.9.0

func SetProvider(p CryptoProvider)

SetProvider replaces the global crypto implementation. Passing nil restores the default stdlib-backed provider.

func Sign

func Sign(privateKey ed25519.PrivateKey, message []byte) []byte

Sign signs message with privateKey.

func ValidateHMAC

func ValidateHMAC(key, message, messageHMAC []byte) bool

ValidateHMAC performs a constant-time comparison of the MAC.

func Verify

func Verify(publicKey ed25519.PublicKey, message, signature []byte) bool

Verify reports whether signature is valid for message under publicKey.

Types

type CryptoProvider added in v0.9.0

type CryptoProvider interface {
	GenerateKeyPair() (privateKey, publicKey []byte, err error)
	PublicKeyFromPrivate(privateKey []byte) ([]byte, error)
	DeriveSharedSecret(privateKey, peerPublicKey []byte) ([]byte, error)
	GetBasepoint() []byte

	GenerateSigningKeyPair() (ed25519.PublicKey, ed25519.PrivateKey, error)
	Sign(privateKey ed25519.PrivateKey, message []byte) []byte
	Verify(publicKey ed25519.PublicKey, message, signature []byte) bool

	EncryptAES256CBC(key, plaintext []byte) ([]byte, error)
	DecryptAES256CBC(key, ciphertext []byte) ([]byte, error)

	ComputeHMAC(key, message []byte) []byte
	ValidateHMAC(key, message, messageHMAC []byte) bool

	Hash(data []byte) []byte
	DeriveKey(secret, salt, info []byte, length int) ([]byte, error)
	ExpandEncryptWithHMACKeyMaterial(key32 []byte) (hmacKey, aesKey []byte, err error)
	DeriveIdentityKeyMaterial(sharedSecret, salt, context []byte) ([]byte, error)
}

CryptoProvider abstracts the cryptographic primitives used by Reticulum. The default implementation matches the on-wire protocol (X25519, Ed25519, AES-256-CBC, HMAC-SHA256, HKDF-SHA256). Call SetProvider to substitute implementations for testing or future algorithm agility; callers must preserve wire compatibility unless all peers are upgraded together.

func ActiveProvider added in v0.9.0

func ActiveProvider() CryptoProvider

ActiveProvider returns the current CryptoProvider (for tests or advanced use).

type Ed25519Signer added in v0.9.0

type Ed25519Signer interface {
	Sign(message []byte) ([]byte, error)
	Ed25519PublicKey() ed25519.PublicKey
}

Ed25519Signer signs Reticulum identity material with Ed25519. Use a software implementation from seed (NewSoftwareEd25519Signer) or wrap an HSM-backed crypto.Signer with NewEd25519SignerFromCryptoSigner. The public key must be the 32-byte Ed25519 key used in identity announcements.

func NewEd25519SignerFromCryptoSigner added in v0.9.0

func NewEd25519SignerFromCryptoSigner(s crypto.Signer) (Ed25519Signer, error)

NewEd25519SignerFromCryptoSigner wraps an Ed25519 crypto.Signer, such as PKCS#11 or a hardware key exposed via the standard library interface.

func NewSoftwareEd25519Signer added in v0.9.0

func NewSoftwareEd25519Signer(seed []byte) (Ed25519Signer, error)

NewSoftwareEd25519Signer returns a signer that uses an in-memory Ed25519 seed (same semantics as identity file bytes 32:64).

Jump to

Keyboard shortcuts

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