ruuvi.drivers.c ${PROJECT_VERSION}
Drivers for external sensors and peripherals on embedded systems.
Loading...
Searching...
No Matches
ruuvi_nrf5_sdk15_aes.c
Go to the documentation of this file.
1
12#include "ruuvi_interface_aes.h"
13#if RUUVI_NRF5_SDK15_AES_ENABLED
14
15#include "ruuvi_driver_error.h"
17#include "nrf_crypto_aes.h"
18#include "nrf_sdh.h"
19#include "nrf_soc.h"
20
21#define ECB_128_BLOCK_SIZE_BYTES (16U)
22
23static void aes_ecb_128_encrypt_sdapi (const uint8_t * const cleartext,
24 uint8_t * const ciphertext,
25 const uint8_t * const key,
26 const size_t data_length)
27{
28 const size_t crypt_rounds = (data_length / ECB_128_BLOCK_SIZE_BYTES);
29 nrf_ecb_hal_data_t soc_ecb_data;
30 memcpy (soc_ecb_data.key, key, ECB_128_BLOCK_SIZE_BYTES);
31
32 for (size_t round = 0; round < crypt_rounds; round++)
33 {
34 memcpy (soc_ecb_data.cleartext,
35 cleartext + (round * ECB_128_BLOCK_SIZE_BYTES),
36 ECB_128_BLOCK_SIZE_BYTES);
37 sd_ecb_block_encrypt (&soc_ecb_data);
38 memcpy (ciphertext + (round * ECB_128_BLOCK_SIZE_BYTES),
39 soc_ecb_data.ciphertext,
40 ECB_128_BLOCK_SIZE_BYTES);
41 }
42}
43
44static rd_status_t
45aes_ecb_128_encrypt_nrfapi (const uint8_t * const cleartext,
46 uint8_t * const ciphertext,
47 const uint8_t * const key,
48 const size_t data_length)
49{
50 ret_code_t err_code = NRF_SUCCESS;
51 size_t out_data_len;
52 // nRF API does not use consts
53 err_code |= nrf_crypto_aes_crypt (NULL,
54 &g_nrf_crypto_aes_ecb_128_info,
55 NRF_CRYPTO_ENCRYPT,
56 (uint8_t *) key,
57 NULL,
58 (uint8_t *) cleartext,
59 data_length,
60 ciphertext,
61 &out_data_len);
62
63 if (data_length != out_data_len)
64 {
65 err_code |= RD_ERROR_DATA_SIZE;
66 }
67
68 err_code |= ruuvi_nrf5_sdk15_to_ruuvi_error (err_code);
69 return err_code;
70}
71
86rd_status_t ri_aes_ecb_128_encrypt (const uint8_t * const cleartext,
87 uint8_t * const ciphertext,
88 const uint8_t * const key,
89 const size_t data_length)
90{
91 rd_status_t err_code = RD_SUCCESS;
92
93 if ( (NULL == cleartext) || (NULL == ciphertext) || (NULL == key))
94 {
95 err_code |= RD_ERROR_NULL;
96 }
97 else if ( (!data_length) || (data_length % ECB_128_BLOCK_SIZE_BYTES))
98 {
99 err_code |= RD_ERROR_INVALID_LENGTH;
100 }
101 // Softdevice enabled, use SD API
102 else if (nrf_sdh_is_enabled())
103 {
104 aes_ecb_128_encrypt_sdapi (cleartext, ciphertext, key, data_length);
105 }
106 // Softdevice not enabled, use nRF Crypto API
107 else
108 {
109 err_code |= aes_ecb_128_encrypt_nrfapi (cleartext, ciphertext, key, data_length);
110 }
111
112 return err_code;
113}
114
115#endif
rd_status_t ri_aes_ecb_128_encrypt(const uint8_t *const cleartext, uint8_t *const ciphertext, const uint8_t *const key, const size_t data_length)
encrypt a block with AES ECB 128 encryption
#define RD_ERROR_NULL
Null Pointer.
uint32_t rd_status_t
bitfield for representing errors
#define RD_ERROR_INVALID_LENGTH
Invalid Length.
rd_status_t ruuvi_nrf5_sdk15_to_ruuvi_error(const ret_code_t error)
convert nrf5 sdk15 error code into Ruuvi error code.
#define RD_SUCCESS
Internal Error.
#define RD_ERROR_DATA_SIZE
Invalid Data size.
Header to enable and disable module compilation.
Ruuvi error codes and error check function.