Arduino Cryptography Library
AESEsp32.cpp
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 #include "AES.h"
24 #include "Crypto.h"
25 #include <string.h>
26 
27 // AES implementation for ESP32 using the hardware crypto module.
28 
29 #if defined(CRYPTO_AES_ESP32)
30 
31 // Declare the functions in the esp-idf SDK that we need.
32 extern "C" {
33 int esp_aes_setkey(unsigned char *ctx, const unsigned char *key,
34  unsigned int keybits);
35 int esp_aes_crypt_ecb(unsigned char *ctx, int mode,
36  const unsigned char *input,
37  unsigned char *output);
38 };
39 
40 AESCommon::AESCommon(uint8_t keySize)
41 {
42  memset(ctx, 0, sizeof(ctx));
43  ctx[0] = keySize;
44 }
45 
47 {
48  clean(ctx, sizeof(ctx));
49 }
50 
51 size_t AESCommon::blockSize() const
52 {
53  return 16;
54 }
55 
56 size_t AESCommon::keySize() const
57 {
58  return ctx[0];
59 }
60 
61 bool AESCommon::setKey(const uint8_t *key, size_t len)
62 {
63  if (len == ctx[0]) {
64  esp_aes_setkey(ctx, key, len * 8);
65  return true;
66  }
67  return false;
68 }
69 
70 void AESCommon::encryptBlock(uint8_t *output, const uint8_t *input)
71 {
72  esp_aes_crypt_ecb(ctx, 1, input, output);
73 }
74 
75 void AESCommon::decryptBlock(uint8_t *output, const uint8_t *input)
76 {
77  esp_aes_crypt_ecb(ctx, 0, input, output);
78 }
79 
80 void AESCommon::clear()
81 {
82  uint8_t keySize = ctx[0];
83  clean(ctx, sizeof(ctx));
84  ctx[0] = keySize;
85 }
86 
87 AES128::~AES128()
88 {
89 }
90 
91 AES192::~AES192()
92 {
93 }
94 
95 AES256::~AES256()
96 {
97 }
98 
99 #endif // CRYPTO_AES_ESP32
void encryptBlock(uint8_t *output, const uint8_t *input)
Encrypts a single block using this cipher.
Definition: AESCommon.cpp:270
void clear()
Clears all security-sensitive state from this block cipher.
Definition: AESCommon.cpp:332
virtual ~AESCommon()
Destroys this AES block cipher object after clearing sensitive information.
Definition: AESCommon.cpp:136
void decryptBlock(uint8_t *output, const uint8_t *input)
Decrypts a single block using this cipher.
Definition: AESCommon.cpp:301
size_t blockSize() const
Size of an AES block in bytes.
Definition: AESCommon.cpp:144
AESCommon()
Constructs an AES block cipher object.
Definition: AESCommon.cpp:127
virtual bool setKey(const uint8_t *key, size_t len)=0
Sets the key to use for future encryption and decryption operations.
virtual size_t keySize() const =0
Default size of the key for this block cipher, in bytes.