Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xts: avoid redundant bounds checks #302

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions xts/xts.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (c *Cipher) Encrypt(ciphertext, plaintext []byte, sectorNum uint64) {

c.k2.Encrypt(tweak[:], tweak[:])

for len(plaintext) > 0 {
for len(plaintext) >= blockSize && len(ciphertext) >= blockSize {
for j := range tweak {
ciphertext[j] = plaintext[j] ^ tweak[j]
}
Expand Down Expand Up @@ -126,7 +126,7 @@ func (c *Cipher) Decrypt(plaintext, ciphertext []byte, sectorNum uint64) {

c.k2.Encrypt(tweak[:], tweak[:])

for len(ciphertext) > 0 {
for len(ciphertext) >= blockSize && len(plaintext) >= blockSize {
for j := range tweak {
plaintext[j] = ciphertext[j] ^ tweak[j]
}
Expand Down
7 changes: 4 additions & 3 deletions xts/xts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,14 @@ func TestShorterCiphertext(t *testing.T) {

func BenchmarkXTS(b *testing.B) {
b.ReportAllocs()
b.SetBytes(320)
c, err := NewCipher(aes.NewCipher, make([]byte, 32))
if err != nil {
b.Fatalf("NewCipher failed: %s", err)
}
plaintext := make([]byte, 32)
encrypted := make([]byte, 48)
decrypted := make([]byte, 48)
plaintext := make([]byte, 320)
encrypted := make([]byte, 336)
decrypted := make([]byte, 336)

for i := 0; i < b.N; i++ {
c.Encrypt(encrypted, plaintext, 0)
Expand Down