hash.h 5.1 KB

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