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