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