rsa_lib.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "sec_osal_light.h"
  2. #include "sec_typedef.h"
  3. #include "rsa_def.h"
  4. #include "alg_sha1.h"
  5. #include "bgn_export.h"
  6. #include "sec_cust_struct.h"
  7. #include "sec_auth.h"
  8. #include "sec_error.h"
  9. #include "sec_rom_info.h"
  10. #include "sec_boot_lib.h"
  11. #include "sec_sign_header.h"
  12. #include "sec_key_util.h"
  13. #include "sec_log.h"
  14. /**************************************************************************
  15. * MODULE NAME
  16. **************************************************************************/
  17. #define MOD "AUTHEN"
  18. /**************************************************************************
  19. * LOCAL VARIABLE
  20. **************************************************************************/
  21. unsigned char bRsaKeyInit = false;
  22. unsigned char bRsaImgKeyInit = false;
  23. CUST_SEC_INTER g_cus_inter;
  24. /**************************************************************************
  25. * RSA SML KEY INIT
  26. **************************************************************************/
  27. int lib_init_key(unsigned char *nKey, unsigned int nKey_len, unsigned char *eKey, unsigned int eKey_len)
  28. {
  29. int ret = SEC_OK;
  30. /* ------------------------------ */
  31. /* avoid re-init aes key
  32. if re-init key again, key value will be decoded twice .. */
  33. /* ------------------------------ */
  34. if (true == bRsaKeyInit)
  35. return ret;
  36. bRsaKeyInit = true;
  37. if (0 != mcmp(rom_info.m_id, RI_NAME, RI_NAME_LEN)) {
  38. SMSG(true, "[%s] error. key not found\n", MOD);
  39. ret = ERR_RSA_KEY_NOT_FOUND;
  40. goto _end;
  41. }
  42. /* ------------------------------ */
  43. /* clean rsa variable */
  44. /* ------------------------------ */
  45. memset(&rsa, 0, sizeof(rsa_ctx));
  46. /* ------------------------------ */
  47. /* init RSA module / exponent key */
  48. /* ------------------------------ */
  49. rsa.len = RSA_KEY_SIZE;
  50. /* ------------------------------ */
  51. /* decode key */
  52. /* ------------------------------ */
  53. sec_decode_key(nKey, nKey_len,
  54. rom_info.m_SEC_KEY.crypto_seed, sizeof(rom_info.m_SEC_KEY.crypto_seed));
  55. /* ------------------------------ */
  56. /* init mpi library */
  57. /* ------------------------------ */
  58. bgn_read_str(&rsa.N, 16, (char *)nKey, nKey_len);
  59. bgn_read_str(&rsa.E, 16, (char *)eKey, eKey_len);
  60. /* ------------------------------ */
  61. /* debugging */
  62. /* ------------------------------ */
  63. dump_buf(nKey, 0x4);
  64. _end:
  65. return ret;
  66. }
  67. /**************************************************************************
  68. * SIGNING
  69. **************************************************************************/
  70. int lib_sign(unsigned char *data_buf, unsigned int data_len, unsigned char *sig_buf, unsigned int sig_len)
  71. {
  72. return 0;
  73. }
  74. /**************************************************************************
  75. * HASHING
  76. **************************************************************************/
  77. int lib_hash(unsigned char *data_buf, unsigned int data_len, unsigned char *hash_buf, unsigned int hash_len)
  78. {
  79. if (HASH_LEN != hash_len) {
  80. SMSG(true, "hash length is wrong (%d)\n", hash_len);
  81. goto _err;
  82. }
  83. /* hash the plain text */
  84. sha1(data_buf, data_len, hash_buf);
  85. return 0;
  86. _err:
  87. return -1;
  88. }
  89. /**************************************************************************
  90. * VERIFY SIGNATURE
  91. **************************************************************************/
  92. int lib_verify(unsigned char *data_buf, unsigned int data_len, unsigned char *sig_buf, unsigned int sig_len)
  93. {
  94. if (RSA_KEY_LEN != sig_len) {
  95. SMSG(true, "signature length is wrong (%d)\n", sig_len);
  96. goto _err;
  97. }
  98. SMSG(true, "[%s] 0x%x,0x%x,0x%x,0x%x\n", MOD, data_buf[0], data_buf[1], data_buf[2],
  99. data_buf[3]);
  100. /* hash the plain text */
  101. sha1(data_buf, data_len, sha1sum);
  102. /* verify this signature */
  103. SMSG(true, "[%s] verify signature", MOD);
  104. if (rsa_verify(&rsa, HASH_LEN, sha1sum, sig_buf) != 0) {
  105. SMSG(true, " ... failed\n");
  106. goto _err;
  107. }
  108. SMSG(true, " ... pass\n");
  109. return 0;
  110. _err:
  111. return -1;
  112. }