hacc_lib.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * (c) MediaTek Inc. 2011
  3. */
  4. #include "sec_osal.h"
  5. /*#include <mach/mt_typedefs.h>*/
  6. #include "sec_hal.h"
  7. #include "hacc_mach.h"
  8. /*#include "sec_log.h"*/
  9. #include "sec_error.h"
  10. #include "sec_typedef.h"
  11. /******************************************************************************
  12. * Crypto Engine Test Driver Debug Control
  13. ******************************************************************************/
  14. #define MOD "CE"
  15. /******************************************************************************
  16. * Seed Definition
  17. ******************************************************************************/
  18. #define _CRYPTO_SEED_LEN (16)
  19. /******************************************************************************
  20. * GLOBAL FUNCTIONS
  21. ******************************************************************************/
  22. /* return the result of hwEnableClock ( )
  23. - TRUE (1) means crypto engine init success
  24. - FALSE (0) means crypto engine init fail */
  25. unsigned char masp_hal_secure_algo_init(void)
  26. {
  27. bool ret = TRUE;
  28. return ret;
  29. }
  30. /* return the result of hwDisableClock ( )
  31. - TRUE (1) means crypto engine de-init success
  32. - FALSE (0) means crypto engine de-init fail */
  33. unsigned char masp_hal_secure_algo_deinit(void)
  34. {
  35. bool ret = TRUE;
  36. return ret;
  37. }
  38. /******************************************************************************
  39. * CRYPTO ENGINE EXPORTED APIs
  40. ******************************************************************************/
  41. /* perform crypto operation
  42. @ Direction : TRUE (1) means encrypt
  43. FALSE (0) means decrypt
  44. @ ContentAddr : input source address
  45. @ ContentLen : input source length
  46. @ CustomSeed : customization seed for crypto engine
  47. @ ResText : output destination address */
  48. void masp_hal_secure_algo(unsigned char Direction, unsigned char *ContentAddr,
  49. unsigned int ContentLen, unsigned char *CustomSeed,
  50. unsigned char *ResText)
  51. {
  52. unsigned int err;
  53. unsigned char *src, *dst, *seed;
  54. unsigned int i = 0;
  55. /* try to get hacc lock */
  56. do {
  57. /* If the semaphore is successfully acquired, this function returns 0. */
  58. err = osal_hacc_lock();
  59. } while (0 != err);
  60. /* initialize hacc crypto configuration */
  61. seed = (unsigned char *)CustomSeed;
  62. err = masp_hal_sp_hacc_init(seed, _CRYPTO_SEED_LEN);
  63. if (SEC_OK != err)
  64. goto _error;
  65. /* initialize source and destination address */
  66. src = (unsigned char *)ContentAddr;
  67. dst = (unsigned char *)ResText;
  68. /* according to input parameter to encrypt or decrypt */
  69. switch (Direction) {
  70. case TRUE:
  71. /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
  72. /* ! CCCI driver already got HACC lock ! */
  73. /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
  74. dst =
  75. masp_hal_sp_hacc_enc((unsigned char *)src, ContentLen, TRUE, HACC_USER3, FALSE);
  76. break;
  77. case FALSE:
  78. /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
  79. /* ! CCCI driver already got HACC lock ! */
  80. /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
  81. dst =
  82. masp_hal_sp_hacc_dec((unsigned char *)src, ContentLen, TRUE, HACC_USER3, FALSE);
  83. break;
  84. default:
  85. err = ERR_KER_CRYPTO_INVALID_MODE;
  86. goto _error;
  87. }
  88. /* copy result */
  89. for (i = 0; i < ContentLen; i++)
  90. *(ResText + i) = *(dst + i);
  91. /* try to release hacc lock */
  92. osal_hacc_unlock();
  93. return;
  94. _error:
  95. /* try to release hacc lock */
  96. osal_hacc_unlock();
  97. pr_err("[%s] masp_hal_secure_algo error (0x%x)\n", MOD, err);
  98. BUG_ON(!(0));
  99. }