drbg

package
v0.26.1 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package drbg implements Random Number Generation Using Deterministic Random Bit Generators.

Index

Examples

Constants

View Source
const DRBG_RESEED_COUNTER_INTERVAL_LEVEL1 uint64 = 1 << 20
View Source
const DRBG_RESEED_COUNTER_INTERVAL_LEVEL2 uint64 = 1 << 10
View Source
const DRBG_RESEED_COUNTER_INTERVAL_LEVEL_TEST uint64 = 8
View Source
const DRBG_RESEED_TIME_INTERVAL_LEVEL1 = time.Duration(600) * time.Second
View Source
const DRBG_RESEED_TIME_INTERVAL_LEVEL2 = time.Duration(60) * time.Second
View Source
const DRBG_RESEED_TIME_INTERVAL_LEVEL_TEST = time.Duration(6) * time.Second
View Source
const HASH_DRBG_MAX_SEED_SIZE = 111
View Source
const HASH_DRBG_SEED_SIZE = 55
View Source
const MAX_BYTES = 1 << 27
View Source
const MAX_BYTES_PER_GENERATE = 1 << 11

Variables

View Source
var ErrReseedRequired = errors.New("drbg: reseed reuqired")

Functions

This section is empty.

Types

type BaseDrbg

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

func (*BaseDrbg) NeedReseed

func (hd *BaseDrbg) NeedReseed() bool

type CtrDrbg

type CtrDrbg struct {
	BaseDrbg
	// contains filtered or unexported fields
}

CtrDrbg CTR DRBG structure, its instance is NOT goroutine safe!!!

func NewCtrDrbg

func NewCtrDrbg(cipherProvider func(key []byte) (cipher.Block, error), keyLen int, securityLevel SecurityLevel, gm bool, entropy, nonce, personalization []byte) (*CtrDrbg, error)

NewCtrDrbg create one CTR DRBG instance

func NewGMCtrDrbg

func NewGMCtrDrbg(securityLevel SecurityLevel, entropy, nonce, personalization []byte) (*CtrDrbg, error)

NewGMCtrDrbg create one CTR DRBG implementation which follows GM/T 0105-2021 standard

func NewNISTCtrDrbg

func NewNISTCtrDrbg(cipherProvider func(key []byte) (cipher.Block, error), keyLen int, securityLevel SecurityLevel, entropy, nonce, personalization []byte) (*CtrDrbg, error)

NewNISTCtrDrbg create one CTR DRBG implementation which follows NIST standard

func (*CtrDrbg) Generate

func (hd *CtrDrbg) Generate(b, additional []byte) error

Generate CTR DRBG pseudorandom bits generate process.

func (*CtrDrbg) MaxBytesPerRequest

func (hd *CtrDrbg) MaxBytesPerRequest() int

func (*CtrDrbg) Reseed

func (hd *CtrDrbg) Reseed(entropy, additional []byte) error

type DRBG

type DRBG interface {
	// check internal state, return if reseed required
	NeedReseed() bool
	// reseed process
	Reseed(entropy, additional []byte) error
	// generate requrested bytes to b
	Generate(b, additional []byte) error
	// MaxBytesPerRequest return max bytes per request
	MaxBytesPerRequest() int
}

DRBG interface for both hash and ctr drbg implementations

type DrbgPrng

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

DrbgPrng sample pseudo random number generator base on DRBG

func NewCtrDrbgPrng

func NewCtrDrbgPrng(cipherProvider func(key []byte) (cipher.Block, error), keyLen int, entropySource io.Reader, securityStrength int, gm bool, securityLevel SecurityLevel, personalization []byte) (*DrbgPrng, error)

NewCtrDrbgPrng create pseudo random number generator base on CTR DRBG

func NewGmCtrDrbgPrng

func NewGmCtrDrbgPrng(entropySource io.Reader, securityStrength int, securityLevel SecurityLevel, personalization []byte) (*DrbgPrng, error)

NewNistCtrDrbgPrng create pseudo random number generator base on CTR DRBG which follows GM/T 0105-2021 standard

Example
package main

import (
	"bytes"
	"fmt"

	"github.com/emmansun/gmsm/drbg"
)

