Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

crypto: sha256 - Implement export_core() and import_core()

Since commit 9d7a0ab1c753 ("crypto: ahash - Handle partial blocks in
API"), the recently-added export_core() and import_core() methods in
struct shash_alg have effectively become mandatory (even though it is
not tested or enforced), since legacy drivers that need a fallback
depend on them. Make crypto/sha256.c compatible with these legacy
drivers by adding export_core() and import_core() methods to it.

Reported-by: Giovanni Cabiddu <giovanni.cabiddu@intel.com>
Reported-by: Ovidiu Panait <ovidiu.panait.oss@gmail.com>
Closes: https://lore.kernel.org/r/aLSnCc9Ws5L9y+8X@gcabiddu-mobl.ger.corp.intel.com
Fixes: 07f090959bba ("crypto: sha256 - Use same state format as legacy drivers")
Tested-by: Giovanni Cabiddu <giovanni.cabiddu@intel.com>
Tested-by: Ovidiu Panait <ovidiu.panait.oss@gmail.com>
Link: https://lore.kernel.org/r/20250901165013.48649-3-ebiggers@kernel.org
Signed-off-by: Eric Biggers <ebiggers@kernel.org>

+71
+71
crypto/sha256.c
··· 50 50 return 0; 51 51 } 52 52 53 + static int __crypto_sha256_export_core(const struct __sha256_ctx *ctx, 54 + void *out) 55 + { 56 + memcpy(out, ctx, offsetof(struct __sha256_ctx, buf)); 57 + return 0; 58 + } 59 + 60 + static int __crypto_sha256_import_core(struct __sha256_ctx *ctx, const void *in) 61 + { 62 + memcpy(ctx, in, offsetof(struct __sha256_ctx, buf)); 63 + return 0; 64 + } 65 + 53 66 /* SHA-224 */ 54 67 55 68 const u8 sha224_zero_message_hash[SHA224_DIGEST_SIZE] = { ··· 111 98 return __crypto_sha256_import(&SHA224_CTX(desc)->ctx, in); 112 99 } 113 100 101 + static int crypto_sha224_export_core(struct shash_desc *desc, void *out) 102 + { 103 + return __crypto_sha256_export_core(&SHA224_CTX(desc)->ctx, out); 104 + } 105 + 106 + static int crypto_sha224_import_core(struct shash_desc *desc, const void *in) 107 + { 108 + return __crypto_sha256_import_core(&SHA224_CTX(desc)->ctx, in); 109 + } 110 + 114 111 /* SHA-256 */ 115 112 116 113 const u8 sha256_zero_message_hash[SHA256_DIGEST_SIZE] = { ··· 167 144 static int crypto_sha256_import(struct shash_desc *desc, const void *in) 168 145 { 169 146 return __crypto_sha256_import(&SHA256_CTX(desc)->ctx, in); 147 + } 148 + 149 + static int crypto_sha256_export_core(struct shash_desc *desc, void *out) 150 + { 151 + return __crypto_sha256_export_core(&SHA256_CTX(desc)->ctx, out); 152 + } 153 + 154 + static int crypto_sha256_import_core(struct shash_desc *desc, const void *in) 155 + { 156 + return __crypto_sha256_import_core(&SHA256_CTX(desc)->ctx, in); 170 157 } 171 158 172 159 /* HMAC-SHA224 */ ··· 231 198 return __crypto_sha256_import(&ctx->ctx.sha_ctx, in); 232 199 } 233 200 201 + static int crypto_hmac_sha224_export_core(struct shash_desc *desc, void *out) 202 + { 203 + return __crypto_sha256_export_core(&HMAC_SHA224_CTX(desc)->ctx.sha_ctx, 204 + out); 205 + } 206 + 207 + static int crypto_hmac_sha224_import_core(struct shash_desc *desc, 208 + const void *in) 209 + { 210 + struct hmac_sha224_ctx *ctx = HMAC_SHA224_CTX(desc); 211 + 212 + ctx->ctx.ostate = HMAC_SHA224_KEY(desc->tfm)->key.ostate; 213 + return __crypto_sha256_import_core(&ctx->ctx.sha_ctx, in); 214 + } 215 + 234 216 /* HMAC-SHA256 */ 235 217 236 218 #define HMAC_SHA256_KEY(tfm) ((struct hmac_sha256_key *)crypto_shash_ctx(tfm)) ··· 298 250 return __crypto_sha256_import(&ctx->ctx.sha_ctx, in); 299 251 } 300 252 253 + static int crypto_hmac_sha256_export_core(struct shash_desc *desc, void *out) 254 + { 255 + return __crypto_sha256_export_core(&HMAC_SHA256_CTX(desc)->ctx.sha_ctx, 256 + out); 257 + } 258 + 259 + static int crypto_hmac_sha256_import_core(struct shash_desc *desc, 260 + const void *in) 261 + { 262 + struct hmac_sha256_ctx *ctx = HMAC_SHA256_CTX(desc); 263 + 264 + ctx->ctx.ostate = HMAC_SHA256_KEY(desc->tfm)->key.ostate; 265 + return __crypto_sha256_import_core(&ctx->ctx.sha_ctx, in); 266 + } 267 + 301 268 /* Algorithm definitions */ 302 269 303 270 static struct shash_alg algs[] = { ··· 329 266 .digest = crypto_sha224_digest, 330 267 .export = crypto_sha224_export, 331 268 .import = crypto_sha224_import, 269 + .export_core = crypto_sha224_export_core, 270 + .import_core = crypto_sha224_import_core, 332 271 .descsize = sizeof(struct sha224_ctx), 333 272 .statesize = SHA256_SHASH_STATE_SIZE, 334 273 }, ··· 347 282 .digest = crypto_sha256_digest, 348 283 .export = crypto_sha256_export, 349 284 .import = crypto_sha256_import, 285 + .export_core = crypto_sha256_export_core, 286 + .import_core = crypto_sha256_import_core, 350 287 .descsize = sizeof(struct sha256_ctx), 351 288 .statesize = SHA256_SHASH_STATE_SIZE, 352 289 }, ··· 367 300 .digest = crypto_hmac_sha224_digest, 368 301 .export = crypto_hmac_sha224_export, 369 302 .import = crypto_hmac_sha224_import, 303 + .export_core = crypto_hmac_sha224_export_core, 304 + .import_core = crypto_hmac_sha224_import_core, 370 305 .descsize = sizeof(struct hmac_sha224_ctx), 371 306 .statesize = SHA256_SHASH_STATE_SIZE, 372 307 }, ··· 387 318 .digest = crypto_hmac_sha256_digest, 388 319 .export = crypto_hmac_sha256_export, 389 320 .import = crypto_hmac_sha256_import, 321 + .export_core = crypto_hmac_sha256_export_core, 322 + .import_core = crypto_hmac_sha256_import_core, 390 323 .descsize = sizeof(struct hmac_sha256_ctx), 391 324 .statesize = SHA256_SHASH_STATE_SIZE, 392 325 },