Simon Glass | 460408e | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The Chromium OS Authors. |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 3 | * SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 460408e | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #ifndef _HASH_H |
| 7 | #define _HASH_H |
| 8 | |
Michael van der Westhuizen | 1de7bb4 | 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 | |
| 20 | #ifndef USE_HOSTCC |
Simon Glass | d20a40d | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 21 | #if defined(CONFIG_SHA1SUM_VERIFY) || defined(CONFIG_CRC32_VERIFY) |
Simon Glass | 460408e | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 22 | #define CONFIG_HASH_VERIFY |
| 23 | #endif |
| 24 | |
| 25 | struct hash_algo { |
| 26 | const char *name; /* Name of algorithm */ |
| 27 | int digest_size; /* Length of digest */ |
| 28 | /** |
| 29 | * hash_func_ws: Generic hashing function |
| 30 | * |
| 31 | * This is the generic prototype for a hashing function. We only |
| 32 | * have the watchdog version at present. |
| 33 | * |
| 34 | * @input: Input buffer |
| 35 | * @ilen: Input buffer length |
| 36 | * @output: Checksum result (length depends on algorithm) |
| 37 | * @chunk_sz: Trigger watchdog after processing this many bytes |
| 38 | */ |
| 39 | void (*hash_func_ws)(const unsigned char *input, unsigned int ilen, |
| 40 | unsigned char *output, unsigned int chunk_sz); |
| 41 | int chunk_size; /* Watchdog chunk size */ |
Hung-ying Tyan | bf007eb | 2014-03-03 12:19:28 +0100 | [diff] [blame] | 42 | /* |
| 43 | * hash_init: Create the context for progressive hashing |
| 44 | * |
| 45 | * @algo: Pointer to the hash_algo struct |
| 46 | * @ctxp: Pointer to the pointer of the context for hashing |
| 47 | * @return 0 if ok, -1 on error |
| 48 | */ |
| 49 | int (*hash_init)(struct hash_algo *algo, void **ctxp); |
| 50 | /* |
| 51 | * hash_update: Perform hashing on the given buffer |
| 52 | * |
| 53 | * The context is freed by this function if an error occurs. |
| 54 | * |
| 55 | * @algo: Pointer to the hash_algo struct |
| 56 | * @ctx: Pointer to the context for hashing |
| 57 | * @buf: Pointer to the buffer being hashed |
| 58 | * @size: Size of the buffer being hashed |
| 59 | * @is_last: 1 if this is the last update; 0 otherwise |
| 60 | * @return 0 if ok, -1 on error |
| 61 | */ |
| 62 | int (*hash_update)(struct hash_algo *algo, void *ctx, const void *buf, |
| 63 | unsigned int size, int is_last); |
| 64 | /* |
| 65 | * hash_finish: Write the hash result to the given buffer |
| 66 | * |
| 67 | * The context is freed by this function. |
| 68 | * |
| 69 | * @algo: Pointer to the hash_algo struct |
| 70 | * @ctx: Pointer to the context for hashing |
| 71 | * @dest_buf: Pointer to the buffer for the result |
| 72 | * @size: Size of the buffer for the result |
| 73 | * @return 0 if ok, -ENOSPC if size of the result buffer is too small |
| 74 | * or -1 on other errors |
| 75 | */ |
| 76 | int (*hash_finish)(struct hash_algo *algo, void *ctx, void *dest_buf, |
| 77 | int size); |
Simon Glass | 460408e | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
Simon Glass | 460408e | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 80 | /** |
| 81 | * hash_command: Process a hash command for a particular algorithm |
| 82 | * |
| 83 | * This common function is used to implement specific hash commands. |
| 84 | * |
Simon Glass | 218da0f | 2013-02-24 17:33:32 +0000 | [diff] [blame] | 85 | * @algo_name: Hash algorithm being used (lower case!) |
Simon Glass | d5b7667 | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 86 | * @flags: Flags value (HASH_FLAG_...) |
Simon Glass | 460408e | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 87 | * @cmdtp: Pointer to command table entry |
| 88 | * @flag: Some flags normally 0 (see CMD_FLAG_.. above) |
| 89 | * @argc: Number of arguments (arg 0 must be the command text) |
| 90 | * @argv: Arguments |
| 91 | */ |
Simon Glass | d5b7667 | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 92 | int hash_command(const char *algo_name, int flags, cmd_tbl_t *cmdtp, int flag, |
Simon Glass | 460408e | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 93 | int argc, char * const argv[]); |
| 94 | |
Simon Glass | 6f907b4 | 2013-05-07 06:11:47 +0000 | [diff] [blame] | 95 | /** |
| 96 | * hash_block() - Hash a block according to the requested algorithm |
| 97 | * |
| 98 | * The caller probably knows the hash length for the chosen algorithm, but |
| 99 | * in order to provide a general interface, and output_size parameter is |
| 100 | * provided. |
| 101 | * |
| 102 | * @algo_name: Hash algorithm to use |
| 103 | * @data: Data to hash |
| 104 | * @len: Lengh of data to hash in bytes |
| 105 | * @output: Place to put hash value |
| 106 | * @output_size: On entry, pointer to the number of bytes available in |
| 107 | * output. On exit, pointer to the number of bytes used. |
| 108 | * If NULL, then it is assumed that the caller has |
| 109 | * allocated enough space for the hash. This is possible |
| 110 | * since the caller is selecting the algorithm. |
| 111 | * @return 0 if ok, -ve on error: -EPROTONOSUPPORT for an unknown algorithm, |
| 112 | * -ENOSPC if the output buffer is not large enough. |
| 113 | */ |
| 114 | int hash_block(const char *algo_name, const void *data, unsigned int len, |
| 115 | uint8_t *output, int *output_size); |
| 116 | |
Hung-ying Tyan | bf007eb | 2014-03-03 12:19:28 +0100 | [diff] [blame] | 117 | /** |
| 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 | /** |
| 131 | * hash_show() - Print out a hash algorithm and value |
| 132 | * |
| 133 | * You will get a message like this (without a newline at the end): |
| 134 | * |
| 135 | * "sha1 for 9eb3337c ... 9eb3338f ==> 7942ef1df479fd3130f716eb9613d107dab7e257" |
| 136 | * |
| 137 | * @algo: Algorithm used for hash |
| 138 | * @addr: Address of data that was hashed |
| 139 | * @len: Length of data that was hashed |
| 140 | * @output: Hash value to display |
| 141 | */ |
Simon Glass | 04819a4 | 2014-06-12 07:24:41 -0600 | [diff] [blame] | 142 | void hash_show(struct hash_algo *algo, ulong addr, ulong len, |
| 143 | uint8_t *output); |
Michael van der Westhuizen | 1de7bb4 | 2014-05-30 20:59:00 +0200 | [diff] [blame] | 144 | #endif /* !USE_HOSTCC */ |
Simon Glass | 460408e | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 145 | #endif |