ck_sha.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*
  2. * Copyright (C) 2017 C-SKY Microsystems Co., Ltd. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /******************************************************************************
  17. * @file ck_sha.c
  18. * @brief CSI Source File for SHA Driver
  19. * @version V1.0
  20. * @date 02. June 2017
  21. ******************************************************************************/
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdbool.h>
  25. #include "csi_core.h"
  26. #include "drv_sha.h"
  27. #include "ck_sha.h"
  28. typedef struct {
  29. uint32_t base;
  30. uint32_t irq;
  31. sha_event_cb_t cb;
  32. sha_status_t status;
  33. sha_mode_e mode;
  34. sha_endian_mode_e endian;
  35. } ck_sha_priv_t;
  36. static ck_sha_priv_t sha_handle[CONFIG_SHA_NUM];
  37. bool finish_flag = 0;
  38. /* Driver Capabilities */
  39. static const sha_capabilities_t driver_capabilities = {
  40. .sha1 = 1, /* sha1 mode */
  41. .sha224 = 1, /* sha224 mode */
  42. .sha256 = 1, /* sha256 mode */
  43. .sha384 = 1, /* sha384 mode */
  44. .sha512 = 1, /* sha512 mode */
  45. .sha512_224 = 1, /* sha512_224 mode */
  46. .sha512_256 = 1, /* sha512_256 mode */
  47. .endianmode = 1, /* endian mode */
  48. .interruptmode = 1 /* interrupt mode */
  49. };
  50. #define ERR_SHA(errno) (CSI_DRV_ERRNO_SHA_BASE | errno)
  51. #define SHA_NULL_PARAM_CHK(para) \
  52. do { \
  53. if (para == NULL) { \
  54. return ERR_SHA(EDRV_PARAMETER); \
  55. } \
  56. } while (0)
  57. //
  58. // Functions
  59. //
  60. ck_sha_reg_t *sha_reg = NULL;
  61. volatile static uint8_t sha_int_flag = 1;
  62. static int32_t sha_set_mode(sha_mode_e mode)
  63. {
  64. sha_reg->SHA_CON = mode;
  65. return 0;
  66. }
  67. static int32_t sha_enable_interrupt(void)
  68. {
  69. sha_reg->SHA_CON |= 1 << SHA_INT_ENABLE_OFFSET;
  70. return 0;
  71. }
  72. static int32_t sha_disable_interrupt(void)
  73. {
  74. sha_reg->SHA_CON &= ~(1 << SHA_INT_ENABLE_OFFSET);
  75. return 0;
  76. }
  77. static void sha_clear_interrupt(void)
  78. {
  79. sha_reg->SHA_INTSTATE = 0;
  80. }
  81. static int32_t sha_enable_initial(void)
  82. {
  83. sha_reg->SHA_CON |= 1 << SHA_INIT_OFFSET;
  84. return 0;
  85. }
  86. static int32_t sha_enable_calculate(void)
  87. {
  88. sha_reg->SHA_CON |= 1 << SHA_CAL_OFFSET;
  89. return 0;
  90. }
  91. static int32_t sha_select_endian_mode(sha_endian_mode_e mode)
  92. {
  93. sha_reg->SHA_CON |= mode << SHA_ENDIAN_OFFSET;
  94. return 0;
  95. }
  96. static int32_t sha_input_data(uint32_t *data, uint32_t length)
  97. {
  98. uint8_t i;
  99. uint32_t *input_data = (uint32_t *) & (sha_reg->SHA_DATA1);
  100. for (i = 0; i < length; i++) {
  101. *(input_data + i) = *(data + i);
  102. }
  103. return 0;
  104. }
  105. static int32_t sha_get_data(sha_handle_t handle, uint32_t *data)
  106. {
  107. ck_sha_priv_t *sha_priv = handle;
  108. uint8_t len;
  109. uint8_t i;
  110. uint32_t *result = (uint32_t *)&sha_reg->SHA_H0L;
  111. /* according to different mode to obtain the hash result */
  112. if (sha_priv->mode == SHA_MODE_1 || sha_priv->mode == SHA_MODE_224 || sha_priv->mode == SHA_MODE_256) {
  113. if (sha_priv->mode == SHA_MODE_1) {
  114. len = 5;
  115. } else if (sha_priv->mode == SHA_MODE_224) {
  116. len = 7;
  117. } else if (sha_priv->mode == SHA_MODE_256) {
  118. len = 8;
  119. }
  120. for (i = 0; i < len; i++) {
  121. data[i] = *(result + i);
  122. }
  123. } else {
  124. if (sha_priv->mode == SHA_MODE_384) {
  125. len = 6;
  126. } else if (sha_priv->mode == SHA_MODE_512) {
  127. len = 8;
  128. }
  129. uint32_t *resulth = (uint32_t *)&sha_reg->SHA_H0H;
  130. for (i = 0; i < len; i++) {
  131. data[i << 1] = *(resulth + i);
  132. data[(i << 1) + 1] = *(result + i);
  133. }
  134. }
  135. return 0;
  136. }
  137. static inline void sha_reverse_order(uint8_t *pdata, int32_t length)
  138. {
  139. uint8_t input_data[length];
  140. uint8_t result[length];
  141. uint32_t tmp = 0;
  142. int32_t i = 0;
  143. memcpy((void *)input_data, (void *)pdata, length);
  144. for (i = 0; i < length; i++) {
  145. tmp = i >> 2;
  146. tmp = tmp << 3;
  147. result[i] = input_data[tmp + 3 - i];
  148. }
  149. memcpy((void *)pdata, (void *)result, length);
  150. }
  151. void ck_sha_irqhandler(int32_t idx)
  152. {
  153. sha_int_flag = 0;
  154. sha_clear_interrupt(); //clear sha interrupt
  155. ck_sha_priv_t *sha_priv = &sha_handle[idx];
  156. if (finish_flag != 0) {
  157. if (sha_priv->cb != NULL) {
  158. sha_priv->cb(SHA_EVENT_COMPLETE); //execute the callback function
  159. }
  160. }
  161. }
  162. int32_t __attribute__((weak)) target_get_sha_count(void)
  163. {
  164. return 0;
  165. }
  166. int32_t __attribute__((weak)) target_get_sha(int32_t idx, uint32_t *base, uint32_t *irq)
  167. {
  168. return NULL;
  169. }
  170. /**
  171. \brief get sha handle count.
  172. \return sha handle count
  173. */
  174. int32_t csi_sha_get_instance_count(void)
  175. {
  176. return target_get_sha_count();
  177. }
  178. /**
  179. \brief Initialize SHA Interface. 1. Initializes the resources needed for the SHA interface 2.registers event callback function
  180. \param[in] idx must not exceed return value of csi_sha_get_instance_count()
  181. \param[in] cb_event Pointer to \ref sha_event_cb_t
  182. \return return sha handle if success
  183. */
  184. sha_handle_t csi_sha_initialize(int32_t idx, sha_event_cb_t cb_event)
  185. {
  186. if (idx < 0 || idx >= CONFIG_SHA_NUM) {
  187. return NULL;
  188. }
  189. uint32_t base = 0u;
  190. uint32_t irq;
  191. /* obtain the sha information */
  192. int32_t real_idx = target_get_sha(idx, &base, &irq);
  193. if (real_idx != idx) {
  194. return NULL;
  195. }
  196. ck_sha_priv_t *sha_priv = &sha_handle[idx];
  197. sha_priv->base = base;
  198. sha_priv->irq = irq;
  199. /* initialize the sha context */
  200. sha_priv->cb = cb_event;
  201. sha_priv->status.busy = 0;
  202. drv_nvic_enable_irq(sha_priv->irq);
  203. return (sha_handle_t)sha_priv;
  204. }
  205. /**
  206. \brief De-initialize SHA Interface. stops operation and releases the software resources used by the interface
  207. \param[in] handle sha handle to operate.
  208. \return error code
  209. */
  210. int32_t csi_sha_uninitialize(sha_handle_t handle)
  211. {
  212. SHA_NULL_PARAM_CHK(handle);
  213. ck_sha_priv_t *sha_priv = handle;
  214. sha_priv->cb = NULL;
  215. sha_disable_interrupt();
  216. drv_nvic_disable_irq(sha_priv->irq);
  217. return 0;
  218. }
  219. /**
  220. \brief Get driver capabilities.
  221. \param[in] handle sha handle to operate.
  222. \return \ref sha_capabilities_t
  223. */
  224. sha_capabilities_t csi_sha_get_capabilities(sha_handle_t handle)
  225. {
  226. return driver_capabilities;
  227. }
  228. /**
  229. \brief config sha mode.
  230. \param[in] handle sha handle to operate.
  231. \param[in] mode \ref sha_mode_e
  232. \param[in] endian \ref sha_endian_mode_e
  233. \return error code
  234. */
  235. int32_t csi_sha_config(sha_handle_t handle, sha_mode_e mode, sha_endian_mode_e endian_mode)
  236. {
  237. SHA_NULL_PARAM_CHK(handle);
  238. ck_sha_priv_t *sha_priv = handle;
  239. sha_reg = (ck_sha_reg_t *)(sha_priv->base);
  240. /* config the sha mode */
  241. switch (mode) {
  242. case SHA_MODE_512_256:
  243. case SHA_MODE_512_224:
  244. return ERR_SHA(EDRV_UNSUPPORTED);
  245. case SHA_MODE_1:
  246. case SHA_MODE_224:
  247. case SHA_MODE_256:
  248. case SHA_MODE_384:
  249. case SHA_MODE_512:
  250. sha_priv->mode = mode;
  251. break;
  252. default:
  253. return ERR_SHA(EDRV_PARAMETER);
  254. }
  255. sha_set_mode(mode);
  256. /*config the sha endian mode */
  257. if (endian_mode == SHA_ENDIAN_MODE_LITTLE) {
  258. sha_priv->endian = endian_mode;
  259. sha_select_endian_mode(endian_mode);
  260. } else if (endian_mode == SHA_ENDIAN_MODE_BIG) {
  261. sha_priv->endian = endian_mode;
  262. sha_select_endian_mode(endian_mode);
  263. } else {
  264. return ERR_SHA(EDRV_PARAMETER);
  265. }
  266. sha_enable_interrupt();
  267. return 0;
  268. }
  269. /**
  270. \brief start the engine
  271. \param[in] handle sha handle to operate.
  272. \param[in] context Pointer to the sha context.
  273. \return error code
  274. */
  275. int32_t csi_sha_starts(sha_handle_t handle, void *context)
  276. {
  277. SHA_NULL_PARAM_CHK(handle);
  278. ck_sha_priv_t *sha_priv = handle;
  279. sha_enable_initial();
  280. sha_priv->status.busy = 1;
  281. return 0;
  282. }
  283. /**
  284. \brief updata the engine
  285. \param[in] handle sha handle to operate.
  286. \param[in] context Pointer to the sha context.
  287. \param[in] input Pointer to the Source data
  288. \param[in] len the data len
  289. \return error code
  290. */
  291. static uint8_t sha_buffer[128];
  292. static uint32_t total[2] = {0x0};
  293. static uint32_t last_left = 0;
  294. int32_t csi_sha_update(sha_handle_t handle, void *context, const void *input, uint32_t len)
  295. {
  296. SHA_NULL_PARAM_CHK(handle);
  297. SHA_NULL_PARAM_CHK(input);
  298. if (len <= 0) {
  299. return ERR_SHA(EDRV_PARAMETER);
  300. }
  301. ck_sha_priv_t *sha_priv = handle;
  302. sha_reg = (ck_sha_reg_t *)(sha_priv->base);
  303. uint32_t block_size;
  304. uint32_t left_len = 0;
  305. if (sha_priv->mode < 4) {
  306. block_size = 64;
  307. left_len = len & 0x3f;
  308. } else {
  309. block_size = 128;
  310. left_len = len & 0x7f;
  311. }
  312. uint32_t left = total[0] & (block_size - 1);
  313. uint32_t fill = block_size - left;
  314. total[0] += len;
  315. total[0] &= 0xffffffff;
  316. uint32_t word_left = total[0] & 0x3;
  317. uint8_t *p = (uint8_t *)input;
  318. /* when the text is not aligned by block and len > fill */
  319. if (left && len >= fill) {
  320. if (last_left && sha_priv->endian == SHA_ENDIAN_MODE_LITTLE) {
  321. uint32_t i;
  322. for (i = 0; i < 4 - last_left; i++) {
  323. if (finish_flag) {
  324. *(sha_buffer + 3 - last_left - i) = *((uint8_t *)p + 3 - last_left - i);
  325. } else {
  326. *(sha_buffer + left + 3 - last_left - i) = *((uint8_t *)p + 3 - last_left - i);
  327. }
  328. }
  329. fill = fill - 4 + last_left;
  330. p = (p + 4 - last_left);
  331. }
  332. if (last_left) {
  333. memcpy((void *)(sha_buffer + left + 4 - last_left), p, fill);
  334. } else {
  335. memcpy((void *)(sha_buffer + left), p, fill);
  336. }
  337. /* set the input data */
  338. sha_input_data((uint32_t *)sha_buffer, block_size >> 2);
  339. sha_enable_calculate();
  340. while (sha_int_flag);
  341. sha_int_flag = 1;
  342. p += fill;
  343. len -= fill;
  344. left = 0;
  345. }
  346. /* calculate the hash by block */
  347. while (len >= block_size) {
  348. sha_input_data((uint32_t *)p, block_size >> 2);
  349. sha_enable_calculate();
  350. while (sha_int_flag);
  351. sha_int_flag = 1;
  352. p += block_size;
  353. len -= block_size;
  354. }
  355. /* when the text is not aligned by block and len < fill */
  356. if (len > 0) {
  357. if (sha_priv->endian == SHA_ENDIAN_MODE_BIG || word_left == 0) {
  358. memcpy((void *)(sha_buffer + left), p, len);
  359. } else {
  360. memcpy((void *)(sha_buffer + left), p, len + 4 - word_left);
  361. last_left = word_left;
  362. }
  363. }
  364. sha_priv->status.busy = 0;
  365. return 0;
  366. }
  367. static unsigned char sha_padding[128] = {
  368. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  369. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  370. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  371. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  372. };
  373. /**
  374. \brief finish the engine
  375. \param[in] handle sha handle to operate.
  376. \param[in] context Pointer to the sha context.
  377. \param[out] output Pointer to the dest data
  378. \return error code
  379. */
  380. static uint32_t total_length;
  381. int32_t csi_sha_finish(sha_handle_t handle, void *context, void *output)
  382. {
  383. SHA_NULL_PARAM_CHK(handle);
  384. SHA_NULL_PARAM_CHK(output);
  385. ck_sha_priv_t *sha_priv = handle;
  386. uint32_t block_size;
  387. if (sha_priv->mode < 4) {
  388. block_size = 64;
  389. } else {
  390. block_size = 128;
  391. }
  392. total_length = total[0] << 3;
  393. uint32_t last = total[0] & (block_size - 1);
  394. uint32_t padn = (last < block_size) ? (block_size - last) : (block_size + block_size - last);
  395. uint32_t left = total[0] & 0x3;
  396. uint8_t temp_data[4];
  397. uint32_t j;
  398. /*calculate the final word*/
  399. for (j = 0; j < 4; j++) {
  400. temp_data[j] = (total_length >> (8 * j)) & 0xff;
  401. }
  402. /* group the final package according to the endian mode */
  403. if (sha_priv->endian == SHA_ENDIAN_MODE_BIG) {
  404. memset(sha_padding, 0x0, sizeof(sha_padding));
  405. sha_padding[0] = 0x80;
  406. for (j = 0; j < 4; j++) {
  407. sha_padding[padn - 4 + j] = temp_data[3 - j];
  408. }
  409. } else {
  410. memset(sha_padding, 0x0, sizeof(sha_padding));
  411. sha_padding[3 - left] = 0x80;
  412. for (j = 0; j < 4; j++) {
  413. sha_padding[padn - 4 + j] = temp_data[j];
  414. }
  415. }
  416. finish_flag = 1;
  417. csi_sha_update(handle, NULL, sha_padding, padn);
  418. /* get the hash result */
  419. sha_get_data(handle, (uint32_t *)output);
  420. uint8_t *p = output;
  421. /* convert the data endian according the sha mode */
  422. if (sha_priv->mode == SHA_MODE_1) {
  423. sha_reverse_order(p, 20);
  424. } else if (sha_priv->mode == SHA_MODE_224) {
  425. sha_reverse_order(p, 28);
  426. } else if (sha_priv->mode == SHA_MODE_256) {
  427. sha_reverse_order(p, 32);
  428. } else if (sha_priv->mode == SHA_MODE_512) {
  429. sha_reverse_order(p, 64);
  430. } else if (sha_priv->mode == SHA_MODE_384) {
  431. sha_reverse_order(p, 48);
  432. }
  433. total[0] = 0;
  434. memset(sha_buffer, 0, sizeof(sha_buffer));
  435. memset(sha_padding, 0, sizeof(sha_padding));
  436. last_left = 0;
  437. finish_flag = 0;
  438. return 0;
  439. }
  440. /**
  441. \brief Get SHA status.
  442. \param[in] handle sha handle to operate.
  443. \return SHA status \ref sha_status_t
  444. */
  445. sha_status_t csi_sha_get_status(sha_handle_t handle)
  446. {
  447. ck_sha_priv_t *sha_priv = handle;
  448. return sha_priv->status;
  449. }