Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed May 15, 2024
1 parent ce9bc02 commit 8129081
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
16 changes: 9 additions & 7 deletions hash/sm3/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ import (
const MAX_RESEED_COUNTER = (1<<20)
const MAX_RESEED_SECONDS = 600

var num = [4]uint8{ 0, 1, 2, 3 }
var num = [4]byte{ 0, 1, 2, 3 }

type Rand struct {
V [55]uint8
C [55]uint8
V []byte
C []byte
reseedCounter uint32
lastReseedTime time.Time
}

func NewRand(nonce []byte, label []byte) *Rand {
rand := new(Rand)
rand.V = make([]byte, 55)
rand.C = make([]byte, 55)
rand.init(nonce, label)

return rand
Expand All @@ -39,13 +41,13 @@ func (this *Rand) init(nonce []byte, label []byte) {
df.Write(entropy[:])
df.Write(nonce[:])
df.Write(label[:])
this.V = df.Sum()
this.V = df.Sum(nil)

// C = sm3_df(0x00 || V)
df = NewDF()
df.Write(num[0:1])
df.Write(this.V[:])
this.C = df.Sum()
this.C = df.Sum(nil)

// reseedCounter = 1, last_ressed_time = now()
this.reseedCounter = 1
Expand Down Expand Up @@ -126,13 +128,13 @@ func (this *Rand) reseed(addin []byte) {
df.Write(entropy[:])
df.Write(this.V[:])
df.Write(addin[:])
this.V = df.Sum()
this.V = df.Sum(nil)

// C = sm3_df(0x00 || V)
df = NewDF()
df.Write(num[0:1])
df.Write(this.V[:])
this.C = df.Sum()
this.C = df.Sum(nil)

// reseedCounter = 1, last_ressed_time = now()
this.reseedCounter = 1
Expand Down
29 changes: 25 additions & 4 deletions hash/sm3/rand_df.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ import (
"hash"
)

// The size of an SM3-DF checksum in bytes.
const SizeDF = 55

// The blocksize of SM3-DF in bytes.
const BlockSizeDF = 64

type DF struct {
sm3 [2]hash.Hash
}

func NewDF() *DF {
df := new(DF)
df.init()
df.Reset()

return df
}

func (this *DF) init() {
func (this *DF) Reset() {
var counter = [4]byte{0, 0, 0, 1}
var seedlen = [4]byte{0, 0, 440/256, 440%256}

Expand All @@ -30,19 +36,34 @@ func (this *DF) init() {
this.sm3[1].Write(seedlen[:])
}

func (this *DF) Size() int {
return SizeDF
}

func (this *DF) BlockSize() int {
return BlockSizeDF
}

func (this *DF) Write(data []byte) {
if len(data) > 0 {
this.sm3[0].Write(data)
this.sm3[1].Write(data)
}
}

func (this *DF) Sum() (out [55]byte) {
func (this *DF) Sum(in []byte) []byte {
// Make a copy of d so that caller can keep writing and summing.
d := *this
sum := d.checkSum()
return append(in, sum[:]...)
}

func (this *DF) checkSum() (out [SizeDF]byte) {
o := this.sm3[0].Sum(nil)
buf := this.sm3[1].Sum(nil)

copy(out[:], o)
copy(out[:55 - 32], buf)
copy(out[:SizeDF - 32], buf)

return
}
2 changes: 1 addition & 1 deletion hash/sm3/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func GG16(x, y, z uint32) uint32 {
return (((y ^ z) & x) ^ z)
}

func compressBlocks(digest []uint32, data []uint8) {
func compressBlocks(digest []uint32, data []byte) {
var A uint32
var B uint32
var C uint32
Expand Down

0 comments on commit 8129081

Please sign in to comment.