aes.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /**
  2. *
  3. * \file
  4. *
  5. * \brief SAM Advanced Encryption Standard driver.
  6. *
  7. * This file defines a useful set of functions for the AES on SAM devices.
  8. *
  9. * Copyright (c) 2014-2016 Atmel Corporation. All rights reserved.
  10. *
  11. * \asf_license_start
  12. *
  13. * \page License
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions are met:
  17. *
  18. * 1. Redistributions of source code must retain the above copyright notice,
  19. * this list of conditions and the following disclaimer.
  20. *
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. *
  25. * 3. The name of Atmel may not be used to endorse or promote products derived
  26. * from this software without specific prior written permission.
  27. *
  28. * 4. This software may only be redistributed and used in connection with an
  29. * Atmel microcontroller product.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  32. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  33. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  34. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  35. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  36. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  37. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  40. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  41. * POSSIBILITY OF SUCH DAMAGE.
  42. *
  43. * \asf_license_stop
  44. *
  45. */
  46. /*
  47. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  48. */
  49. #include <aes.h>
  50. #include <system.h>
  51. /**
  52. * \brief Initializes an AES configuration structure to defaults.
  53. *
  54. * Initializes the specified AES configuration structure to a set of
  55. * known default values.
  56. *
  57. * \note This function should be called to initialize <b>all</b> new instances of
  58. * AES configuration structures before they are further modified by the user
  59. * application.
  60. *
  61. * The default configuration is as follows:
  62. * - Data encryption
  63. * - 128-bit AES key size
  64. * - 128-bit cipher feedback size
  65. * - Manual start mode
  66. * - Electronic Codebook (ECB) mode
  67. * - All countermeasures are enabled
  68. * - XRO key is disabled
  69. * - Key generation is disabled
  70. * - Last output data mode is disabled
  71. *
  72. * \param[out] config Pointer to an AES configuration structure
  73. */
  74. void aes_get_config_defaults(
  75. struct aes_config *const config)
  76. {
  77. /* Sanity check arguments */
  78. Assert(config);
  79. config->encrypt_mode = AES_ENCRYPTION;
  80. config->key_size = AES_KEY_SIZE_128;
  81. config->start_mode = AES_MANUAL_START;
  82. config->opmode= AES_ECB_MODE;
  83. config->cfb_size = AES_CFB_SIZE_128;
  84. config->ctype = AES_COUNTERMEASURE_TYPE_ALL;
  85. config->enable_xor_key = false;
  86. config->enable_key_gen = false;
  87. config->lod = false;
  88. }
  89. /**
  90. * \brief Initialize the AES module.
  91. *
  92. * \param[out] module Pointer to the software instance struct
  93. * \param[in] hw Module hardware register base address pointer
  94. * \param[in] config Pointer to an AES configuration structure
  95. */
  96. void aes_init(
  97. struct aes_module *const module,
  98. Aes *const hw,
  99. struct aes_config *const config)
  100. {
  101. /* Sanity check arguments */
  102. Assert(hw);
  103. Assert(config);
  104. Assert(module);
  105. /* Enable clock for AES */
  106. system_apb_clock_set_mask(SYSTEM_CLOCK_APB_APBC, MCLK_APBCMASK_AES);
  107. /* Perform a software reset */
  108. hw->CTRLA.reg = AES_CTRLA_SWRST;
  109. /* Initialize the AES with new configurations */
  110. aes_set_config(module,hw, config);
  111. }
  112. /**
  113. * \brief Enable the AES module.
  114. *
  115. * \param[in,out] module Pointer to the software instance struct
  116. */
  117. void aes_enable(struct aes_module *const module)
  118. {
  119. Assert(module);
  120. Assert(module->hw);
  121. module->hw->CTRLA.reg |= AES_CTRLA_ENABLE;
  122. }
  123. /**
  124. * \brief Disable the AES module.
  125. * \param[in] module Pointer to the software instance struct
  126. */
  127. void aes_disable(struct aes_module *const module)
  128. {
  129. Assert(module);
  130. Assert(module->hw);
  131. /* Disbale interrupt */
  132. module->hw->INTENCLR.reg = AES_INTENCLR_MASK;
  133. /* Clear interrupt flag */
  134. module->hw->INTFLAG.reg = AES_INTFLAG_MASK;
  135. module->hw->CTRLA.reg &= (~AES_CTRLA_ENABLE);
  136. }
  137. /**
  138. * \brief Configure the AES module.
  139. *
  140. * \param[out] module Pointer to the software instance struct
  141. * \param[in] hw Module hardware register base address pointer
  142. * \param[in] config Pointer to an AES configuration structure
  143. */
  144. void aes_set_config(
  145. struct aes_module *const module,
  146. Aes *const hw,
  147. struct aes_config *const config)
  148. {
  149. uint32_t ul_mode = 0;
  150. /* Validate arguments. */
  151. Assert(hw);
  152. Assert(config);
  153. Assert(module);
  154. module->opmode = config->opmode;
  155. module->hw = hw;
  156. module->key_size = config->key_size;
  157. module->cfb_size = config->cfb_size;
  158. ul_mode |= (config->encrypt_mode << AES_CTRLA_CIPHER_Pos)
  159. | (config->start_mode << AES_CTRLA_STARTMODE_Pos)
  160. | (config->key_size << AES_CTRLA_KEYSIZE_Pos)
  161. | (config->opmode << AES_CTRLA_AESMODE_Pos)
  162. | (config->cfb_size << AES_CTRLA_CFBS_Pos)
  163. | (AES_CTRLA_CTYPE(config->ctype))
  164. | (config->enable_xor_key << AES_CTRLA_XORKEY_Pos)
  165. | (config->enable_key_gen << AES_CTRLA_KEYGEN_Pos)
  166. | (config->lod << AES_CTRLA_LOD_Pos);
  167. if (hw->CTRLA.reg & AES_CTRLA_ENABLE) {
  168. aes_disable(module);
  169. hw->CTRLA.reg = ul_mode;
  170. aes_enable(module);
  171. } else {
  172. hw->CTRLA.reg = ul_mode;
  173. }
  174. }
  175. /**
  176. * \brief Write the 128/192/256-bit cryptographic key.
  177. *
  178. * \param[in] module Pointer to the software instance struct
  179. * \param[in] key Pointer to 4/6/8 contiguous 32-bit words
  180. *
  181. * \note The key size depends on the current AES configuration.
  182. */
  183. void aes_write_key(
  184. struct aes_module *const module,
  185. const uint32_t *key)
  186. {
  187. uint32_t i, key_length = 0;
  188. /* Validate arguments. */
  189. Assert(module);
  190. Assert(module->hw);
  191. Assert(key);
  192. switch (module->key_size) {
  193. case AES_KEY_SIZE_128:
  194. key_length = 4;
  195. break;
  196. case AES_KEY_SIZE_192:
  197. key_length = 6;
  198. break;
  199. case AES_KEY_SIZE_256:
  200. key_length = 8;
  201. break;
  202. default:
  203. break;
  204. }
  205. for (i = 0; i < key_length; i++) {
  206. module->hw->KEYWORD[i].reg = *key;
  207. key++;
  208. }
  209. }
  210. /**
  211. * \brief Write the initialization vector (for the CBC, CFB, OFB, CTR, and GCM
  212. * cipher modes).
  213. *
  214. * \param[in] module Pointer to the software instance struct
  215. * \param[in] vector Pointer to four contiguous 32-bit words
  216. */
  217. void aes_write_init_vector(
  218. struct aes_module *const module,
  219. const uint32_t *vector)
  220. {
  221. uint32_t i;
  222. /* Validate arguments. */
  223. Assert(module);
  224. Assert(module->hw);
  225. Assert(module->opmode != AES_ECB_MODE);
  226. for (i = 0; i < 4; i++) {
  227. module->hw->INTVECTV[i].reg = *vector;
  228. vector++;
  229. }
  230. }
  231. /**
  232. * \brief Write the input data (four consecutive 32-bit words).
  233. *
  234. * \param[in] module Pointer to the software instance struct
  235. * \param[in] input_data_buffer Pointer to an input data buffer
  236. */
  237. void aes_write_input_data(
  238. struct aes_module *const module,
  239. const uint32_t *input_data_buffer)
  240. {
  241. uint32_t i;
  242. /* Validate arguments. */
  243. Assert(module);
  244. Assert(module->hw);;
  245. Assert(input_data_buffer);
  246. module->hw->DATABUFPTR.reg = 0;
  247. if (module->opmode == AES_CFB_MODE
  248. && module->cfb_size == AES_CFB_SIZE_64){
  249. for (i = 0; i < 2; i++) {
  250. module->hw->INDATA.reg = *input_data_buffer;
  251. input_data_buffer++;
  252. }
  253. } else if (module->opmode == AES_CFB_MODE
  254. && (module->cfb_size == AES_CFB_SIZE_32 || module->cfb_size == AES_CFB_SIZE_16)){
  255. module->hw->INDATA.reg = *input_data_buffer;
  256. } else {
  257. for (i = 0; i < 4; i++) {
  258. module->hw->INDATA.reg = *input_data_buffer;
  259. input_data_buffer++;
  260. }
  261. }
  262. }
  263. /**
  264. * \brief Read the output data.
  265. *
  266. * \note The data buffer that holds the processed data must be large enough to hold
  267. * four consecutive 32-bit words.
  268. *
  269. * \param[in] module Pointer to the software instance struct
  270. * \param[in] output_data_buffer Pointer to an output buffer
  271. */
  272. void aes_read_output_data(
  273. struct aes_module *const module,
  274. uint32_t *output_data_buffer)
  275. {
  276. uint32_t i;
  277. /* Validate arguments. */
  278. Assert(module);
  279. Assert(module->hw);
  280. Assert(output_data_buffer);
  281. module->hw->DATABUFPTR.reg = 0;
  282. if (module->opmode == AES_CFB_MODE
  283. && module->cfb_size == AES_CFB_SIZE_64){
  284. for (i = 0; i < 2; i++) {
  285. *output_data_buffer = module->hw->INDATA.reg;
  286. output_data_buffer++;
  287. }
  288. } else if (module->opmode == AES_CFB_MODE
  289. && (module->cfb_size == AES_CFB_SIZE_32 || module->cfb_size == AES_CFB_SIZE_16)){
  290. *output_data_buffer = module->hw->INDATA.reg;
  291. } else {
  292. for (i = 0; i < 4; i++) {
  293. *output_data_buffer = module->hw->INDATA.reg;
  294. output_data_buffer++;
  295. }
  296. }
  297. }