keys

package
v0.0.0-...-840fb12 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2025 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ClientCHKExtraLength     = 5
	ClientCHKCryptoKeyLength = 32
)
View Source
const (
	CompressionNone     int16 = -1
	CompressionGZIP     int16 = 0
	CompressionBZIP2    int16 = 1
	CompressionLZMA     int16 = 2
	CompressionLZMA_NEW int16 = 3
)

Compression algorithms

View Source
const (
	ClientSSKCryptoKeyLength = 32
	ClientSSKExtraLength     = 5
)
View Source
const (
	// Crypto algorithms
	AlgoAESPCFB256SHA256 byte = 2
	AlgoAESCTR256SHA256  byte = 3

	// Hash algorithms
	HashSHA256 = 1

	// Key lengths
	RoutingKeyLength = 32
	CryptoKeyLength  = 32

	// Base types
	CHKBaseType uint8 = 1
	SSKBaseType uint8 = 2
)

Key constants

View Source
const (
	NodeCHKKeyLength     = 32
	NodeCHKFullKeyLength = 34 // 2 (type) + 32 (routing key)
)
View Source
const (
	NodeSSKPubKeyHashSize   = 32
	NodeSSKEHDocnameSize    = 32
	NodeSSKRoutingKeyLength = 32
	NodeSSKFullKeyLength    = 66 // 2 (type) + 32 (ehDocname) + 32 (pubKeyHash)
	NodeSSKVersion          = 1
)

Variables

View Source
var ValidKeyTypes = []string{"CHK", "SSK", "KSK", "USK"}

Valid key types

Functions

func DecryptDataCTR

func DecryptDataCTR(data, key, iv []byte) ([]byte, error)

DecryptDataCTR decrypts data using AES-256-CTR mode

func DecryptDataPCFB

func DecryptDataPCFB(data, key, iv []byte) ([]byte, error)

DecryptDataPCFB decrypts data using AES-256-PCFB mode

func DecryptHashedDocname

func DecryptHashedDocname(encryptedHashedDocname, cryptoKey []byte) ([]byte, error)

DecryptHashedDocname decrypts the encrypted hashed docname

func EncryptDataCTR

func EncryptDataCTR(data, key, iv []byte) ([]byte, error)

EncryptDataCTR encrypts data using AES-256-CTR mode

func EncryptDataPCFB

func EncryptDataPCFB(data, key, iv []byte) ([]byte, error)

