wm_crypto_hard.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /**
  2. * @file wm_crypto_hard.h
  3. *
  4. * @brief hard crypto module
  5. *
  6. * @author dave
  7. *
  8. * Copyright (c) 2014 Winner Microelectronics Co., Ltd.
  9. */
  10. #ifndef WM_CRYPTO_HARD_H
  11. #define WM_CRYPTO_HARD_H
  12. #include "wm_type_def.h"
  13. #include "wm_osal.h"
  14. #ifndef min
  15. #define min(a,b) (((a) < (b)) ? (a) : (b))
  16. #endif /* min */
  17. /** The base address of the registers of encryption/decryption module. */
  18. #define HR_CRYPTO_BASE_ADDR 0x40000600
  19. /** The address of the source text address register. */
  20. #define HR_CRYPTO_SRC_ADDR (HR_CRYPTO_BASE_ADDR + 0x00)
  21. /** The address of the encrypted/decrypted text address register. */
  22. #define HR_CRYPTO_DEST_ADDR (HR_CRYPTO_BASE_ADDR + 0x04)
  23. /** The address of the configuration register. */
  24. #define HR_CRYPTO_SEC_CFG (HR_CRYPTO_BASE_ADDR + 0x08)
  25. /** The address of the control register. */
  26. #define HR_CRYPTO_SEC_CTRL (HR_CRYPTO_BASE_ADDR + 0x0C)
  27. /** The address of the key0 register. */
  28. #define HR_CRYPTO_KEY0 (HR_CRYPTO_BASE_ADDR + 0x10)
  29. /** The address of the key1 register. */
  30. #define HR_CRYPTO_KEY1 (HR_CRYPTO_BASE_ADDR + 0x14)
  31. /** The address of the key2 register. */
  32. #define HR_CRYPTO_KEY2 (HR_CRYPTO_BASE_ADDR + 0x18)
  33. /** The address of the key3 register. */
  34. #define HR_CRYPTO_KEY3 (HR_CRYPTO_BASE_ADDR + 0x1C)
  35. /** The address of the key4 register. */
  36. #define HR_CRYPTO_KEY4 (HR_CRYPTO_BASE_ADDR + 0x20)
  37. /** The address of the key5 register. */
  38. #define HR_CRYPTO_KEY5 (HR_CRYPTO_BASE_ADDR + 0x24)
  39. /** The address of the key6 register. */
  40. #define HR_CRYPTO_KEY6 (HR_CRYPTO_BASE_ADDR + 0x4c)
  41. /** The address of the key7 register. */
  42. #define HR_CRYPTO_KEY7 (HR_CRYPTO_BASE_ADDR + 0x50)
  43. /** The address of the IV0 register. */
  44. #define HR_CRYPTO_IV0 (HR_CRYPTO_BASE_ADDR + 0x28)
  45. /** The address of the IV1 register. */
  46. #define HR_CRYPTO_IV1 (HR_CRYPTO_BASE_ADDR + 0x20)
  47. /** The address of the encrypted/decrypted status register. */
  48. #define HR_CRYPTO_SEC_STS (HR_CRYPTO_BASE_ADDR + 0x30)
  49. /** The address of the SHA1/MD5 digest0 register. */
  50. #define HR_CRYPTO_SHA1_DIGEST0 (HR_CRYPTO_BASE_ADDR + 0x34)
  51. /** The address of the SHA1/MD5 digest1 register. */
  52. #define HR_CRYPTO_SHA1_DIGEST1 (HR_CRYPTO_BASE_ADDR + 0x38)
  53. /** The address of the SHA1/MD5 digest2 register. */
  54. #define HR_CRYPTO_SHA1_DIGEST2 (HR_CRYPTO_BASE_ADDR + 0x3C)
  55. /** The address of the SHA1/MD5 digest3 register. */
  56. #define HR_CRYPTO_SHA1_DIGEST3 (HR_CRYPTO_BASE_ADDR + 0x40)
  57. /** The address of the SHA1 digest4 and CRC in/output register. */
  58. #define HR_CRYPTO_SHA1_DIGEST4 (HR_CRYPTO_BASE_ADDR + 0x44)
  59. /** The address of the RNG RESULT register. */
  60. #define HR_CRYPTO_RNG_RESULT (HR_CRYPTO_BASE_ADDR + 0x48)
  61. /** The address of the TRNG CR register. */
  62. #define HR_CRYPTO_TRNG_CR (HR_CRYPTO_BASE_ADDR + 0x54)
  63. /** The address of the CRC in register. */
  64. #define HR_CRYPTO_CRC_KEY HR_CRYPTO_SHA1_DIGEST4
  65. /** The address of the CRC output register. */
  66. #define HR_CRYPTO_CRC_RESULT HR_CRYPTO_SHA1_DIGEST4
  67. #define ERR_CRY_OK 0 ///< No error, everything OK.
  68. #define ERR_FAILURE -1 /* failure */
  69. #define ERR_ARG_FAIL -6 /* Failure due to bad function param */
  70. # define PS_SUCCESS 0
  71. # define PS_FAILURE -1
  72. # define PS_FAIL PS_FAILURE/* Just another name */
  73. /* NOTE: Failure return codes MUST be < 0 */
  74. /* NOTE: The range for core error codes should be between -2 and -29 */
  75. # define PS_ARG_FAIL -6 /* Failure due to bad function param */
  76. # define PS_PLATFORM_FAIL -7 /* Failure as a result of system call error */
  77. # define PS_MEM_FAIL -8 /* Failure to allocate requested memory */
  78. # define PS_LIMIT_FAIL -9 /* Failure on sanity/limit tests */
  79. # define PS_UNSUPPORTED_FAIL -10 /* Unimplemented feature error */
  80. # define PS_DISABLED_FEATURE_FAIL -11 /* Incorrect #define toggle for feature */
  81. # define PS_PROTOCOL_FAIL -12 /* A protocol error occurred */
  82. # define PS_TIMEOUT_FAIL -13 /* A timeout occurred and MAY be an error */
  83. # define PS_INTERRUPT_FAIL -14 /* An interrupt occurred and MAY be an error */
  84. # define PS_PENDING -15 /* In process. Not necessarily an error */
  85. # define PS_EAGAIN -16 /* Try again later. Not necessarily an error */
  86. # define PS_OUTPUT_LENGTH -17 /* Output length negotiation:
  87. output buffer is too small. */
  88. # define PS_HOSTNAME_RESOLUTION -18 /* Cannot resolve host name. */
  89. # define PS_CONNECT -19 /* Cannot connect to remote host. */
  90. # define PS_INSECURE_PROTOCOL -20 /* The operation needs to use insecure protocol.
  91. The caller needs to accept use of insecure
  92. protocol. */
  93. # define PS_VERIFICATION_FAILED -21 /* Signature verification failed. */
  94. //CRC
  95. #define OUTPUT_REFLECT 1
  96. #define INPUT_REFLECT 2
  97. typedef enum __CRYPTO_METHOD
  98. {
  99. CRYPTO_METHOD_RSV=0,
  100. CRYPTO_METHOD_RC4,
  101. CRYPTO_METHOD_SHA1,
  102. CRYPTO_METHOD_AES,
  103. CRYPTO_METHOD_DES,
  104. CRYPTO_METHOD_3DES,
  105. CRYPTO_METHOD_CRC, ///< CRC
  106. CRYPTO_METHOD_MD5 ///< MD5
  107. }CRYPTO_METHOD;
  108. /**
  109. * The enum of the encryption/decryption way.
  110. */
  111. typedef enum __CRYPTO_WAY
  112. {
  113. CRYPTO_WAY_ENCRYPT=0, ///< Encryption
  114. CRYPTO_WAY_DECRYPT ///< Decryption
  115. }CRYPTO_WAY;
  116. typedef enum __CRYPTO_RNG_SWITCH
  117. {
  118. CRYPTO_RNG_SWITCH_16 = 0,
  119. CRYPTO_RNG_SWITCH_32
  120. }CRYPTO_RNG_SWITCH;
  121. /**
  122. * The enum of the encryption/decryption mode, only for (aes des 3des).
  123. */
  124. typedef enum __CRYPTO_MODE
  125. {
  126. CRYPTO_MODE_ECB=0, ///< ECB
  127. CRYPTO_MODE_CBC, ///< CBC
  128. CRYPTO_MODE_CTR, ///< CTR, only for AES
  129. CRYPTO_MODE_CMAC ///< MAC, only for AES
  130. }CRYPTO_MODE;
  131. /**
  132. * The enum of the CRC type.
  133. */
  134. typedef enum __CRYPTO_CRC_TYPE
  135. {
  136. CRYPTO_CRC_TYPE_8=0, ///< 8 bits CRC
  137. CRYPTO_CRC_TYPE_16_MODBUS, ///< 16 bits Modbus CRC
  138. CRYPTO_CRC_TYPE_16_CCITT, ///< 16 bits CCITT CRC
  139. CRYPTO_CRC_TYPE_32 ///< 32 bits CRC
  140. }CRYPTO_CRC_TYPE;
  141. /**
  142. * The struct of the CRC context.
  143. */
  144. typedef struct {
  145. u32 state; ///< The initial value input and result value output.
  146. u8 type; ///< The type of CRC, refernec the CRYPTO_CRC_TYPE enum.
  147. u8 mode; ///< The mode of CRC, bit0 means output reflection and bit1 means input reflection.
  148. }psCrcContext_t;
  149. #if 1
  150. typedef u32 hstm_digit;
  151. typedef struct {
  152. int16 used, alloc, sign;
  153. hstm_digit *dp;
  154. } hstm_int;
  155. typedef struct
  156. {
  157. # ifdef HAVE_NATIVE_INT64
  158. uint64 length;
  159. # else
  160. uint32 lengthHi;
  161. uint32 lengthLo;
  162. # endif /* HAVE_NATIVE_INT64 */
  163. uint32 state[5], curlen;
  164. unsigned char buf[64];
  165. } psSha1_t;
  166. typedef struct
  167. {
  168. # ifdef HAVE_NATIVE_INT64
  169. uint64 length;
  170. # else
  171. uint32 lengthHi;
  172. uint32 lengthLo;
  173. # endif /* HAVE_NATIVE_INT64 */
  174. uint32 state[4], curlen;
  175. unsigned char buf[64];
  176. } psMd5_t;
  177. typedef struct
  178. {
  179. union
  180. {
  181. psSha1_t sha1;
  182. psMd5_t md5;
  183. } u;
  184. int32_t hashAlgId;
  185. } psDigestContext_t;
  186. # define AES_BLOCKLEN 16
  187. # define AES_IVLEN AES_BLOCKLEN
  188. # define AES128_KEYLEN 16
  189. # define AES192_KEYLEN 24
  190. # define AES256_KEYLEN 32
  191. # define DES3_BLOCKLEN 8
  192. # define DES3_IVLEN DES3_BLOCKLEN
  193. # define DES3_KEYLEN 24
  194. typedef struct
  195. {
  196. uint32_t skey[64]; /**< Key schedule (either encrypt or decrypt) */
  197. uint16_t rounds; /**< Number of rounds */
  198. uint16_t type; /**< PS_AES_ENCRYPT or PS_AES_DECRYPT (inverse) key */
  199. } psAesKey_t;
  200. typedef struct
  201. {
  202. psAesKey_t key;
  203. unsigned char IV[AES_BLOCKLEN];
  204. } psAesCbc_t;
  205. typedef struct
  206. {
  207. unsigned char state[256];
  208. uint32_t byteCount;
  209. unsigned char x;
  210. unsigned char y;
  211. } psArc4_t;
  212. typedef struct
  213. {
  214. uint32_t ek[3][32];
  215. uint32_t dk[3][32];
  216. } psDes3Key_t;
  217. typedef struct
  218. {
  219. psDes3Key_t key;
  220. unsigned char IV[DES3_BLOCKLEN];
  221. uint32_t blocklen;
  222. } psDes3_t;
  223. typedef union
  224. {
  225. psAesCbc_t aes;
  226. psArc4_t arc4;
  227. psDes3_t des3;
  228. } psCipherContext_t;
  229. #endif
  230. struct wm_crypto_ctx
  231. {
  232. volatile u8 rsa_complete;
  233. volatile u8 gpsec_complete;
  234. #ifndef CONFIG_KERNEL_NONE
  235. tls_os_sem_t *gpsec_lock;
  236. #endif
  237. };
  238. /**
  239. * @defgroup System_APIs System APIs
  240. * @brief System APIs
  241. */
  242. /**
  243. * @addtogroup System_APIs
  244. * @{
  245. */
  246. /**
  247. * @defgroup HARD_CRYPTO_APIs HARD CRYPTO APIs
  248. * @brief hard crypto APIs
  249. */
  250. /**
  251. * @addtogroup HARD_CRYPTO_APIs
  252. * @{
  253. */
  254. /**
  255. * @brief This function is used to stop random produce.
  256. *
  257. * @param[in] None
  258. *
  259. * @retval 0 success
  260. * @retval other failed
  261. *
  262. * @note None
  263. */
  264. int tls_crypto_random_stop(void);
  265. /**
  266. * @brief This function initializes random digit seed and BIT number.
  267. *
  268. * @param[in] seed The random digit seed.
  269. * @param[in] rng_switch The random digit bit number. (0: 16bit 1:32bit)
  270. *
  271. * @retval 0 success
  272. * @retval other failed
  273. *
  274. * @note None
  275. */
  276. int tls_crypto_random_init(u32 seed, CRYPTO_RNG_SWITCH rng_switch);
  277. /**
  278. * @brief This function is used to get random digit content.
  279. *
  280. * @param[in] out Pointer to the output of random digit.
  281. * @param[in] len The random digit bit number will output.
  282. *
  283. * @retval 0 success
  284. * @retval other failed
  285. *
  286. * @note None
  287. */
  288. int tls_crypto_random_bytes(unsigned char *out, u32 len);
  289. /**
  290. * @brief This function is used to generate true random number.
  291. *
  292. * @param[in] out Pointer to the output of random number.
  293. * @param[in] len The random number length.
  294. *
  295. * @retval 0 success
  296. * @retval other failed
  297. *
  298. * @note None
  299. */
  300. int tls_crypto_trng(unsigned char *out, u32 len);
  301. /**
  302. * @brief This function initializes a RC4 encryption algorithm,
  303. * i.e. fills the psCipherContext_t structure pointed to by ctx with necessary data.
  304. *
  305. * @param[in] ctx Pointer to the Cipher Context.
  306. * @param[in] key Pointer to the key.
  307. * @param[in] keylen the length of key.
  308. *
  309. * @retval 0 success
  310. * @retval other failed
  311. *
  312. * @note The first parameter ctx must be a structure which is allocated externally.
  313. * And all of Context parameters in the initializing methods should be allocated externally too.
  314. */
  315. int tls_crypto_rc4_init(psCipherContext_t * ctx, const unsigned char *key, u32 keylen);
  316. /**
  317. * @brief This function encrypts a variable length data stream according to RC4.
  318. * The RC4 algorithm it generates a "keystream" which is simply XORed with the plaintext to produce the ciphertext stream.
  319. * Decryption is exactly the same as encryption. This function also decrypts a variable length data stream according to RC4.
  320. *
  321. * @param[in] ctx Pointer to the Cipher Context.
  322. * @param[in] in Pointer to the input plaintext data stream(or the encrypted text data stream) of variable length.
  323. * @param[in] out Pointer to the resulting ciphertext data stream.
  324. * @param[in] len Length of the plaintext data stream in octets.
  325. *
  326. * @retval 0 success
  327. * @retval other failed
  328. *
  329. * @note None
  330. */
  331. int tls_crypto_rc4(psCipherContext_t * ctx, unsigned char *in, unsigned char *out, u32 len);
  332. /**
  333. * @brief This function initializes a AES encryption algorithm, i.e. fills the psCipherContext_t structure pointed to by ctx with necessary data.
  334. *
  335. * @param[in] ctx Pointer to the Cipher Context.
  336. * @param[in] IV Pointer to the Initialization Vector
  337. * @param[in] key Pointer to the key.
  338. * @param[in] keylen the length of key.
  339. * @param[in] cbc the encryption mode, AES supports ECB/CBC/CTR modes.
  340. *
  341. * @retval 0 success
  342. * @retval other failed
  343. *
  344. * @note None
  345. */
  346. int tls_crypto_aes_init(psCipherContext_t * ctx, const unsigned char *IV, const unsigned char *key, u32 keylen, CRYPTO_MODE cbc);
  347. /**
  348. * @brief This function encrypts or decrypts a variable length data stream according to AES.
  349. *
  350. * @param[in] ctx Pointer to the Cipher Context.
  351. * @param[in] in Pointer to the input plaintext data stream(or the encrypted text data stream) of variable length.
  352. * @param[in] out Pointer to the resulting ciphertext data stream.
  353. * @param[in] len Length of the plaintext data stream in octets.
  354. * @param[in] dec The cryption way which indicates encryption or decryption.
  355. *
  356. * @retval 0 success
  357. * @retval other failed
  358. *
  359. * @note None
  360. */
  361. int tls_crypto_aes_encrypt_decrypt(psCipherContext_t * ctx, unsigned char *in, unsigned char *out, u32 len, CRYPTO_WAY dec);
  362. /**
  363. * @brief This function initializes a 3DES encryption algorithm, i.e. fills the psCipherContext_t structure pointed to by ctx with necessary data.
  364. *
  365. * @param[in] ctx Pointer to the Cipher Context.
  366. * @param[in] IV Pointer to the Initialization Vector
  367. * @param[in] key Pointer to the key.
  368. * @param[in] keylen the length of key.
  369. * @param[in] cbc the encryption mode, 3DES supports ECB/CBC modes.
  370. *
  371. * @retval 0 success
  372. * @retval other failed
  373. *
  374. * @note None
  375. */
  376. int tls_crypto_3des_init(psCipherContext_t * ctx, const unsigned char *IV, const unsigned char *key, u32 keylen, CRYPTO_MODE cbc);
  377. /**
  378. * @brief This function encrypts or decrypts a variable length data stream according to 3DES.
  379. *
  380. * @param[in] ctx Pointer to the Cipher Context.
  381. * @param[in] in Pointer to the input plaintext data stream(or the encrypted text data stream) of variable length.
  382. * @param[in] out Pointer to the resulting ciphertext data stream.
  383. * @param[in] len Length of the plaintext data stream in octets.
  384. * @param[in] dec The cryption way which indicates encryption or decryption.
  385. *
  386. * @retval 0 success
  387. * @retval other failed
  388. *
  389. * @note None
  390. */
  391. int tls_crypto_3des_encrypt_decrypt(psCipherContext_t * ctx, unsigned char *in, unsigned char *out, u32 len, CRYPTO_WAY dec);
  392. /**
  393. * @brief This function initializes a DES encryption algorithm, i.e. fills the psCipherContext_t structure pointed to by ctx with necessary data.
  394. *
  395. * @param[in] ctx Pointer to the Cipher Context.
  396. * @param[in] IV Pointer to the Initialization Vector
  397. * @param[in] key Pointer to the key.
  398. * @param[in] keylen the length of key.
  399. * @param[in] cbc the encryption mode, DES supports ECB/CBC modes.
  400. *
  401. * @retval 0 success
  402. * @retval other failed
  403. *
  404. * @note None
  405. */
  406. int tls_crypto_des_init(psCipherContext_t * ctx, const unsigned char *IV, const unsigned char *key, u32 keylen, CRYPTO_MODE cbc);
  407. /**
  408. * @brief This function encrypts or decrypts a variable length data stream according to DES.
  409. *
  410. * @param[in] ctx Pointer to the Cipher Context.
  411. * @param[in] in Pointer to the input plaintext data stream(or the encrypted text data stream) of variable length.
  412. * @param[in] out Pointer to the resulting ciphertext data stream.
  413. * @param[in] len Length of the plaintext data stream in octets.
  414. * @param[in] dec The cryption way which indicates encryption or decryption.
  415. *
  416. * @retval 0 success
  417. * @retval other failed
  418. *
  419. * @note None
  420. */
  421. int tls_crypto_des_encrypt_decrypt(psCipherContext_t * ctx, unsigned char *in, unsigned char *out, u32 len, CRYPTO_WAY dec);
  422. /**
  423. * @brief This function initializes a CRC algorithm, i.e. fills the psCrcContext_t structure pointed to by ctx with necessary data.
  424. *
  425. * @param[in] ctx Pointer to the CRC Context.
  426. * @param[in] key The initialization key.
  427. * @param[in] crc_type The CRC type, supports CRC8/CRC16 MODBUS/CRC16 CCITT/CRC32
  428. * @param[in] mode Set input or outpu reflect.
  429. * @param[in] dec The cryption way which indicates encryption or decryption.
  430. * see OUTPUT_REFLECT
  431. * see INPUT_REFLECT
  432. *
  433. * @retval 0 success
  434. * @retval other failed
  435. *
  436. * @note None
  437. */
  438. int tls_crypto_crc_init(psCrcContext_t * ctx, u32 key, CRYPTO_CRC_TYPE crc_type, u8 mode);
  439. /**
  440. * @brief This function updates the CRC value with a variable length bytes.
  441. * This function may be called as many times as necessary, so the message may be processed in blocks.
  442. *
  443. * @param[in] ctx Pointer to the CRC Context.
  444. * @param[in] in Pointer to a variable length bytes
  445. * @param[in] len The bytes 's length
  446. *
  447. * @retval 0 success
  448. * @retval other failed
  449. *
  450. * @note None
  451. */
  452. int tls_crypto_crc_update(psCrcContext_t * ctx, unsigned char *in, u32 len);
  453. /**
  454. * @brief This function ends a CRC operation and produces a CRC value.
  455. *
  456. * @param[in] ctx Pointer to the CRC Context.
  457. * @param[in] crc_val Pointer to the CRC value.
  458. *
  459. * @retval 0 success
  460. * @retval other failed
  461. *
  462. * @note None
  463. */
  464. int tls_crypto_crc_final(psCrcContext_t * ctx, u32 *crc_val);
  465. /**
  466. * @brief This function initializes Message-Diggest context for usage in SHA1 algorithm, starts a new SHA1 operation and writes a new Digest Context.
  467. *
  468. * @param[in] md Pointer to the SHA1 Digest Context.
  469. *
  470. * @retval 0 success
  471. * @retval other failed
  472. *
  473. * @note None
  474. */
  475. void tls_crypto_sha1_init(psDigestContext_t * md);
  476. /**
  477. * @brief Process a message block using SHA1 algorithm.
  478. * This function performs a SHA1 block update operation. It continues an SHA1 message-digest operation,
  479. * by processing InputLen-byte length message block pointed to by buf, and by updating the SHA1 context pointed to by md.
  480. * This function may be called as many times as necessary, so the message may be processed in blocks.
  481. *
  482. * @param[in] md Pointer to the SHA1 Digest Context.
  483. * @param[in] buf InputLen-byte length message block
  484. * @param[in] len The buf 's length
  485. *
  486. * @returnl None
  487. *
  488. * @note None
  489. */
  490. void tls_crypto_sha1_update(psDigestContext_t * md, const unsigned char *buf, u32 len);
  491. /**
  492. * @brief This function ends a SHA1 operation and produces a Message-Digest.
  493. * This function finalizes SHA1 algorithm, i.e. ends an SHA1 Message-Digest operation,
  494. * writing the Message-Digest in the 20-byte buffer pointed to by hash in according to the information stored in context.
  495. *
  496. * @param[in] md Pointer to the SHA1 Digest Context.
  497. * @param[in] hash Pointer to the Message-Digest
  498. *
  499. * @retval 20 success, return the hash size.
  500. * @retval <0 failed
  501. *
  502. * @note None
  503. */
  504. int tls_crypto_sha1_final(psDigestContext_t * md, unsigned char *hash);
  505. /**
  506. * @brief This function initializes Message-Diggest context for usage in MD5 algorithm, starts a new MD5 operation and writes a new Digest Context.
  507. * This function begins a MD5 Message-Diggest Algorithm, i.e. fills the psDigestContext_t structure pointed to by md with necessary data.
  508. * MD5 is the algorithm which takes as input a message of arbitrary length and produces as output a 128-bit "fingerprint" or "message digest" of the input.
  509. * It is conjectured that it is computationally infeasible to produce two messages having the same message digest,
  510. * or to produce any message having a given prespecified target message digest.
  511. *
  512. * @param[in] md MD5 Digest Context.
  513. *
  514. * @return None
  515. *
  516. * @note None
  517. */
  518. void tls_crypto_md5_init(psDigestContext_t * md);
  519. /**
  520. * @brief Process a message block using MD5 algorithm.
  521. * This function performs a MD5 block update operation. It continues an MD5 message-digest operation,
  522. * by processing InputLen-byte length message block pointed to by buf, and by updating the MD5 context pointed to by md.
  523. * This function may be called as many times as necessary, so the message may be processed in blocks.
  524. *
  525. * @param[in] md MD5 Digest Context.
  526. * @param[in] buf InputLen-byte length message block
  527. * @param[in] len The buf 's length
  528. *
  529. * @return None
  530. *
  531. * @note None
  532. */
  533. void tls_crypto_md5_update(psDigestContext_t * md, const unsigned char *buf, u32 len);
  534. /**
  535. * @brief This function ends a MD5 operation and produces a Message-Digest.
  536. * This function finalizes MD5 algorithm, i.e. ends an MD5 Message-Digest operation,
  537. * writing the Message-Digest in the 16-byte buffer pointed to by hash in according to the information stored in context.
  538. *
  539. * @param[in] md MD5 Digest Context.
  540. * @param[in] hash the Message-Digest
  541. *
  542. * @retval 16 success, return the hash size.
  543. * @retval <0 failed
  544. *
  545. * @note None
  546. */
  547. int tls_crypto_md5_final(psDigestContext_t * md, unsigned char *hash);
  548. /**
  549. * @brief This function implements the large module power multiplication algorithm.
  550. * res = a**e (mod n)
  551. *
  552. * @param[in] a Pointer to a bignumber.
  553. * @param[in] e Pointer to a bignumber.
  554. * @param[in] n Pointer to a bignumber.
  555. * @param[out] res Pointer to the result bignumber.
  556. *
  557. * @retval 0 success
  558. * @retval other failed
  559. *
  560. * @note None
  561. */
  562. int tls_crypto_exptmod(hstm_int *a, hstm_int *e, hstm_int *n, hstm_int *res);
  563. /**
  564. * @brief This function initializes the encryption module.
  565. *
  566. * @param None
  567. *
  568. * @retval 0 success
  569. * @retval other failed
  570. *
  571. * @note None
  572. */
  573. int tls_crypto_init(void);
  574. /**
  575. * @}
  576. */
  577. /**
  578. * @}
  579. */
  580. #endif