scclib
Stable Cloud Computing C++ Library
cipher.h
Go to the documentation of this file.
1 /*
2 BSD 3-Clause License
3 
4 Copyright (c) 2022, Stable Cloud Computing, Inc.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice, this
10  list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #ifndef _SCC_CRYPTO_CIPHER_H
32 #define _SCC_CRYPTO_CIPHER_H
33 
34 #include <stdint.h>
35 #include <string>
36 #include <vector>
37 //#include <util/iobase.h>
38 
39 namespace scc::crypto {
40 
83 {
84 public:
85  virtual void reset(const void*, int, const void*, int/*, uint64_t*/) = 0;
86  virtual void aad(const void*, int) = 0;
87  virtual void encrypt(const void*, int, void*, int) = 0;
88  virtual void decrypt(const void*, int, void*, int) = 0;
89  virtual void auth_tag(void*, int) = 0;
90 };
91 
96 class Cipher
97 {
98  CipherBase* m_ctx;
99  int m_type;
100 public:
101 
102  enum Type
103  {
104  aes_gcm_type =1000,
105  aes_ccm_type =2000,
106  };
107 
134  Cipher(Type type, const void* key_loc, int key_len, int = 16);
135  Cipher(Type type, const std::vector<char>& key, int tag_len = 16) : Cipher(type, key.data(), key.size(), tag_len) {}
136  Cipher(Type type, const std::string& key, int tag_len = 16) : Cipher(type, key.data(), key.size(), tag_len) {}
137  virtual ~Cipher();
138  Cipher(const Cipher&) = delete; // no copy
139  Cipher& operator=(const Cipher&) = delete;
140  Cipher(Cipher&& other)
141  {
142  m_ctx = other.m_ctx;
143  m_type = other.m_type;
144  other.m_ctx = nullptr;
145  other.m_type = 0;
146  }
147  Cipher& operator=(Cipher&& other)
148  {
149  m_ctx = other.m_ctx;
150  m_type = other.m_type;
151  other.m_ctx = nullptr;
152  other.m_type = 0;
153  return *this;
154  }
155 
156  size_t nonce_min() const
157  {
158  switch (m_type)
159  {
160  case aes_gcm_type: return 1;
161  case aes_ccm_type: return 8;
162  }
163  }
164  size_t nonce_max() const
165  {
166  switch (m_type)
167  {
168  case aes_gcm_type: return 128; // not specified
169  case aes_ccm_type: return 12;
170  }
171  }
172 
191  void reset(const void* nonce_loc, int nonce_len, const void* aad_loc = nullptr, int aad_len = 0/*, size_t msg_len*/)
192  {
193  m_ctx->reset(nonce_loc, nonce_len, aad_loc, aad_len);
194  }
195 
204  void aad(const void* aad_loc, int aad_len)
205  {
206  m_ctx->aad(aad_loc, aad_len);
207  }
208 
220  void encrypt(const void* msg_loc, int msg_len, void* cipher_loc, int cipher_len)
221  {
222  m_ctx->encrypt(msg_loc, msg_len, cipher_loc, cipher_len);
223  }
224 
236  void decrypt(const void* cipher_loc, int cipher_len, void* msg_loc, int msg_len)
237  {
238  m_ctx->decrypt(cipher_loc, cipher_len, msg_loc, msg_len);
239  }
240 
248  void auth_tag(char* tag_loc, int tag_len)
249  {
250  m_ctx->auth_tag(tag_loc, tag_len);
251  }
252 };
256 } // namespace
257 
258 #endif
Symmetric block cipher.
Definition: cipher.h:97
Cipher(Type type, const void *key_loc, int key_len, int=16)
Create a cipher.
void decrypt(const void *cipher_loc, int cipher_len, void *msg_loc, int msg_len)
Decrypt a message.
Definition: cipher.h:236
void reset(const void *nonce_loc, int nonce_len, const void *aad_loc=nullptr, int aad_len=0)
Reset the processor to prepare to encrypt or decrypt a new message.
Definition: cipher.h:191
void encrypt(const void *msg_loc, int msg_len, void *cipher_loc, int cipher_len)
Encrypt a message.
Definition: cipher.h:220
void aad(const void *aad_loc, int aad_len)
Set additional authenticated data (for GCM type only).
Definition: cipher.h:204
void auth_tag(char *tag_loc, int tag_len)
Gets the authentication tag.
Definition: cipher.h:248