AWS SDK for C++  0.12.9
AWS SDK for C++
Cipher.h
Go to the documentation of this file.
1 /*
2 * Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15 
16 #pragma once
17 
18 #include <aws/core/Core_EXPORTS.h>
19 #include <aws/core/utils/Array.h>
21 
22 namespace Aws
23 {
24  namespace Utils
25  {
26  namespace Crypto
27  {
28  static const size_t SYMMETRIC_KEY_LENGTH = 32;
29  static const size_t MIN_IV_LENGTH = 12;
30 
36  {
37  public:
42  SymmetricCipher(const CryptoBuffer& key, size_t ivSize, bool ivGenerationInCtrMode = false) :
43  m_key(key), m_initializationVector(GenerateIV(ivSize, ivGenerationInCtrMode)), m_failure(false) { Validate(); }
44 
48  SymmetricCipher(const CryptoBuffer& key, const CryptoBuffer& initializationVector, const CryptoBuffer& tag = CryptoBuffer(0)) :
49  m_key(key), m_initializationVector(initializationVector), m_tag(tag), m_failure(false) { Validate(); }
50 
54  SymmetricCipher(CryptoBuffer&& key, CryptoBuffer&& initializationVector, CryptoBuffer&& tag = CryptoBuffer(0)) :
55  m_key(std::move(key)), m_initializationVector(std::move(initializationVector)), m_tag(std::move(tag)), m_failure(false) { Validate(); }
56 
57  SymmetricCipher(const SymmetricCipher& other) = delete;
58  SymmetricCipher& operator=(const SymmetricCipher& other) = delete;
59 
66  m_key(std::move(toMove.m_key)),
67  m_initializationVector(std::move(toMove.m_initializationVector)),
68  m_tag(std::move(toMove.m_tag)),
69  m_failure(toMove.m_failure)
70  {
71  Validate();
72  }
73 
80  {
81  m_key = std::move(toMove.m_key);
82  m_initializationVector = std::move(toMove.m_initializationVector);
83  m_tag = std::move(toMove.m_tag);
84  m_failure = toMove.m_failure;
85 
86  Validate();
87 
88  return *this;
89  }
90 
91  virtual ~SymmetricCipher() = default;
92 
97  operator bool() const { return Good(); }
98 
104  virtual CryptoBuffer EncryptBuffer( const CryptoBuffer& unEncryptedData) = 0;
105 
109  virtual CryptoBuffer FinalizeEncryption () = 0;
110 
116  virtual CryptoBuffer DecryptBuffer(const CryptoBuffer& encryptedData) = 0;
117 
121  virtual CryptoBuffer FinalizeDecryption () = 0;
122 
123  virtual void Reset() = 0;
124 
128  inline const CryptoBuffer& GetIV() const { return m_initializationVector; }
129 
134  inline const CryptoBuffer& GetTag() const { return m_tag; }
135 
136  inline bool Fail() const { return m_failure; }
137  inline bool Good() const { return !Fail(); }
138 
144  static CryptoBuffer GenerateIV(size_t ivLengthBytes, bool ctrMode = false);
145 
149  static CryptoBuffer GenerateKey(size_t keyLengthBytes = SYMMETRIC_KEY_LENGTH);
150 
151  protected:
155  bool m_failure;
156 
157  private:
158  void Validate();
159  };
160 
165  {
166  public:
168 
172  virtual std::shared_ptr<SymmetricCipher> CreateImplementation(const CryptoBuffer& key) const = 0;
176  virtual std::shared_ptr<SymmetricCipher> CreateImplementation(const CryptoBuffer& key, const CryptoBuffer& iv, const CryptoBuffer& tag = CryptoBuffer(0)) const = 0;
180  virtual std::shared_ptr<SymmetricCipher> CreateImplementation(CryptoBuffer&& key, CryptoBuffer&& iv, CryptoBuffer&& tag = CryptoBuffer(0)) const = 0;
181 
186  virtual void InitStaticState() {}
187 
192  virtual void CleanupStaticState() {}
193  };
194  }
195  }
196 }
SymmetricCipher(SymmetricCipher &&toMove)
Definition: Cipher.h:65
SymmetricCipher & operator=(SymmetricCipher &&toMove)
Definition: Cipher.h:79
static const size_t SYMMETRIC_KEY_LENGTH
Definition: Cipher.h:28
SymmetricCipher(const CryptoBuffer &key, size_t ivSize, bool ivGenerationInCtrMode=false)
Definition: Cipher.h:42
Definition: json.h:1499
SymmetricCipher(CryptoBuffer &&key, CryptoBuffer &&initializationVector, CryptoBuffer &&tag=CryptoBuffer(0))
Definition: Cipher.h:54
const CryptoBuffer & GetTag() const
Definition: Cipher.h:134
static const size_t MIN_IV_LENGTH
Definition: Cipher.h:29
SymmetricCipher(const CryptoBuffer &key, const CryptoBuffer &initializationVector, const CryptoBuffer &tag=CryptoBuffer(0))
Definition: Cipher.h:48
const CryptoBuffer & GetIV() const
Definition: Cipher.h:128
#define AWS_CORE_API
Definition: Core_EXPORTS.h:30
JSON (JavaScript Object Notation).