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