Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
CC32xxAesTest.hxx
Go to the documentation of this file.
1
35#ifndef _FREERTOS_DRIVERS_TI_CC32XXAESTEST_HXX_
36#define _FREERTOS_DRIVERS_TI_CC32XXAESTEST_HXX_
37
38#define LOGLEVEL INFO
39
40#include "freertos_drivers/ti/CC32xxAes.hxx"
43#include "fs.h"
44
45namespace aes_test {
46
47int nibble_to_int(char n)
48{
49 if ('0' <= n && n <= '9')
50 {
51 return n - '0';
52 }
53 if ('a' <= n && n <= 'f')
54 {
55 return n - 'a' + 10;
56 }
57 if ('A' <= n && n <= 'F')
58 {
59 return n - 'A' + 10;
60 }
61 DIE("Unknown nibble arrived.");
62}
63
64std::string hex2str(const char *hex)
65{
66 std::string ret;
67 while (*hex && *(hex + 1))
68 {
69 if (*hex == ' ') {
70 ++hex;
71 continue;
72 }
73 ret.push_back((nibble_to_int(*hex) << 4) | (nibble_to_int(*(hex + 1))));
74 hex += 2;
75 }
76 return ret;
77}
78
79const char HEXCHR[17] = "0123456789abcdef";
80
81std::string str2hex(const string& s) {
82 std::string ret;
83 for (char c : s) {
84 ret.push_back(HEXCHR[c>>4]);
85 ret.push_back(HEXCHR[c & 0xf]);
86 }
87 return ret;
88}
89
90void get_example(int index, string &Key, string &Nonce, string &Adata,
91 string &Payload, string &CT)
92{
93#include "utils/AesCcmTestVectors.hxx"
94#include "utils/AesCcmTestVectorsEx.hxx"
95}
96
97void get_sha_example(int index, string &Key, string &Hash, string &Payload)
98{
99#include "utils/ShaTestVectors.hxx"
100}
101
102bool have_failure = false;
103
104void printcomp(const string& exp, const string& act, const char* name) {
105 if (exp == act) {
106 LOG(INFO, "%s correct.", name);
107 return;
108 }
109 have_failure = true;
110 LOG(INFO, "%s failed.", name);
111 LOG(INFO, "Expected=%s", str2hex(exp).c_str());
112 LOG(INFO, "Actual =%s", str2hex(act).c_str());
113}
114
117{
118 string key;
119 string nonce;
120 string auth_data;
121 string plain;
122 string cipher;
123 string tag;
124
125 for (int i : {40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 60, 61, 62, 63, 63, 64, 65, 66, 10, 11, 12, 13, 14, 15, 16, 17, 18})
126 {
127 get_example(i, key, nonce, auth_data, plain, cipher);
128 // Move the last part of the cipher to the tag.
129 tag = cipher.substr(plain.size());
130 cipher.resize(plain.size());
131 LOG(INFO, "\nExample %d keylen=%d nlen=%d alen=%d taglen=%d datalen=%d", i, (int)key.size(), (int)nonce.size(), (int)auth_data.size(), (int)tag.size(), (int)plain.size());
132 usleep(5000);
133 string o_plain;
134 string o_tag;
135 o_tag.resize(tag.size());
136 HASSERT(cipher.size() == plain.size());
138 key, nonce, auth_data, cipher, &o_plain, &o_tag);
139 printcomp(tag, o_tag, "tag");
140 printcomp(plain, o_plain, "plain");
141
142 LOG(INFO, "\nEncrypting");
143
144 string o_cipher;
145 o_tag.clear();
147 key, nonce, auth_data, plain, tag.size(), &o_cipher, &o_tag);
148 printcomp(tag, o_tag, "tag");
149 printcomp(cipher, o_cipher, "cipher");
150 }
151
152 for (int i = 0; i <= 67; i++)
153 {
154 string digest;
155 get_sha_example(i, key, digest, plain);
156 LOG(INFO, "SHA256 Example %d datalen=%d", i, (int)plain.size());
157 string o_digest = SHAHelper::sha256(plain.data(), plain.size());
158 printcomp(digest, o_digest, "hash");
159 }
160
161 return !have_failure;
162}
163
164} // namespace aes_test
165
166#endif // _FREERTOS_DRIVERS_TI_CC32XXAESTEST_HXX_
bool run_all_tests()
static void decrypt(const std::string &aes_key, const std::string &nonce, const std::string &auth_data, const std::string &cipher, std::string *plain, std::string *tag)
Performs authenticated decryption using CCM in one call.
Definition CC32xxAes.hxx:70
static void encrypt(const std::string &aes_key, const std::string &nonce, const std::string &auth_data, const std::string &plain, unsigned tag_len, std::string *cipher, std::string *tag)
Performs authenticated encryption using CCM in one call.
static std::string sha256(const void *data, size_t len)
Computes a SHA-256 hash of the given input data.
Definition CC32xxSha.hxx:61
#define LOG(level, message...)
Conditionally write a message to the logging output.
Definition logging.h:99
static const int INFO
Loglevel that is printed by default, reporting some status information.
Definition logging.h:57
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138
#define DIE(MSG)
Unconditionally terminates the current process with a message.
Definition macros.h:143