Skip to content

Commit

Permalink
remove variant cipher block size, cause variant blocksize can cap to
Browse files Browse the repository at this point in the history
8/16 bytes
  • Loading branch information
xtaci committed Feb 9, 2020
1 parent da0cced commit 3bedbae
Showing 1 changed file with 2 additions and 192 deletions.
194 changes: 2 additions & 192 deletions crypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func encrypt(block cipher.Block, dst, src, buf []byte) {
case 16:
encrypt16(block, dst, src, buf)
default:
encryptVariant(block, dst, src, buf)
panic("unsupported cipher block size")
}
}

Expand Down Expand Up @@ -413,97 +413,6 @@ func encrypt16(block cipher.Block, dst, src, buf []byte) {
}
}

func encryptVariant(block cipher.Block, dst, src, buf []byte) {
blocksize := block.BlockSize()
tbl := buf[:blocksize]
block.Encrypt(tbl, initialVector)
n := len(src) / blocksize
base := 0
repeat := n / 8
left := n % 8
for i := 0; i < repeat; i++ {
// 1
xor.BytesB(dst[base:], src[base:], tbl)
block.Encrypt(tbl, dst[base:])
base += blocksize

// 2
xor.BytesB(dst[base:], src[base:], tbl)
block.Encrypt(tbl, dst[base:])
base += blocksize

// 3
xor.BytesB(dst[base:], src[base:], tbl)
block.Encrypt(tbl, dst[base:])
base += blocksize

// 4
xor.BytesB(dst[base:], src[base:], tbl)
block.Encrypt(tbl, dst[base:])
base += blocksize

// 5
xor.BytesB(dst[base:], src[base:], tbl)
block.Encrypt(tbl, dst[base:])
base += blocksize

// 6
xor.BytesB(dst[base:], src[base:], tbl)
block.Encrypt(tbl, dst[base:])
base += blocksize

// 7
xor.BytesB(dst[base:], src[base:], tbl)
block.Encrypt(tbl, dst[base:])
base += blocksize

// 8
xor.BytesB(dst[base:], src[base:], tbl)
block.Encrypt(tbl, dst[base:])
base += blocksize
}

switch left {
case 7:
xor.BytesB(dst[base:], src[base:], tbl)
block.Encrypt(tbl, dst[base:])
base += blocksize
fallthrough
case 6:
xor.BytesB(dst[base:], src[base:], tbl)
block.Encrypt(tbl, dst[base:])
base += blocksize
fallthrough
case 5:
xor.BytesB(dst[base:], src[base:], tbl)
block.Encrypt(tbl, dst[base:])
base += blocksize
fallthrough
case 4:
xor.BytesB(dst[base:], src[base:], tbl)
block.Encrypt(tbl, dst[base:])
base += blocksize
fallthrough
case 3:
xor.BytesB(dst[base:], src[base:], tbl)
block.Encrypt(tbl, dst[base:])
base += blocksize
fallthrough
case 2:
xor.BytesB(dst[base:], src[base:], tbl)
block.Encrypt(tbl, dst[base:])
base += blocksize
fallthrough
case 1:
xor.BytesB(dst[base:], src[base:], tbl)
block.Encrypt(tbl, dst[base:])
base += blocksize
fallthrough
case 0:
xorBytes(dst[base:], src[base:], tbl)
}
}

// decryption
func decrypt(block cipher.Block, dst, src, buf []byte) {
switch block.BlockSize() {
Expand All @@ -512,7 +421,7 @@ func decrypt(block cipher.Block, dst, src, buf []byte) {
case 16:
decrypt16(block, dst, src, buf)
default:
decryptVariant(block, dst, src, buf)
panic("unsupported cipher block size")
}
}

Expand Down Expand Up @@ -692,105 +601,6 @@ func decrypt16(block cipher.Block, dst, src, buf []byte) {
}
}

func decryptVariant(block cipher.Block, dst, src, buf []byte) {
blocksize := block.BlockSize()
tbl := buf[:blocksize]
next := buf[blocksize:]
block.Encrypt(tbl, initialVector)
n := len(src) / blocksize
base := 0
repeat := n / 8
left := n % 8
for i := 0; i < repeat; i++ {
// 1
block.Encrypt(next, src[base:])
xor.BytesB(dst[base:], src[base:], tbl)
base += blocksize

// 2
block.Encrypt(tbl, src[base:])
xor.BytesB(dst[base:], src[base:], next)
base += blocksize

// 3
block.Encrypt(next, src[base:])
xor.BytesB(dst[base:], src[base:], tbl)
base += blocksize

// 4
block.Encrypt(tbl, src[base:])
xor.BytesB(dst[base:], src[base:], next)
base += blocksize

// 5
block.Encrypt(next, src[base:])
xor.BytesB(dst[base:], src[base:], tbl)
base += blocksize

// 6
block.Encrypt(tbl, src[base:])
xor.BytesB(dst[base:], src[base:], next)
base += blocksize

// 7
block.Encrypt(next, src[base:])
xor.BytesB(dst[base:], src[base:], tbl)
base += blocksize

// 8
block.Encrypt(tbl, src[base:])
xor.BytesB(dst[base:], src[base:], next)
base += blocksize
}

switch left {
case 7:
block.Encrypt(next, src[base:])
xor.BytesB(dst[base:], src[base:], tbl)
tbl, next = next, tbl
base += blocksize
fallthrough
case 6:
block.Encrypt(next, src[base:])
xor.BytesB(dst[base:], src[base:], tbl)
tbl, next = next, tbl
base += blocksize
fallthrough
case 5:
block.Encrypt(next, src[base:])
xor.BytesB(dst[base:], src[base:], tbl)
tbl, next = next, tbl
base += blocksize
fallthrough
case 4:
block.Encrypt(next, src[base:])
xor.BytesB(dst[base:], src[base:], tbl)
tbl, next = next, tbl
base += blocksize
fallthrough
case 3:
block.Encrypt(next, src[base:])
xor.BytesB(dst[base:], src[base:], tbl)
tbl, next = next, tbl
base += blocksize
fallthrough
case 2:
block.Encrypt(next, src[base:])
xor.BytesB(dst[base:], src[base:], tbl)
tbl, next = next, tbl
base += blocksize
fallthrough
case 1:
block.Encrypt(next, src[base:])
xor.BytesB(dst[base:], src[base:], tbl)
tbl, next = next, tbl
base += blocksize
fallthrough
case 0:
xorBytes(dst[base:], src[base:], tbl)
}
}

// per bytes xors
func xorBytes(dst, a, b []byte) int {
n := len(a)
Expand Down

0 comments on commit 3bedbae

Please sign in to comment.