xref: /nrf52832-nimble/nordic/nrfx/hal/nrf_ecb.c (revision 150812a83cab50279bd772ef6db1bfaf255f2c5b)
1*150812a8SEvalZero /*
2*150812a8SEvalZero  * Copyright (c) 2012 - 2018, Nordic Semiconductor ASA
3*150812a8SEvalZero  * All rights reserved.
4*150812a8SEvalZero  *
5*150812a8SEvalZero  * Redistribution and use in source and binary forms, with or without
6*150812a8SEvalZero  * modification, are permitted provided that the following conditions are met:
7*150812a8SEvalZero  *
8*150812a8SEvalZero  * 1. Redistributions of source code must retain the above copyright notice, this
9*150812a8SEvalZero  *    list of conditions and the following disclaimer.
10*150812a8SEvalZero  *
11*150812a8SEvalZero  * 2. Redistributions in binary form must reproduce the above copyright
12*150812a8SEvalZero  *    notice, this list of conditions and the following disclaimer in the
13*150812a8SEvalZero  *    documentation and/or other materials provided with the distribution.
14*150812a8SEvalZero  *
15*150812a8SEvalZero  * 3. Neither the name of the copyright holder nor the names of its
16*150812a8SEvalZero  *    contributors may be used to endorse or promote products derived from this
17*150812a8SEvalZero  *    software without specific prior written permission.
18*150812a8SEvalZero  *
19*150812a8SEvalZero  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20*150812a8SEvalZero  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*150812a8SEvalZero  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*150812a8SEvalZero  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23*150812a8SEvalZero  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*150812a8SEvalZero  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*150812a8SEvalZero  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*150812a8SEvalZero  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*150812a8SEvalZero  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*150812a8SEvalZero  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*150812a8SEvalZero  * POSSIBILITY OF SUCH DAMAGE.
30*150812a8SEvalZero  */
31*150812a8SEvalZero 
32*150812a8SEvalZero //lint -e438
33*150812a8SEvalZero 
34*150812a8SEvalZero #include <nrfx.h>
35*150812a8SEvalZero #include "nrf_ecb.h"
36*150812a8SEvalZero #include <string.h>
37*150812a8SEvalZero 
38*150812a8SEvalZero static uint8_t  ecb_data[48];   ///< ECB data structure for RNG peripheral to access.
39*150812a8SEvalZero static uint8_t* ecb_key;        ///< Key:        Starts at ecb_data
40*150812a8SEvalZero static uint8_t* ecb_cleartext;  ///< Cleartext:  Starts at ecb_data + 16 bytes.
41*150812a8SEvalZero static uint8_t* ecb_ciphertext; ///< Ciphertext: Starts at ecb_data + 32 bytes.
42*150812a8SEvalZero 
nrf_ecb_init(void)43*150812a8SEvalZero bool nrf_ecb_init(void)
44*150812a8SEvalZero {
45*150812a8SEvalZero   ecb_key = ecb_data;
46*150812a8SEvalZero   ecb_cleartext  = ecb_data + 16;
47*150812a8SEvalZero   ecb_ciphertext = ecb_data + 32;
48*150812a8SEvalZero 
49*150812a8SEvalZero   NRF_ECB->ECBDATAPTR = (uint32_t)ecb_data;
50*150812a8SEvalZero   return true;
51*150812a8SEvalZero }
52*150812a8SEvalZero 
53*150812a8SEvalZero 
nrf_ecb_crypt(uint8_t * dest_buf,const uint8_t * src_buf)54*150812a8SEvalZero bool nrf_ecb_crypt(uint8_t * dest_buf, const uint8_t * src_buf)
55*150812a8SEvalZero {
56*150812a8SEvalZero    uint32_t counter = 0x1000000;
57*150812a8SEvalZero    if (src_buf != ecb_cleartext)
58*150812a8SEvalZero    {
59*150812a8SEvalZero      memcpy(ecb_cleartext,src_buf,16);
60*150812a8SEvalZero    }
61*150812a8SEvalZero    NRF_ECB->EVENTS_ENDECB = 0;
62*150812a8SEvalZero    NRF_ECB->TASKS_STARTECB = 1;
63*150812a8SEvalZero    while (NRF_ECB->EVENTS_ENDECB == 0)
64*150812a8SEvalZero    {
65*150812a8SEvalZero     counter--;
66*150812a8SEvalZero     if (counter == 0)
67*150812a8SEvalZero     {
68*150812a8SEvalZero       return false;
69*150812a8SEvalZero     }
70*150812a8SEvalZero    }
71*150812a8SEvalZero    NRF_ECB->EVENTS_ENDECB = 0;
72*150812a8SEvalZero    if (dest_buf != ecb_ciphertext)
73*150812a8SEvalZero    {
74*150812a8SEvalZero      memcpy(dest_buf,ecb_ciphertext,16);
75*150812a8SEvalZero    }
76*150812a8SEvalZero    return true;
77*150812a8SEvalZero }
78*150812a8SEvalZero 
nrf_ecb_set_key(const uint8_t * key)79*150812a8SEvalZero void nrf_ecb_set_key(const uint8_t * key)
80*150812a8SEvalZero {
81*150812a8SEvalZero   memcpy(ecb_key,key,16);
82*150812a8SEvalZero }
83*150812a8SEvalZero 
84*150812a8SEvalZero 
85