Skip to content

Commit

Permalink
cache xof_len
Browse files Browse the repository at this point in the history
  • Loading branch information
qmuntal committed Jan 7, 2025
1 parent 1526cf8 commit cff0bbb
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions cshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ func supportsSHAKE(size int) bool {

// SHAKE is an instance of a SHAKE extendable output function.
type SHAKE struct {
alg *shakeAlgorithm
ctx C.GO_EVP_MD_CTX_PTR
alg *shakeAlgorithm
ctx C.GO_EVP_MD_CTX_PTR
lastXofLen int
}

// NewSHAKE128 creates a new SHAKE128 XOF.
Expand Down Expand Up @@ -168,8 +169,11 @@ func (s *SHAKE) Read(p []byte) (n int, err error) {
if len(p) == 0 {
return 0, nil
}
if C.go_openssl_EVP_MD_CTX_ctrl(s.ctx, C.EVP_MD_CTRL_XOF_LEN, C.int(len(p)), nil) != 1 {
panic(newOpenSSLError("EVP_MD_CTX_ctrl"))
if len(p) != s.lastXofLen {
if C.go_openssl_EVP_MD_CTX_ctrl(s.ctx, C.EVP_MD_CTRL_XOF_LEN, C.int(len(p)), nil) != 1 {
panic(newOpenSSLError("EVP_MD_CTX_ctrl"))
}
s.lastXofLen = len(p)
}
if C.go_openssl_EVP_DigestSqueeze(s.ctx, (*C.uchar)(unsafe.Pointer(&*addr(p))), C.size_t(len(p))) != 1 {
panic(newOpenSSLError("EVP_DigestSqueeze"))
Expand Down

0 comments on commit cff0bbb

Please sign in to comment.