None

Dependents:   nRF51822

Fork of nrf51-sdk by Lancaster University

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers nrf_ecb.c Source File

nrf_ecb.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) Nordic Semiconductor ASA
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without modification,
00006  * are permitted provided that the following conditions are met:
00007  *
00008  *   1. Redistributions of source code must retain the above copyright notice, this
00009  *   list of conditions and the following disclaimer.
00010  *
00011  *   2. Redistributions in binary form must reproduce the above copyright notice, this
00012  *   list of conditions and the following disclaimer in the documentation and/or
00013  *   other materials provided with the distribution.
00014  *
00015  *   3. Neither the name of Nordic Semiconductor ASA nor the names of other
00016  *   contributors to this software may be used to endorse or promote products
00017  *   derived from this software without specific prior written permission.
00018  *
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00021  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00022  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00023  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00024  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00025  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00026  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00027  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030  *
00031  */ 
00032 
00033 /** 
00034  * @file
00035  * @brief Implementation of AES ECB driver
00036  */
00037 
00038 
00039 //lint -e438
00040 
00041 #include <stdlib.h>
00042 #include <stdbool.h>
00043 #include <string.h>
00044 #include "nrf.h" 
00045 #include "nrf_ecb.h"
00046 
00047 static uint8_t  ecb_data[48];   ///< ECB data structure for RNG peripheral to access.
00048 static uint8_t* ecb_key;        ///< Key:        Starts at ecb_data 
00049 static uint8_t* ecb_cleartext;  ///< Cleartext:  Starts at ecb_data + 16 bytes.
00050 static uint8_t* ecb_ciphertext; ///< Ciphertext: Starts at ecb_data + 32 bytes.
00051 
00052 bool nrf_ecb_init(void)
00053 {
00054   ecb_key = ecb_data;
00055   ecb_cleartext  = ecb_data + 16;
00056   ecb_ciphertext = ecb_data + 32;
00057 
00058   NRF_ECB->ECBDATAPTR = (uint32_t)ecb_data;
00059   return true;
00060 }
00061 
00062 
00063 bool nrf_ecb_crypt(uint8_t * dest_buf, const uint8_t * src_buf)
00064 {
00065    uint32_t counter = 0x1000000;
00066    if(src_buf != ecb_cleartext)
00067    {
00068      memcpy(ecb_cleartext,src_buf,16);
00069    }
00070    NRF_ECB->EVENTS_ENDECB = 0;
00071    NRF_ECB->TASKS_STARTECB = 1;
00072    while(NRF_ECB->EVENTS_ENDECB == 0)
00073    {
00074     counter--;
00075     if(counter == 0)
00076     {
00077       return false;
00078     }
00079    }
00080    NRF_ECB->EVENTS_ENDECB = 0;
00081    if(dest_buf != ecb_ciphertext)
00082    {
00083      memcpy(dest_buf,ecb_ciphertext,16);
00084    }
00085    return true;
00086 }
00087 
00088 void nrf_ecb_set_key(const uint8_t * key)
00089 {
00090   memcpy(ecb_key,key,16);
00091 }
00092 
00093