Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions jwe/internal/concatkdf/concatkdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,25 @@ type KDF struct {
hash crypto.Hash
}

func ndata(src []byte) []byte {
buf := make([]byte, 4+len(src))
binary.BigEndian.PutUint32(buf, uint32(len(src)))
copy(buf[4:], src)
return buf
}

func New(hash crypto.Hash, alg, Z, apu, apv, pubinfo, privinfo []byte) *KDF {
algbuf := ndata(alg)
apubuf := ndata(apu)
apvbuf := ndata(apv)
// Write length-prefixed fields directly into a single buffer,
// avoiding intermediate allocations from ndata().
totalSize := (4 + len(alg)) + (4 + len(apu)) + (4 + len(apv)) + len(pubinfo) + len(privinfo)
concat := make([]byte, totalSize)

n := 0
binary.BigEndian.PutUint32(concat[n:], uint32(len(alg)))
n += 4
n += copy(concat[n:], alg)

binary.BigEndian.PutUint32(concat[n:], uint32(len(apu)))
n += 4
n += copy(concat[n:], apu)

binary.BigEndian.PutUint32(concat[n:], uint32(len(apv)))
n += 4
n += copy(concat[n:], apv)

concat := make([]byte, len(algbuf)+len(apubuf)+len(apvbuf)+len(pubinfo)+len(privinfo))
n := copy(concat, algbuf)
n += copy(concat[n:], apubuf)
n += copy(concat[n:], apvbuf)
n += copy(concat[n:], pubinfo)
copy(concat[n:], privinfo)

Expand Down
Loading