Skip to content

Commit 802d0a6

Browse files
authored
Merge pull request #5 from simmscmi/master
add nRFCrypto_Hmac wrapper with example
2 parents 67d1ab0 + 96407d9 commit 802d0a6

File tree

6 files changed

+187
-1
lines changed

6 files changed

+187
-1
lines changed

examples/hmac/.feather52832.test.skip

Whitespace-only changes.

examples/hmac/.feather52833.test.skip

Whitespace-only changes.

examples/hmac/hmac.ino

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include "Adafruit_nRFCrypto.h"
2+
#include <Adafruit_TinyUSB.h> // for Serial
3+
4+
/* Input data for testing
5+
* to verify the result, run openssl on your PC
6+
* and compare the result with this sketch
7+
*
8+
* $ echo -ne "Hello World!" | openssl dgst -sha1 -hmac "secret"
9+
* (stdin)= 5efed98b0787c83f9cb0135ba283c390ca49320e
10+
*
11+
* $ echo -ne "Hello World!" | openssl dgst -sha224 -hmac "secret"
12+
* (stdin)= d1f9ff720c7558366f2b67e672b4f4ee7cb58b5803cd9e46380123f2
13+
*
14+
* $ echo -ne "Hello World!" | openssl dgst -sha256 -hmac "secret"
15+
* (stdin)= 6fa7b4dea28ee348df10f9bb595ad985ff150a4adfd6131cca677d9acee07dc6
16+
*
17+
* $ echo -ne "Hello World!" | openssl dgst -sha512 -hmac "secret"
18+
* (stdin)= fd3716eb8e1903ecb8deb9320f1baecf0f8dbe04e4c4c71e64fbb37080222ca42deb0b646cabeb53e269a2446507fb5442961656f8007959bd330ef48df0e13e
19+
*/
20+
uint8_t input_data[] = // "Hello World!"
21+
{
22+
0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20,
23+
0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21
24+
};
25+
26+
uint8_t key[] = // "secret"
27+
{
28+
0x73, 0x65, 0x63, 0x72, 0x65, 0x74
29+
};
30+
31+
nRFCrypto_Hmac hmac;
32+
33+
// the setup function runs once when you press reset or power the board
34+
void setup()
35+
{
36+
// initialize digital pin LED_BUILTIN as an output.
37+
pinMode(LED_BUILTIN, OUTPUT);
38+
39+
while( !Serial) delay(10);
40+
Serial.println("nRFCrypto Hmac example");
41+
42+
nRFCrypto.begin();
43+
44+
test_hmac(CRYS_HASH_SHA1_mode , "SHA-1");
45+
test_hmac(CRYS_HASH_SHA224_mode , "SHA-224");
46+
test_hmac(CRYS_HASH_SHA256_mode , "SHA-256");
47+
test_hmac(CRYS_HASH_SHA512_mode , "SHA-512");
48+
49+
nRFCrypto.end();
50+
}
51+
52+
void test_hmac(CRYS_HASH_OperationMode_t mode, const char* modestr)
53+
{
54+
uint32_t result[16];
55+
uint8_t result_len; // depending on Hash mode
56+
57+
hmac.begin(mode, key, sizeof(key));
58+
hmac.update(input_data, sizeof(input_data));
59+
result_len = hmac.end(result);
60+
61+
Serial.print(" ");
62+
Serial.flush();
63+
Serial.println(modestr);
64+
65+
Serial.printBuffer( (uint8_t*) result, result_len, ' ', 16);
66+
67+
Serial.println();
68+
Serial.println();
69+
70+
Serial.flush();
71+
}
72+
73+
void loop()
74+
{
75+
digitalToggle(LED_BUILTIN);
76+
delay(1000);
77+
}

src/Adafruit_nRFCrypto.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
* The MIT License (MIT)
33
*
44
* Copyright (c) 2020 Ha Thach (tinyusb.org) for Adafruit Industries
@@ -30,6 +30,7 @@
3030

3131
#include "nRFCrypto_Random.h"
3232
#include "nRFCrypto_Hash.h"
33+
#include "nRFCrypto_Hmac.h"
3334
#include "ecc/nRFCrypto_ECC.h"
3435

3536
class Adafruit_nRFCrypto

src/nRFCrypto_Hmac.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2023 Marcus Schmid (lanaticasylum.de)
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
#include "Adafruit_nRFCrypto.h"
26+
27+
uint8_t digestSize(CRYS_HASH_OperationMode_t mode) {
28+
switch (mode) {
29+
case CRYS_HASH_SHA1_mode:
30+
return CRYS_HASH_SHA1_DIGEST_SIZE_IN_BYTES;
31+
case CRYS_HASH_SHA224_mode:
32+
return CRYS_HASH_SHA224_DIGEST_SIZE_IN_BYTES;
33+
case CRYS_HASH_SHA256_mode:
34+
return CRYS_HASH_SHA256_DIGEST_SIZE_IN_BYTES;
35+
case CRYS_HASH_SHA384_mode:
36+
return CRYS_HASH_SHA384_DIGEST_SIZE_IN_BYTES;
37+
case CRYS_HASH_SHA512_mode:
38+
return CRYS_HASH_SHA512_DIGEST_SIZE_IN_BYTES;
39+
case CRYS_HASH_MD5_mode:
40+
return CRYS_HASH_MD5_DIGEST_SIZE_IN_BYTES;
41+
default:
42+
return 0;
43+
}
44+
}
45+
46+
nRFCrypto_Hmac::nRFCrypto_Hmac(void) { _digest_size = 0; }
47+
48+
bool nRFCrypto_Hmac::begin(CRYS_HASH_OperationMode_t mode, uint8_t *key_ptr,
49+
uint16_t keySize) {
50+
_digest_size = digestSize(mode);
51+
VERIFY_ERROR(CRYS_HMAC_Init(&_context, mode, key_ptr, keySize), false);
52+
return true;
53+
}
54+
55+
bool nRFCrypto_Hmac::update(uint8_t data[], size_t size) {
56+
VERIFY_ERROR(CRYS_HMAC_Update(&_context, data, size), false);
57+
return true;
58+
}
59+
60+
uint8_t nRFCrypto_Hmac::end(uint32_t result[16]) {
61+
VERIFY_ERROR(CRYS_HMAC_Finish(&_context, result), 0);
62+
return _digest_size;
63+
}

src/nRFCrypto_Hmac.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2023 Marcus Schmid (lanaticasylum.de)
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
#ifndef NRFCRYPTO_HMAC_H_
26+
#define NRFCRYPTO_HMAC_H_
27+
28+
#include "nrf_cc310/include/crys_hash.h"
29+
#include "nrf_cc310/include/crys_hmac.h"
30+
31+
class nRFCrypto_Hmac
32+
{
33+
public:
34+
nRFCrypto_Hmac(void);
35+
36+
bool begin(CRYS_HASH_OperationMode_t mode, uint8_t *key_ptr, uint16_t keySize);
37+
bool update(uint8_t data[], size_t size);
38+
uint8_t end(uint32_t result[16]);
39+
40+
private:
41+
CRYS_HMACUserContext_t _context;
42+
uint8_t _digest_size;
43+
};
44+
45+
#endif /* NRFCRYPTO_HMAC_H_ */

0 commit comments

Comments
 (0)