File indexing completed on 2025-01-05 04:37:19

0001 /*
0002     SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #ifndef MSERC4ENCRYPTOR_H
0007 #define MSERC4ENCRYPTOR_H
0008 
0009 #include <gcrypt.h>
0010 #include <ktorrent_export.h>
0011 #include <util/constants.h>
0012 #include <util/sha1hash.h>
0013 
0014 using bt::Uint32;
0015 using bt::Uint8;
0016 
0017 namespace mse
0018 {
0019 /**
0020  * @author Joris Guisson <joris.guisson@gmail.com>
0021  *
0022  * RC4 encryptor. Uses the RC4 algorithm to encrypt and decrypt data.
0023  * This class has a static encryption buffer, which makes it not thread safe
0024  * because the buffer is not protected by mutexes.
0025  */
0026 class KTORRENT_EXPORT RC4Encryptor
0027 {
0028 public:
0029     RC4Encryptor(const bt::SHA1Hash &dkey, const bt::SHA1Hash &ekey);
0030     virtual ~RC4Encryptor();
0031 
0032     /**
0033      * Decrypt some data, decryption happens in place (original data gets overwritten)
0034      * @param data The data
0035      * @param len Size of the data
0036      */
0037     void decrypt(Uint8 *data, Uint32 len);
0038 
0039     /**
0040      * Encrypt the data. Encryption happens into the static buffer.
0041      * So that the data passed to this function is never overwritten.
0042      * If we send pieces we point directly to the mmap region of data,
0043      * this cannot be overwritten, hence the static buffer.
0044      * @param data The data
0045      * @param len The length of the data
0046      * @return Pointer to the static buffer
0047      */
0048     const Uint8 *encrypt(const Uint8 *data, Uint32 len);
0049 
0050     /**
0051      * Encrypt data, encryption will happen in the same buffer. So data will
0052      * be changed replaced by it's encrypted version.
0053      * @param data The data to encrypt
0054      * @param len The length of the data
0055      */
0056     void encryptReplace(Uint8 *data, Uint32 len);
0057 
0058 private:
0059     gcry_cipher_hd_t enc;
0060     gcry_cipher_hd_t dec;
0061 };
0062 
0063 }
0064 
0065 #endif