luat_crypto.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #ifndef LUAT_CRYPTO_H
  2. #define LUAT_CRYPTO_H
  3. #include "luat_base.h"
  4. #include "mbedtls/md.h"
  5. #define LUAT_CRYPTO_AES_ECB 1
  6. #define LUAT_CRYPTO_AES_CBC 2
  7. #define LUAT_CRYPTO_AES_CTR 3
  8. #define LUAT_CRYPTO_AES_CFB 4
  9. #define LUAT_CRYPTO_AES_OFB 5
  10. #define LUAT_CRYPTO_AES_PAD_ZERO 1
  11. #define LUAT_CRYPTO_AES_PAD_5 2
  12. #define LUAT_CRYPTO_AES_PAD_7 3
  13. typedef struct
  14. {
  15. char tp[16];
  16. size_t key_len;
  17. mbedtls_md_context_t *ctx;
  18. }luat_crypt_stream_t;
  19. /**
  20. * @defgroup luatos_crypto crypto数据加密
  21. * @{
  22. */
  23. int luat_crypto_trng(char* buff, size_t len);
  24. /// @brief 计算md5值
  25. /// @param str 需要计算的字符串
  26. /// @param str_size 需要计算的字符串的长度
  27. /// @param out_ptr 输出
  28. /// @return 成功0,失败-1
  29. int luat_crypto_md5_simple(const char* str, size_t str_size, void* out_ptr);
  30. /// @brief 计算hmac_md5值
  31. /// @param str 需要计算的字符串
  32. /// @param str_size 字符串长度
  33. /// @param mac 密钥
  34. /// @param mac_size 密钥的长度
  35. /// @param out_ptr 输出
  36. /// @return 成功0,失败-1
  37. int luat_crypto_hmac_md5_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr);
  38. /// @brief 计算sha1值
  39. /// @param str 需要计算的字符串
  40. /// @param str_size 需要计算的字符串的长度
  41. /// @param out_ptr 输出
  42. /// @return 成功0,失败-1
  43. int luat_crypto_sha1_simple(const char* str, size_t str_size, void* out_ptr);
  44. /// @brief 计算hmac_sha1值
  45. /// @param str 需要计算的字符串
  46. /// @param str_size 字符串长度
  47. /// @param mac 密钥
  48. /// @param mac_size 密钥的长度
  49. /// @param out_ptr 输出
  50. /// @return 成功0,失败-1
  51. int luat_crypto_hmac_sha1_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr);
  52. /// @brief 计算sha256值
  53. /// @param str 需要计算的字符串
  54. /// @param str_size 需要计算的字符串的长度
  55. /// @param out_ptr 输出
  56. /// @return 成功0,失败-1
  57. int luat_crypto_sha256_simple(const char* str, size_t str_size, void* out_ptr);
  58. /// @brief 计算hmac_sha256值
  59. /// @param str 需要计算的字符串
  60. /// @param str_size 字符串长度
  61. /// @param mac 密钥
  62. /// @param mac_size 密钥的长度
  63. /// @param out_ptr 输出
  64. /// @return 成功0,失败-1
  65. int luat_crypto_hmac_sha256_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr) ;
  66. /// @brief 计算sha512值
  67. /// @param str 需要计算的字符串
  68. /// @param str_size 需要计算的字符串的长度
  69. /// @param out_ptr 输出
  70. /// @return 成功0,失败-1
  71. int luat_crypto_sha512_simple(const char* str, size_t str_size, void* out_ptr) ;
  72. /// @brief 计算hmac_sha512值
  73. /// @param str 需要计算的字符串
  74. /// @param str_size 字符串长度
  75. /// @param mac 密钥
  76. /// @param mac_size 密钥的长度
  77. /// @param out_ptr 输出
  78. /// @return 成功0,失败-1
  79. int luat_crypto_hmac_sha512_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr) ;
  80. /**
  81. * @brief BASE64加密
  82. * @param dst buffer
  83. * @param dlen buffer长度
  84. * @param olen 写入的字节数
  85. * @param src 加密密钥
  86. * @param slen 加密密钥长度
  87. * @return 0成功
  88. */
  89. int luat_crypto_base64_encode( unsigned char *dst, size_t dlen, size_t *olen, const unsigned char *src, size_t slen ) ;
  90. /**
  91. * @brief BASE64解密
  92. * @param dst buffer
  93. * @param dlen buffer长度
  94. * @param olen 写入的字节数
  95. * @param src 密钥
  96. * @param slen 密钥长度
  97. * @return 0成功
  98. */
  99. int luat_crypto_base64_decode( unsigned char *dst, size_t dlen, size_t *olen, const unsigned char *src, size_t slen ) ;
  100. /**@}*/
  101. int luat_crypto_cipher_list(const char** list, size_t* len);
  102. int luat_crypto_cipher_suites(const char** list, size_t* len);
  103. int luat_crypto_md(const char* md, const char* str, size_t str_size, void* out_ptr, const char* key, size_t key_len);
  104. int luat_crypto_md_file(const char* md, void* out_ptr, const char* key, size_t key_len, const char* path);
  105. int luat_crypto_md_init(const char* md, const char* key, luat_crypt_stream_t *stream);
  106. int luat_crypto_md_update(const char* md, const char* str, size_t str_size, luat_crypt_stream_t *stream);
  107. int luat_crypto_md_finish(const char* md, void* out_ptr, luat_crypt_stream_t *stream);
  108. #endif