.. | .. |
---|
| 1 | +/* SPDX-License-Identifier: GPL-2.0-or-later */ |
---|
1 | 2 | /* |
---|
2 | 3 | * Hash: Hash algorithms under the crypto API |
---|
3 | 4 | * |
---|
4 | 5 | * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au> |
---|
5 | | - * |
---|
6 | | - * This program is free software; you can redistribute it and/or modify it |
---|
7 | | - * under the terms of the GNU General Public License as published by the Free |
---|
8 | | - * Software Foundation; either version 2 of the License, or (at your option) |
---|
9 | | - * any later version. |
---|
10 | | - * |
---|
11 | 6 | */ |
---|
12 | 7 | |
---|
13 | 8 | #ifndef _CRYPTO_HASH_H |
---|
.. | .. |
---|
63 | 58 | |
---|
64 | 59 | void *__ctx[] CRYPTO_MINALIGN_ATTR; |
---|
65 | 60 | }; |
---|
66 | | - |
---|
67 | | -#define AHASH_REQUEST_ON_STACK(name, ahash) \ |
---|
68 | | - char __##name##_desc[sizeof(struct ahash_request) + \ |
---|
69 | | - crypto_ahash_reqsize(ahash)] CRYPTO_MINALIGN_ATTR; \ |
---|
70 | | - struct ahash_request *name = (void *)__##name##_desc |
---|
71 | 61 | |
---|
72 | 62 | /** |
---|
73 | 63 | * struct ahash_alg - asynchronous message digest definition |
---|
.. | .. |
---|
128 | 118 | * data so the transformation can continue from this point onward. No |
---|
129 | 119 | * data processing happens at this point. Driver must not use |
---|
130 | 120 | * req->result. |
---|
| 121 | + * @init_tfm: Initialize the cryptographic transformation object. |
---|
| 122 | + * This function is called only once at the instantiation |
---|
| 123 | + * time, right after the transformation context was |
---|
| 124 | + * allocated. In case the cryptographic hardware has |
---|
| 125 | + * some special requirements which need to be handled |
---|
| 126 | + * by software, this function shall check for the precise |
---|
| 127 | + * requirement of the transformation and put any software |
---|
| 128 | + * fallbacks in place. |
---|
| 129 | + * @exit_tfm: Deinitialize the cryptographic transformation object. |
---|
| 130 | + * This is a counterpart to @init_tfm, used to remove |
---|
| 131 | + * various changes set in @init_tfm. |
---|
131 | 132 | * @halg: see struct hash_alg_common |
---|
132 | 133 | */ |
---|
133 | 134 | struct ahash_alg { |
---|
.. | .. |
---|
140 | 141 | int (*import)(struct ahash_request *req, const void *in); |
---|
141 | 142 | int (*setkey)(struct crypto_ahash *tfm, const u8 *key, |
---|
142 | 143 | unsigned int keylen); |
---|
| 144 | + int (*init_tfm)(struct crypto_ahash *tfm); |
---|
| 145 | + void (*exit_tfm)(struct crypto_ahash *tfm); |
---|
143 | 146 | |
---|
144 | 147 | struct hash_alg_common halg; |
---|
145 | 148 | }; |
---|
146 | 149 | |
---|
147 | 150 | struct shash_desc { |
---|
148 | 151 | struct crypto_shash *tfm; |
---|
149 | | - u32 flags; |
---|
150 | | - |
---|
151 | | - void *__ctx[] CRYPTO_MINALIGN_ATTR; |
---|
| 152 | + void *__ctx[] __aligned(UL(16)); |
---|
152 | 153 | }; |
---|
153 | 154 | |
---|
154 | | -#define SHASH_DESC_ON_STACK(shash, ctx) \ |
---|
155 | | - char __##shash##_desc[sizeof(struct shash_desc) + \ |
---|
156 | | - crypto_shash_descsize(ctx)] CRYPTO_MINALIGN_ATTR; \ |
---|
| 155 | +#define HASH_MAX_DIGESTSIZE 64 |
---|
| 156 | + |
---|
| 157 | +/* |
---|
| 158 | + * Worst case is hmac(sha3-224-generic). Its context is a nested 'shash_desc' |
---|
| 159 | + * containing a 'struct sha3_state'. |
---|
| 160 | + */ |
---|
| 161 | +#define HASH_MAX_DESCSIZE (sizeof(struct shash_desc) + 360) |
---|
| 162 | + |
---|
| 163 | +#define HASH_MAX_STATESIZE 512 |
---|
| 164 | + |
---|
| 165 | +#define SHASH_DESC_ON_STACK(shash, ctx) \ |
---|
| 166 | + char __##shash##_desc[sizeof(struct shash_desc) + HASH_MAX_DESCSIZE] \ |
---|
| 167 | + __aligned(__alignof__(struct shash_desc)); \ |
---|
157 | 168 | struct shash_desc *shash = (struct shash_desc *)__##shash##_desc |
---|
158 | 169 | |
---|
159 | 170 | /** |
---|
.. | .. |
---|
166 | 177 | * @export: see struct ahash_alg |
---|
167 | 178 | * @import: see struct ahash_alg |
---|
168 | 179 | * @setkey: see struct ahash_alg |
---|
| 180 | + * @init_tfm: Initialize the cryptographic transformation object. |
---|
| 181 | + * This function is called only once at the instantiation |
---|
| 182 | + * time, right after the transformation context was |
---|
| 183 | + * allocated. In case the cryptographic hardware has |
---|
| 184 | + * some special requirements which need to be handled |
---|
| 185 | + * by software, this function shall check for the precise |
---|
| 186 | + * requirement of the transformation and put any software |
---|
| 187 | + * fallbacks in place. |
---|
| 188 | + * @exit_tfm: Deinitialize the cryptographic transformation object. |
---|
| 189 | + * This is a counterpart to @init_tfm, used to remove |
---|
| 190 | + * various changes set in @init_tfm. |
---|
169 | 191 | * @digestsize: see struct ahash_alg |
---|
170 | 192 | * @statesize: see struct ahash_alg |
---|
171 | 193 | * @descsize: Size of the operational state for the message digest. This state |
---|
.. | .. |
---|
186 | 208 | int (*import)(struct shash_desc *desc, const void *in); |
---|
187 | 209 | int (*setkey)(struct crypto_shash *tfm, const u8 *key, |
---|
188 | 210 | unsigned int keylen); |
---|
| 211 | + int (*init_tfm)(struct crypto_shash *tfm); |
---|
| 212 | + void (*exit_tfm)(struct crypto_shash *tfm); |
---|
189 | 213 | |
---|
190 | 214 | unsigned int descsize; |
---|
191 | 215 | |
---|
.. | .. |
---|
224 | 248 | * CRYPTO_ALG_TYPE_AHASH (listed as type "ahash" in /proc/crypto) |
---|
225 | 249 | * |
---|
226 | 250 | * The asynchronous cipher operation discussion provided for the |
---|
227 | | - * CRYPTO_ALG_TYPE_ABLKCIPHER API applies here as well. |
---|
| 251 | + * CRYPTO_ALG_TYPE_SKCIPHER API applies here as well. |
---|
228 | 252 | */ |
---|
229 | 253 | |
---|
230 | 254 | static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm) |
---|
.. | .. |
---|
524 | 548 | */ |
---|
525 | 549 | static inline int crypto_ahash_update(struct ahash_request *req) |
---|
526 | 550 | { |
---|
527 | | - return crypto_ahash_reqtfm(req)->update(req); |
---|
| 551 | + struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
---|
| 552 | + struct crypto_alg *alg = tfm->base.__crt_alg; |
---|
| 553 | + unsigned int nbytes = req->nbytes; |
---|
| 554 | + int ret; |
---|
| 555 | + |
---|
| 556 | + crypto_stats_get(alg); |
---|
| 557 | + ret = crypto_ahash_reqtfm(req)->update(req); |
---|
| 558 | + crypto_stats_ahash_update(nbytes, ret, alg); |
---|
| 559 | + return ret; |
---|
528 | 560 | } |
---|
529 | 561 | |
---|
530 | 562 | /** |
---|
.. | .. |
---|
584 | 616 | */ |
---|
585 | 617 | static inline void ahash_request_free(struct ahash_request *req) |
---|
586 | 618 | { |
---|
587 | | - kzfree(req); |
---|
| 619 | + kfree_sensitive(req); |
---|
588 | 620 | } |
---|
589 | 621 | |
---|
590 | 622 | static inline void ahash_request_zero(struct ahash_request *req) |
---|
.. | .. |
---|
665 | 697 | * The message digest API is able to maintain state information for the |
---|
666 | 698 | * caller. |
---|
667 | 699 | * |
---|
668 | | - * The synchronous message digest API can store user-related context in in its |
---|
| 700 | + * The synchronous message digest API can store user-related context in its |
---|
669 | 701 | * shash_desc request data structure. |
---|
670 | 702 | */ |
---|
671 | 703 | |
---|
.. | .. |
---|
811 | 843 | * cipher handle must point to a keyed message digest cipher in order for this |
---|
812 | 844 | * function to succeed. |
---|
813 | 845 | * |
---|
| 846 | + * Context: Any context. |
---|
814 | 847 | * Return: 0 if the setting of the key was successful; < 0 if an error occurred |
---|
815 | 848 | */ |
---|
816 | 849 | int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key, |
---|
.. | .. |
---|
827 | 860 | * crypto_shash_update and crypto_shash_final. The parameters have the same |
---|
828 | 861 | * meaning as discussed for those separate three functions. |
---|
829 | 862 | * |
---|
| 863 | + * Context: Any context. |
---|
830 | 864 | * Return: 0 if the message digest creation was successful; < 0 if an error |
---|
831 | 865 | * occurred |
---|
832 | 866 | */ |
---|
833 | 867 | int crypto_shash_digest(struct shash_desc *desc, const u8 *data, |
---|
834 | 868 | unsigned int len, u8 *out); |
---|
| 869 | + |
---|
| 870 | +/** |
---|
| 871 | + * crypto_shash_tfm_digest() - calculate message digest for buffer |
---|
| 872 | + * @tfm: hash transformation object |
---|
| 873 | + * @data: see crypto_shash_update() |
---|
| 874 | + * @len: see crypto_shash_update() |
---|
| 875 | + * @out: see crypto_shash_final() |
---|
| 876 | + * |
---|
| 877 | + * This is a simplified version of crypto_shash_digest() for users who don't |
---|
| 878 | + * want to allocate their own hash descriptor (shash_desc). Instead, |
---|
| 879 | + * crypto_shash_tfm_digest() takes a hash transformation object (crypto_shash) |
---|
| 880 | + * directly, and it allocates a hash descriptor on the stack internally. |
---|
| 881 | + * Note that this stack allocation may be fairly large. |
---|
| 882 | + * |
---|
| 883 | + * Context: Any context. |
---|
| 884 | + * Return: 0 on success; < 0 if an error occurred. |
---|
| 885 | + */ |
---|
| 886 | +int crypto_shash_tfm_digest(struct crypto_shash *tfm, const u8 *data, |
---|
| 887 | + unsigned int len, u8 *out); |
---|
835 | 888 | |
---|
836 | 889 | /** |
---|
837 | 890 | * crypto_shash_export() - extract operational state for message digest |
---|
.. | .. |
---|
842 | 895 | * caller-allocated output buffer out which must have sufficient size (e.g. by |
---|
843 | 896 | * calling crypto_shash_descsize). |
---|
844 | 897 | * |
---|
| 898 | + * Context: Any context. |
---|
845 | 899 | * Return: 0 if the export creation was successful; < 0 if an error occurred |
---|
846 | 900 | */ |
---|
847 | 901 | static inline int crypto_shash_export(struct shash_desc *desc, void *out) |
---|
.. | .. |
---|
858 | 912 | * the input buffer. That buffer should have been generated with the |
---|
859 | 913 | * crypto_ahash_export function. |
---|
860 | 914 | * |
---|
| 915 | + * Context: Any context. |
---|
861 | 916 | * Return: 0 if the import was successful; < 0 if an error occurred |
---|
862 | 917 | */ |
---|
863 | 918 | static inline int crypto_shash_import(struct shash_desc *desc, const void *in) |
---|
.. | .. |
---|
878 | 933 | * operational state handle. Any potentially existing state created by |
---|
879 | 934 | * previous operations is discarded. |
---|
880 | 935 | * |
---|
| 936 | + * Context: Any context. |
---|
881 | 937 | * Return: 0 if the message digest initialization was successful; < 0 if an |
---|
882 | 938 | * error occurred |
---|
883 | 939 | */ |
---|
.. | .. |
---|
899 | 955 | * |
---|
900 | 956 | * Updates the message digest state of the operational state handle. |
---|
901 | 957 | * |
---|
| 958 | + * Context: Any context. |
---|
902 | 959 | * Return: 0 if the message digest update was successful; < 0 if an error |
---|
903 | 960 | * occurred |
---|
904 | 961 | */ |
---|
.. | .. |
---|
915 | 972 | * into the output buffer. The caller must ensure that the output buffer is |
---|
916 | 973 | * large enough by using crypto_shash_digestsize. |
---|
917 | 974 | * |
---|
| 975 | + * Context: Any context. |
---|
918 | 976 | * Return: 0 if the message digest creation was successful; < 0 if an error |
---|
919 | 977 | * occurred |
---|
920 | 978 | */ |
---|
.. | .. |
---|
931 | 989 | * crypto_shash_update and crypto_shash_final. The parameters have the same |
---|
932 | 990 | * meaning as discussed for those separate functions. |
---|
933 | 991 | * |
---|
| 992 | + * Context: Any context. |
---|
934 | 993 | * Return: 0 if the message digest creation was successful; < 0 if an error |
---|
935 | 994 | * occurred |
---|
936 | 995 | */ |
---|