当前位置:   article > 正文

ecc加密算法的使用(c语言实现)_ecc加密算法c语言实现

ecc加密算法c语言实现

参考nodic官方代码:

  1. /*ecc.c文件*/
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "nordic_common.h"
  6. #include "app_timer.h"
  7. #include "app_util.h"
  8. #include "nrf_log.h"
  9. #include "nrf_drv_rng.h"
  10. #include "ecc.h"
  11. #include "uECC.h"
  12. static int ecc_rng(uint8_t *dest, unsigned size)//生成随机数
  13. {
  14. nrf_drv_rng_block_rand(dest, (uint32_t) size);
  15. return 1;
  16. }
  17. void ecc_init(bool rng)
  18. {
  19. if (rng)
  20. {
  21. uECC_set_rng(ecc_rng);
  22. }
  23. }
  24. ret_code_t ecc_p256_keypair_gen(uint8_t *p_le_sk, uint8_t *p_le_pk)//生成私钥和公钥
  25. {
  26. const struct uECC_Curve_t * p_curve;
  27. if (!p_le_sk || !p_le_pk)
  28. {
  29. return NRF_ERROR_NULL;
  30. }
  31. if (!is_word_aligned(p_le_sk) || !is_word_aligned(p_le_pk))
  32. {
  33. return NRF_ERROR_INVALID_ADDR;
  34. }
  35. p_curve = uECC_secp256r1();
  36. int ret = uECC_make_key((uint8_t *) p_le_pk, (uint8_t *) p_le_sk, p_curve);
  37. if (!ret)
  38. {
  39. return NRF_ERROR_INTERNAL;
  40. }
  41. return NRF_SUCCESS;
  42. }
  43. /*公钥计算*/
  44. ret_code_t ecc_p256_public_key_compute(uint8_t const *p_le_sk, uint8_t *p_le_pk)
  45. {
  46. const struct uECC_Curve_t * p_curve;
  47. if (!p_le_sk || !p_le_pk)
  48. {
  49. return NRF_ERROR_NULL;
  50. }
  51. if (!is_word_aligned(p_le_sk) || !is_word_aligned(p_le_pk))
  52. {
  53. return NRF_ERROR_INVALID_ADDR;
  54. }
  55. p_curve = uECC_secp256r1();
  56. //NRF_LOG_INFO("uECC_compute_public_key");
  57. int ret = uECC_compute_public_key((uint8_t *) p_le_sk, (uint8_t *) p_le_pk, p_curve);
  58. if (!ret)
  59. {
  60. return NRF_ERROR_INTERNAL;
  61. }
  62. //NRF_LOG_INFO("uECC_compute_public_key complete: %d", ret);
  63. return NRF_SUCCESS;
  64. }
  65. /*私钥计算*/
  66. ret_code_t ecc_p256_shared_secret_compute(uint8_t const *p_le_sk, uint8_t const *p_le_pk, uint8_t *p_le_ss)
  67. {
  68. int ret;
  69. const struct uECC_Curve_t * p_curve;
  70. if (!p_le_sk || !p_le_pk || !p_le_ss)
  71. {
  72. return NRF_ERROR_NULL;
  73. }
  74. if (!is_word_aligned(p_le_sk) || !is_word_aligned(p_le_pk) || !is_word_aligned(p_le_ss))
  75. {
  76. return NRF_ERROR_INVALID_ADDR;
  77. }
  78. p_curve = uECC_secp256r1();
  79. // Validate the remote public key
  80. ret = uECC_valid_public_key((uint8_t*) p_le_pk, p_curve);
  81. if (!ret)
  82. {
  83. return NRF_ERROR_INTERNAL;
  84. }
  85. //NRF_LOG_INFO("uECC_shared_secret");
  86. ret = uECC_shared_secret((uint8_t *) p_le_pk, (uint8_t *) p_le_sk, p_le_ss, p_curve);
  87. if (!ret)
  88. {
  89. return NRF_ERROR_INTERNAL;
  90. }
  91. //NRF_LOG_INFO("uECC_shared_secret complete: %d", ret);
  92. return NRF_SUCCESS;
  93. }
  94. /*签名*/
  95. ret_code_t ecc_p256_sign(uint8_t const *p_le_sk, uint8_t const * p_le_hash, uint32_t hlen, uint8_t *p_le_sig)
  96. {
  97. const struct uECC_Curve_t * p_curve;
  98. if (!p_le_sk || !p_le_hash || !p_le_sig)
  99. {
  100. return NRF_ERROR_NULL;
  101. }
  102. if (!is_word_aligned(p_le_sk) || !is_word_aligned(p_le_hash) || !is_word_aligned(p_le_sig))
  103. {
  104. return NRF_ERROR_INVALID_ADDR;
  105. }
  106. p_curve = uECC_secp256r1();
  107. //NRF_LOG_INFO("uECC_sign");
  108. int ret = uECC_sign((const uint8_t *) p_le_sk, (const uint8_t *) p_le_hash, (unsigned) hlen, (uint8_t *) p_le_sig, p_curve);
  109. if (!ret)
  110. {
  111. return NRF_ERROR_INTERNAL;
  112. }
  113. //NRF_LOG_INFO("uECC_sign complete: %d", ret);
  114. return NRF_SUCCESS;
  115. }
  116. /*验证*/
  117. ret_code_t ecc_p256_verify(uint8_t const *p_le_pk, uint8_t const * p_le_hash, uint32_t hlen, uint8_t const *p_le_sig)
  118. {
  119. const struct uECC_Curve_t * p_curve;
  120. if (!p_le_pk || !p_le_hash || !p_le_sig)
  121. {
  122. return NRF_ERROR_NULL;
  123. }
  124. if (!is_word_aligned(p_le_pk) || !is_word_aligned(p_le_hash) || !is_word_aligned(p_le_sig))
  125. {
  126. return NRF_ERROR_INVALID_ADDR;
  127. }
  128. p_curve = uECC_secp256r1();
  129. //NRF_LOG_INFO("uECC_verify");
  130. int ret = uECC_verify((const uint8_t *) p_le_pk, (const uint8_t *) p_le_hash, (unsigned) hlen, (uint8_t *) p_le_sig, p_curve);
  131. if (!ret)
  132. {
  133. return NRF_ERROR_INVALID_DATA;
  134. }
  135. //NRF_LOG_INFO("uECC_verify complete: %d", ret);
  136. return NRF_SUCCESS;
  137. }

 

  1. /*ecc.h头文件*/
  2. #ifndef ECC_H__
  3. #define ECC_H__
  4. #include <stdint.h>
  5. #include <stdbool.h>
  6. #include "nordic_common.h"
  7. #include "nrf_error.h"
  8. #include "sdk_errors.h"
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. #define ECC_P256_SK_LEN 32
  13. #define ECC_P256_PK_LEN 64
  14. void ecc_init(bool rng);
  15. ret_code_t ecc_p256_keypair_gen(uint8_t *p_le_sk, uint8_t* p_le_pk);
  16. ret_code_t ecc_p256_public_key_compute(uint8_t const *p_le_sk, uint8_t* p_le_pk);
  17. ret_code_t ecc_p256_shared_secret_compute(uint8_t const *p_le_sk, uint8_t const * p_le_pk, uint8_t *p_le_ss);
  18. ret_code_t ecc_p256_sign(uint8_t const *p_le_sk, uint8_t const * p_le_hash, uint32_t hlen, uint8_t *p_le_sig);
  19. ret_code_t ecc_p256_verify(uint8_t const *p_le_pk, uint8_t const * p_le_hash, uint32_t hlen, uint8_t const *p_le_sig);
  20. #ifdef __cplusplus
  21. }
  22. #endif
  23. #endif // ECC_H__
  1. /*main.c文件*/
  2. int main(){
  3. uint8_t sk[32];
  4. uint8_t pk[64];
  5. ecc_init (true);
  6. NRF_LOG_INFO("----------ECC Begins-------------\r\n");
  7. NRF_LOG_FLUSH();
  8. uint32_t err_code = ecc_p256_keypair_gen(sk,pk);//秘钥和公钥生成
  9. APP_ERROR_CHECK(err_code);
  10. NRF_LOG_INFO("Private Key:\r\n");
  11. NRF_LOG_FLUSH();
  12. NRF_LOG_HEXDUMP_INFO(sk,32);
  13. NRF_LOG_FLUSH();
  14. NRF_LOG_INFO("Public Key:\r\n");
  15. NRF_LOG_HEXDUMP_INFO(pk,64);
  16. NRF_LOG_FLUSH();
  17. return 0;
  18. }

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/731286?site
推荐阅读
相关标签
  

闽ICP备14008679号