Skip to content

Commit

Permalink
use byte slice instead of byte pointers in sys functions
Browse files Browse the repository at this point in the history
  • Loading branch information
qmuntal committed Mar 18, 2022
1 parent 938ad52 commit 9eae00e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
18 changes: 9 additions & 9 deletions cng/aes.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func NewAESCipher(key []byte) (cipher.Block, error) {
}
c := &aesCipher{key: make([]byte, len(key))}
copy(c.key, key)
err = bcrypt.GenerateSymmetricKey(h.h, &c.kh, nil, 0, &c.key[0], uint32(len(c.key)), 0)
err = bcrypt.GenerateSymmetricKey(h.h, &c.kh, nil, c.key, 0)
if err != nil {
return nil, err
}
Expand All @@ -105,7 +105,7 @@ func (c *aesCipher) Encrypt(dst, src []byte) {
panic("crypto/aes: output not full block")
}
var ret uint32
err := bcrypt.Encrypt(c.kh, &src[0], uint32(len(src)), nil, nil, 0, &dst[0], uint32(len(dst)), &ret, 0)
err := bcrypt.Encrypt(c.kh, src, nil, nil, dst, &ret, 0)
if err != nil {
panic(err)
}
Expand All @@ -127,7 +127,7 @@ func (c *aesCipher) Decrypt(dst, src []byte) {
}

var ret uint32
err := bcrypt.Decrypt(c.kh, &src[0], uint32(len(src)), nil, nil, 0, &dst[0], uint32(len(dst)), &ret, 0)
err := bcrypt.Decrypt(c.kh, src, nil, nil, dst, &ret, 0)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -180,7 +180,7 @@ func newCBC(encrypt bool, key, iv []byte) *aesCBC {
}
x := &aesCBC{encrypt: encrypt}
x.SetIV(iv)
err = bcrypt.GenerateSymmetricKey(h.h, &x.kh, nil, 0, &key[0], uint32(len(key)), 0)
err = bcrypt.GenerateSymmetricKey(h.h, &x.kh, nil, key, 0)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -210,9 +210,9 @@ func (x *aesCBC) CryptBlocks(dst, src []byte) {
var ret uint32
var err error
if x.encrypt {
err = bcrypt.Encrypt(x.kh, &src[0], uint32(len(src)), nil, &x.iv[0], uint32(len(x.iv)), &dst[0], uint32(len(dst)), &ret, 0)
err = bcrypt.Encrypt(x.kh, src, nil, x.iv[:], dst, &ret, 0)
} else {
err = bcrypt.Decrypt(x.kh, &src[0], uint32(len(src)), nil, &x.iv[0], uint32(len(x.iv)), &dst[0], uint32(len(dst)), &ret, 0)
err = bcrypt.Decrypt(x.kh, src, nil, x.iv[:], dst, &ret, 0)
}
if err != nil {
panic(err)
Expand Down Expand Up @@ -253,7 +253,7 @@ func newGCM(key []byte, tls bool) (*aesGCM, error) {
return nil, err
}
g := &aesGCM{tls: tls}
err = bcrypt.GenerateSymmetricKey(h.h, &g.kh, nil, 0, &key[0], uint32(len(key)), 0)
err = bcrypt.GenerateSymmetricKey(h.h, &g.kh, nil, key, 0)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -308,7 +308,7 @@ func (g *aesGCM) Seal(dst, nonce, plaintext, additionalData []byte) []byte {

info := bcrypt.NewAUTHENTICATED_CIPHER_MODE_INFO(nonce, additionalData, out[len(out)-gcmTagSize:])
var encSize uint32
err := bcrypt.Encrypt(g.kh, &plaintext[0], uint32(len(plaintext)), info, nil, 0, &out[0], uint32(len(out)), &encSize, 0)
err := bcrypt.Encrypt(g.kh, plaintext, info, nil, out, &encSize, 0)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -345,7 +345,7 @@ func (g *aesGCM) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, er

info := bcrypt.NewAUTHENTICATED_CIPHER_MODE_INFO(nonce, additionalData, tag)
var decSize uint32
err := bcrypt.Decrypt(g.kh, &ciphertext[0], uint32(len(ciphertext)), info, nil, 0, &out[0], uint32(len(out)), &decSize, 0)
err := bcrypt.Decrypt(g.kh, ciphertext, info, nil, out, &decSize, 0)
if err != nil || int(decSize) != len(ciphertext) {
for i := range out {
out[i] = 0
Expand Down
6 changes: 3 additions & 3 deletions internal/bcrypt/bcrypt_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func NewAUTHENTICATED_CIPHER_MODE_INFO(nonce, additionalData, tag []byte) *AUTHE

// Keys

//sys GenerateSymmetricKey(hAlgorithm ALG_HANDLE, phKey *KEY_HANDLE, pbKeyObject *byte, cbKeyObject uint32, pbSecret *byte, cbSecret uint32, dwFlags uint32) (s error) = bcrypt.BCryptGenerateSymmetricKey
//sys GenerateSymmetricKey(hAlgorithm ALG_HANDLE, phKey *KEY_HANDLE, pbKeyObject []byte, pbSecret []byte, dwFlags uint32) (s error) = bcrypt.BCryptGenerateSymmetricKey
//sys DestroyKey(hKey KEY_HANDLE) (s error) = bcrypt.BCryptDestroyKey
//sys Encrypt(hKey KEY_HANDLE, pbInput *byte, cbInput uint32, pPaddingInfo *AUTHENTICATED_CIPHER_MODE_INFO, pbIV *byte, cbIV uint32, pbOutput *byte, cbOutput uint32, pcbResult *uint32, dwFlags uint32) (s error) = bcrypt.BCryptEncrypt
//sys Decrypt(hKey KEY_HANDLE, pbInput *byte, cbInput uint32, pPaddingInfo *AUTHENTICATED_CIPHER_MODE_INFO, pbIV *byte, cbIV uint32, pbOutput *byte, cbOutput uint32, pcbResult *uint32, dwFlags uint32) (s error) = bcrypt.BCryptDecrypt
//sys Encrypt(hKey KEY_HANDLE, pbInput []byte, pPaddingInfo *AUTHENTICATED_CIPHER_MODE_INFO, pbIV []byte, pbOutput []byte, pcbResult *uint32, dwFlags uint32) (s error) = bcrypt.BCryptEncrypt
//sys Decrypt(hKey KEY_HANDLE, pbInput []byte, pPaddingInfo *AUTHENTICATED_CIPHER_MODE_INFO, pbIV []byte, pbOutput []byte, pcbResult *uint32, dwFlags uint32) (s error) = bcrypt.BCryptDecrypt
44 changes: 38 additions & 6 deletions internal/bcrypt/zsyscall_windows.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9eae00e

Please sign in to comment.