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 <string>
41 #include <map>
42 
43 #ifdef HAVE_LIBCRYPTO
44 # include <openssl/ossl_typ.h>
45 #endif
46 
47 namespace protobuf_comm {
48 #if 0 /* just to make Emacs auto-indent happy */
49 }
50 #endif
51 
53  public:
54  BufferEncryptor(const std::string &key, std::string cipher_name = "AES-128-ECB");
56 
57  void encrypt(const std::string &plain, std::string &enc);
58 
59  /** Get cipher ID.
60  * @return cipher ID */
61  int cipher_id() const
62  { return cipher_id_; }
63 
64  size_t encrypted_buffer_size(size_t plain_length);
65 
66  private:
67  unsigned char *key_;
68  long long unsigned int iv_;
69 
70  const EVP_CIPHER *cipher_;
71 
72  int cipher_id_;
73 };
74 
75 
77  public:
78  BufferDecryptor(const std::string &key);
79  ~BufferDecryptor();
80 
81  size_t decrypt(int cipher, const void *enc, size_t enc_size, void *plain, size_t plain_size);
82 
83  private:
84  void generate_key(int cipher);
85 
86  private:
87  std::string key_;
88  std::map<int, std::string> keys_;
89 };
90 
91 const char * cipher_name_by_id(int cipher);
92 int cipher_name_to_id(const char *cipher);
93 
94 #ifdef HAVE_LIBCRYPTO
95 const EVP_CIPHER * cipher_by_id(int cipher);
96 const EVP_CIPHER * cipher_by_name(const char *cipher);
97 #endif
98 
99 } // end namespace fawkes
100 
101 #endif
BufferEncryptor(const std::string &key, std::string cipher_name="AES-128-ECB")
Constructor.
Definition: crypto.cpp:64
~BufferEncryptor()
Destructor.
Definition: crypto.cpp:86
Decrypt buffers encrypted with BufferEncryptor.
Definition: crypto.h:76
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:154
int cipher_id() const
Get cipher ID.
Definition: crypto.h:61
Encrypt buffers using AES128 in ECB mode.
Definition: crypto.h:52
void encrypt(const std::string &plain, std::string &enc)
Encrypt a buffer.
Definition: crypto.cpp:98