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