EncryptDataPCFB encrypts data using AES-256-PCFB mode (Freenet's variant) PCFB is similar to CFB but with specific padding

func EncryptHashedDocname

func EncryptHashedDocname(hashedDocname, cryptoKey []byte) ([]byte, error)

EncryptHashedDocname encrypts the hashed docname with the crypto key This is used for SSK key generation: E(H(docname))

func GenerateSSKKeypair

func GenerateSSKKeypair() ([]byte, []byte, error)

GenerateSSKKeypair generates a new SSK keypair (DSA) Returns private key bytes and public key bytes

func GetSSKExtraBytes

func GetSSKExtraBytes(cryptoAlgorithm byte) []byte

GetSSKExtraBytes creates the extra bytes for an SSK

func HashData

func HashData(data []byte) []byte

HashData computes SHA256 hash of data

func HashDocname

func HashDocname(docname string) []byte

HashDocname computes SHA256 hash of a document name

func SignData

func SignData(data []byte, privKeyBytes []byte) (r, s *big.Int, err error)

SignData signs data using a DSA private key This is a simplified version for SSK signatures

func VerifySignature

func VerifySignature(data []byte, pubKeyBytes []byte, r, s *big.Int) bool

VerifySignature verifies a DSA signature

Types

type BaseKey

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

BaseKey provides common functionality for all key types

func (*BaseKey) GetRoutingKey

func (k *BaseKey) GetRoutingKey() []byte

GetRoutingKey returns the routing key

func (*BaseKey) ToNormalizedDouble

func (k *BaseKey) ToNormalizedDouble(keyType uint16) float64

ToNormalizedDouble converts key to 0.0-1.0 range for routing This matches the Java implementation's keyDigestAsNormalizedDouble

type ClientCHK

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

ClientCHK represents a client-level Content Hash Key Contains both routing information and decryption key

func NewClientCHK

func NewClientCHK(routingKey, cryptoKey []byte, cryptoAlgorithm byte, compressionAlgorithm int16, controlDocument bool) (*ClientCHK, error)

NewClientCHK creates a new ClientCHK

func NewClientCHKFromData

func NewClientCHKFromData(data []byte) (*ClientCHK, error)

NewClientCHKFromData creates a ClientCHK by encrypting data and computing its hash This is used for inserting new data into the network

func NewClientCHKFromURI

func NewClientCHKFromURI(uri *FreenetURI) (*ClientCHK, error)

NewClientCHKFromURI creates a ClientCHK from a FreenetURI

func ReadClientCHK

func ReadClientCHK(r io.Reader) (*ClientCHK, error)

ReadClientCHK deserializes a ClientCHK from a reader

func (*ClientCHK) Clone

func (c *ClientCHK) Clone() *ClientCHK

Clone creates a deep copy of this ClientCHK

func (*ClientCHK) Equals

func (c *ClientCHK) Equals(other *ClientCHK) bool

Equals checks if two ClientCHKs are equal

func (*ClientCHK) GetCompressionAlgorithm

func (c *ClientCHK) GetCompressionAlgorithm() int16

GetCompressionAlgorithm returns the compression algorithm

func (*ClientCHK) GetCryptoAlgorithm

func (c *ClientCHK) GetCryptoAlgorithm() byte

GetCryptoAlgorithm returns the crypto algorithm

func (*ClientCHK) GetCryptoKey

func (c *ClientCHK) GetCryptoKey() []byte

GetCryptoKey returns the decryption key

func (*ClientCHK) GetExtra

func (c *ClientCHK) GetExtra() []byte

GetExtra returns the extra bytes for this CHK

func (*ClientCHK) GetNodeCHK

func (c *ClientCHK) GetNodeCHK() *NodeCHK

GetNodeCHK returns the corresponding NodeCHK (for routing)

func (*ClientCHK) GetRoutingKey

func (c *ClientCHK) GetRoutingKey() []byte

GetRoutingKey returns the routing key

func (*ClientCHK) GetURI

func (c *ClientCHK) GetURI() *FreenetURI

GetURI converts this ClientCHK to a FreenetURI

func (*ClientCHK) HashCode

func (c *ClientCHK) HashCode() int

HashCode returns a hash code for this key

func (*ClientCHK) IsControlDocument

func (c *ClientCHK) IsControlDocument() bool

IsControlDocument returns whether this is a control document (metadata)

func (*ClientCHK) ToNormalizedDouble

func (c *ClientCHK) ToNormalizedDouble() float64

ToNormalizedDouble converts key to 0.0-1.0 range for routing

func (*ClientCHK) WriteBinary

func (c *ClientCHK) WriteBinary(w io.Writer) error

WriteBinary writes the key in binary format with type prefix

func (*ClientCHK) WriteRawBinaryKey

func (c *ClientCHK) WriteRawBinaryKey(w io.Writer) error

WriteRawBinaryKey serializes the key in compact binary format

type ClientSSK

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

ClientSSK represents a client-level Signed Subspace Key Contains document name, public key, and decryption information

func NewClientSSK

func NewClientSSK(docName string, pubKeyHash, extra []byte, pubKey *dsa.PublicKey, cryptoKey []byte) (*ClientSSK, error)

NewClientSSK creates a new ClientSSK

func NewClientSSKFromURI

func NewClientSSKFromURI(uri *FreenetURI) (*ClientSSK, error)

NewClientSSKFromURI creates a ClientSSK from a FreenetURI

func (*ClientSSK) Clone

func (c *ClientSSK) Clone() *ClientSSK

Clone creates a deep copy of this ClientSSK

func (*ClientSSK) Equals

func (c *ClientSSK) Equals(other *ClientSSK) bool

Equals checks if two ClientSSKs are equal

func (*ClientSSK) GetCryptoAlgorithm

func (c *ClientSSK) GetCryptoAlgorithm() byte

GetCryptoAlgorithm returns the crypto algorithm

func (*ClientSSK) GetCryptoKey

func (c *ClientSSK) GetCryptoKey() []byte

GetCryptoKey returns the decryption key

func (*ClientSSK) GetDocName

func (c *ClientSSK) GetDocName() string

GetDocName returns the document name

func (*ClientSSK) GetEncryptedHashedDocname

func (c *ClientSSK) GetEncryptedHashedDocname() []byte

GetEncryptedHashedDocname returns E(H(docname))

func (*ClientSSK) GetNodeKey

func (c *ClientSSK) GetNodeKey(clone bool) (*NodeSSK, error)

GetNodeKey returns the corresponding NodeSSK (for routing)

func (*ClientSSK) GetPubKey

func (c *ClientSSK) GetPubKey() *dsa.PublicKey

GetPubKey returns the public key if available

func (*ClientSSK) GetPubKeyHash

func (c *ClientSSK) GetPubKeyHash() []byte

GetPubKeyHash returns the public key hash

func (*ClientSSK) GetRoutingKey

func (c *ClientSSK) GetRoutingKey() []byte

GetRoutingKey returns the routing key (for compatibility)

func (*ClientSSK) GetURI

func (c *ClientSSK) GetURI() *FreenetURI

GetURI converts this ClientSSK to a FreenetURI

func (*ClientSSK) HashCode

func (c *ClientSSK) HashCode() int

HashCode returns a hash code for this key

func (*ClientSSK) SetPubKey

func (c *ClientSSK) SetPubKey(pubKey *dsa.PublicKey) error

SetPubKey sets the public key if it matches the hash

func (*ClientSSK) ToNormalizedDouble

func (c *ClientSSK) ToNormalizedDouble() float64

ToNormalizedDouble converts key to 0.0-1.0 range for routing

func (*ClientSSK) VerifyDocname

func (c *ClientSSK) VerifyDocname() error

VerifyDocname verifies that the docname matches the encrypted hashed docname

type FreenetURI

type FreenetURI struct {
	KeyType          string   // "CHK", "SSK", "KSK", "USK"
	DocName          string   // document name (SSK, KSK, USK)
	MetaStr          []string // manifest path components
	RoutingKey       []byte   // 32 bytes (CHK, SSK, USK) - for SSK it's pubKeyHash
	CryptoKey        []byte   // 32 bytes (CHK, SSK, USK)
	Extra            []byte   // 5 bytes - algorithm info
	SuggestedEdition int64    // only for USK (-1 = not specified)
	// contains filtered or unexported fields
}

FreenetURI represents a Freenet/Hyphanet URI Format: freenet:[KeyType@]RoutingKey,CryptoKey,Extra[/docname][/meta1/meta2/...]

func ParseFreenetURI

func ParseFreenetURI(uriStr string) (*FreenetURI, error)

ParseFreenetURI parses a Freenet URI string

func (*FreenetURI) Clone

func (uri *FreenetURI) Clone() *FreenetURI

Clone creates a deep copy of the URI

func (*FreenetURI) Equals

func (uri *FreenetURI) Equals(other *FreenetURI) bool

Equals checks if two URIs are equal

func (*FreenetURI) GetEdition

func (uri *FreenetURI) GetEdition() int64

GetEdition returns the edition for USK URIs

func (*FreenetURI) GetSiteName

func (uri *FreenetURI) GetSiteName() string

GetSiteName returns the site name portion of the URI

func (*FreenetURI) SetEdition

func (uri *FreenetURI) SetEdition(edition int64) error

SetEdition sets the edition for USK URIs

func (*FreenetURI) String

func (uri *FreenetURI) String() string

String converts the URI to a string representation

func (*FreenetURI) ToClientCHK

func (uri *FreenetURI) ToClientCHK() (*ClientCHK, error)

ToClientCHK converts this URI to a ClientCHK (if it's a CHK URI)

func (*FreenetURI) ToClientSSK

func (uri *FreenetURI) ToClientSSK() (*ClientSSK, error)

ToClientSSK converts this URI to a ClientSSK (if it's an SSK/USK URI)

func (*FreenetURI) WriteBinary

func (uri *FreenetURI) WriteBinary(w io.Writer) error

WriteBinary writes the URI in binary format

type Key

type Key interface {
	// GetRoutingKey returns the routing key bytes used for network routing
	GetRoutingKey() []byte

	// GetKeyBytes returns the key-specific bytes (implementation-dependent)
	GetKeyBytes() []byte

	// GetFullKey returns the complete serialized key including type
	GetFullKey() []byte

	// GetType returns the key type (high byte = base type, low byte = crypto algorithm)
	GetType() uint16

	// ToNormalizedDouble converts the key to a 0.0-1.0 value for location-based routing
	ToNormalizedDouble() float64

	// Write serializes the key to a writer
	Write(w io.Writer) error

	// Clone creates a deep copy of the key
	Clone() Key

	// Equals checks if two keys are equal
	Equals(other Key) bool
}

Key is the base interface for all Freenet keys

type NodeCHK

type NodeCHK struct {
	BaseKey
	// contains filtered or unexported fields
}

NodeCHK represents a node-level Content Hash Key (immutable content) This is used for routing and does not contain decryption information

func NewNodeCHK

func NewNodeCHK(routingKey []byte, cryptoAlgorithm byte) (*NodeCHK, error)

NewNodeCHK creates a new NodeCHK from a routing key and crypto algorithm

func ReadNodeCHK

func ReadNodeCHK(r io.Reader, cryptoAlgorithm byte) (*NodeCHK, error)

ReadNodeCHK deserializes a NodeCHK from a reader

func ReadNodeCHKWithType

func ReadNodeCHKWithType(r io.Reader) (*NodeCHK, error)

ReadNodeCHKWithType deserializes a NodeCHK including its type prefix

func (*NodeCHK) Clone

func (nchk *NodeCHK) Clone() Key

Clone creates a deep copy of the key

func (*NodeCHK) Equals

func (nchk *NodeCHK) Equals(other Key) bool

Equals checks if two keys are equal

func (*NodeCHK) GetCryptoAlgorithm

func (nchk *NodeCHK) GetCryptoAlgorithm() byte

GetCryptoAlgorithm returns the crypto algorithm

func (*NodeCHK) GetFullKey

func (nchk *NodeCHK) GetFullKey() []byte

GetFullKey returns the complete serialized key

func (*NodeCHK) GetKeyBytes

func (nchk *NodeCHK) GetKeyBytes() []byte

GetKeyBytes returns the routing key for CHK

func (*NodeCHK) GetType

func (nchk *NodeCHK) GetType() uint16

GetType returns the key type (high byte = CHK, low byte = algorithm)

func (*NodeCHK) ToNormalizedDouble

func (nchk *NodeCHK) ToNormalizedDouble() float64

ToNormalizedDouble converts key to 0.0-1.0 range for routing

func (*NodeCHK) Write

func (nchk *NodeCHK) Write(w io.Writer) error

Write serializes the key to a writer

type NodeSSK

type NodeSSK struct {
	BaseKey
	// contains filtered or unexported fields
}

NodeSSK represents a node-level Signed Subspace Key (mutable content) This is used for routing and does not contain decryption information

func NewNodeSSK

func NewNodeSSK(pubKeyHash, ehDocname []byte, pubKey *dsa.PublicKey, cryptoAlgorithm byte) (*NodeSSK, error)

NewNodeSSK creates a new NodeSSK

func ReadNodeSSK

func ReadNodeSSK(r io.Reader, cryptoAlgorithm byte) (*NodeSSK, error)

ReadNodeSSK deserializes a NodeSSK from a reader

func ReadNodeSSKWithType

func ReadNodeSSKWithType(r io.Reader) (*NodeSSK, error)

ReadNodeSSKWithType deserializes a NodeSSK including its type prefix

func (*NodeSSK) Clone

func (nssk *NodeSSK) Clone() Key

Clone creates a deep copy of the key

func (*NodeSSK) Equals

func (nssk *NodeSSK) Equals(other Key) bool

Equals checks if two keys are equal

func (*NodeSSK) GetCryptoAlgorithm

func (nssk *NodeSSK) GetCryptoAlgorithm() byte

GetCryptoAlgorithm returns the crypto algorithm

func (*NodeSSK) GetEncryptedHashedDocname

func (nssk *NodeSSK) GetEncryptedHashedDocname() []byte

GetEncryptedHashedDocname returns the encrypted hashed docname

func (*NodeSSK) GetFullKey

func (nssk *NodeSSK) GetFullKey() []byte

GetFullKey returns the complete serialized key

func (*NodeSSK) GetKeyBytes

func (nssk *NodeSSK) GetKeyBytes() []byte

GetKeyBytes returns the encrypted hashed docname for SSK

func (*NodeSSK) GetPubKey

func (nssk *NodeSSK) GetPubKey() *dsa.PublicKey

GetPubKey returns the public key if available

func (*NodeSSK) GetPubKeyHash

func (nssk *NodeSSK) GetPubKeyHash() []byte

GetPubKeyHash returns the public key hash

func (*NodeSSK) GetType

func (nssk *NodeSSK) GetType() uint16

GetType returns the key type (high byte = SSK, low byte = algorithm)

func (*NodeSSK) SetPubKey

func (nssk *NodeSSK) SetPubKey(pubKey *dsa.PublicKey) error

SetPubKey sets the public key if it matches the hash

func (*NodeSSK) ToNormalizedDouble

func (nssk *NodeSSK) ToNormalizedDouble() float64

ToNormalizedDouble converts key to 0.0-1.0 range for routing

func (*NodeSSK) Write

func (nssk *NodeSSK) Write(w io.Writer) error

Write serializes the key to a writer

Jump to

Keyboard shortcuts

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