at master 6.7 kB view raw
1// SPDX-License-Identifier: GPL-2.0-or-later 2/* 3 * Crypto API support for SHA-1 and HMAC-SHA1 4 * 5 * Copyright (c) Alan Smithee. 6 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk> 7 * Copyright (c) Jean-Francois Dive <jef@linuxbe.org> 8 * Copyright 2025 Google LLC 9 */ 10#include <crypto/internal/hash.h> 11#include <crypto/sha1.h> 12#include <linux/kernel.h> 13#include <linux/module.h> 14 15/* 16 * Export and import functions. crypto_shash wants a particular format that 17 * matches that used by some legacy drivers. It currently is the same as the 18 * library SHA context, except the value in bytecount must be block-aligned and 19 * the remainder must be stored in an extra u8 appended to the struct. 20 */ 21 22#define SHA1_SHASH_STATE_SIZE (sizeof(struct sha1_ctx) + 1) 23static_assert(sizeof(struct sha1_ctx) == sizeof(struct sha1_state)); 24static_assert(offsetof(struct sha1_ctx, state) == offsetof(struct sha1_state, state)); 25static_assert(offsetof(struct sha1_ctx, bytecount) == offsetof(struct sha1_state, count)); 26static_assert(offsetof(struct sha1_ctx, buf) == offsetof(struct sha1_state, buffer)); 27 28static int __crypto_sha1_export(const struct sha1_ctx *ctx0, void *out) 29{ 30 struct sha1_ctx ctx = *ctx0; 31 unsigned int partial; 32 u8 *p = out; 33 34 partial = ctx.bytecount % SHA1_BLOCK_SIZE; 35 ctx.bytecount -= partial; 36 memcpy(p, &ctx, sizeof(ctx)); 37 p += sizeof(ctx); 38 *p = partial; 39 return 0; 40} 41 42static int __crypto_sha1_import(struct sha1_ctx *ctx, const void *in) 43{ 44 const u8 *p = in; 45 46 memcpy(ctx, p, sizeof(*ctx)); 47 p += sizeof(*ctx); 48 ctx->bytecount += *p; 49 return 0; 50} 51 52static int __crypto_sha1_export_core(const struct sha1_ctx *ctx, void *out) 53{ 54 memcpy(out, ctx, offsetof(struct sha1_ctx, buf)); 55 return 0; 56} 57 58static int __crypto_sha1_import_core(struct sha1_ctx *ctx, const void *in) 59{ 60 memcpy(ctx, in, offsetof(struct sha1_ctx, buf)); 61 return 0; 62} 63 64const u8 sha1_zero_message_hash[SHA1_DIGEST_SIZE] = { 65 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 66 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 67 0xaf, 0xd8, 0x07, 0x09 68}; 69EXPORT_SYMBOL_GPL(sha1_zero_message_hash); 70 71#define SHA1_CTX(desc) ((struct sha1_ctx *)shash_desc_ctx(desc)) 72 73static int crypto_sha1_init(struct shash_desc *desc) 74{ 75 sha1_init(SHA1_CTX(desc)); 76 return 0; 77} 78 79static int crypto_sha1_update(struct shash_desc *desc, 80 const u8 *data, unsigned int len) 81{ 82 sha1_update(SHA1_CTX(desc), data, len); 83 return 0; 84} 85 86static int crypto_sha1_final(struct shash_desc *desc, u8 *out) 87{ 88 sha1_final(SHA1_CTX(desc), out); 89 return 0; 90} 91 92static int crypto_sha1_digest(struct shash_desc *desc, 93 const u8 *data, unsigned int len, u8 *out) 94{ 95 sha1(data, len, out); 96 return 0; 97} 98 99static int crypto_sha1_export(struct shash_desc *desc, void *out) 100{ 101 return __crypto_sha1_export(SHA1_CTX(desc), out); 102} 103 104static int crypto_sha1_import(struct shash_desc *desc, const void *in) 105{ 106 return __crypto_sha1_import(SHA1_CTX(desc), in); 107} 108 109static int crypto_sha1_export_core(struct shash_desc *desc, void *out) 110{ 111 return __crypto_sha1_export_core(SHA1_CTX(desc), out); 112} 113 114static int crypto_sha1_import_core(struct shash_desc *desc, const void *in) 115{ 116 return __crypto_sha1_import_core(SHA1_CTX(desc), in); 117} 118 119#define HMAC_SHA1_KEY(tfm) ((struct hmac_sha1_key *)crypto_shash_ctx(tfm)) 120#define HMAC_SHA1_CTX(desc) ((struct hmac_sha1_ctx *)shash_desc_ctx(desc)) 121 122static int crypto_hmac_sha1_setkey(struct crypto_shash *tfm, 123 const u8 *raw_key, unsigned int keylen) 124{ 125 hmac_sha1_preparekey(HMAC_SHA1_KEY(tfm), raw_key, keylen); 126 return 0; 127} 128 129static int crypto_hmac_sha1_init(struct shash_desc *desc) 130{ 131 hmac_sha1_init(HMAC_SHA1_CTX(desc), HMAC_SHA1_KEY(desc->tfm)); 132 return 0; 133} 134 135static int crypto_hmac_sha1_update(struct shash_desc *desc, 136 const u8 *data, unsigned int len) 137{ 138 hmac_sha1_update(HMAC_SHA1_CTX(desc), data, len); 139 return 0; 140} 141 142static int crypto_hmac_sha1_final(struct shash_desc *desc, u8 *out) 143{ 144 hmac_sha1_final(HMAC_SHA1_CTX(desc), out); 145 return 0; 146} 147 148static int crypto_hmac_sha1_digest(struct shash_desc *desc, 149 const u8 *data, unsigned int len, u8 *out) 150{ 151 hmac_sha1(HMAC_SHA1_KEY(desc->tfm), data, len, out); 152 return 0; 153} 154 155static int crypto_hmac_sha1_export(struct shash_desc *desc, void *out) 156{ 157 return __crypto_sha1_export(&HMAC_SHA1_CTX(desc)->sha_ctx, out); 158} 159 160static int crypto_hmac_sha1_import(struct shash_desc *desc, const void *in) 161{ 162 struct hmac_sha1_ctx *ctx = HMAC_SHA1_CTX(desc); 163 164 ctx->ostate = HMAC_SHA1_KEY(desc->tfm)->ostate; 165 return __crypto_sha1_import(&ctx->sha_ctx, in); 166} 167 168static int crypto_hmac_sha1_export_core(struct shash_desc *desc, void *out) 169{ 170 return __crypto_sha1_export_core(&HMAC_SHA1_CTX(desc)->sha_ctx, out); 171} 172 173static int crypto_hmac_sha1_import_core(struct shash_desc *desc, const void *in) 174{ 175 struct hmac_sha1_ctx *ctx = HMAC_SHA1_CTX(desc); 176 177 ctx->ostate = HMAC_SHA1_KEY(desc->tfm)->ostate; 178 return __crypto_sha1_import_core(&ctx->sha_ctx, in); 179} 180 181static struct shash_alg algs[] = { 182 { 183 .base.cra_name = "sha1", 184 .base.cra_driver_name = "sha1-lib", 185 .base.cra_priority = 300, 186 .base.cra_blocksize = SHA1_BLOCK_SIZE, 187 .base.cra_module = THIS_MODULE, 188 .digestsize = SHA1_DIGEST_SIZE, 189 .init = crypto_sha1_init, 190 .update = crypto_sha1_update, 191 .final = crypto_sha1_final, 192 .digest = crypto_sha1_digest, 193 .export = crypto_sha1_export, 194 .import = crypto_sha1_import, 195 .export_core = crypto_sha1_export_core, 196 .import_core = crypto_sha1_import_core, 197 .descsize = sizeof(struct sha1_ctx), 198 .statesize = SHA1_SHASH_STATE_SIZE, 199 }, 200 { 201 .base.cra_name = "hmac(sha1)", 202 .base.cra_driver_name = "hmac-sha1-lib", 203 .base.cra_priority = 300, 204 .base.cra_blocksize = SHA1_BLOCK_SIZE, 205 .base.cra_ctxsize = sizeof(struct hmac_sha1_key), 206 .base.cra_module = THIS_MODULE, 207 .digestsize = SHA1_DIGEST_SIZE, 208 .setkey = crypto_hmac_sha1_setkey, 209 .init = crypto_hmac_sha1_init, 210 .update = crypto_hmac_sha1_update, 211 .final = crypto_hmac_sha1_final, 212 .digest = crypto_hmac_sha1_digest, 213 .export = crypto_hmac_sha1_export, 214 .import = crypto_hmac_sha1_import, 215 .export_core = crypto_hmac_sha1_export_core, 216 .import_core = crypto_hmac_sha1_import_core, 217 .descsize = sizeof(struct hmac_sha1_ctx), 218 .statesize = SHA1_SHASH_STATE_SIZE, 219 }, 220}; 221 222static int __init crypto_sha1_mod_init(void) 223{ 224 return crypto_register_shashes(algs, ARRAY_SIZE(algs)); 225} 226module_init(crypto_sha1_mod_init); 227 228static void __exit crypto_sha1_mod_exit(void) 229{ 230 crypto_unregister_shashes(algs, ARRAY_SIZE(algs)); 231} 232module_exit(crypto_sha1_mod_exit); 233 234MODULE_LICENSE("GPL"); 235MODULE_DESCRIPTION("Crypto API support for SHA-1 and HMAC-SHA1"); 236 237MODULE_ALIAS_CRYPTO("sha1"); 238MODULE_ALIAS_CRYPTO("sha1-lib"); 239MODULE_ALIAS_CRYPTO("hmac(sha1)"); 240MODULE_ALIAS_CRYPTO("hmac-sha1-lib");