hash.h 5.2 KB

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