func main() {
	prng, err := drbg.NewGmCtrDrbgPrng(nil, 32, drbg.SECURITY_LEVEL_TEST, nil)
	if err != nil {
		panic(err)
	}
	c := 10
	b := make([]byte, c)
	_, err = prng.Read(b)
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	// The slice should now contain random bytes instead of only zeroes.
	fmt.Println(bytes.Equal(b, make([]byte, c)))

}
Output:

false

func NewGmHashDrbgPrng

func NewGmHashDrbgPrng(entropySource io.Reader, securityStrength int, securityLevel SecurityLevel, personalization []byte) (*DrbgPrng, error)

NewGmHashDrbgPrng create pseudo random number generator base on hash DRBG which follows GM/T 0105-2021 standard

Example
package main

import (
	"bytes"
	"fmt"

	"github.com/emmansun/gmsm/drbg"
)

func main() {
	prng, err := drbg.NewGmHashDrbgPrng(nil, 32, drbg.SECURITY_LEVEL_TEST, nil)
	if err != nil {
		panic(err)
	}
	c := 10
	b := make([]byte, c)
	_, err = prng.Read(b)
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	// The slice should now contain random bytes instead of only zeroes.
	fmt.Println(bytes.Equal(b, make([]byte, c)))

}
Output:

false

func NewHashDrbgPrng

func NewHashDrbgPrng(newHash func() hash.Hash, entropySource io.Reader, securityStrength int, gm bool, securityLevel SecurityLevel, personalization []byte) (*DrbgPrng, error)

NewHashDrbgPrng create pseudo random number generator base on HASH DRBG

func NewNistCtrDrbgPrng

func NewNistCtrDrbgPrng(cipherProvider func(key []byte) (cipher.Block, error), keyLen int, entropySource io.Reader, securityStrength int, securityLevel SecurityLevel, personalization []byte) (*DrbgPrng, error)

NewNistCtrDrbgPrng create pseudo random number generator base on CTR DRBG which follows NIST standard

func NewNistHashDrbgPrng

func NewNistHashDrbgPrng(newHash func() hash.Hash, entropySource io.Reader, securityStrength int, securityLevel SecurityLevel, personalization []byte) (*DrbgPrng, error)

NewNistHashDrbgPrng create pseudo random number generator base on hash DRBG which follows NIST standard

func (*DrbgPrng) Read

func (prng *DrbgPrng) Read(data []byte) (int, error)

type HashDrbg

type HashDrbg struct {
	BaseDrbg
	// contains filtered or unexported fields
}

HashDrbg hash DRBG structure, its instance is NOT goroutine safe!!!

func NewGMHashDrbg

func NewGMHashDrbg(securityLevel SecurityLevel, entropy, nonce, personalization []byte) (*HashDrbg, error)

NewGMHashDrbg return hash DRBG implementation which follows GM/T 0105-2021 standard

func NewHashDrbg

func NewHashDrbg(newHash func() hash.Hash, securityLevel SecurityLevel, gm bool, entropy, nonce, personalization []byte) (*HashDrbg, error)

NewHashDrbg create one hash DRBG instance

func NewNISTHashDrbg

func NewNISTHashDrbg(newHash func() hash.Hash, securityLevel SecurityLevel, entropy, nonce, personalization []byte) (*HashDrbg, error)

NewNISTHashDrbg return hash DRBG implementation which follows NIST standard

func (*HashDrbg) Generate

func (hd *HashDrbg) Generate(b, additional []byte) error

Generate hash DRBG pseudorandom bits process. GM/T 0105-2021 has a little different with NIST. GM/T 0105-2021 can only generate no more than hash.Size bytes once.

func (*HashDrbg) MaxBytesPerRequest

func (hd *HashDrbg) MaxBytesPerRequest() int

func (*HashDrbg) Reseed

func (hd *HashDrbg) Reseed(entropy, additional []byte) error

Reseed hash DRBG reseed process. GM/T 0105-2021 has a little different with NIST.

type SecurityLevel

type SecurityLevel byte
const (
	SECURITY_LEVEL_ONE  SecurityLevel = 0x01
	SECURITY_LEVEL_TWO  SecurityLevel = 0x02
	SECURITY_LEVEL_TEST SecurityLevel = 0x99
)

Jump to

Keyboard shortcuts

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