Nordic nrf51 sdk sources. Mirrored from https://github.com/ARMmbed/nrf51-sdk.
source/nordic_sdk/components/drivers_nrf/hal/nrf_ecb.c@28:041dac1366b2, 2016-04-07 (annotated)
- Committer:
- vcoubard
- Date:
- Thu Apr 07 17:37:56 2016 +0100
- Revision:
- 28:041dac1366b2
- Parent:
- 20:a90c48eb1d30
- Child:
- 29:286940b7ee5a
Synchronized with git rev 012b8118
Author: Liyou Zhou
Pull in files from sdk 10.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vcoubard | 28:041dac1366b2 | 1 | /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved. |
vcoubard | 28:041dac1366b2 | 2 | * |
vcoubard | 28:041dac1366b2 | 3 | * The information contained herein is property of Nordic Semiconductor ASA. |
vcoubard | 28:041dac1366b2 | 4 | * Terms and conditions of usage are described in detail in NORDIC |
vcoubard | 28:041dac1366b2 | 5 | * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. |
vcoubard | 28:041dac1366b2 | 6 | * |
vcoubard | 28:041dac1366b2 | 7 | * Licensees are granted free, non-transferable use of the information. NO |
vcoubard | 28:041dac1366b2 | 8 | * WARRANTY of ANY KIND is provided. This heading must NOT be removed from |
vcoubard | 28:041dac1366b2 | 9 | * the file. |
vcoubard | 28:041dac1366b2 | 10 | * |
vcoubard | 28:041dac1366b2 | 11 | * $LastChangedRevision: 25419 $ |
vcoubard | 28:041dac1366b2 | 12 | */ |
Vincent Coubard |
0:f2542974c862 | 13 | |
Vincent Coubard |
0:f2542974c862 | 14 | /** |
Vincent Coubard |
0:f2542974c862 | 15 | * @file |
Vincent Coubard |
0:f2542974c862 | 16 | * @brief Implementation of AES ECB driver |
Vincent Coubard |
0:f2542974c862 | 17 | */ |
Vincent Coubard |
0:f2542974c862 | 18 | |
Vincent Coubard |
0:f2542974c862 | 19 | |
Vincent Coubard |
0:f2542974c862 | 20 | //lint -e438 |
Vincent Coubard |
0:f2542974c862 | 21 | |
Vincent Coubard |
0:f2542974c862 | 22 | #include <stdlib.h> |
Vincent Coubard |
0:f2542974c862 | 23 | #include <stdbool.h> |
Vincent Coubard |
0:f2542974c862 | 24 | #include <string.h> |
Vincent Coubard |
0:f2542974c862 | 25 | #include "nrf.h" |
Vincent Coubard |
0:f2542974c862 | 26 | #include "nrf_ecb.h" |
Vincent Coubard |
0:f2542974c862 | 27 | |
Vincent Coubard |
0:f2542974c862 | 28 | static uint8_t ecb_data[48]; ///< ECB data structure for RNG peripheral to access. |
Vincent Coubard |
0:f2542974c862 | 29 | static uint8_t* ecb_key; ///< Key: Starts at ecb_data |
Vincent Coubard |
0:f2542974c862 | 30 | static uint8_t* ecb_cleartext; ///< Cleartext: Starts at ecb_data + 16 bytes. |
Vincent Coubard |
0:f2542974c862 | 31 | static uint8_t* ecb_ciphertext; ///< Ciphertext: Starts at ecb_data + 32 bytes. |
Vincent Coubard |
0:f2542974c862 | 32 | |
Vincent Coubard |
0:f2542974c862 | 33 | bool nrf_ecb_init(void) |
Vincent Coubard |
0:f2542974c862 | 34 | { |
Vincent Coubard |
0:f2542974c862 | 35 | ecb_key = ecb_data; |
Vincent Coubard |
0:f2542974c862 | 36 | ecb_cleartext = ecb_data + 16; |
Vincent Coubard |
0:f2542974c862 | 37 | ecb_ciphertext = ecb_data + 32; |
Vincent Coubard |
0:f2542974c862 | 38 | |
Vincent Coubard |
0:f2542974c862 | 39 | NRF_ECB->ECBDATAPTR = (uint32_t)ecb_data; |
Vincent Coubard |
0:f2542974c862 | 40 | return true; |
Vincent Coubard |
0:f2542974c862 | 41 | } |
Vincent Coubard |
0:f2542974c862 | 42 | |
Vincent Coubard |
0:f2542974c862 | 43 | |
Vincent Coubard |
0:f2542974c862 | 44 | bool nrf_ecb_crypt(uint8_t * dest_buf, const uint8_t * src_buf) |
Vincent Coubard |
0:f2542974c862 | 45 | { |
Vincent Coubard |
0:f2542974c862 | 46 | uint32_t counter = 0x1000000; |
Vincent Coubard |
0:f2542974c862 | 47 | if(src_buf != ecb_cleartext) |
Vincent Coubard |
0:f2542974c862 | 48 | { |
Vincent Coubard |
0:f2542974c862 | 49 | memcpy(ecb_cleartext,src_buf,16); |
Vincent Coubard |
0:f2542974c862 | 50 | } |
Vincent Coubard |
0:f2542974c862 | 51 | NRF_ECB->EVENTS_ENDECB = 0; |
Vincent Coubard |
0:f2542974c862 | 52 | NRF_ECB->TASKS_STARTECB = 1; |
Vincent Coubard |
0:f2542974c862 | 53 | while(NRF_ECB->EVENTS_ENDECB == 0) |
Vincent Coubard |
0:f2542974c862 | 54 | { |
Vincent Coubard |
0:f2542974c862 | 55 | counter--; |
Vincent Coubard |
0:f2542974c862 | 56 | if(counter == 0) |
Vincent Coubard |
0:f2542974c862 | 57 | { |
Vincent Coubard |
0:f2542974c862 | 58 | return false; |
Vincent Coubard |
0:f2542974c862 | 59 | } |
Vincent Coubard |
0:f2542974c862 | 60 | } |
Vincent Coubard |
0:f2542974c862 | 61 | NRF_ECB->EVENTS_ENDECB = 0; |
Vincent Coubard |
0:f2542974c862 | 62 | if(dest_buf != ecb_ciphertext) |
Vincent Coubard |
0:f2542974c862 | 63 | { |
Vincent Coubard |
0:f2542974c862 | 64 | memcpy(dest_buf,ecb_ciphertext,16); |
Vincent Coubard |
0:f2542974c862 | 65 | } |
Vincent Coubard |
0:f2542974c862 | 66 | return true; |
Vincent Coubard |
0:f2542974c862 | 67 | } |
Vincent Coubard |
0:f2542974c862 | 68 | |
Vincent Coubard |
0:f2542974c862 | 69 | void nrf_ecb_set_key(const uint8_t * key) |
Vincent Coubard |
0:f2542974c862 | 70 | { |
Vincent Coubard |
0:f2542974c862 | 71 | memcpy(ecb_key,key,16); |
Vincent Coubard |
0:f2542974c862 | 72 | } |
Vincent Coubard |
0:f2542974c862 | 73 | |
vcoubard | 1:ebc0e0ef0a11 | 74 |