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 | |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 9 | #if defined(CONFIG_SHA1SUM_VERIFY) || defined(CONFIG_CRC32_VERIFY) |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 10 | #define CONFIG_HASH_VERIFY |
| 11 | #endif |
| 12 | |
| 13 | struct hash_algo { |
| 14 | const char *name; /* Name of algorithm */ |
| 15 | int digest_size; /* Length of digest */ |
| 16 | /** |
| 17 | * hash_func_ws: Generic hashing function |
| 18 | * |
| 19 | * This is the generic prototype for a hashing function. We only |
| 20 | * have the watchdog version at present. |
| 21 | * |
| 22 | * @input: Input buffer |
| 23 | * @ilen: Input buffer length |
| 24 | * @output: Checksum result (length depends on algorithm) |
| 25 | * @chunk_sz: Trigger watchdog after processing this many bytes |
| 26 | */ |
| 27 | void (*hash_func_ws)(const unsigned char *input, unsigned int ilen, |
| 28 | unsigned char *output, unsigned int chunk_sz); |
| 29 | int chunk_size; /* Watchdog chunk size */ |
| 30 | }; |
| 31 | |
| 32 | /* |
| 33 | * Maximum digest size for all algorithms we support. Having this value |
| 34 | * avoids a malloc() or C99 local declaration in common/cmd_hash.c. |
| 35 | */ |
| 36 | #define HASH_MAX_DIGEST_SIZE 32 |
| 37 | |
Simon Glass | 80e345a | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 38 | enum { |
| 39 | HASH_FLAG_VERIFY = 1 << 0, /* Enable verify mode */ |
| 40 | HASH_FLAG_ENV = 1 << 1, /* Allow env vars */ |
| 41 | }; |
| 42 | |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 43 | /** |
| 44 | * hash_command: Process a hash command for a particular algorithm |
| 45 | * |
| 46 | * This common function is used to implement specific hash commands. |
| 47 | * |
Simon Glass | e22a6fc | 2013-02-24 17:33:32 +0000 | [diff] [blame] | 48 | * @algo_name: Hash algorithm being used (lower case!) |
Simon Glass | 80e345a | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 49 | * @flags: Flags value (HASH_FLAG_...) |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 50 | * @cmdtp: Pointer to command table entry |
| 51 | * @flag: Some flags normally 0 (see CMD_FLAG_.. above) |
| 52 | * @argc: Number of arguments (arg 0 must be the command text) |
| 53 | * @argv: Arguments |
| 54 | */ |
Simon Glass | 80e345a | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 55 | 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] | 56 | int argc, char * const argv[]); |
| 57 | |
Simon Glass | c75d53d | 2013-05-07 06:11:47 +0000 | [diff] [blame] | 58 | /** |
| 59 | * hash_block() - Hash a block according to the requested algorithm |
| 60 | * |
| 61 | * The caller probably knows the hash length for the chosen algorithm, but |
| 62 | * in order to provide a general interface, and output_size parameter is |
| 63 | * provided. |
| 64 | * |
| 65 | * @algo_name: Hash algorithm to use |
| 66 | * @data: Data to hash |
| 67 | * @len: Lengh of data to hash in bytes |
| 68 | * @output: Place to put hash value |
| 69 | * @output_size: On entry, pointer to the number of bytes available in |
| 70 | * output. On exit, pointer to the number of bytes used. |
| 71 | * If NULL, then it is assumed that the caller has |
| 72 | * allocated enough space for the hash. This is possible |
| 73 | * since the caller is selecting the algorithm. |
| 74 | * @return 0 if ok, -ve on error: -EPROTONOSUPPORT for an unknown algorithm, |
| 75 | * -ENOSPC if the output buffer is not large enough. |
| 76 | */ |
| 77 | int hash_block(const char *algo_name, const void *data, unsigned int len, |
| 78 | uint8_t *output, int *output_size); |
| 79 | |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 80 | #endif |