📄 cts.cpp
字号:
/************************************************** CTS Mode Source File ** (C) 1999-2002 The Botan Project **************************************************/#include <botan/cts.h>namespace Botan {/************************************************** CTS Encryption Constructor **************************************************/CTS_Encryption::CTS_Encryption(const std::string& cipher_name, const SymmetricKey& key, const BlockCipherModeIV& iv) : BlockCipherMode(cipher_name, "CTS_Encrytion", key, iv, 2) { if(!valid_iv_length(iv.length())) throw Invalid_IV_Length(name(), iv.length()); }/************************************************** Encrypt a block **************************************************/void CTS_Encryption::encrypt(const byte block[]) { xor_buf(state, block, BLOCK_SIZE); cipher->encrypt(state); send(state, BLOCK_SIZE); }/************************************************** Encrypt in CTS mode **************************************************/void CTS_Encryption::write(const byte input[], u32bit length) { u32bit copied = std::min(BUFFER_SIZE - position, length); buffer.copy(position, input, copied); length -= copied; input += copied; position += copied; if(length == 0) return; if(length >= BLOCK_SIZE + 1) { encrypt(buffer); encrypt(buffer + BLOCK_SIZE); while(length > 2*BLOCK_SIZE + 1) { encrypt(input); length -= BLOCK_SIZE; input += BLOCK_SIZE; } position = 0; } else { encrypt(buffer); copy_mem(buffer.ptr(), buffer + BLOCK_SIZE, BLOCK_SIZE); position = BLOCK_SIZE; } buffer.copy(position, input, length); position += length; }/************************************************** Finish encrypting in CTS mode **************************************************/void CTS_Encryption::end_msg() { if(position < BLOCK_SIZE + 1) throw Exception("CTS_Encryption: insufficient data to encrypt"); xor_buf(state, buffer, BLOCK_SIZE); cipher->encrypt(state); SecureVector<byte> cn = state; clear_mem(buffer + position, BUFFER_SIZE - position); encrypt(buffer + BLOCK_SIZE); send(cn, position - BLOCK_SIZE); }/************************************************** CTS Decryption Constructor **************************************************/CTS_Decryption::CTS_Decryption(const std::string& cipher_name, const SymmetricKey& key, const BlockCipherModeIV& iv) : BlockCipherMode(cipher_name, "CTS_Decryption", key, iv, 2) { if(!valid_iv_length(iv.length())) throw Invalid_IV_Length(name(), iv.length()); temp.create(BLOCK_SIZE); }/************************************************** Decrypt a block **************************************************/void CTS_Decryption::decrypt(const byte block[]) { cipher->decrypt(block, temp); xor_buf(temp, state, BLOCK_SIZE); send(temp, BLOCK_SIZE); state.copy(block, BLOCK_SIZE); }/************************************************** Decrypt in CTS mode **************************************************/void CTS_Decryption::write(const byte input[], u32bit length) { u32bit copied = std::min(BUFFER_SIZE - position, length); buffer.copy(position, input, copied); length -= copied; input += copied; position += copied; if(length == 0) return; if(length >= BLOCK_SIZE + 1) { decrypt(buffer); decrypt(buffer + BLOCK_SIZE); while(length > 2*BLOCK_SIZE + 1) { decrypt(input); length -= BLOCK_SIZE; input += BLOCK_SIZE; } position = 0; } else { decrypt(buffer); copy_mem(buffer.ptr(), buffer + BLOCK_SIZE, BLOCK_SIZE); position = BLOCK_SIZE; } buffer.copy(position, input, length); position += length; }/************************************************** Finish decrypting in CTS mode **************************************************/void CTS_Decryption::end_msg() { cipher->decrypt(buffer, temp); xor_buf(temp, buffer + BLOCK_SIZE, position - BLOCK_SIZE); SecureVector<byte> xn = temp; copy_mem(buffer + position, xn + (position - BLOCK_SIZE), BUFFER_SIZE - position); cipher->decrypt(buffer + BLOCK_SIZE, temp); xor_buf(temp, state, BLOCK_SIZE); send(temp, BLOCK_SIZE); send(xn, position - BLOCK_SIZE); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -