Skip to content

Commit

Permalink
make loadAes more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
qmuntal committed Mar 24, 2022
1 parent c476398 commit 09899dd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/mksyscall/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func generateSyscalls() []byte {
var bout bytes.Buffer
cmd.Stdout = &bout
cmd.Stderr = os.Stderr
cmd.Env = append(os.Environ(), "GOROOT=" + wd)
cmd.Env = append(os.Environ(), "GOROOT="+wd)
err = cmd.Run()
if err != nil {
log.Fatal(err)
Expand Down
8 changes: 7 additions & 1 deletion cng/aes.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,24 @@ func loadAes(id string, mode string) (h aesAlgorithm, err error) {
// but Windows 7 requires that it be set on the algorithm before key creation.
err = setString(bcrypt.HANDLE(h.h), bcrypt.CHAINING_MODE, mode)
if err != nil {
bcrypt.CloseAlgorithmProvider(h.h, 0)
return
}
var info bcrypt.KEY_LENGTHS_STRUCT
var discard uint32
err = bcrypt.GetProperty(bcrypt.HANDLE(h.h), utf16PtrFromString(bcrypt.KEY_LENGTHS), (*(*[1<<31 - 1]byte)(unsafe.Pointer(&info)))[:unsafe.Sizeof(info)], &discard, 0)
if err != nil {
bcrypt.CloseAlgorithmProvider(h.h, 0)
return
}
for size := info.MinLength; size <= info.MaxLength; size += info.Increment {
h.allowedKeySized = append(h.allowedKeySized, int(size))
}
aesCache.Store(aesCacheEntry{id, mode}, h)
if existing, loaded := aesCache.LoadOrStore(aesCacheEntry{id, mode}, h); loaded {
// We can safely use a provider that has already been cached in another concurrent goroutine.
bcrypt.CloseAlgorithmProvider(h.h, 0)
h = existing.(aesAlgorithm)
}
return
}

Expand Down

0 comments on commit 09899dd

Please sign in to comment.