1 /*
2  * Copyright (C) 2018 Rafael Ostertag
3  *
4  * This file is part of YAPET.
5  *
6  * YAPET is free software: you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation, either version 3 of the License, or (at your option) any later
9  * version.
10  *
11  * YAPET is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * YAPET.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  * Additional permission under GNU GPL version 3 section 7
20  *
21  * If you modify this program, or any covered work, by linking or combining it
22  * with the OpenSSL project's OpenSSL library (or a modified version of that
23  * library), containing parts covered by the terms of the OpenSSL or SSLeay
24  * licenses, Rafael Ostertag grants you additional permission to convey the
25  * resulting work.  Corresponding Source for a non-source form of such a
26  * combination shall include the source code for the parts of OpenSSL used as
27  * well as that of the covered work.
28  */
29 
30 #ifndef _CRYPTO_HH
31 #define _CRYPTO_HH
32 
33 #include <openssl/evp.h>
34 #include <memory>
35 
36 #include "key.hh"
37 #include "securearray.hh"
38 
39 namespace yapet {
40 /**
41  * Base class for encryption and decryption.
42  */
43 class Crypto {
44    private:
45     std::shared_ptr<Key> _key;
46 
47    protected:
48     static constexpr auto SSL_SUCCESS{1};
49     enum MODE { DECRYPTION = 0, ENCRYPTION = 1 };
50 
51     EVP_CIPHER_CTX* createContext();
52     void destroyContext(EVP_CIPHER_CTX* context);
53     EVP_CIPHER_CTX* initializeOrThrow(MODE mode);
54 
55     void checkIVSizeOrThrow();
56     void validateCipherOrThrow();
57 
58     int cipherIvecSize() const;
59     int cipherBlockSize() const;
60 
61     virtual const EVP_CIPHER* getCipher() const = 0;
62 
63    public:
64     /**
65      * Initializes the class with the given key, which is used for
66      * encryption and decryption.
67      *
68      * The constructor tries to set the key length of the cipher used to
69      * the length of the key provided. If this fails, a \c YAPETException is
70      * thrown.
71      *
72      * @param k the key used for encryption/decryption.
73      */
74     Crypto(const std::shared_ptr<Key>& key);
~Crypto()75     virtual ~Crypto(){};
76 
77     Crypto(const Crypto&);
78     Crypto& operator=(const Crypto& c);
79 
80     Crypto(Crypto&& c);
81     Crypto& operator=(Crypto&& c);
82 
83     /**
84      * Encrypt data using the cipher provided by \c getCipher().
85      *
86      * @throw YAPETException in case the key length of the cipher cannot be
87      * set to the length of the key provided.
88      *
89      * @throw YAPETEncryptionException in case of cipher errors.
90      */
91     virtual SecureArray encrypt(const SecureArray& plainText);
92     /**
93      * Decrypt data using the cipher provided by \c getCipher().
94      *
95      * @throw YAPETException in case the key length of the cipher cannot be
96      * set to the length of the key provided.
97      *
98      * @throw YAPETEncryptionException in case of cipher errors.
99      */
100     virtual SecureArray decrypt(const SecureArray& cipherText);
101 
getKey() const102     std::shared_ptr<Key> getKey() const { return _key; }
103 };
104 }  // namespace yapet
105 
106 #endif