crypto_hw.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. */
  10. /* MediaTek Inc. (C) 2017. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. *
  31. * The following software/firmware and/or related documentation ("MediaTek Software")
  32. * have been modified by MediaTek Inc. All revisions are subject to any receiver\'s
  33. * applicable license agreements with MediaTek Inc.
  34. */
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <arch/arm/mmu.h>
  38. #include <arch/ops.h>
  39. #include <platform/crypto_hw.h>
  40. #include <platform/memory_layout.h>
  41. #include <verified_boot_error.h>
  42. #include <pal_typedefs.h>
  43. uint32_t g_crypto_hw_initialized = 0;
  44. uint32_t g_crypto_hw_disabled = 0;
  45. CRYPTO_HW_SHARE_MEM *g_crypto = (CRYPTO_HW_SHARE_MEM *)LK_SECURE_BOOT_BASE;
  46. extern uint32_t crypto_hw_init(uint32_t share_mem_addr, uint32_t share_mem_len);
  47. extern uint32_t crypto_hw_disable(void);
  48. extern uint32_t sha256atf_init(void);
  49. extern uint32_t sha256atf_process(const uint8_t *in, unsigned long len);
  50. extern uint32_t sha256atf_done(void);
  51. /* crypto hw engine can only be accessed in secure world */
  52. /* share memory address can only be passed into atf once, and associated smc calls will be disabled before leaving LK */
  53. /* note: if multi-thread is enabled, a mutex must be held in a hash calculation session */
  54. static uint32_t crypto_hw_engine_init()
  55. {
  56. uint32_t ret = 0;
  57. if (g_crypto_hw_initialized == 1)
  58. return 0;
  59. arch_mmu_map(((uint64_t)LK_SECURE_BOOT_BASE) & 0xffffffff,
  60. (uint32_t)LK_SECURE_BOOT_BASE,
  61. MMU_MEMORY_TYPE_NORMAL_WRITE_BACK | MMU_MEMORY_AP_P_RW_U_NA,
  62. LK_SECURE_BOOT_MAX_SIZE);
  63. ret = crypto_hw_init(LK_SECURE_BOOT_BASE, LK_SECURE_BOOT_MAX_SIZE);
  64. if (ret)
  65. return ret;
  66. g_crypto_hw_initialized = 1;
  67. return ret;
  68. }
  69. uint32_t crypto_hw_engine_disable()
  70. {
  71. uint32_t ret = 0;
  72. if (g_crypto_hw_disabled == 1)
  73. return 0;
  74. ret = crypto_hw_disable();
  75. if (ret)
  76. return ret;
  77. g_crypto_hw_disabled = 1;
  78. return ret;
  79. }
  80. uint32_t sha256hw_init(void)
  81. {
  82. uint32_t ret = 0;
  83. ret = crypto_hw_engine_init();
  84. if (ret)
  85. return ret;
  86. ret = sha256atf_init();
  87. if (ret)
  88. return ret;
  89. return ret;
  90. }
  91. uint32_t sha256hw_process(const uint8_t *in, unsigned long len)
  92. {
  93. uint32_t ret = 0;
  94. if (in == NULL)
  95. return ERR_VB_INVALID_ADDR;
  96. arch_clean_invalidate_cache_range((addr_t)in, len);
  97. ret = sha256atf_process(in, len);
  98. if (ret)
  99. return ret;
  100. return ret;
  101. }
  102. uint32_t sha256hw_done(uint8_t *hash)
  103. {
  104. uint32_t ret = 0;
  105. if (hash == NULL)
  106. return ERR_VB_INVALID_ADDR;
  107. memset(hash, 0x0, SHA256_HASH_SIZE);
  108. /* do not call crypto_hw_engine_deinit here, since it disables ATF crypto SMC permanently */
  109. arch_clean_invalidate_cache_range((addr_t)LK_SECURE_BOOT_BASE,
  110. LK_SECURE_BOOT_MAX_SIZE);
  111. ret = sha256atf_done();
  112. if (ret)
  113. return ret;
  114. memcpy(hash, g_crypto->hash, SHA256_HASH_SIZE);
  115. return ret;
  116. }
  117. /* padding is not handled currently, and alignment is fixed to 16 bytes */
  118. uint32_t calc_hash_hw(uint8_t *buf, uint32_t buf_sz, uint32_t padding_sz, uint8_t *hash, uint32_t hash_sz)
  119. {
  120. uint32_t ret = 0;
  121. uint32_t process_len = 0;
  122. if (hash_sz < SHA256_HASH_SIZE)
  123. return ERR_INVALID_HASH_SZ;
  124. if (padding_sz >= AES_BLK_SIZE)
  125. return ERR_INVALID_HASH_SZ;
  126. if ((NULL == hash) || (NULL == buf))
  127. return ERR_VB_INVALID_ADDR;
  128. memset(hash, 0x0, SHA256_HASH_SIZE);
  129. ret = sha256hw_init();
  130. if (ret) {
  131. dprintf(CRITICAL, "sha256hw_init fail, ret = 0x%x\n", ret);
  132. return ret;
  133. }
  134. process_len = (buf_sz / AES_BLK_SIZE) * AES_BLK_SIZE;
  135. ret = sha256hw_process(buf, process_len);
  136. if (ret) {
  137. dprintf(CRITICAL, "sha256hw_process fail, ret = 0x%x\n", ret);
  138. return ret;
  139. }
  140. /* note alignment != 0 does not indicate process_len == len */
  141. if (process_len != buf_sz) {
  142. memset(g_crypto->last_block, 0x0, AES_BLK_SIZE);
  143. memcpy(g_crypto->last_block, buf + process_len, buf_sz - process_len);
  144. arch_clean_invalidate_cache_range((addr_t)LK_SECURE_BOOT_BASE, LK_SECURE_BOOT_MAX_SIZE);
  145. /* secure world should detect this buffer is in shared memory and avoid mapping again */
  146. ret = sha256hw_process(g_crypto->last_block, AES_BLK_SIZE);
  147. if (ret) {
  148. dprintf(CRITICAL, "sha256hw_process fail, ret = 0x%x\n", ret);
  149. return ret;
  150. }
  151. }
  152. ret = sha256hw_done(hash);
  153. if (ret) {
  154. dprintf(CRITICAL, "sha256hw_done fail, ret = 0x%x\n", ret);
  155. return ret;
  156. }
  157. return ret;
  158. }