Part 4: CBC (Lockbox3's version) implemented correctly
Lockbox3 distinguishes between 4 types of plaintext / ciphertext byte arrays
1) Zero length/empty message: The output should be an empty result. This is covered in the published library
2) Short messages: A message that has an underlying byte array of less then 16 bytes. This is covered in published library/code above
3) Round messages: A message that has an underlying byte array of 16 or more bytes and whose length is a multiple of 16. This is covered in the published library/code above
4) Sharp message: A message that has an underlying byte array of more than 16 bytes and whose length is not a multiple of 16. This is where everything goes wrong. For these types of messages, Lockbox3 uses ciphertext stealing. In the crypto libraries for Java (B4J/B4A), this refers to the CTS mode of a cipher. If you look at the code above, I use CTS mode for all messages larger than 15 bytes. For messages that are a multiple of 16 in length, CTS is just plain CBC and does not perform any ciphertext stealing. So with the code above (in the case of encryption)
If dataBytes.Length > 15 Then
c.Initialize("AES/CTS/NoPadding")
Else
c.Initialize("AES/CFB8/NoPadding")
End If
c.InitialisationVector = IV
Dim encryptedBytes() as Byte = c.Encrypt(dataBytes, k.Key, True)
I thought I had cases 2 through 4 covered, with the CFB8 mode covering case 2 and the CTS mode covering case 3 and 4 (case #1 is a quick check on the incoming ciphertext/plaintext to see if it contains anything and returning an empty string in case of no content). The problem is, that for sharp messages, everything seemed to encrypt/decrypt correctly except for the last two blocks. It is the last two blocks that ciphertext stealing concentrates upon.
So now what? First, lets look at the steps performed upon the last two blocks in a "normal" ciphertext stealing process (source:
https://en.wikipedia.org/wiki/Ciphertext_stealing) (BTW, everything up to the last two blocks uses standard CBC for decryption/encryption):
For decryption:
c = ciphertext bytes
p = plaintext bytes
N = the last block
N - 1 = the second to last block
N - 2 = the third to last block
Prep work:
If c(N-2) exists, set IV to c(N-2), otherwise the IV is the IV passed to the algorithm
Step A:
Decrypt c(N-1) using ECB mode with given key
Step B:
1) Take c(N) and fill in the remaining bytes from the result generated in step A above to create a 16 byte c(N) block. For example, if c(N) is 15 bytes long, the fill in its 16th byte from the 16th byte of step A result
2) Decrypt c(N) using ECB mode with the given key and XOR the result with IV, generating p(N-1)
Step C:
1) XOR the result from step A with the result from step B1, generating p(N)
2) Trim p(N) to original length of c(N)
Final steps:
Decrypt (ciphertext - last two blocks) using CBC mode with given IV and password
Append p(N-1) and then p(N).
So how does Lockbox3 differ? I looked at the source code, but alas, I could not decipher the Pascal (Delphi) code. Luckily, I found an article on stackoverflow:
https://stackoverflow.com/a/12330169.
LockBox3 CTS decryption:
Prep work:
Unchanged
Step A:
1) Decrypt c(N-1) using ECB mode with given key
2) XOR result with IV, generating p(N) (Notice Lockbox uses the same IV for both XOR's)
Step B:
1) Take c(N) and fill in the remaining bytes from the result generated in
step A2 above to create a 16 byte c(N) block. For example, if c(N) is 15 bytes long, the fill in its 16th byte from the 16th byte of
step A2 result
2) Decrypt c(N) using ECB mode with the given key and XOR the result with IV, generating p(N-1)
Step C:
1)
2) Trim p(N) to original length of c(N)
Final steps:
Decrypt (ciphertext - last two blocks) using CBC mode with given IV and password
Append p(N-1) and then p(N).
So, a simple
If dataBytes.Length > 15 Then
c.Initialize("AES/CTS/NoPadding")
Else
c.Initialize("AES/CFB8/NoPadding")
End If
c.InitialisationVector = IV
Dim plainTextBytes() As Byte = c.Decrypt(dataBytes, k.Key, True)
plainText = BytesToString(plainTextBytes, 0, plainTextBytes.Length, "UTF8")
becomes
Dim decryptedBytes() As Byte
If dataBytes.Length > 15 Then
Dim fullBlocks As Int = dataBytes.Length / 16
Dim lastBlockSize As Int = dataBytes.Length Mod 16
Dim numberOfBlocks As Int = fullBlocks
If lastBlockSize > 0 Then numberOfBlocks = numberOfBlocks + 1
cCBC.InitialisationVector = IV
If fullBlocks = numberOfBlocks Then
'We have a round message
decryptedBytes = cCBC.Decrypt(dataBytes, k.Key, True)
Else
'We have a sharp message
Dim bb As B4XBytesBuilder
bb.Initialize
'Everything but the last full block and it's trailing partial block can be decrypted via standard CBC
If fullBlocks > 1 Then
Dim frontCipherData(dataBytes.Length - 16 - lastBlockSize) As Byte
bc.ArrayCopy(dataBytes, 0, frontCipherData, 0, dataBytes.Length - 16 - lastBlockSize)
Dim frontCipherDecrypted() As Byte = cCBC.Decrypt(frontCipherData, k.Key, True)
bb.Append(frontCipherDecrypted)
'IV is the last block of "frontCipherData"
bc.ArrayCopy(frontCipherData, frontCipherData.Length - 16, IV, 0, 16)
End If
'Take 2nd to last block of ciphertext data, ECB decrypt and XOR with IV
Dim secondToLastBlock(16) As Byte
bc.ArrayCopy(dataBytes, dataBytes.Length - 16 - lastBlockSize, secondToLastBlock, 0, 16)
Dim lastBlockDecrypted() As Byte = cECB.Decrypt(secondToLastBlock, k.Key, False)
For x = 0 To lastBlockDecrypted.Length - 1
lastBlockDecrypted(x) = Bit.Xor(lastBlockDecrypted(x), IV(x))
Next
'Take last block of ciphertext data, fill remaining bytes with corresponding bytes (in position) from lastBlockDecrypted created above,
' ECB decrypt and XOR with IV (note: IV does not change!)
Dim lastBlock(16) As Byte
bc.ArrayCopy(dataBytes, dataBytes.Length - lastBlockSize, lastBlock, 0, lastBlockSize)
For x = lastBlockSize To lastBlock.Length - 1
lastBlock(x) = lastBlockDecrypted(x)
Next
Dim secondToLastBlockDecrypted(16) As Byte = cECB.Decrypt(lastBlock, k.Key, False)
For x = 0 To secondToLastBlockDecrypted.Length - 1
secondToLastBlockDecrypted(x) = Bit.Xor(secondToLastBlockDecrypted(x), IV(x))
Next
'Add the last two blocks and generate plaintext bytes
bb.Append(secondToLastBlockDecrypted)
'Trim last block to appropriate size
Dim lastBlockDecryptedSized(lastBlockSize) As Byte
bc.ArrayCopy(lastBlockDecrypted, 0, lastBlockDecryptedSized, 0, lastBlockSize)
bb.Append(lastBlockDecryptedSized)
decryptedBytes = bb.ToArray
End If
Else
'We have a short message
cCFB.InitialisationVector = IV
decryptedBytes = cCFB.Decrypt(dataBytes, k.Key, True)
End If
plainText = BytesToString(decryptedBytes, 0, decryptedBytes.Length, "UTF8")
So please, for anyone thinking about creating their own encryption library, don't. Sooner or later, someone wants to interoperate with your library and then the same person or someone else has to figure out what the heck you did!