Fawkes API  Fawkes Development Version
crypto.h
1 
2 /***************************************************************************
3  * crypto.h - Protobuf stream protocol - crypto utils
4  *
5  * Created: Tue Mar 11 21:12:35 2014
6  * Copyright 2014 Tim Niemueller [www.niemueller.de]
7  ****************************************************************************/
8 
9 /* Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * - Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * - Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  * - Neither the name of the authors nor the names of its contributors
20  * may be used to endorse or promote products derived from this
21  * software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34  * OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 #ifndef _PROTOBUF_COMM_CRYPTO_H_
38 #define _PROTOBUF_COMM_CRYPTO_H_
39 
40 #include <map>
41 #include <string>
42 
43 #ifdef HAVE_LIBCRYPTO
44 # include <openssl/ossl_typ.h>
45 #endif
46 
47 namespace protobuf_comm {
48 
50 {
51 public:
52  BufferEncryptor(const std::string &key, std::string cipher_name = "AES-128-ECB");
54 
55  void encrypt(const std::string &plain, std::string &enc);
56 
57  /** Get cipher ID.
58  * @return cipher ID */
59  int
60  cipher_id() const
61  {
62  return cipher_id_;
63  }
64 
65  size_t encrypted_buffer_size(size_t plain_length);
66 
67 private:
68  unsigned char * key_;
69  long long unsigned int iv_;
70 
71 #ifdef HAVE_LIBCRYPTO
72  const EVP_CIPHER *cipher_;
73 #endif
74 
75  int cipher_id_;
76 };
77 
79 {
80 public:
81  BufferDecryptor(const std::string &key);
83 
84  size_t decrypt(int cipher, const void *enc, size_t enc_size, void *plain, size_t plain_size);
85 
86 private:
87  void generate_key(int cipher);
88 
89 private:
90  std::string key_;
91  std::map<int, std::string> keys_;
92 };
93 
94 #ifdef HAVE_LIBCRYPTO
95 const char *cipher_name_by_id(int cipher);
96 int cipher_name_to_id(const char *cipher);
97 
98 const EVP_CIPHER *cipher_by_id(int cipher);
99 const EVP_CIPHER *cipher_by_name(const char *cipher);
100 #endif
101 
102 } // namespace protobuf_comm
103 
104 #endif
BufferEncryptor(const std::string &key, std::string cipher_name="AES-128-ECB")
Constructor.
Definition: crypto.cpp:62
~BufferEncryptor()
Destructor.
Definition: crypto.cpp:86
Decrypt buffers encrypted with BufferEncryptor.
Definition: crypto.h:78
size_t encrypted_buffer_size(size_t plain_length)
Get required size for an encrypted buffer of the given plain text length.
Definition: crypto.cpp:148
size_t decrypt(int cipher, const void *enc, size_t enc_size, void *plain, size_t plain_size)
Decrypt a buffer.
Definition: crypto.cpp:221
BufferDecryptor(const std::string &key)
Constructor.
Definition: crypto.cpp:171
int cipher_id() const
Get cipher ID.
Definition: crypto.h:60
Encrypt buffers using AES128 in ECB mode.
Definition: crypto.h:49
void encrypt(const std::string &plain, std::string &enc)
Encrypt a buffer.
Definition: crypto.cpp:97