security

package
v0.0.0-...-37b37ea Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package security provides constant-time operations for cryptographic security

IMPORTANT: This implementation provides defense-in-depth against timing attacks. For maximum security in production: 1. Use hardware that supports constant-time instructions (e.g., ARM64 crypto extensions) 2. Consider using assembly-optimized libraries for critical paths 3. Run on dedicated hardware without SMT/hyperthreading for high-value operations 4. Use blinding/masking for all secret-dependent operations

Package security provides security utilities for protecting sensitive data

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidThreshold is returned when threshold parameters are invalid
	ErrInvalidThreshold = errors.New("invalid threshold: must satisfy 1 <= t <= n")

	// ErrInvalidPartyID is returned when party ID is out of range
	ErrInvalidPartyID = errors.New("invalid party ID: must be in range [0, n)")

	// ErrInvalidPartyCount is returned when party count is too small
	ErrInvalidPartyCount = errors.New("invalid party count: must be >= 2")

	// ErrInvalidRange is returned when a value is outside expected range
	ErrInvalidRange = errors.New("value out of valid range")
)

Functions

func ConstantTimeArrayAccess

func ConstantTimeArrayAccess(array []*big.Int, index int) *big.Int

ConstantTimeArrayAccess accesses an array element in constant time This prevents cache-timing attacks based on array access patterns Returns array[index] if index is valid, zero otherwise

func ConstantTimeBigIntEqual

func ConstantTimeBigIntEqual(a, b *big.Int) int

ConstantTimeBigIntEqual compares two big.Ints in constant time Returns 1 if equal, 0 otherwise

func ConstantTimeByteEq

func ConstantTimeByteEq(a, b uint8) int

ConstantTimeByteEq returns 1 if a == b and 0 otherwise

func ConstantTimeBytesCopy

func ConstantTimeBytesCopy(dst, src []byte)

ConstantTimeBytesCopy copies bytes in constant time

func ConstantTimeCompare

func ConstantTimeCompare(a, b []byte) bool

ConstantTimeCompare compares two byte slices in constant time Returns true if they are equal, false otherwise

func ConstantTimeCondSwap

func ConstantTimeCondSwap(swap int, a, b *big.Int) (*big.Int, *big.Int)

ConstantTimeCondSwap conditionally swaps two big.Ints based on condition bit If swap == 1, swaps a and b; if swap == 0, leaves unchanged Uses XOR-based swap to prevent branch prediction leaks

func ConstantTimeEq

func ConstantTimeEq(x, y int32) int

ConstantTimeEq returns 1 if x == y and 0 otherwise

func ConstantTimeGreater

func ConstantTimeGreater(a, b int) int

ConstantTimeGreater returns 1 if a > b, 0 otherwise (constant-time) Both a and b must be in range [0, 2^31)

func ConstantTimeIsNonZero

func ConstantTimeIsNonZero(x *big.Int) int

ConstantTimeIsNonZero returns 1 if x is non-zero, 0 otherwise (constant-time)

func ConstantTimeIsOdd

func ConstantTimeIsOdd(x *big.Int) int

ConstantTimeIsOdd returns 1 if x is odd, 0 if even (constant-time)

func ConstantTimeIsZero

func ConstantTimeIsZero(x *big.Int) int

ConstantTimeIsZero returns 1 if x is zero, 0 otherwise (constant-time)

func ConstantTimeLeadingZeros

func ConstantTimeLeadingZeros(x *big.Int) int

ConstantTimeLeadingZeros counts leading zeros in constant time This prevents timing leaks from bit length checks

func ConstantTimeLessOrEq

func ConstantTimeLessOrEq(x, y int) int

ConstantTimeLessOrEq returns 1 if x <= y and 0 otherwise Both x and y must be non-negative and less than 2^31

func ConstantTimeLimbsEqual

func ConstantTimeLimbsEqual(a, b *big.Int) bool

ConstantTimeLimbsEqual compares big.Int at the limb level More resistant to compiler optimization than byte comparison

func ConstantTimeModAdd

func ConstantTimeModAdd(a, b, m *big.Int) *big.Int

ConstantTimeModAdd performs constant-time modular addition result = (a + b) mod m Uses constant-time reduction to prevent timing leaks

func ConstantTimeModExp

func ConstantTimeModExp(base, exp, m *big.Int) *big.Int

ConstantTimeModExp performs constant-time modular exponentiation result = base^exp mod m Uses Montgomery ladder or fixed-window exponentiation

func ConstantTimeModInv

