blob: e92d27218c486f1dca86368bbca8a788d63e6584 [file] [log] [blame]
Simon Glass460408e2012-12-05 14:46:36 +00001/*
2 * Copyright (c) 2012 The Chromium OS Authors.
Wolfgang Denk1a459662013-07-08 09:37:19 +02003 * SPDX-License-Identifier: GPL-2.0+
Simon Glass460408e2012-12-05 14:46:36 +00004 */
5
6#ifndef _HASH_H
7#define _HASH_H
8
Simon Glassd20a40d2013-02-24 20:30:22 +00009#if defined(CONFIG_SHA1SUM_VERIFY) || defined(CONFIG_CRC32_VERIFY)
Simon Glass460408e2012-12-05 14:46:36 +000010#define CONFIG_HASH_VERIFY
11#endif
12
13struct 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 Glassd5b76672013-02-24 17:33:26 +000038enum {
39 HASH_FLAG_VERIFY = 1 << 0, /* Enable verify mode */
40 HASH_FLAG_ENV = 1 << 1, /* Allow env vars */
41};
42
Simon Glass460408e2012-12-05 14:46:36 +000043/**
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 Glass218da0f2013-02-24 17:33:32 +000048 * @algo_name: Hash algorithm being used (lower case!)
Simon Glassd5b76672013-02-24 17:33:26 +000049 * @flags: Flags value (HASH_FLAG_...)
Simon Glass460408e2012-12-05 14:46:36 +000050 * @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 Glassd5b76672013-02-24 17:33:26 +000055int hash_command(const char *algo_name, int flags, cmd_tbl_t *cmdtp, int flag,
Simon Glass460408e2012-12-05 14:46:36 +000056 int argc, char * const argv[]);
57
Simon Glass6f907b42013-05-07 06:11:47 +000058/**
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 */
77int hash_block(const char *algo_name, const void *data, unsigned int len,
78 uint8_t *output, int *output_size);
79
Simon Glass460408e2012-12-05 14:46:36 +000080#endif