Skinny-C
 All Data Structures Files Functions Variables Groups Pages
CTR.h
1 /*
2  * Copyright (C) 2015 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef CRYPTO_CTR_h
24 #define CRYPTO_CTR_h
25 
26 #include "Cipher.h"
27 #include "BlockCipher.h"
28 
29 class CTRCommon : public Cipher
30 {
31 public:
32  virtual ~CTRCommon();
33 
34  size_t keySize() const;
35  size_t ivSize() const;
36 
37  bool setCounterSize(size_t size);
38 
39  bool setKey(const uint8_t *key, size_t len);
40  bool setIV(const uint8_t *iv, size_t len);
41 
42  void encrypt(uint8_t *output, const uint8_t *input, size_t len);
43  void decrypt(uint8_t *output, const uint8_t *input, size_t len);
44 
45  void clear();
46 
47 protected:
48  CTRCommon();
49  void setBlockCipher(BlockCipher *cipher) { blockCipher = cipher; }
50 
51 private:
52  BlockCipher *blockCipher;
53  uint8_t counter[16];
54  uint8_t state[16];
55  uint8_t posn;
56  uint8_t counterStart;
57 };
58 
59 template <typename T>
60 class CTR : public CTRCommon
61 {
62 public:
63  CTR() { setBlockCipher(&cipher); }
64 
65 private:
66  T cipher;
67 };
68 
69 #endif
Abstract base class for stream ciphers.
Definition: Cipher.h:29
CTR()
Constructs a new CTR object for the 128-bit block cipher T.
Definition: CTR.h:63
Abstract base class for block ciphers.
Definition: BlockCipher.h:29
void setBlockCipher(BlockCipher *cipher)
Sets the block cipher to use for this CTR object.
Definition: CTR.h:49
bool setIV(const uint8_t *iv, size_t len)
Sets the initial counter value to use for future encryption and decryption operations.
Definition: CTR.cpp:119
bool setCounterSize(size_t size)
Sets the counter size for the IV.
Definition: CTR.cpp:86
void encrypt(uint8_t *output, const uint8_t *input, size_t len)
Encrypts an input buffer and writes the ciphertext to an output buffer.
Definition: CTR.cpp:128
void decrypt(uint8_t *output, const uint8_t *input, size_t len)
Decrypts an input buffer and writes the plaintext to an output buffer.
Definition: CTR.cpp:160
CTRCommon()
Constructs a new cipher in CTR mode.
Definition: CTR.cpp:42
size_t keySize() const
Default size of the key for this cipher, in bytes.
Definition: CTR.cpp:57
size_t ivSize() const
Size of the initialization vector for this cipher, in bytes.
Definition: CTR.cpp:62
bool setKey(const uint8_t *key, size_t len)
Sets the key to use for future encryption and decryption operations.
Definition: CTR.cpp:94
Implementation of the Counter (CTR) mode for 128-bit block ciphers.
Definition: CTR.h:60
void clear()
Clears all security-sensitive state from this cipher.
Definition: CTR.cpp:165
Concrete base class to assist with implementing CTR mode for 128-bit block ciphers.
Definition: CTR.h:29