func ConstantTimeModInv(a, m *big.Int) *big.Int

ConstantTimeModInv performs constant-time modular inversion result = a^(-1) mod m using Extended Euclidean Algorithm Returns nil if inverse doesn't exist

Note: Go 1.20+ uses constant-time ModInverse for prime moduli For composite moduli, timing leaks may exist

func ConstantTimeModMul

func ConstantTimeModMul(a, b, m *big.Int) *big.Int

ConstantTimeModMul performs constant-time modular multiplication result = (a * b) mod m

func ConstantTimeModNeg

func ConstantTimeModNeg(x, m *big.Int) *big.Int

ConstantTimeModNeg performs constant-time modular negation result = -x mod m = m - x

func ConstantTimeModSqr

func ConstantTimeModSqr(x, m *big.Int) *big.Int

ConstantTimeModSqr performs constant-time modular squaring result = x^2 mod m Squaring can be faster than multiplication

func ConstantTimeModSub

func ConstantTimeModSub(a, b, m *big.Int) *big.Int

ConstantTimeModSub performs constant-time modular subtraction result = (a - b) mod m

func ConstantTimeMontgomeryLadder

func ConstantTimeMontgomeryLadder(k *big.Int, scalarMult func(*big.Int) *big.Int) *big.Int

ConstantTimeMontgomeryLadder performs constant-time scalar multiplication This is used for ECC operations: result = k * P The Montgomery ladder always performs the same operations regardless of k

func ConstantTimeSelect

func ConstantTimeSelect(v int, x, y *big.Int) *big.Int

ConstantTimeSelect returns x if v == 1 and y if v == 0 v must be 0 or 1, operation is constant-time This is critical for preventing branch-based timing attacks

func ConstantTimeSelectBytes

func ConstantTimeSelectBytes(v int, x, y []byte) []byte

ConstantTimeSelectBytes returns x if v == 1 and y if v == 0 The value v must be 0 or 1. This function is constant-time.

func GenerateRandomScalar

func GenerateRandomScalar(max *big.Int) (*big.Int, error)

GenerateRandomScalar generates a cryptographically secure random scalar in range [1, max). This is a wrapper around pkg/crypto/rand for convenience

func MaskBigInt

func MaskBigInt(x, m *big.Int) (masked, mask *big.Int, err error)

MaskBigInt masks a big.Int with random blinding factor This provides defense against differential power analysis (DPA) Returns: (masked value, mask, error)

func SanitizeInput

func SanitizeInput(input string, maxLength int) error

SanitizeInput validates and sanitizes string input Returns error if input contains null bytes or exceeds max length

func SecureCompareScalars

func SecureCompareScalars(a, b *big.Int) bool

SecureCompareScalars compares two scalars in constant time Returns true if equal, false otherwise

func SecureZero

func SecureZero(data []byte)

SecureZero securely zeros out a byte slice to prevent secrets from remaining in memory This uses a method that prevents the compiler from optimizing away the zeroing

func SecureZeroBigInt

func SecureZeroBigInt(b *big.Int)

SecureZeroBigInt securely zeros a big.Int by clearing its internal buffer Note: Go's big.Int doesn't expose internal bytes directly Best practice: Set to zero and rely on garbage collector

func SecureZeroWords

func SecureZeroWords(words []big.Word)

SecureZeroWords zeros out a Word slice in constant time This is used for clearing intermediate values in big.Int operations

func TimingSafeDiv

func TimingSafeDiv(a, b, m *big.Int) *big.Int

TimingSafeDiv performs timing-safe division using multiplicative inverse Returns a / b mod m = a * b^(-1) mod m

func UnmaskBigInt

func UnmaskBigInt(masked, mask, m *big.Int) *big.Int

UnmaskBigInt removes blinding mask from a big.Int x = (masked - mask) mod m

func ValidateNonZeroScalar

func ValidateNonZeroScalar(value *big.Int) error

ValidateNonZeroScalar checks if scalar is non-zero

func ValidatePartyID

func ValidatePartyID(partyID, parties int) error

ValidatePartyID checks if party ID is valid for given party count

func ValidateScalarInRange

func ValidateScalarInRange(value, max *big.Int) error

ValidateScalarInRange checks if scalar is in valid range [1, max)

func ValidateThreshold

func ValidateThreshold(threshold, parties int) error

ValidateThreshold checks if threshold parameters are valid Returns error if: - threshold < 1 - threshold > parties - parties < 2

Types

This section is empty.

Jump to

Keyboard shortcuts

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