selecting blockcipher+mode in an easier way.

39 views
Skip to first unread message

Johannes Winkler

unread,
Jan 4, 2024, 11:19:17 AMJan 4
to Crypto++ Users
Hello,

I am working on a GUI programme for crypto++. When selecting block ciphers dynamically (from GUI), there is a problem to choose the cipher dynamically in code.

Here is an example:
let's do AES-CFB-Encryption
CryptoPP::CFB_Mode<CryptoPP::AES>::Encryption obj;

However, if I want to switch to Twofish-OFB-Decryption
CryptoPP::OFB_Mode<CryptoPP::Twofish>::Decryption obj;
I have to change 3 spots in this line. Unfortunately, I cannot put CryptoPP::AES in a variable. Is there a possibility to choose the algorithm like
CryptoPP::A<B>::C obj; with A=CBC_Mode, B=AES, C=Decryption
Or a string-based approach like Cipher("AES-CBC-Decryption")

Currently, I have a if-else tree with 6 algorithms, 5 modes, Encrypt/Decrypt, therefore, 6x5x2=60 branches, which is annoying. Is there a better solution?


greetings from Austria 

Jeffrey Walton

unread,
Jan 4, 2024, 1:34:23 PMJan 4
to cryptop...@googlegroups.com
On Thu, Jan 4, 2024 at 11:19 AM Johannes Winkler <jwi...@gmail.com> wrote:
>
> I am working on a GUI programme for crypto++. When selecting block ciphers dynamically (from GUI), there is a problem to choose the cipher dynamically in code.
>
> Here is an example:
> let's do AES-CFB-Encryption
> CryptoPP::CFB_Mode<CryptoPP::AES>::Encryption obj;
>
> However, if I want to switch to Twofish-OFB-Decryption
> CryptoPP::OFB_Mode<CryptoPP::Twofish>::Decryption obj;
> I have to change 3 spots in this line. Unfortunately, I cannot put CryptoPP::AES in a variable. Is there a possibility to choose the algorithm like
> CryptoPP::A<B>::C obj; with A=CBC_Mode, B=AES, C=Decryption
> Or a string-based approach like Cipher("AES-CBC-Decryption")
>
> Currently, I have a if-else tree with 6 algorithms, 5 modes, Encrypt/Decrypt, therefore, 6x5x2=60 branches, which is annoying. Is there a better solution?

Yeah, the if-then-else is the way I do it, in code for PEM parsing.
See <https://github.com/noloader/cryptopp-pem/blob/master/pem_read.cpp#L312>.

You might be able to do it with an External Mode Cipher. Take a look
at CFB_Mode_ExternalCipher and friends:

$ grep Mode_ExternalCipher *.h
ccm.h: CTR_Mode_ExternalCipher::Encryption m_ctr;
eax.h: CTR_Mode_ExternalCipher::Encryption m_ctr;
gcm.h: class CRYPTOPP_DLL GCTR : public CTR_Mode_ExternalCipher::Encryption
modes.h:struct CFB_Mode_ExternalCipher : public CipherModeDocumentation
modes.h:struct CFB_FIPS_Mode_ExternalCipher : public CipherModeDocumentation
modes.h:struct OFB_Mode_ExternalCipher : public CipherModeDocumentation
modes.h:struct CTR_Mode_ExternalCipher : public CipherModeDocumentation
modes.h:struct ECB_Mode_ExternalCipher : public CipherModeDocumentation
modes.h:struct CBC_Mode_ExternalCipher : public CipherModeDocumentation
modes.h:struct CBC_CTS_Mode_ExternalCipher : public CipherModeDocumentation

Jeff
Reply all
Reply to author
Forward
0 new messages