Skip to content

Commit

Permalink
Merge pull request #12 from tdakkota/refactor/force-bce-prevent-iv-leak
Browse files Browse the repository at this point in the history
Prevent `iv` parameter leaking, force BCE using full slice expressions
  • Loading branch information
tdakkota authored Jun 13, 2021
2 parents 56a70f4 + 29c4885 commit af79707
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 40 deletions.
37 changes: 18 additions & 19 deletions decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,35 @@ func (i *igeDecrypter) BlockSize() int {
}

func (i *igeDecrypter) CryptBlocks(dst, src []byte) {
if len(src)%i.block.BlockSize() != 0 {
DecryptBlocks(i.block, i.iv, dst, src)
}

// DecryptBlocks is a simple shorthand for IGE decrypting.
// Note: unlike NewIGEDecrypter, DecryptBlocks does NOT COPY iv.
// So you must not modify passed iv.
func DecryptBlocks(block cipher.Block, iv, dst, src []byte) {
if err := checkIV(block, iv); err != nil {
panic(err.Error())
}
if len(src)%block.BlockSize() != 0 {
panic("src not full blocks")
}
if len(dst) < len(src) {
panic("len(dst) < len(src)")
}

b := i.block.BlockSize()
c := i.iv[:b]
m := i.iv[b:]
b := block.BlockSize()
c := iv[:b]
m := iv[b:]

for o := 0; o < len(src); o += b {
t := src[o : o+b]
t := src[o : o+b : o+b]

xor.Bytes(dst[o:o+b], src[o:o+b], m)
i.block.Decrypt(dst[o:o+b], dst[o:o+b])
xor.Bytes(dst[o:o+b], dst[o:o+b], c)
xor.Bytes(dst[o:o+b:o+b], src[o:o+b:o+b], m)
block.Decrypt(dst[o:o+b:o+b], dst[o:o+b:o+b])
xor.Bytes(dst[o:o+b:o+b], dst[o:o+b:o+b], c)

m = dst[o : o+b]
c = t
}
}

// DecryptBlocks is a simple shorthand for IGE decrypting.
// Note: unlike NewIGEDecrypter, DecryptBlocks does NOT COPY iv.
// So you must not modify passed iv.
func DecryptBlocks(b cipher.Block, iv, dst, src []byte) {
if err := checkIV(b, iv); err != nil {
panic(err.Error())
}
dec := igeDecrypter{block: b, iv: iv}
dec.CryptBlocks(dst, src)
}
41 changes: 20 additions & 21 deletions encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,33 @@ func (i *igeEncrypter) BlockSize() int {
}

func (i *igeEncrypter) CryptBlocks(dst, src []byte) {
if len(src)%i.block.BlockSize() != 0 {
EncryptBlocks(i.block, i.iv, dst, src)
}

// EncryptBlocks is a simple shorthand for IGE encrypting.
// Note: unlike NewIGEEncrypter, EncryptBlocks does NOT COPY iv.
// So you must not modify passed iv.
func EncryptBlocks(block cipher.Block, iv, dst, src []byte) {
if err := checkIV(block, iv); err != nil {
panic(err.Error())
}
if len(src)%block.BlockSize() != 0 {
panic("src not full blocks")
}
if len(dst) < len(src) {
panic("len(dst) < len(src")
panic("len(dst) < len(src)")
}

b := i.block.BlockSize()
c := i.iv[:b]
m := i.iv[b:]
b := block.BlockSize()
c := iv[:b]
m := iv[b:]

for o := 0; o < len(src); o += b {
xor.Bytes(dst[o:o+b], src[o:o+b], c)
i.block.Encrypt(dst[o:o+b], dst[o:o+b])
xor.Bytes(dst[o:o+b], dst[o:o+b], m)
xor.Bytes(dst[o:o+b:o+b], src[o:o+b:o+b], c)
block.Encrypt(dst[o:o+b:o+b], dst[o:o+b:o+b])
xor.Bytes(dst[o:o+b:o+b], dst[o:o+b:o+b], m)

c = dst[o : o+b]
m = src[o : o+b]
}
}

// EncryptBlocks is a simple shorthand for IGE encrypting.
// Note: unlike NewIGEEncrypter, EncryptBlocks does NOT COPY iv.
// So you must not modify passed iv.
func EncryptBlocks(b cipher.Block, iv, dst, src []byte) {
if err := checkIV(b, iv); err != nil {
panic(err.Error())
c = dst[o : o+b : o+b]
m = src[o : o+b : o+b]
}
enc := igeEncrypter{block: b, iv: iv}
enc.CryptBlocks(dst, src)
}

0 comments on commit af79707

Please sign in to comment.