slhdsa

package
v1.6.4 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2026 License: BSD-3-Clause Imports: 18 Imported by: 3

Documentation

Overview

Package slhdsa provides Stateless Hash-based Digital Signature Algorithm.

This package is compliant with FIPS 205 and the ID represents the following parameter sets:

Category 1

Category 3

Category 5

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrContext  = errors.New("sign/slhdsa: context is larger than 255 bytes")
	ErrMsgLen   = errors.New("sign/slhdsa: invalid message length")
	ErrParam    = errors.New("sign/slhdsa: invalid SLH-DSA parameter")
	ErrPreHash  = errors.New("sign/slhdsa: invalid prehash function")
	ErrSigParse = errors.New("sign/slhdsa: failed to decode the signature")
	ErrTree     = errors.New("sign/slhdsa: invalid tree height or tree index")
	ErrWriting  = errors.New("sign/slhdsa: failed to write to a hash function")
)

Functions

func GenerateKey

func GenerateKey(
	random io.Reader, id ID,
) (pub PublicKey, priv PrivateKey, err error)

GenerateKey returns a pair of keys using the parameter set specified. It returns an error if it fails reading from the random source.

func SignDeterministic

func SignDeterministic(
	priv *PrivateKey, message *Message, context []byte,
) (signature []byte, err error)

SignDeterministic returns the signature of the message with the specified context.

func SignRandomized

func SignRandomized(
	priv *PrivateKey, random io.Reader, message *Message, context []byte,
) (signature []byte, err error)

SignRandomized returns a random signature of the message with the specified context. It returns an error if it fails reading from the random source.

func Verify

func Verify(key *PublicKey, message *Message, signature, context []byte) bool

Verify returns true if the signature of the message with the specified context is valid.

Types

type ID

type ID byte

ID identifies the supported parameter sets of SLH-DSA. Note that the zero value is not a valid identifier.

const (
	SHA2_128s  ID = iota + 1 // SLH-DSA-SHA2-128s
	SHAKE_128s               // SLH-DSA-SHAKE-128s
	SHA2_128f                // SLH-DSA-SHA2-128f
	SHAKE_128f               // SLH-DSA-SHAKE-128f
	SHA2_192s                // SLH-DSA-SHA2-192s
	SHAKE_192s               // SLH-DSA-SHAKE-192s
	SHA2_192f                // SLH-DSA-SHA2-192f
	SHAKE_192f               // SLH-DSA-SHAKE-192f
	SHA2_256s                // SLH-DSA-SHA2-256s
	SHAKE_256s               // SLH-DSA-SHAKE-256s
	SHA2_256f                // SLH-DSA-SHA2-256f
	SHAKE_256f               // SLH-DSA-SHAKE-256f

)

func IDByName

func IDByName(name string) (ID, error)

IDByName returns the ID that corresponds to the given name, or an error if no parameter set was found. See ID documentation for the specific names of each parameter set. Names are case insensitive.

Example:

IDByName("SLH-DSA-SHAKE-256s") // returns (SHAKESmall256, nil)

func (ID) IsValid

func (id ID) IsValid() bool

IsValid returns true if the parameter set is supported.

func (ID) Scheme

func (id ID) Scheme() sign.Scheme

func (ID) String

func (id ID) String() string

type Message

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

Message wraps a message for signing.

func NewMessage

func NewMessage(msg []byte) *Message

For pure signatures, use NewMessage to pass the message to be signed. For pre-hashed signatures, use PreHash to hash the message first, and then use PreHash.BuildMessage to get a Message to be signed.

type PreHash

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

PreHash is a helper for hashing a message before signing. It implements the io.Writer interface, so the message can be provided in chunks before calling the SignDeterministic, SignRandomized, or Verify functions. Pre-hash must not be used for generating pure signatures.

func NewPreHashWithHash

func NewPreHashWithHash(h crypto.Hash) (*PreHash, error)

NewPreHashWithHash is used to prehash messages using either the SHA2 or SHA3 hash functions. Returns ErrPreHash if the function is not supported.

