Skip to content

Commit

Permalink
Changed to use builtin copy.
Browse files Browse the repository at this point in the history
  • Loading branch information
markkurossi committed Jul 25, 2024
1 parent 2a2e52c commit 6c5c269
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions pkg/crypto/cipher/cts/cts.mpcl
Original file line number Diff line number Diff line change
Expand Up @@ -42,37 +42,37 @@ func EncryptAES128(key [16]byte, iv [aes.BlockSize]byte, data []byte) []byte {
panic("cts.EncryptAES128: input must be at least 2 block")
}
var block [aes.BlockSize]byte
block = memcpy(block, 0, iv, 0)
copy(block, iv)

var plain [aes.BlockSize]byte
var cipher [len(data)]byte

// Standard CBC for the first numBlocks-1 blocks.
for i := 0; i < numBlocks-1; i++ {
plain = memcpy(plain, 0, data, i*aes.BlockSize)
copy(plain, data[i*aes.BlockSize:])
for j := 0; j < aes.BlockSize; j++ {
plain[j] ^= block[j]
}
block = aes.EncryptBlock(key, plain)
if i < numBlocks-2 {
// Store standard CBC output block.
cipher = memcpy(cipher, i*aes.BlockSize, block, 0)
copy(cipher[i*aes.BlockSize:], block)
} else {
// Store last ciphertext block.
cipher = memcpy(cipher, (numBlocks-1)*aes.BlockSize, block, 0)
copy(cipher[(numBlocks-1)*aes.BlockSize:], block)
}
}

// Create last input block.
plain = memcpy(plain, 0, data, (numBlocks-1)*aes.BlockSize)
copy(plain, data[(numBlocks-1)*aes.BlockSize:])
for i := tail; i < aes.BlockSize; i++ {
plain[i] = 0
}
for j := 0; j < aes.BlockSize; j++ {
plain[j] ^= block[j]
}
block = aes.EncryptBlock(key, plain)
cipher = memcpy(cipher, (numBlocks-2)*aes.BlockSize, block, 0)
copy(cipher[(numBlocks-2)*aes.BlockSize:], block)

return cipher
}
Expand Down Expand Up @@ -100,43 +100,36 @@ func DecryptAES128(key [16]byte, iv [aes.BlockSize]byte, data []byte) []byte {

// Standard CBC for the first numBlocks-2 blocks.
for i := 0; i < numBlocks-2; i++ {
cipher = memcpy(cipher, 0, data, i*aes.Blocks)
copy(cipher, data[i*aes.Blocks:])
block = aes.DecryptBlock(key, cipher)
for j := 0; j < aes.BlockSize; j++ {
block[j] ^= iv[j]
}
plain = memcpy(plain, i*aes.BlockSize, block, 0)
iv = memcpy(iv, 0, cipher, 0)
copy(plain[i*aes.BlockSize:], block)
copy(iv, cipher)
}

// Decrypt second-to-last cipher block.
cipher = memcpy(cipher, 0, data, (numBlocks-2)*aes.BlockSize)
copy(cipher, data[(numBlocks-2)*aes.BlockSize:])
tmp := aes.DecryptBlock(key, cipher)

// Create padded last cipher block.
tmp2 = memcpy(tmp2, 0, data, (numBlocks-1)*aes.BlockSize)
tmp2 = memcpy(tmp2, tail, tmp, tail)
copy(tmp2, data[(numBlocks-1)*aes.BlockSize:])
copy(tmp2[tail:], tmp[tail:])

// Decrypt second-to-last block.
block = aes.DecryptBlock(key, tmp2)
for j := 0; j < aes.BlockSize; j++ {
block[j] ^= iv[j]
}
plain = memcpy(plain, (numBlocks-2)*aes.BlockSize, block, 0)
iv = memcpy(iv, 0, tmp2, 0)
copy(plain[(numBlocks-2)*aes.BlockSize:], block)
copy(iv, tmp2)

// Finalize last block.
for j := 0; j < aes.BlockSize; j++ {
tmp[j] ^= iv[j]
}
plain = memcpy(plain, (numBlocks-1)*aes.BlockSize, tmp, 0)
copy(plain[(numBlocks-1)*aes.BlockSize:], tmp)

return plain
}

func memcpy(dst []byte, dstOfs int, src []byte, srcOfs int) []byte {
for i := 0; srcOfs+i < len(src) && dstOfs+i < len(dst); i++ {
dst[dstOfs+i] = src[srcOfs+i]
}
return dst
}

0 comments on commit 6c5c269

Please sign in to comment.