summaryrefslogtreecommitdiffstats
path: root/cryptopp562/elgamal.h
diff options
context:
space:
mode:
authorMalfurious <m@lfurio.us>2024-10-24 06:44:24 -0400
committerMalfurious <m@lfurio.us>2024-10-24 06:44:24 -0400
commit512aa4c77b3dc0d72db713a9215ff65a98a99ec3 (patch)
tree6db82e0109dc987b5b021f81d4e8a0926eb75ff7 /cryptopp562/elgamal.h
parent428471d39fb8c205a9fad899c88c30a2cb7df685 (diff)
parent10affea371406c0ae4c080e5a19390a8e9bd154b (diff)
downloadcompass-512aa4c77b3dc0d72db713a9215ff65a98a99ec3.tar.gz
compass-512aa4c77b3dc0d72db713a9215ff65a98a99ec3.zip
Merge branch 'mbedtls'
Replace Crypto++ 5.6.2 with Mbed TLS 3.6.0 Newer compilers are starting to show the age of the crypto library we've been using, as it is sometimes a pain to recompile compass lately. So, the tracked version of Crypto++ was at least due for an upgrade. However, I plan to soon begin reimplementing compass in C. So, I'm taking this opportunity to first just migrate the cryptography library to a newer C alternative. This branch does so, and integrates its use into the current C++ version of compass. * mbedtls: Remove unnecessary exception handler catch block Refactor random password generation to use mbedtls entropy source Refactor SHA256 function to use mbedtls Refactor AES functions to use mbedtls Add Mbedtls library Remove Crypto++ library
Diffstat (limited to '')
-rw-r--r--cryptopp562/elgamal.h121
1 files changed, 0 insertions, 121 deletions
diff --git a/cryptopp562/elgamal.h b/cryptopp562/elgamal.h
deleted file mode 100644
index 9afc30e..0000000
--- a/cryptopp562/elgamal.h
+++ /dev/null
@@ -1,121 +0,0 @@
-#ifndef CRYPTOPP_ELGAMAL_H
-#define CRYPTOPP_ELGAMAL_H
-
-#include "modexppc.h"
-#include "dsa.h"
-
-NAMESPACE_BEGIN(CryptoPP)
-
-class CRYPTOPP_NO_VTABLE ElGamalBase : public DL_KeyAgreementAlgorithm_DH<Integer, NoCofactorMultiplication>,
- public DL_KeyDerivationAlgorithm<Integer>,
- public DL_SymmetricEncryptionAlgorithm
-{
-public:
- void Derive(const DL_GroupParameters<Integer> &groupParams, byte *derivedKey, size_t derivedLength, const Integer &agreedElement, const Integer &ephemeralPublicKey, const NameValuePairs &derivationParams) const
- {
- agreedElement.Encode(derivedKey, derivedLength);
- }
-
- size_t GetSymmetricKeyLength(size_t plainTextLength) const
- {
- return GetGroupParameters().GetModulus().ByteCount();
- }
-
- size_t GetSymmetricCiphertextLength(size_t plainTextLength) const
- {
- unsigned int len = GetGroupParameters().GetModulus().ByteCount();
- if (plainTextLength <= GetMaxSymmetricPlaintextLength(len))
- return len;
- else
- return 0;
- }
-
- size_t GetMaxSymmetricPlaintextLength(size_t cipherTextLength) const
- {
- unsigned int len = GetGroupParameters().GetModulus().ByteCount();
- if (cipherTextLength == len)
- return STDMIN(255U, len-3);
- else
- return 0;
- }
-
- void SymmetricEncrypt(RandomNumberGenerator &rng, const byte *key, const byte *plainText, size_t plainTextLength, byte *cipherText, const NameValuePairs &parameters) const
- {
- const Integer &p = GetGroupParameters().GetModulus();
- unsigned int modulusLen = p.ByteCount();
-
- SecByteBlock block(modulusLen-1);
- rng.GenerateBlock(block, modulusLen-2-plainTextLength);
- memcpy(block+modulusLen-2-plainTextLength, plainText, plainTextLength);
- block[modulusLen-2] = (byte)plainTextLength;
-
- a_times_b_mod_c(Integer(key, modulusLen), Integer(block, modulusLen-1), p).Encode(cipherText, modulusLen);
- }
-
- DecodingResult SymmetricDecrypt(const byte *key, const byte *cipherText, size_t cipherTextLength, byte *plainText, const NameValuePairs &parameters) const
- {
- const Integer &p = GetGroupParameters().GetModulus();
- unsigned int modulusLen = p.ByteCount();
-
- if (cipherTextLength != modulusLen)
- return DecodingResult();
-
- Integer m = a_times_b_mod_c(Integer(cipherText, modulusLen), Integer(key, modulusLen).InverseMod(p), p);
-
- m.Encode(plainText, 1);
- unsigned int plainTextLength = plainText[0];
- if (plainTextLength > GetMaxSymmetricPlaintextLength(modulusLen))
- return DecodingResult();
- m >>= 8;
- m.Encode(plainText, plainTextLength);
- return DecodingResult(plainTextLength);
- }
-
- virtual const DL_GroupParameters_GFP & GetGroupParameters() const =0;
-};
-
-template <class BASE, class SCHEME_OPTIONS, class KEY>
-class ElGamalObjectImpl : public DL_ObjectImplBase<BASE, SCHEME_OPTIONS, KEY>, public ElGamalBase
-{
-public:
- size_t FixedMaxPlaintextLength() const {return this->MaxPlaintextLength(FixedCiphertextLength());}
- size_t FixedCiphertextLength() const {return this->CiphertextLength(0);}
-
- const DL_GroupParameters_GFP & GetGroupParameters() const {return this->GetKey().GetGroupParameters();}
-
- DecodingResult FixedLengthDecrypt(RandomNumberGenerator &rng, const byte *cipherText, byte *plainText) const
- {return Decrypt(rng, cipherText, FixedCiphertextLength(), plainText);}
-
-protected:
- const DL_KeyAgreementAlgorithm<Integer> & GetKeyAgreementAlgorithm() const {return *this;}
- const DL_KeyDerivationAlgorithm<Integer> & GetKeyDerivationAlgorithm() const {return *this;}
- const DL_SymmetricEncryptionAlgorithm & GetSymmetricEncryptionAlgorithm() const {return *this;}
-};
-
-struct ElGamalKeys
-{
- typedef DL_CryptoKeys_GFP::GroupParameters GroupParameters;
- typedef DL_PrivateKey_GFP_OldFormat<DL_CryptoKeys_GFP::PrivateKey> PrivateKey;
- typedef DL_PublicKey_GFP_OldFormat<DL_CryptoKeys_GFP::PublicKey> PublicKey;
-};
-
-//! ElGamal encryption scheme with non-standard padding
-struct ElGamal
-{
- typedef DL_CryptoSchemeOptions<ElGamal, ElGamalKeys, int, int, int> SchemeOptions;
-
- static const char * StaticAlgorithmName() {return "ElgamalEnc/Crypto++Padding";}
-
- typedef SchemeOptions::GroupParameters GroupParameters;
- //! implements PK_Encryptor interface
- typedef PK_FinalTemplate<ElGamalObjectImpl<DL_EncryptorBase<Integer>, SchemeOptions, SchemeOptions::PublicKey> > Encryptor;
- //! implements PK_Decryptor interface
- typedef PK_FinalTemplate<ElGamalObjectImpl<DL_DecryptorBase<Integer>, SchemeOptions, SchemeOptions::PrivateKey> > Decryptor;
-};
-
-typedef ElGamal::Encryptor ElGamalEncryptor;
-typedef ElGamal::Decryptor ElGamalDecryptor;
-
-NAMESPACE_END
-
-#endif