Arduino Cryptography Library
Acorn128.h
1 /*
2  * Copyright (C) 2018 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_ACORN128_H
24 #define CRYPTO_ACORN128_H
25 
26 #include "AuthenticatedCipher.h"
27 
30 // The ACORN-128 state consists of 293 bits split across six
31 // Linear Feedback Shift Registers (LFSR's) and 4 bits spare.
32 // In this implementation, each LFSR is represented by a
33 // 48-bit or 64-bit register split into 32/16-bit words.
34 // The optimized reference implementation from the algorithm's
35 // authors uses 7 uint64_t registers, for a total state size
36 // of 448 bits. This version uses 328 bits for same data and
37 // should be efficient on 8-bit and 32-bit microcontrollers.
38 typedef struct
39 {
40  uint32_t k[4]; // Cached copy of the key for multiple requests.
41  uint32_t s1_l; // LFSR1, 61 bits, 0..60, low word
42  uint32_t s1_h; // LFSR1, high word
43  uint32_t s2_l; // LFSR2, 46 bits, 61..106, low word
44  uint16_t s2_h; // LFSR2, high word
45  uint16_t s3_h; // LFSR3, 47 bits, 107..153, high word
46  uint32_t s3_l; // LFSR3, low word
47  uint32_t s4_l; // LFSR4, 39 bits, 154..192, low word
48  uint16_t s4_h; // LFSR4, high word
49  uint16_t s5_h; // LFSR5, 37 bits, 193..229, high word
50  uint32_t s5_l; // LFSR5, low word
51  uint32_t s6_l; // LFSR6, 59 bits, 230..288, low word
52  uint32_t s6_h; // LFSR6, high word
53  uint8_t s7; // Top most 4 bits, 289..292
54  uint8_t authDone; // Non-zero once authentication is done.
55 
56 } Acorn128State;
57 
58 // Determine which Acorn128 implementation to export to applications.
59 #if defined(__AVR__)
60 #define CRYPTO_ACORN128_AVR 1
61 #else
62 #define CRYPTO_ACORN128_DEFAULT 1
63 #endif
64 
68 {
69 public:
70  Acorn128();
71  virtual ~Acorn128();
72 
73  size_t keySize() const;
74  size_t ivSize() const;
75  size_t tagSize() const;
76 
77  bool setKey(const uint8_t *key, size_t len);
78  bool setIV(const uint8_t *iv, size_t len);
79 
80  void encrypt(uint8_t *output, const uint8_t *input, size_t len);
81  void decrypt(uint8_t *output, const uint8_t *input, size_t len);
82 
83  void addAuthData(const void *data, size_t len);
84 
85  void computeTag(void *tag, size_t len);
86  bool checkTag(const void *tag, size_t len);
87 
88  void clear();
89 
90 private:
91  Acorn128State state;
92 };
93 
94 #endif
ACORN-128 authenticated cipher.
Definition: Acorn128.h:68
bool checkTag(const void *tag, size_t len)
Finalizes the decryption process and checks the authentication tag.
Definition: Acorn128.cpp:660
virtual ~Acorn128()
Destroys this Acorn128 authenticated cipher.
Definition: Acorn128.cpp:54
bool setKey(const uint8_t *key, size_t len)
Sets the key to use for future encryption and decryption operations.
Definition: Acorn128.cpp:477
void computeTag(void *tag, size_t len)
Finalizes the encryption process and computes the authentication tag.
Definition: Acorn128.cpp:631
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: Acorn128.cpp:554
size_t keySize() const
Gets the size of the Acorn128 key in bytes.
Definition: Acorn128.cpp:64
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: Acorn128.cpp:580
size_t tagSize() const
Gets the size of the Acorn128 authentication tag in bytes.
Definition: Acorn128.cpp:87
void addAuthData(const void *data, size_t len)
Adds extra data that will be authenticated but not encrypted.
Definition: Acorn128.cpp:606
void clear()
Clears all security-sensitive state from this cipher object.
Definition: Acorn128.cpp:677
bool setIV(const uint8_t *iv, size_t len)
Sets the initialization vector to use for future encryption and decryption operations.
Definition: Acorn128.cpp:495
size_t ivSize() const
Gets the size of the Acorn128 initialization vector in bytes.
Definition: Acorn128.cpp:77
Acorn128()
Constructs a new Acorn128 authenticated cipher.
Definition: Acorn128.cpp:46
Abstract base class for authenticated ciphers.