Skip to content

Commit

Permalink
fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
RyuaNerin committed Jun 13, 2024
1 parent 3f27d23 commit d8eb5d7
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 63 deletions.
4 changes: 2 additions & 2 deletions cmd/cavp/cavp_kbkdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func processKBKDF_HMAC(cavp *cavpProcessor, hashInfo *HashInfo, fn funcKBKDF) {
cs := cavp.ReadValues()

if cs.ContainsKey("PRF") {
if cs.ContainsValue("NO COUNTER") {
if cs.ContainsValue("[NO COUNTER]") {
rlen = 0
} else {
rlen = cs.Int("RLEN")
Expand Down Expand Up @@ -111,7 +111,7 @@ func processKBKDF_CMAC(cavp *cavpProcessor, newCipher funcNewBlockCipher, fn fun
cs := cavp.ReadValues()

if cs.ContainsKey("PRF") {
if cs.ContainsValue("NO COUNTER") {
if cs.ContainsValue("[NO COUNTER]") {
rlen = 0
} else {
rlen = cs.Int("RLEN")
Expand Down
113 changes: 60 additions & 53 deletions cmd/cavp/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,73 +253,80 @@ func (p *cavpProcessor) WriteLine(s string) {
}

func (p *cavpProcessor) Next() bool {
return !p.eof
}

func (p *cavpProcessor) ReadValues() cavpSection {
p.cs = p.cs[:0]

for !p.eof {
lineRaw, prefix, err := p.br.ReadLine()
if err == io.EOF {
p.eof = true
prefix = false
err = nil
}
if err != nil {
panic(err)
}
p.lineBuf.Write(lineRaw)
for {
for !p.eof {
lineRaw, prefix, err := p.br.ReadLine()
if err == io.EOF {
p.eof = true
prefix = false
err = nil
}
if err != nil {
panic(err)
}
p.lineBuf.Write(lineRaw)

if prefix {
continue
}
if prefix {
continue
}

line := strings.TrimSpace(p.lineBuf.String())
p.lineBuf.Reset()
line := strings.TrimSpace(p.lineBuf.String())
p.lineBuf.Reset()

p.lineCur++
if len(line) == 0 {
break
}
p.lineCur++
if len(line) == 0 {
break
}

if strings.HasPrefix(line, "#") {
p.cs = append(p.cs, cavpRow{"", internal.StringClone(line), false})
} else {
idx := strings.Index(line, "=")
if idx == -1 {
if strings.HasPrefix(line, "#") {
p.cs = append(p.cs, cavpRow{"", internal.StringClone(line), false})
} else {
bo := strings.HasPrefix(line, "[")
bc := strings.HasSuffix(line, "]")

var section bool
switch {
case bo && bc:
section = true
line = strings.TrimSuffix(strings.TrimPrefix(line, "["), "]")
idx = strings.Index(line, "=")
fallthrough
case !bo && !bc:
p.cs = append(
p.cs,
cavpRow{
Key: internal.StringClone(strings.TrimSpace(line[:idx])),
Value: internal.StringClone(strings.TrimSpace(line[idx+1:])),
Section: section,
},
)

case !bo && bc:
fallthrough
case bo && !bc:
idx := strings.Index(line, "=")
if idx == -1 {
p.cs = append(p.cs, cavpRow{"", internal.StringClone(line), false})
} else {
bo := strings.HasPrefix(line, "[")
bc := strings.HasSuffix(line, "]")

var section bool
switch {
case bo && bc:
section = true
line = strings.TrimSuffix(strings.TrimPrefix(line, "["), "]")
idx = strings.Index(line, "=")
fallthrough
case !bo && !bc:
p.cs = append(
p.cs,
cavpRow{
Key: internal.StringClone(strings.TrimSpace(line[:idx])),
Value: internal.StringClone(strings.TrimSpace(line[idx+1:])),
Section: section,
},
)

case !bo && bc:
fallthrough
case bo && !bc:
p.cs = append(p.cs, cavpRow{"", internal.StringClone(line), false})
}
}
}
}

p.verbose()

if p.eof || len(p.cs) > 0 {
break
}
}

p.verbose()
return len(p.cs) > 0
}

func (p *cavpProcessor) ReadValues() cavpSection {
return p.cs
}

Expand Down
15 changes: 7 additions & 8 deletions internal/drbg/hmacdrbg/hmacdrbg.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ func GetSecurityStrengthBits(requested_strength int) int {
// 참고로 갱신 함수는 인스턴스 생성 함수와 리시드 함수에서
// 시드 생성을 위한 유도 함수(derivation function)의 역할도 수행한다.
func (state *State) Update(providedData ...[]byte) {
providedDataLen := 0

// 1: Key ← HMAC(Key, V ‖ 0x00 ‖ provided_data )
h := hmac.New(state.New, state.Key)
h.Write(state.V)
h.Write([]byte{0x00})
for _, v := range providedData {
h.Write(v)
n, _ := h.Write(v)
providedDataLen += n
}
copy(state.Key, h.Sum(state.sum[:0]))

Expand All @@ -63,7 +66,7 @@ func (state *State) Update(providedData ...[]byte) {
// 3: if (provided_data = Null) then
// 4: return (Key, V )
// 5: end if
if len(providedData) == 0 {
if providedDataLen == 0 {
return
}

Expand Down Expand Up @@ -269,7 +272,7 @@ func (state *State) Generate(
// 33: end if

// 34: if (additional_input ≠ Null) then
if additionalInput != nil {
if len(additionalInput) > 0 {
// 35: (Key, V ) ← HMAC_DRBG_Update(additional_input, Key, V )
state.Update(additionalInput)
}
Expand All @@ -291,11 +294,7 @@ func (state *State) Generate(
// 42: pseudorandom_bits ← leftmost(temp, requested_no_of_bits )

// 43: (Key, V ) ← HMAC_DRBG_Update(additional_input, Key, V )
if additionalInput != nil {
state.Update(additionalInput)
} else {
state.Update()
}
state.Update(additionalInput)

// 44: state(state_handle).V ← V
// 45: state(state_handle).Key ← Key
Expand Down

0 comments on commit d8eb5d7

Please sign in to comment.