nrf_ecb.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. * $LastChangedRevision: 25419 $
  12. */
  13. /**
  14. * @file
  15. * @brief Implementation of AES ECB driver
  16. */
  17. //lint -e438
  18. #include <stdlib.h>
  19. #include <stdbool.h>
  20. #include <string.h>
  21. #include "nrf.h"
  22. #include "nrf_ecb.h"
  23. static uint8_t ecb_data[48]; ///< ECB data structure for RNG peripheral to access.
  24. static uint8_t* ecb_key; ///< Key: Starts at ecb_data
  25. static uint8_t* ecb_cleartext; ///< Cleartext: Starts at ecb_data + 16 bytes.
  26. static uint8_t* ecb_ciphertext; ///< Ciphertext: Starts at ecb_data + 32 bytes.
  27. bool nrf_ecb_init(void)
  28. {
  29. ecb_key = ecb_data;
  30. ecb_cleartext = ecb_data + 16;
  31. ecb_ciphertext = ecb_data + 32;
  32. NRF_ECB->ECBDATAPTR = (uint32_t)ecb_data;
  33. return true;
  34. }
  35. bool nrf_ecb_crypt(uint8_t * dest_buf, const uint8_t * src_buf)
  36. {
  37. uint32_t counter = 0x1000000;
  38. if(src_buf != ecb_cleartext)
  39. {
  40. memcpy(ecb_cleartext,src_buf,16);
  41. }
  42. NRF_ECB->EVENTS_ENDECB = 0;
  43. NRF_ECB->TASKS_STARTECB = 1;
  44. while(NRF_ECB->EVENTS_ENDECB == 0)
  45. {
  46. counter--;
  47. if(counter == 0)
  48. {
  49. return false;
  50. }
  51. }
  52. NRF_ECB->EVENTS_ENDECB = 0;
  53. if(dest_buf != ecb_ciphertext)
  54. {
  55. memcpy(dest_buf,ecb_ciphertext,16);
  56. }
  57. return true;
  58. }
  59. void nrf_ecb_set_key(const uint8_t * key)
  60. {
  61. memcpy(ecb_key,key,16);
  62. }