hash.h 5.1 KB

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