Arduino Cryptography Library
XTS.h
1 /*
2  * Copyright (C) 2016 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_XTS_h
24 #define CRYPTO_XTS_h
25 
26 #include "BlockCipher.h"
27 
28 class XTSSingleKeyCommon;
29 
30 class XTSCommon
31 {
32 public:
33  virtual ~XTSCommon();
34 
35  virtual size_t keySize() const;
36  size_t tweakSize() const;
37 
38  size_t sectorSize() const { return sectSize; }
39  bool setSectorSize(size_t size);
40 
41  virtual bool setKey(const uint8_t *key, size_t len);
42  bool setTweak(const uint8_t *tweak, size_t len);
43 
44  void encryptSector(uint8_t *output, const uint8_t *input);
45  void decryptSector(uint8_t *output, const uint8_t *input);
46 
47  void clear();
48 
49 protected:
50  XTSCommon();
51  void setBlockCiphers(BlockCipher *cipher1, BlockCipher *cipher2)
52  {
53  blockCipher1 = cipher1;
54  blockCipher2 = cipher2;
55  }
56 
57 private:
58  BlockCipher *blockCipher1;
59  BlockCipher *blockCipher2;
60  uint32_t twk[4];
61  size_t sectSize;
62 
63  friend class XTSSingleKeyCommon;
64 };
65 
67 {
68 public:
69  virtual ~XTSSingleKeyCommon();
70 
71  size_t keySize() const;
72  bool setKey(const uint8_t *key, size_t len);
73 
74 protected:
76 };
77 
78 template <typename T1, typename T2 = T1>
79 class XTS : public XTSCommon
80 {
81 public:
82  XTS() { setBlockCiphers(&cipher1, &cipher2); }
83  ~XTS() {}
84 
85 private:
86  T1 cipher1;
87  T2 cipher2;
88 };
89 
90 template <typename T>
92 {
93 public:
94  XTSSingleKey() { setBlockCiphers(&cipher, &cipher); }
96 
97 private:
98  T cipher;
99 };
100 
101 #endif
Abstract base class for block ciphers.
Definition: BlockCipher.h:30
Concrete base class to assist with implementing XTS mode for 128-bit block ciphers.
Definition: XTS.h:31
size_t tweakSize() const
Gets the maximum supported size for the tweak.
Definition: XTS.cpp:73
virtual size_t keySize() const
Gets the size of the key for XTS mode.
Definition: XTS.cpp:62
virtual bool setKey(const uint8_t *key, size_t len)
Sets the key to use for XTS mode.
Definition: XTS.cpp:119
bool setTweak(const uint8_t *tweak, size_t len)
Sets the tweak value for the current sector to encrypt or decrypt.
Definition: XTS.cpp:142
size_t sectorSize() const
Gets the size of sectors encrypted or decrypted by this class.
Definition: XTS.h:38
void decryptSector(uint8_t *output, const uint8_t *input)
Decrypts an entire sector of data.
Definition: XTS.cpp:215
bool setSectorSize(size_t size)
Sets the size of sectors encrypted or decrypted by this class.
Definition: XTS.cpp:97
void encryptSector(uint8_t *output, const uint8_t *input)
Encrypts an entire sector of data.
Definition: XTS.cpp:170
void clear()
Clears all security-sensitive state from this XTS object.
Definition: XTS.cpp:266
void setBlockCiphers(BlockCipher *cipher1, BlockCipher *cipher2)
Sets the two block ciphers to use for XTS mode.
Definition: XTS.h:51
virtual ~XTSCommon()
Clears all sensitive information and destroys this object.
Definition: XTS.cpp:49
XTSCommon()
Constructs an XTS object with a default sector size of 512 bytes.
Definition: XTS.cpp:41
Concrete base class to assist with implementing single-key XTS mode for 128-bit block ciphers.
Definition: XTS.h:67
virtual ~XTSSingleKeyCommon()
Clears all sensitive information and destroys this object.
Definition: XTS.cpp:303
size_t keySize() const
Gets the size of the key for single-pkey XTS mode.
Definition: XTS.cpp:315
XTSSingleKeyCommon()
Constructs an XTS object with a default sector size of 512 bytes.
Definition: XTS.h:75
bool setKey(const uint8_t *key, size_t len)
Sets the key to use for single-keyh XTS mode.
Definition: XTS.cpp:334
Implementation of the single-key XTS mode for 128-bit block ciphers.
Definition: XTS.h:92
XTSSingleKey()
Constructs an object for encrypting sectors in XTS mode with a single key instead of two split keys.
Definition: XTS.h:94
~XTSSingleKey()
Clears all sensitive information and destroys this object.
Definition: XTS.h:95
Implementation of the XTS mode for 128-bit block ciphers.
Definition: XTS.h:80
~XTS()
Clears all sensitive information and destroys this object.
Definition: XTS.h:83
XTS()
Constructs an object for encrypting sectors in XTS mode.
Definition: XTS.h:82