hash.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (c) 2012 The Chromium OS Authors.
  4. */
  5. #ifndef _HASH_H
  6. #define _HASH_H
  7. struct cmd_tbl;
  8. /*
  9. * Maximum digest size for all algorithms we support. Having this value
  10. * avoids a malloc() or C99 local declaration in common/cmd_hash.c.
  11. */
  12. #if defined(CONFIG_SHA384) || defined(CONFIG_SHA512)
  13. #define HASH_MAX_DIGEST_SIZE 64
  14. #else
  15. #define HASH_MAX_DIGEST_SIZE 32
  16. #endif
  17. enum {
  18. HASH_FLAG_VERIFY = 1 << 0, /* Enable verify mode */
  19. HASH_FLAG_ENV = 1 << 1, /* Allow env vars */
  20. };
  21. struct hash_algo {
  22. const char *name; /* Name of algorithm */
  23. int digest_size; /* Length of digest */
  24. /**
  25. * hash_func_ws: Generic hashing function
  26. *
  27. * This is the generic prototype for a hashing function. We only
  28. * have the watchdog version at present.
  29. *
  30. * @input: Input buffer
  31. * @ilen: Input buffer length
  32. * @output: Checksum result (length depends on algorithm)
  33. * @chunk_sz: Trigger watchdog after processing this many bytes
  34. */
  35. void (*hash_func_ws)(const unsigned char *input, unsigned int ilen,
  36. unsigned char *output, unsigned int chunk_sz);
  37. int chunk_size; /* Watchdog chunk size */
  38. /*
  39. * hash_init: Create the context for progressive hashing
  40. *
  41. * @algo: Pointer to the hash_algo struct
  42. * @ctxp: Pointer to the pointer of the context for hashing
  43. * @return 0 if ok, -1 on error
  44. */
  45. int (*hash_init)(struct hash_algo *algo, void **ctxp);
  46. /*
  47. * hash_update: Perform hashing on the given buffer
  48. *
  49. * The context is freed by this function if an error occurs.
  50. *
  51. * @algo: Pointer to the hash_algo struct
  52. * @ctx: Pointer to the context for hashing
  53. * @buf: Pointer to the buffer being hashed
  54. * @size: Size of the buffer being hashed
  55. * @is_last: 1 if this is the last update; 0 otherwise
  56. * @return 0 if ok, -1 on error
  57. */
  58. int (*hash_update)(struct hash_algo *algo, void *ctx, const void *buf,
  59. unsigned int size, int is_last);
  60. /*
  61. * hash_finish: Write the hash result to the given buffer
  62. *
  63. * The context is freed by this function.
  64. *
  65. * @algo: Pointer to the hash_algo struct
  66. * @ctx: Pointer to the context for hashing
  67. * @dest_buf: Pointer to the buffer for the result
  68. * @size: Size of the buffer for the result
  69. * @return 0 if ok, -ENOSPC if size of the result buffer is too small
  70. * or -1 on other errors
  71. */
  72. int (*hash_finish)(struct hash_algo *algo, void *ctx, void *dest_buf,
  73. int size);
  74. };
  75. #ifndef USE_HOSTCC
  76. /**
  77. * hash_command: Process a hash command for a particular algorithm
  78. *
  79. * This common function is used to implement specific hash commands.
  80. *
  81. * @algo_name: Hash algorithm being used (lower case!)
  82. * @flags: Flags value (HASH_FLAG_...)
  83. * @cmdtp: Pointer to command table entry
  84. * @flag: Some flags normally 0 (see CMD_FLAG_.. above)
  85. * @argc: Number of arguments (arg 0 must be the command text)
  86. * @argv: Arguments
  87. */
  88. int hash_command(const char *algo_name, int flags, struct cmd_tbl *cmdtp,
  89. int flag, int argc, char *const argv[]);
  90. /**
  91. * hash_block() - Hash a block according to the requested algorithm
  92. *
  93. * The caller probably knows the hash length for the chosen algorithm, but
  94. * in order to provide a general interface, and output_size parameter is
  95. * provided.
  96. *
  97. * @algo_name: Hash algorithm to use
  98. * @data: Data to hash
  99. * @len: Lengh of data to hash in bytes
  100. * @output: Place to put hash value
  101. * @output_size: On entry, pointer to the number of bytes available in
  102. * output. On exit, pointer to the number of bytes used.
  103. * If NULL, then it is assumed that the caller has
  104. * allocated enough space for the hash. This is possible
  105. * since the caller is selecting the algorithm.
  106. * @return 0 if ok, -ve on error: -EPROTONOSUPPORT for an unknown algorithm,
  107. * -ENOSPC if the output buffer is not large enough.
  108. */
  109. int hash_block(const char *algo_name, const void *data, unsigned int len,
  110. uint8_t *output, int *output_size);
  111. #endif /* !USE_HOSTCC */
  112. /**
  113. * hash_lookup_algo() - Look up the hash_algo struct for an algorithm
  114. *
  115. * The function returns the pointer to the struct or -EPROTONOSUPPORT if the
  116. * algorithm is not available.
  117. *
  118. * @algo_name: Hash algorithm to look up
  119. * @algop: Pointer to the hash_algo struct if found
  120. *
  121. * @return 0 if ok, -EPROTONOSUPPORT for an unknown algorithm.
  122. */
  123. int hash_lookup_algo(const char *algo_name, struct hash_algo **algop);
  124. /**
  125. * hash_progressive_lookup_algo() - Look up hash_algo for prog. hash support
  126. *
  127. * The function returns the pointer to the struct or -EPROTONOSUPPORT if the
  128. * algorithm is not available with progressive hash support.
  129. *
  130. * @algo_name: Hash algorithm to look up
  131. * @algop: Pointer to the hash_algo struct if found
  132. *
  133. * @return 0 if ok, -EPROTONOSUPPORT for an unknown algorithm.
  134. */
  135. int hash_progressive_lookup_algo(const char *algo_name,
  136. struct hash_algo **algop);
  137. /**
  138. * hash_parse_string() - Parse hash string into a binary array
  139. *
  140. * The function parses a hash string into a binary array that
  141. * can for example easily be used to compare to hash values.
  142. *
  143. * @algo_name: Hash algorithm to look up
  144. * @str: Hash string to get parsed
  145. * @result: Binary array of the parsed hash string
  146. *
  147. * @return 0 if ok, -EPROTONOSUPPORT for an unknown algorithm.
  148. */
  149. int hash_parse_string(const char *algo_name, const char *str, uint8_t *result);
  150. #endif