1460408efSSimon Glass /* 2460408efSSimon Glass * Copyright (c) 2012 The Chromium OS Authors. 3*1a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 4460408efSSimon Glass */ 5460408efSSimon Glass 6460408efSSimon Glass #ifndef _HASH_H 7460408efSSimon Glass #define _HASH_H 8460408efSSimon Glass 9d20a40deSSimon Glass #if defined(CONFIG_SHA1SUM_VERIFY) || defined(CONFIG_CRC32_VERIFY) 10460408efSSimon Glass #define CONFIG_HASH_VERIFY 11460408efSSimon Glass #endif 12460408efSSimon Glass 13460408efSSimon Glass struct hash_algo { 14460408efSSimon Glass const char *name; /* Name of algorithm */ 15460408efSSimon Glass int digest_size; /* Length of digest */ 16460408efSSimon Glass /** 17460408efSSimon Glass * hash_func_ws: Generic hashing function 18460408efSSimon Glass * 19460408efSSimon Glass * This is the generic prototype for a hashing function. We only 20460408efSSimon Glass * have the watchdog version at present. 21460408efSSimon Glass * 22460408efSSimon Glass * @input: Input buffer 23460408efSSimon Glass * @ilen: Input buffer length 24460408efSSimon Glass * @output: Checksum result (length depends on algorithm) 25460408efSSimon Glass * @chunk_sz: Trigger watchdog after processing this many bytes 26460408efSSimon Glass */ 27460408efSSimon Glass void (*hash_func_ws)(const unsigned char *input, unsigned int ilen, 28460408efSSimon Glass unsigned char *output, unsigned int chunk_sz); 29460408efSSimon Glass int chunk_size; /* Watchdog chunk size */ 30460408efSSimon Glass }; 31460408efSSimon Glass 32460408efSSimon Glass /* 33460408efSSimon Glass * Maximum digest size for all algorithms we support. Having this value 34460408efSSimon Glass * avoids a malloc() or C99 local declaration in common/cmd_hash.c. 35460408efSSimon Glass */ 36460408efSSimon Glass #define HASH_MAX_DIGEST_SIZE 32 37460408efSSimon Glass 38d5b76673SSimon Glass enum { 39d5b76673SSimon Glass HASH_FLAG_VERIFY = 1 << 0, /* Enable verify mode */ 40d5b76673SSimon Glass HASH_FLAG_ENV = 1 << 1, /* Allow env vars */ 41d5b76673SSimon Glass }; 42d5b76673SSimon Glass 43460408efSSimon Glass /** 44460408efSSimon Glass * hash_command: Process a hash command for a particular algorithm 45460408efSSimon Glass * 46460408efSSimon Glass * This common function is used to implement specific hash commands. 47460408efSSimon Glass * 48218da0f3SSimon Glass * @algo_name: Hash algorithm being used (lower case!) 49d5b76673SSimon Glass * @flags: Flags value (HASH_FLAG_...) 50460408efSSimon Glass * @cmdtp: Pointer to command table entry 51460408efSSimon Glass * @flag: Some flags normally 0 (see CMD_FLAG_.. above) 52460408efSSimon Glass * @argc: Number of arguments (arg 0 must be the command text) 53460408efSSimon Glass * @argv: Arguments 54460408efSSimon Glass */ 55d5b76673SSimon Glass int hash_command(const char *algo_name, int flags, cmd_tbl_t *cmdtp, int flag, 56460408efSSimon Glass int argc, char * const argv[]); 57460408efSSimon Glass 586f907b42SSimon Glass /** 596f907b42SSimon Glass * hash_block() - Hash a block according to the requested algorithm 606f907b42SSimon Glass * 616f907b42SSimon Glass * The caller probably knows the hash length for the chosen algorithm, but 626f907b42SSimon Glass * in order to provide a general interface, and output_size parameter is 636f907b42SSimon Glass * provided. 646f907b42SSimon Glass * 656f907b42SSimon Glass * @algo_name: Hash algorithm to use 666f907b42SSimon Glass * @data: Data to hash 676f907b42SSimon Glass * @len: Lengh of data to hash in bytes 686f907b42SSimon Glass * @output: Place to put hash value 696f907b42SSimon Glass * @output_size: On entry, pointer to the number of bytes available in 706f907b42SSimon Glass * output. On exit, pointer to the number of bytes used. 716f907b42SSimon Glass * If NULL, then it is assumed that the caller has 726f907b42SSimon Glass * allocated enough space for the hash. This is possible 736f907b42SSimon Glass * since the caller is selecting the algorithm. 746f907b42SSimon Glass * @return 0 if ok, -ve on error: -EPROTONOSUPPORT for an unknown algorithm, 756f907b42SSimon Glass * -ENOSPC if the output buffer is not large enough. 766f907b42SSimon Glass */ 776f907b42SSimon Glass int hash_block(const char *algo_name, const void *data, unsigned int len, 786f907b42SSimon Glass uint8_t *output, int *output_size); 796f907b42SSimon Glass 80460408efSSimon Glass #endif 81