1460408efSSimon Glass /* 2460408efSSimon Glass * Copyright (c) 2012 The Chromium OS Authors. 31a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 4460408efSSimon Glass */ 5460408efSSimon Glass 6460408efSSimon Glass #ifndef _HASH_H 7460408efSSimon Glass #define _HASH_H 8460408efSSimon Glass 91de7bb4fSMichael van der Westhuizen /* 101de7bb4fSMichael van der Westhuizen * Maximum digest size for all algorithms we support. Having this value 111de7bb4fSMichael van der Westhuizen * avoids a malloc() or C99 local declaration in common/cmd_hash.c. 121de7bb4fSMichael van der Westhuizen */ 131de7bb4fSMichael van der Westhuizen #define HASH_MAX_DIGEST_SIZE 32 141de7bb4fSMichael van der Westhuizen 151de7bb4fSMichael van der Westhuizen enum { 161de7bb4fSMichael van der Westhuizen HASH_FLAG_VERIFY = 1 << 0, /* Enable verify mode */ 171de7bb4fSMichael van der Westhuizen HASH_FLAG_ENV = 1 << 1, /* Allow env vars */ 181de7bb4fSMichael van der Westhuizen }; 191de7bb4fSMichael van der Westhuizen 20460408efSSimon Glass struct hash_algo { 21460408efSSimon Glass const char *name; /* Name of algorithm */ 22460408efSSimon Glass int digest_size; /* Length of digest */ 23460408efSSimon Glass /** 24460408efSSimon Glass * hash_func_ws: Generic hashing function 25460408efSSimon Glass * 26460408efSSimon Glass * This is the generic prototype for a hashing function. We only 27460408efSSimon Glass * have the watchdog version at present. 28460408efSSimon Glass * 29460408efSSimon Glass * @input: Input buffer 30460408efSSimon Glass * @ilen: Input buffer length 31460408efSSimon Glass * @output: Checksum result (length depends on algorithm) 32460408efSSimon Glass * @chunk_sz: Trigger watchdog after processing this many bytes 33460408efSSimon Glass */ 34460408efSSimon Glass void (*hash_func_ws)(const unsigned char *input, unsigned int ilen, 35460408efSSimon Glass unsigned char *output, unsigned int chunk_sz); 36460408efSSimon Glass int chunk_size; /* Watchdog chunk size */ 37bf007ebbSHung-ying Tyan /* 38bf007ebbSHung-ying Tyan * hash_init: Create the context for progressive hashing 39bf007ebbSHung-ying Tyan * 40bf007ebbSHung-ying Tyan * @algo: Pointer to the hash_algo struct 41bf007ebbSHung-ying Tyan * @ctxp: Pointer to the pointer of the context for hashing 42bf007ebbSHung-ying Tyan * @return 0 if ok, -1 on error 43bf007ebbSHung-ying Tyan */ 44bf007ebbSHung-ying Tyan int (*hash_init)(struct hash_algo *algo, void **ctxp); 45bf007ebbSHung-ying Tyan /* 46bf007ebbSHung-ying Tyan * hash_update: Perform hashing on the given buffer 47bf007ebbSHung-ying Tyan * 48bf007ebbSHung-ying Tyan * The context is freed by this function if an error occurs. 49bf007ebbSHung-ying Tyan * 50bf007ebbSHung-ying Tyan * @algo: Pointer to the hash_algo struct 51bf007ebbSHung-ying Tyan * @ctx: Pointer to the context for hashing 52bf007ebbSHung-ying Tyan * @buf: Pointer to the buffer being hashed 53bf007ebbSHung-ying Tyan * @size: Size of the buffer being hashed 54bf007ebbSHung-ying Tyan * @is_last: 1 if this is the last update; 0 otherwise 55bf007ebbSHung-ying Tyan * @return 0 if ok, -1 on error 56bf007ebbSHung-ying Tyan */ 57bf007ebbSHung-ying Tyan int (*hash_update)(struct hash_algo *algo, void *ctx, const void *buf, 58bf007ebbSHung-ying Tyan unsigned int size, int is_last); 59bf007ebbSHung-ying Tyan /* 60bf007ebbSHung-ying Tyan * hash_finish: Write the hash result to the given buffer 61bf007ebbSHung-ying Tyan * 62bf007ebbSHung-ying Tyan * The context is freed by this function. 63bf007ebbSHung-ying Tyan * 64bf007ebbSHung-ying Tyan * @algo: Pointer to the hash_algo struct 65bf007ebbSHung-ying Tyan * @ctx: Pointer to the context for hashing 66bf007ebbSHung-ying Tyan * @dest_buf: Pointer to the buffer for the result 67bf007ebbSHung-ying Tyan * @size: Size of the buffer for the result 68bf007ebbSHung-ying Tyan * @return 0 if ok, -ENOSPC if size of the result buffer is too small 69bf007ebbSHung-ying Tyan * or -1 on other errors 70bf007ebbSHung-ying Tyan */ 71bf007ebbSHung-ying Tyan int (*hash_finish)(struct hash_algo *algo, void *ctx, void *dest_buf, 72bf007ebbSHung-ying Tyan int size); 73460408efSSimon Glass }; 74460408efSSimon Glass 752dd90027SRuchika Gupta #ifndef USE_HOSTCC 76460408efSSimon Glass /** 77460408efSSimon Glass * hash_command: Process a hash command for a particular algorithm 78460408efSSimon Glass * 79460408efSSimon Glass * This common function is used to implement specific hash commands. 80460408efSSimon Glass * 81218da0f3SSimon Glass * @algo_name: Hash algorithm being used (lower case!) 82d5b76673SSimon Glass * @flags: Flags value (HASH_FLAG_...) 83460408efSSimon Glass * @cmdtp: Pointer to command table entry 84460408efSSimon Glass * @flag: Some flags normally 0 (see CMD_FLAG_.. above) 85460408efSSimon Glass * @argc: Number of arguments (arg 0 must be the command text) 86460408efSSimon Glass * @argv: Arguments 87460408efSSimon Glass */ 88d5b76673SSimon Glass int hash_command(const char *algo_name, int flags, cmd_tbl_t *cmdtp, int flag, 89460408efSSimon Glass int argc, char * const argv[]); 90460408efSSimon Glass 916f907b42SSimon Glass /** 926f907b42SSimon Glass * hash_block() - Hash a block according to the requested algorithm 936f907b42SSimon Glass * 946f907b42SSimon Glass * The caller probably knows the hash length for the chosen algorithm, but 956f907b42SSimon Glass * in order to provide a general interface, and output_size parameter is 966f907b42SSimon Glass * provided. 976f907b42SSimon Glass * 986f907b42SSimon Glass * @algo_name: Hash algorithm to use 996f907b42SSimon Glass * @data: Data to hash 1006f907b42SSimon Glass * @len: Lengh of data to hash in bytes 1016f907b42SSimon Glass * @output: Place to put hash value 1026f907b42SSimon Glass * @output_size: On entry, pointer to the number of bytes available in 1036f907b42SSimon Glass * output. On exit, pointer to the number of bytes used. 1046f907b42SSimon Glass * If NULL, then it is assumed that the caller has 1056f907b42SSimon Glass * allocated enough space for the hash. This is possible 1066f907b42SSimon Glass * since the caller is selecting the algorithm. 1076f907b42SSimon Glass * @return 0 if ok, -ve on error: -EPROTONOSUPPORT for an unknown algorithm, 1086f907b42SSimon Glass * -ENOSPC if the output buffer is not large enough. 1096f907b42SSimon Glass */ 1106f907b42SSimon Glass int hash_block(const char *algo_name, const void *data, unsigned int len, 1116f907b42SSimon Glass uint8_t *output, int *output_size); 1126f907b42SSimon Glass 1132dd90027SRuchika Gupta #endif /* !USE_HOSTCC */ 1142dd90027SRuchika Gupta 1152dd90027SRuchika Gupta /** 116bf007ebbSHung-ying Tyan * hash_lookup_algo() - Look up the hash_algo struct for an algorithm 117bf007ebbSHung-ying Tyan * 118bf007ebbSHung-ying Tyan * The function returns the pointer to the struct or -EPROTONOSUPPORT if the 119bf007ebbSHung-ying Tyan * algorithm is not available. 120bf007ebbSHung-ying Tyan * 121bf007ebbSHung-ying Tyan * @algo_name: Hash algorithm to look up 122bf007ebbSHung-ying Tyan * @algop: Pointer to the hash_algo struct if found 123bf007ebbSHung-ying Tyan * 124bf007ebbSHung-ying Tyan * @return 0 if ok, -EPROTONOSUPPORT for an unknown algorithm. 125bf007ebbSHung-ying Tyan */ 126bf007ebbSHung-ying Tyan int hash_lookup_algo(const char *algo_name, struct hash_algo **algop); 12731890ae2SSimon Glass 12831890ae2SSimon Glass /** 12946fe2c04SRuchika Gupta * hash_progressive_lookup_algo() - Look up hash_algo for prog. hash support 13046fe2c04SRuchika Gupta * 13146fe2c04SRuchika Gupta * The function returns the pointer to the struct or -EPROTONOSUPPORT if the 13246fe2c04SRuchika Gupta * algorithm is not available with progressive hash support. 13346fe2c04SRuchika Gupta * 13446fe2c04SRuchika Gupta * @algo_name: Hash algorithm to look up 13546fe2c04SRuchika Gupta * @algop: Pointer to the hash_algo struct if found 13646fe2c04SRuchika Gupta * 13746fe2c04SRuchika Gupta * @return 0 if ok, -EPROTONOSUPPORT for an unknown algorithm. 13846fe2c04SRuchika Gupta */ 13946fe2c04SRuchika Gupta int hash_progressive_lookup_algo(const char *algo_name, 14046fe2c04SRuchika Gupta struct hash_algo **algop); 14146fe2c04SRuchika Gupta 142*8f0b1e24SStefan Roese /** 143*8f0b1e24SStefan Roese * hash_parse_string() - Parse hash string into a binary array 144*8f0b1e24SStefan Roese * 145*8f0b1e24SStefan Roese * The function parses a hash string into a binary array that 146*8f0b1e24SStefan Roese * can for example easily be used to compare to hash values. 147*8f0b1e24SStefan Roese * 148*8f0b1e24SStefan Roese * @algo_name: Hash algorithm to look up 149*8f0b1e24SStefan Roese * @str: Hash string to get parsed 150*8f0b1e24SStefan Roese * @result: Binary array of the parsed hash string 151*8f0b1e24SStefan Roese * 152*8f0b1e24SStefan Roese * @return 0 if ok, -EPROTONOSUPPORT for an unknown algorithm. 153*8f0b1e24SStefan Roese */ 154*8f0b1e24SStefan Roese int hash_parse_string(const char *algo_name, const char *str, uint8_t *result); 155*8f0b1e24SStefan Roese 156460408efSSimon Glass #endif 157