sha1.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Common values for SHA-1 algorithms
  4. */
  5. #ifndef _CRYPTO_SHA1_H
  6. #define _CRYPTO_SHA1_H
  7. #include <linux/types.h>
  8. #define SHA1_DIGEST_SIZE 20
  9. #define SHA1_BLOCK_SIZE 64
  10. #define SHA1_STATE_SIZE offsetof(struct sha1_state, buffer)
  11. #define SHA1_H0 0x67452301UL
  12. #define SHA1_H1 0xefcdab89UL
  13. #define SHA1_H2 0x98badcfeUL
  14. #define SHA1_H3 0x10325476UL
  15. #define SHA1_H4 0xc3d2e1f0UL
  16. extern const u8 sha1_zero_message_hash[SHA1_DIGEST_SIZE];
  17. struct sha1_state {
  18. u32 state[SHA1_DIGEST_SIZE / 4];
  19. u64 count;
  20. u8 buffer[SHA1_BLOCK_SIZE];
  21. };
  22. /* State for the SHA-1 compression function */
  23. struct sha1_block_state {
  24. u32 h[SHA1_DIGEST_SIZE / 4];
  25. };
  26. /**
  27. * struct sha1_ctx - Context for hashing a message with SHA-1
  28. * @state: the compression function state
  29. * @bytecount: number of bytes processed so far
  30. * @buf: partial block buffer; bytecount % SHA1_BLOCK_SIZE bytes are valid
  31. */
  32. struct sha1_ctx {
  33. struct sha1_block_state state;
  34. u64 bytecount;
  35. u8 buf[SHA1_BLOCK_SIZE];
  36. };
  37. /**
  38. * sha1_init() - Initialize a SHA-1 context for a new message
  39. * @ctx: the context to initialize
  40. *
  41. * If you don't need incremental computation, consider sha1() instead.
  42. *
  43. * Context: Any context.
  44. */
  45. void sha1_init(struct sha1_ctx *ctx);
  46. /**
  47. * sha1_update() - Update a SHA-1 context with message data
  48. * @ctx: the context to update; must have been initialized
  49. * @data: the message data
  50. * @len: the data length in bytes
  51. *
  52. * This can be called any number of times.
  53. *
  54. * Context: Any context.
  55. */
  56. void sha1_update(struct sha1_ctx *ctx, const u8 *data, size_t len);
  57. /**
  58. * sha1_final() - Finish computing a SHA-1 message digest
  59. * @ctx: the context to finalize; must have been initialized
  60. * @out: (output) the resulting SHA-1 message digest
  61. *
  62. * After finishing, this zeroizes @ctx. So the caller does not need to do it.
  63. *
  64. * Context: Any context.
  65. */
  66. void sha1_final(struct sha1_ctx *ctx, u8 out[at_least SHA1_DIGEST_SIZE]);
  67. /**
  68. * sha1() - Compute SHA-1 message digest in one shot
  69. * @data: the message data
  70. * @len: the data length in bytes
  71. * @out: (output) the resulting SHA-1 message digest
  72. *
  73. * Context: Any context.
  74. */
  75. void sha1(const u8 *data, size_t len, u8 out[at_least SHA1_DIGEST_SIZE]);
  76. /**
  77. * struct hmac_sha1_key - Prepared key for HMAC-SHA1
  78. * @istate: private
  79. * @ostate: private
  80. */
  81. struct hmac_sha1_key {
  82. struct sha1_block_state istate;
  83. struct sha1_block_state ostate;
  84. };
  85. /**
  86. * struct hmac_sha1_ctx - Context for computing HMAC-SHA1 of a message
  87. * @sha_ctx: private
  88. * @ostate: private
  89. */
  90. struct hmac_sha1_ctx {
  91. struct sha1_ctx sha_ctx;
  92. struct sha1_block_state ostate;
  93. };
  94. /**
  95. * hmac_sha1_preparekey() - Prepare a key for HMAC-SHA1
  96. * @key: (output) the key structure to initialize
  97. * @raw_key: the raw HMAC-SHA1 key
  98. * @raw_key_len: the key length in bytes. All key lengths are supported.
  99. *
  100. * Note: the caller is responsible for zeroizing both the struct hmac_sha1_key
  101. * and the raw key once they are no longer needed.
  102. *
  103. * Context: Any context.
  104. */
  105. void hmac_sha1_preparekey(struct hmac_sha1_key *key,
  106. const u8 *raw_key, size_t raw_key_len);
  107. /**
  108. * hmac_sha1_init() - Initialize an HMAC-SHA1 context for a new message
  109. * @ctx: (output) the HMAC context to initialize
  110. * @key: the prepared HMAC key
  111. *
  112. * If you don't need incremental computation, consider hmac_sha1() instead.
  113. *
  114. * Context: Any context.
  115. */
  116. void hmac_sha1_init(struct hmac_sha1_ctx *ctx, const struct hmac_sha1_key *key);
  117. /**
  118. * hmac_sha1_init_usingrawkey() - Initialize an HMAC-SHA1 context for a new
  119. * message, using a raw key
  120. * @ctx: (output) the HMAC context to initialize
  121. * @raw_key: the raw HMAC-SHA1 key
  122. * @raw_key_len: the key length in bytes. All key lengths are supported.
  123. *
  124. * If you don't need incremental computation, consider hmac_sha1_usingrawkey()
  125. * instead.
  126. *
  127. * Context: Any context.
  128. */
  129. void hmac_sha1_init_usingrawkey(struct hmac_sha1_ctx *ctx,
  130. const u8 *raw_key, size_t raw_key_len);
  131. /**
  132. * hmac_sha1_update() - Update an HMAC-SHA1 context with message data
  133. * @ctx: the HMAC context to update; must have been initialized
  134. * @data: the message data
  135. * @data_len: the data length in bytes
  136. *
  137. * This can be called any number of times.
  138. *
  139. * Context: Any context.
  140. */
  141. static inline void hmac_sha1_update(struct hmac_sha1_ctx *ctx,
  142. const u8 *data, size_t data_len)
  143. {
  144. sha1_update(&ctx->sha_ctx, data, data_len);
  145. }
  146. /**
  147. * hmac_sha1_final() - Finish computing an HMAC-SHA1 value
  148. * @ctx: the HMAC context to finalize; must have been initialized
  149. * @out: (output) the resulting HMAC-SHA1 value
  150. *
  151. * After finishing, this zeroizes @ctx. So the caller does not need to do it.
  152. *
  153. * Context: Any context.
  154. */
  155. void hmac_sha1_final(struct hmac_sha1_ctx *ctx,
  156. u8 out[at_least SHA1_DIGEST_SIZE]);
  157. /**
  158. * hmac_sha1() - Compute HMAC-SHA1 in one shot, using a prepared key
  159. * @key: the prepared HMAC key
  160. * @data: the message data
  161. * @data_len: the data length in bytes
  162. * @out: (output) the resulting HMAC-SHA1 value
  163. *
  164. * If you're using the key only once, consider using hmac_sha1_usingrawkey().
  165. *
  166. * Context: Any context.
  167. */
  168. void hmac_sha1(const struct hmac_sha1_key *key,
  169. const u8 *data, size_t data_len,
  170. u8 out[at_least SHA1_DIGEST_SIZE]);
  171. /**
  172. * hmac_sha1_usingrawkey() - Compute HMAC-SHA1 in one shot, using a raw key
  173. * @raw_key: the raw HMAC-SHA1 key
  174. * @raw_key_len: the key length in bytes. All key lengths are supported.
  175. * @data: the message data
  176. * @data_len: the data length in bytes
  177. * @out: (output) the resulting HMAC-SHA1 value
  178. *
  179. * If you're using the key multiple times, prefer to use hmac_sha1_preparekey()
  180. * followed by multiple calls to hmac_sha1() instead.
  181. *
  182. * Context: Any context.
  183. */
  184. void hmac_sha1_usingrawkey(const u8 *raw_key, size_t raw_key_len,
  185. const u8 *data, size_t data_len,
  186. u8 out[at_least SHA1_DIGEST_SIZE]);
  187. #endif /* _CRYPTO_SHA1_H */