Arduino Cryptography Library
 All Classes Files Functions Variables Enumerations Enumerator Pages
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 AESCommon::AESCommon(uint8_t keySize)
32 {
33  ctx.key_bytes = keySize;
34 }
35 
37 {
38  clean(ctx.key, sizeof(ctx.key));
39 }
40 
41 size_t AESCommon::blockSize() const
42 {
43  return 16;
44 }
45 
46 size_t AESCommon::keySize() const
47 {
48  return ctx.key_bytes;
49 }
50 
51 bool AESCommon::setKey(const uint8_t *key, size_t len)
52 {
53  if (len == ctx.key_bytes) {
54  // Do the effect of esp_aes_setkey() which is just a memcpy().
55  memcpy(ctx.key, key, len);
56  return true;
57  }
58  return false;
59 }
60 
61 void AESCommon::encryptBlock(uint8_t *output, const uint8_t *input)
62 {
63  esp_aes_encrypt(&ctx, input, output);
64 }
65 
66 void AESCommon::decryptBlock(uint8_t *output, const uint8_t *input)
67 {
68  esp_aes_decrypt(&ctx, input, output);
69 }
70 
71 void AESCommon::clear()
72 {
73  clean(ctx.key, sizeof(ctx.key));
74 }
75 
76 AES128::~AES128()
77 {
78 }
79 
80 AES192::~AES192()
81 {
82 }
83 
84 AES256::~AES256()
85 {
86 }
87 
88 #endif // CRYPTO_AES_ESP32
void decryptBlock(uint8_t *output, const uint8_t *input)
Decrypts a single block using this cipher.
Definition: AESCommon.cpp:301
AESCommon()
Constructs an AES block cipher object.
Definition: AESCommon.cpp:127
size_t blockSize() const
Size of an AES block in bytes.
Definition: AESCommon.cpp:144
virtual ~AESCommon()
Destroys this AES block cipher object after clearing sensitive information.
Definition: AESCommon.cpp:136
void clear()
Clears all security-sensitive state from this block cipher.
Definition: AESCommon.cpp:332
void encryptBlock(uint8_t *output, const uint8_t *input)
Encrypts a single block using this cipher.
Definition: AESCommon.cpp:270
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.