1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Crypto API support for SHA-384, SHA-512, HMAC-SHA384, and HMAC-SHA512 4 * 5 * Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com> 6 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk> 7 * Copyright (c) 2003 Kyle McMartin <kyle@debian.org> 8 * Copyright 2025 Google LLC 9 */ 10 #include <crypto/internal/hash.h> 11 #include <crypto/sha2.h> 12 #include <linux/kernel.h> 13 #include <linux/module.h> 14 15 /* SHA-384 */ 16 17 const u8 sha384_zero_message_hash[SHA384_DIGEST_SIZE] = { 18 0x38, 0xb0, 0x60, 0xa7, 0x51, 0xac, 0x96, 0x38, 19 0x4c, 0xd9, 0x32, 0x7e, 0xb1, 0xb1, 0xe3, 0x6a, 20 0x21, 0xfd, 0xb7, 0x11, 0x14, 0xbe, 0x07, 0x43, 21 0x4c, 0x0c, 0xc7, 0xbf, 0x63, 0xf6, 0xe1, 0xda, 22 0x27, 0x4e, 0xde, 0xbf, 0xe7, 0x6f, 0x65, 0xfb, 23 0xd5, 0x1a, 0xd2, 0xf1, 0x48, 0x98, 0xb9, 0x5b 24 }; 25 EXPORT_SYMBOL_GPL(sha384_zero_message_hash); 26 27 #define SHA384_CTX(desc) ((struct sha384_ctx *)shash_desc_ctx(desc)) 28 29 static int crypto_sha384_init(struct shash_desc *desc) 30 { 31 sha384_init(SHA384_CTX(desc)); 32 return 0; 33 } 34 35 static int crypto_sha384_update(struct shash_desc *desc, 36 const u8 *data, unsigned int len) 37 { 38 sha384_update(SHA384_CTX(desc), data, len); 39 return 0; 40 } 41 42 static int crypto_sha384_final(struct shash_desc *desc, u8 *out) 43 { 44 sha384_final(SHA384_CTX(desc), out); 45 return 0; 46 } 47 48 static int crypto_sha384_digest(struct shash_desc *desc, 49 const u8 *data, unsigned int len, u8 *out) 50 { 51 sha384(data, len, out); 52 return 0; 53 } 54 55 /* SHA-512 */ 56 57 const u8 sha512_zero_message_hash[SHA512_DIGEST_SIZE] = { 58 0xcf, 0x83, 0xe1, 0x35, 0x7e, 0xef, 0xb8, 0xbd, 59 0xf1, 0x54, 0x28, 0x50, 0xd6, 0x6d, 0x80, 0x07, 60 0xd6, 0x20, 0xe4, 0x05, 0x0b, 0x57, 0x15, 0xdc, 61 0x83, 0xf4, 0xa9, 0x21, 0xd3, 0x6c, 0xe9, 0xce, 62 0x47, 0xd0, 0xd1, 0x3c, 0x5d, 0x85, 0xf2, 0xb0, 63 0xff, 0x83, 0x18, 0xd2, 0x87, 0x7e, 0xec, 0x2f, 64 0x63, 0xb9, 0x31, 0xbd, 0x47, 0x41, 0x7a, 0x81, 65 0xa5, 0x38, 0x32, 0x7a, 0xf9, 0x27, 0xda, 0x3e 66 }; 67 EXPORT_SYMBOL_GPL(sha512_zero_message_hash); 68 69 #define SHA512_CTX(desc) ((struct sha512_ctx *)shash_desc_ctx(desc)) 70 71 static int crypto_sha512_init(struct shash_desc *desc) 72 { 73 sha512_init(SHA512_CTX(desc)); 74 return 0; 75 } 76 77 static int crypto_sha512_update(struct shash_desc *desc, 78 const u8 *data, unsigned int len) 79 { 80 sha512_update(SHA512_CTX(desc), data, len); 81 return 0; 82 } 83 84 static int crypto_sha512_final(struct shash_desc *desc, u8 *out) 85 { 86 sha512_final(SHA512_CTX(desc), out); 87 return 0; 88 } 89 90 static int crypto_sha512_digest(struct shash_desc *desc, 91 const u8 *data, unsigned int len, u8 *out) 92 { 93 sha512(data, len, out); 94 return 0; 95 } 96 97 /* HMAC-SHA384 */ 98 99 #define HMAC_SHA384_KEY(tfm) ((struct hmac_sha384_key *)crypto_shash_ctx(tfm)) 100 #define HMAC_SHA384_CTX(desc) ((struct hmac_sha384_ctx *)shash_desc_ctx(desc)) 101 102 static int crypto_hmac_sha384_setkey(struct crypto_shash *tfm, 103 const u8 *raw_key, unsigned int keylen) 104 { 105 hmac_sha384_preparekey(HMAC_SHA384_KEY(tfm), raw_key, keylen); 106 return 0; 107 } 108 109 static int crypto_hmac_sha384_init(struct shash_desc *desc) 110 { 111 hmac_sha384_init(HMAC_SHA384_CTX(desc), HMAC_SHA384_KEY(desc->tfm)); 112 return 0; 113 } 114 115 static int crypto_hmac_sha384_update(struct shash_desc *desc, 116 const u8 *data, unsigned int len) 117 { 118 hmac_sha384_update(HMAC_SHA384_CTX(desc), data, len); 119 return 0; 120 } 121 122 static int crypto_hmac_sha384_final(struct shash_desc *desc, u8 *out) 123 { 124 hmac_sha384_final(HMAC_SHA384_CTX(desc), out); 125 return 0; 126 } 127 128 static int crypto_hmac_sha384_digest(struct shash_desc *desc, 129 const u8 *data, unsigned int len, 130 u8 *out) 131 { 132 hmac_sha384(HMAC_SHA384_KEY(desc->tfm), data, len, out); 133 return 0; 134 } 135 136 /* HMAC-SHA512 */ 137 138 #define HMAC_SHA512_KEY(tfm) ((struct hmac_sha512_key *)crypto_shash_ctx(tfm)) 139 #define HMAC_SHA512_CTX(desc) ((struct hmac_sha512_ctx *)shash_desc_ctx(desc)) 140 141 static int crypto_hmac_sha512_setkey(struct crypto_shash *tfm, 142 const u8 *raw_key, unsigned int keylen) 143 { 144 hmac_sha512_preparekey(HMAC_SHA512_KEY(tfm), raw_key, keylen); 145 return 0; 146 } 147 148 static int crypto_hmac_sha512_init(struct shash_desc *desc) 149 { 150 hmac_sha512_init(HMAC_SHA512_CTX(desc), HMAC_SHA512_KEY(desc->tfm)); 151 return 0; 152 } 153 154 static int crypto_hmac_sha512_update(struct shash_desc *desc, 155 const u8 *data, unsigned int len) 156 { 157 hmac_sha512_update(HMAC_SHA512_CTX(desc), data, len); 158 return 0; 159 } 160 161 static int crypto_hmac_sha512_final(struct shash_desc *desc, u8 *out) 162 { 163 hmac_sha512_final(HMAC_SHA512_CTX(desc), out); 164 return 0; 165 } 166 167 static int crypto_hmac_sha512_digest(struct shash_desc *desc, 168 const u8 *data, unsigned int len, 169 u8 *out) 170 { 171 hmac_sha512(HMAC_SHA512_KEY(desc->tfm), data, len, out); 172 return 0; 173 } 174 175 /* Algorithm definitions */ 176 177 static struct shash_alg algs[] = { 178 { 179 .base.cra_name = "sha384", 180 .base.cra_driver_name = "sha384-lib", 181 .base.cra_priority = 300, 182 .base.cra_blocksize = SHA384_BLOCK_SIZE, 183 .base.cra_module = THIS_MODULE, 184 .digestsize = SHA384_DIGEST_SIZE, 185 .init = crypto_sha384_init, 186 .update = crypto_sha384_update, 187 .final = crypto_sha384_final, 188 .digest = crypto_sha384_digest, 189 .descsize = sizeof(struct sha384_ctx), 190 }, 191 { 192 .base.cra_name = "sha512", 193 .base.cra_driver_name = "sha512-lib", 194 .base.cra_priority = 300, 195 .base.cra_blocksize = SHA512_BLOCK_SIZE, 196 .base.cra_module = THIS_MODULE, 197 .digestsize = SHA512_DIGEST_SIZE, 198 .init = crypto_sha512_init, 199 .update = crypto_sha512_update, 200 .final = crypto_sha512_final, 201 .digest = crypto_sha512_digest, 202 .descsize = sizeof(struct sha512_ctx), 203 }, 204 { 205 .base.cra_name = "hmac(sha384)", 206 .base.cra_driver_name = "hmac-sha384-lib", 207 .base.cra_priority = 300, 208 .base.cra_blocksize = SHA384_BLOCK_SIZE, 209 .base.cra_ctxsize = sizeof(struct hmac_sha384_key), 210 .base.cra_module = THIS_MODULE, 211 .digestsize = SHA384_DIGEST_SIZE, 212 .setkey = crypto_hmac_sha384_setkey, 213 .init = crypto_hmac_sha384_init, 214 .update = crypto_hmac_sha384_update, 215 .final = crypto_hmac_sha384_final, 216 .digest = crypto_hmac_sha384_digest, 217 .descsize = sizeof(struct hmac_sha384_ctx), 218 }, 219 { 220 .base.cra_name = "hmac(sha512)", 221 .base.cra_driver_name = "hmac-sha512-lib", 222 .base.cra_priority = 300, 223 .base.cra_blocksize = SHA512_BLOCK_SIZE, 224 .base.cra_ctxsize = sizeof(struct hmac_sha512_key), 225 .base.cra_module = THIS_MODULE, 226 .digestsize = SHA512_DIGEST_SIZE, 227 .setkey = crypto_hmac_sha512_setkey, 228 .init = crypto_hmac_sha512_init, 229 .update = crypto_hmac_sha512_update, 230 .final = crypto_hmac_sha512_final, 231 .digest = crypto_hmac_sha512_digest, 232 .descsize = sizeof(struct hmac_sha512_ctx), 233 }, 234 }; 235 236 static int __init crypto_sha512_mod_init(void) 237 { 238 return crypto_register_shashes(algs, ARRAY_SIZE(algs)); 239 } 240 module_init(crypto_sha512_mod_init); 241 242 static void __exit crypto_sha512_mod_exit(void) 243 { 244 crypto_unregister_shashes(algs, ARRAY_SIZE(algs)); 245 } 246 module_exit(crypto_sha512_mod_exit); 247 248 MODULE_LICENSE("GPL"); 249 MODULE_DESCRIPTION("Crypto API support for SHA-384, SHA-512, HMAC-SHA384, and HMAC-SHA512"); 250 251 MODULE_ALIAS_CRYPTO("sha384"); 252 MODULE_ALIAS_CRYPTO("sha384-lib"); 253 MODULE_ALIAS_CRYPTO("sha512"); 254 MODULE_ALIAS_CRYPTO("sha512-lib"); 255 MODULE_ALIAS_CRYPTO("hmac(sha384)"); 256 MODULE_ALIAS_CRYPTO("hmac-sha384-lib"); 257 MODULE_ALIAS_CRYPTO("hmac(sha512)"); 258 MODULE_ALIAS_CRYPTO("hmac-sha512-lib"); 259