func NewPreHashWithXof

func NewPreHashWithXof(x xof.ID) (*PreHash, error)

NewPreHashWithXof is used to prehash messages using either SHAKE-128 or SHAKE-256. Returns ErrPreHash if the function is not supported.

func (*PreHash) BuildMessage

func (ph *PreHash) BuildMessage() (*Message, error)

BuildMessage returns a Message for signing, and resets the writer.

func (*PreHash) Reset

func (ph *PreHash) Reset()

func (*PreHash) Write

func (ph *PreHash) Write(b []byte) (int, error)

type PrivateKey

type PrivateKey struct {
	ID
	// contains filtered or unexported fields
}

PrivateKey stores a private key of the SLH-DSA scheme. It implements the crypto.Signer and crypto.PrivateKey interfaces. For serialization, it also implements cryptobyte.MarshalingValue, encoding.BinaryMarshaler, and encoding.BinaryUnmarshaler.

func (PrivateKey) Equal

func (k PrivateKey) Equal(x crypto.PrivateKey) bool

func (PrivateKey) Marshal

func (k PrivateKey) Marshal(b *cryptobyte.Builder) error

Marshal serializes the key using a cryptobyte.Builder.

func (PrivateKey) MarshalBinary

func (k PrivateKey) MarshalBinary() ([]byte, error)

func (PrivateKey) Public

func (k PrivateKey) Public() crypto.PublicKey

func (PrivateKey) PublicKey

func (k PrivateKey) PublicKey() (pub PublicKey)

func (PrivateKey) Sign

func (k PrivateKey) Sign(
	random io.Reader, message []byte, _ crypto.SignerOpts,
) (signature []byte, err error)

PrivateKey.Sign returns a randomized signature of the message with an empty context. Any parameter passed in crypto.SignerOpts is discarded. It returns an error if it fails reading from the random source.

func (*PrivateKey) Unmarshal

func (k *PrivateKey) Unmarshal(s *cryptobyte.String) bool

Unmarshal recovers a PrivateKey from a cryptobyte.String. Caller must specify the private key's ID in advance. Example:

key := PrivateKey{ID: SHA2Small192}
key.Unmarshal(str) // returns true

func (*PrivateKey) UnmarshalBinary

func (k *PrivateKey) UnmarshalBinary(b []byte) error

UnmarshalBinary recovers a PrivateKey from a slice of bytes. Caller must specify the private key's ID in advance. Example:

key := PrivateKey{ID: SHA2Small192}
key.UnmarshalBinary(bytes) // returns nil

type PublicKey

type PublicKey struct {
	ID
	// contains filtered or unexported fields
}

PublicKey stores a public key of the SLH-DSA scheme. It implements the crypto.PublicKey interface. For serialization, it also implements cryptobyte.MarshalingValue, encoding.BinaryMarshaler, and encoding.BinaryUnmarshaler.

func (PublicKey) Equal

func (k PublicKey) Equal(x crypto.PublicKey) bool

func (PublicKey) Marshal

func (k PublicKey) Marshal(b *cryptobyte.Builder) error

Marshal serializes the key using a cryptobyte.Builder.

func (PublicKey) MarshalBinary

func (k PublicKey) MarshalBinary() ([]byte, error)

func (*PublicKey) Unmarshal

func (k *PublicKey) Unmarshal(s *cryptobyte.String) bool

Unmarshal recovers a PublicKey from a cryptobyte.String. Caller must specify the public key's ID in advance. Example:

key := PublicKey{ID: SHA2Small192}
key.Unmarshal(str) // returns true

func (*PublicKey) UnmarshalBinary

func (k *PublicKey) UnmarshalBinary(b []byte) error

UnmarshalBinary recovers a PublicKey from a slice of bytes. Caller must specify the public key's ID in advance. Example:

key := PublicKey{ID: SHA2Small192}
key.UnmarshalBinary(bytes) // returns nil

Jump to

Keyboard shortcuts

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