hash.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2012 The Chromium OS Authors.
  3. * See file CREDITS for list of people who contributed to this
  4. * project.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  19. * MA 02111-1307 USA
  20. */
  21. #ifndef _HASH_H
  22. #define _HASH_H
  23. #if defined(CONFIG_SHA1SUM_VERIFY) || defined(CONFIG_CRC32_VERIFY)
  24. #define CONFIG_HASH_VERIFY
  25. #endif
  26. struct hash_algo {
  27. const char *name; /* Name of algorithm */
  28. int digest_size; /* Length of digest */
  29. /**
  30. * hash_func_ws: Generic hashing function
  31. *
  32. * This is the generic prototype for a hashing function. We only
  33. * have the watchdog version at present.
  34. *
  35. * @input: Input buffer
  36. * @ilen: Input buffer length
  37. * @output: Checksum result (length depends on algorithm)
  38. * @chunk_sz: Trigger watchdog after processing this many bytes
  39. */
  40. void (*hash_func_ws)(const unsigned char *input, unsigned int ilen,
  41. unsigned char *output, unsigned int chunk_sz);
  42. int chunk_size; /* Watchdog chunk size */
  43. };
  44. /*
  45. * Maximum digest size for all algorithms we support. Having this value
  46. * avoids a malloc() or C99 local declaration in common/cmd_hash.c.
  47. */
  48. #define HASH_MAX_DIGEST_SIZE 32
  49. enum {
  50. HASH_FLAG_VERIFY = 1 << 0, /* Enable verify mode */
  51. HASH_FLAG_ENV = 1 << 1, /* Allow env vars */
  52. };
  53. /**
  54. * hash_command: Process a hash command for a particular algorithm
  55. *
  56. * This common function is used to implement specific hash commands.
  57. *
  58. * @algo_name: Hash algorithm being used (lower case!)
  59. * @flags: Flags value (HASH_FLAG_...)
  60. * @cmdtp: Pointer to command table entry
  61. * @flag: Some flags normally 0 (see CMD_FLAG_.. above)
  62. * @argc: Number of arguments (arg 0 must be the command text)
  63. * @argv: Arguments
  64. */
  65. int hash_command(const char *algo_name, int flags, cmd_tbl_t *cmdtp, int flag,
  66. int argc, char * const argv[]);
  67. /**
  68. * hash_block() - Hash a block according to the requested algorithm
  69. *
  70. * The caller probably knows the hash length for the chosen algorithm, but
  71. * in order to provide a general interface, and output_size parameter is
  72. * provided.
  73. *
  74. * @algo_name: Hash algorithm to use
  75. * @data: Data to hash
  76. * @len: Lengh of data to hash in bytes
  77. * @output: Place to put hash value
  78. * @output_size: On entry, pointer to the number of bytes available in
  79. * output. On exit, pointer to the number of bytes used.
  80. * If NULL, then it is assumed that the caller has
  81. * allocated enough space for the hash. This is possible
  82. * since the caller is selecting the algorithm.
  83. * @return 0 if ok, -ve on error: -EPROTONOSUPPORT for an unknown algorithm,
  84. * -ENOSPC if the output buffer is not large enough.
  85. */
  86. int hash_block(const char *algo_name, const void *data, unsigned int len,
  87. uint8_t *output, int *output_size);
  88. #endif