hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/include/crypto/hash.h
....@@ -1,13 +1,8 @@
1
+/* SPDX-License-Identifier: GPL-2.0-or-later */
12 /*
23 * Hash: Hash algorithms under the crypto API
34 *
45 * 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
- *
116 */
127
138 #ifndef _CRYPTO_HASH_H
....@@ -63,11 +58,6 @@
6358
6459 void *__ctx[] CRYPTO_MINALIGN_ATTR;
6560 };
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
7161
7262 /**
7363 * struct ahash_alg - asynchronous message digest definition
....@@ -128,6 +118,17 @@
128118 * data so the transformation can continue from this point onward. No
129119 * data processing happens at this point. Driver must not use
130120 * 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.
131132 * @halg: see struct hash_alg_common
132133 */
133134 struct ahash_alg {
....@@ -140,20 +141,30 @@
140141 int (*import)(struct ahash_request *req, const void *in);
141142 int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
142143 unsigned int keylen);
144
+ int (*init_tfm)(struct crypto_ahash *tfm);
145
+ void (*exit_tfm)(struct crypto_ahash *tfm);
143146
144147 struct hash_alg_common halg;
145148 };
146149
147150 struct shash_desc {
148151 struct crypto_shash *tfm;
149
- u32 flags;
150
-
151
- void *__ctx[] CRYPTO_MINALIGN_ATTR;
152
+ void *__ctx[] __aligned(UL(16));
152153 };
153154
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)); \
157168 struct shash_desc *shash = (struct shash_desc *)__##shash##_desc
158169
159170 /**
....@@ -166,6 +177,17 @@
166177 * @export: see struct ahash_alg
167178 * @import: see struct ahash_alg
168179 * @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.
169191 * @digestsize: see struct ahash_alg
170192 * @statesize: see struct ahash_alg
171193 * @descsize: Size of the operational state for the message digest. This state
....@@ -186,6 +208,8 @@
186208 int (*import)(struct shash_desc *desc, const void *in);
187209 int (*setkey)(struct crypto_shash *tfm, const u8 *key,
188210 unsigned int keylen);
211
+ int (*init_tfm)(struct crypto_shash *tfm);
212
+ void (*exit_tfm)(struct crypto_shash *tfm);
189213
190214 unsigned int descsize;
191215
....@@ -224,7 +248,7 @@
224248 * CRYPTO_ALG_TYPE_AHASH (listed as type "ahash" in /proc/crypto)
225249 *
226250 * 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.
228252 */
229253
230254 static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm)
....@@ -524,7 +548,15 @@
524548 */
525549 static inline int crypto_ahash_update(struct ahash_request *req)
526550 {
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;
528560 }
529561
530562 /**
....@@ -584,7 +616,7 @@
584616 */
585617 static inline void ahash_request_free(struct ahash_request *req)
586618 {
587
- kzfree(req);
619
+ kfree_sensitive(req);
588620 }
589621
590622 static inline void ahash_request_zero(struct ahash_request *req)
....@@ -665,7 +697,7 @@
665697 * The message digest API is able to maintain state information for the
666698 * caller.
667699 *
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
669701 * shash_desc request data structure.
670702 */
671703
....@@ -811,6 +843,7 @@
811843 * cipher handle must point to a keyed message digest cipher in order for this
812844 * function to succeed.
813845 *
846
+ * Context: Any context.
814847 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
815848 */
816849 int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
....@@ -827,11 +860,31 @@
827860 * crypto_shash_update and crypto_shash_final. The parameters have the same
828861 * meaning as discussed for those separate three functions.
829862 *
863
+ * Context: Any context.
830864 * Return: 0 if the message digest creation was successful; < 0 if an error
831865 * occurred
832866 */
833867 int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
834868 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);
835888
836889 /**
837890 * crypto_shash_export() - extract operational state for message digest
....@@ -842,6 +895,7 @@
842895 * caller-allocated output buffer out which must have sufficient size (e.g. by
843896 * calling crypto_shash_descsize).
844897 *
898
+ * Context: Any context.
845899 * Return: 0 if the export creation was successful; < 0 if an error occurred
846900 */
847901 static inline int crypto_shash_export(struct shash_desc *desc, void *out)
....@@ -858,6 +912,7 @@
858912 * the input buffer. That buffer should have been generated with the
859913 * crypto_ahash_export function.
860914 *
915
+ * Context: Any context.
861916 * Return: 0 if the import was successful; < 0 if an error occurred
862917 */
863918 static inline int crypto_shash_import(struct shash_desc *desc, const void *in)
....@@ -878,6 +933,7 @@
878933 * operational state handle. Any potentially existing state created by
879934 * previous operations is discarded.
880935 *
936
+ * Context: Any context.
881937 * Return: 0 if the message digest initialization was successful; < 0 if an
882938 * error occurred
883939 */
....@@ -899,6 +955,7 @@
899955 *
900956 * Updates the message digest state of the operational state handle.
901957 *
958
+ * Context: Any context.
902959 * Return: 0 if the message digest update was successful; < 0 if an error
903960 * occurred
904961 */
....@@ -915,6 +972,7 @@
915972 * into the output buffer. The caller must ensure that the output buffer is
916973 * large enough by using crypto_shash_digestsize.
917974 *
975
+ * Context: Any context.
918976 * Return: 0 if the message digest creation was successful; < 0 if an error
919977 * occurred
920978 */
....@@ -931,6 +989,7 @@
931989 * crypto_shash_update and crypto_shash_final. The parameters have the same
932990 * meaning as discussed for those separate functions.
933991 *
992
+ * Context: Any context.
934993 * Return: 0 if the message digest creation was successful; < 0 if an error
935994 * occurred
936995 */