Tim Chen | 87de457 | 2013-03-26 14:00:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Cryptographic API. |
| 3 | * |
| 4 | * Glue code for the SHA512 Secure Hash Algorithm assembler |
| 5 | * implementation using supplemental SSE3 / AVX / AVX2 instructions. |
| 6 | * |
| 7 | * This file is based on sha512_generic.c |
| 8 | * |
| 9 | * Copyright (C) 2013 Intel Corporation |
| 10 | * Author: Tim Chen <tim.c.chen@linux.intel.com> |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify it |
| 13 | * under the terms of the GNU General Public License as published by the Free |
| 14 | * Software Foundation; either version 2 of the License, or (at your option) |
| 15 | * any later version. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | * SOFTWARE. |
| 25 | * |
| 26 | */ |
| 27 | |
| 28 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 29 | |
| 30 | #include <crypto/internal/hash.h> |
| 31 | #include <linux/init.h> |
| 32 | #include <linux/module.h> |
| 33 | #include <linux/mm.h> |
| 34 | #include <linux/cryptohash.h> |
| 35 | #include <linux/types.h> |
| 36 | #include <crypto/sha.h> |
Ard Biesheuvel | e68410e | 2015-04-09 12:55:48 +0200 | [diff] [blame] | 37 | #include <crypto/sha512_base.h> |
Ingo Molnar | df6b35f | 2015-04-24 02:46:00 +0200 | [diff] [blame] | 38 | #include <asm/fpu/api.h> |
Tim Chen | 87de457 | 2013-03-26 14:00:02 -0700 | [diff] [blame] | 39 | |
| 40 | #include <linux/string.h> |
| 41 | |
Ard Biesheuvel | e68410e | 2015-04-09 12:55:48 +0200 | [diff] [blame] | 42 | asmlinkage void sha512_transform_ssse3(u64 *digest, const char *data, |
| 43 | u64 rounds); |
Tim Chen | 87de457 | 2013-03-26 14:00:02 -0700 | [diff] [blame] | 44 | |
tim | be6ec98 | 2015-09-16 16:35:53 -0700 | [diff] [blame] | 45 | typedef void (sha512_transform_fn)(u64 *digest, const char *data, u64 rounds); |
Tim Chen | 87de457 | 2013-03-26 14:00:02 -0700 | [diff] [blame] | 46 | |
tim | be6ec98 | 2015-09-16 16:35:53 -0700 | [diff] [blame] | 47 | static int sha512_update(struct shash_desc *desc, const u8 *data, |
| 48 | unsigned int len, sha512_transform_fn *sha512_xform) |
Tim Chen | 87de457 | 2013-03-26 14:00:02 -0700 | [diff] [blame] | 49 | { |
| 50 | struct sha512_state *sctx = shash_desc_ctx(desc); |
Tim Chen | 87de457 | 2013-03-26 14:00:02 -0700 | [diff] [blame] | 51 | |
Ard Biesheuvel | e68410e | 2015-04-09 12:55:48 +0200 | [diff] [blame] | 52 | if (!irq_fpu_usable() || |
| 53 | (sctx->count[0] % SHA512_BLOCK_SIZE) + len < SHA512_BLOCK_SIZE) |
| 54 | return crypto_sha512_update(desc, data, len); |
Tim Chen | 87de457 | 2013-03-26 14:00:02 -0700 | [diff] [blame] | 55 | |
Ard Biesheuvel | e68410e | 2015-04-09 12:55:48 +0200 | [diff] [blame] | 56 | /* make sure casting to sha512_block_fn() is safe */ |
| 57 | BUILD_BUG_ON(offsetof(struct sha512_state, state) != 0); |
Tim Chen | 87de457 | 2013-03-26 14:00:02 -0700 | [diff] [blame] | 58 | |
Ard Biesheuvel | e68410e | 2015-04-09 12:55:48 +0200 | [diff] [blame] | 59 | kernel_fpu_begin(); |
| 60 | sha512_base_do_update(desc, data, len, |
tim | be6ec98 | 2015-09-16 16:35:53 -0700 | [diff] [blame] | 61 | (sha512_block_fn *)sha512_xform); |
Ard Biesheuvel | e68410e | 2015-04-09 12:55:48 +0200 | [diff] [blame] | 62 | kernel_fpu_end(); |
Tim Chen | 87de457 | 2013-03-26 14:00:02 -0700 | [diff] [blame] | 63 | |
Ard Biesheuvel | e68410e | 2015-04-09 12:55:48 +0200 | [diff] [blame] | 64 | return 0; |
Tim Chen | 87de457 | 2013-03-26 14:00:02 -0700 | [diff] [blame] | 65 | } |
| 66 | |
tim | be6ec98 | 2015-09-16 16:35:53 -0700 | [diff] [blame] | 67 | static int sha512_finup(struct shash_desc *desc, const u8 *data, |
| 68 | unsigned int len, u8 *out, sha512_transform_fn *sha512_xform) |
Ard Biesheuvel | e68410e | 2015-04-09 12:55:48 +0200 | [diff] [blame] | 69 | { |
| 70 | if (!irq_fpu_usable()) |
| 71 | return crypto_sha512_finup(desc, data, len, out); |
| 72 | |
| 73 | kernel_fpu_begin(); |
| 74 | if (len) |
| 75 | sha512_base_do_update(desc, data, len, |
tim | be6ec98 | 2015-09-16 16:35:53 -0700 | [diff] [blame] | 76 | (sha512_block_fn *)sha512_xform); |
| 77 | sha512_base_do_finalize(desc, (sha512_block_fn *)sha512_xform); |
Ard Biesheuvel | e68410e | 2015-04-09 12:55:48 +0200 | [diff] [blame] | 78 | kernel_fpu_end(); |
| 79 | |
| 80 | return sha512_base_finish(desc, out); |
| 81 | } |
Tim Chen | 87de457 | 2013-03-26 14:00:02 -0700 | [diff] [blame] | 82 | |
tim | be6ec98 | 2015-09-16 16:35:53 -0700 | [diff] [blame] | 83 | static int sha512_ssse3_update(struct shash_desc *desc, const u8 *data, |
| 84 | unsigned int len) |
| 85 | { |
| 86 | return sha512_update(desc, data, len, sha512_transform_ssse3); |
| 87 | } |
| 88 | |
| 89 | static int sha512_ssse3_finup(struct shash_desc *desc, const u8 *data, |
| 90 | unsigned int len, u8 *out) |
| 91 | { |
| 92 | return sha512_finup(desc, data, len, out, sha512_transform_ssse3); |
| 93 | } |
| 94 | |
Tim Chen | 87de457 | 2013-03-26 14:00:02 -0700 | [diff] [blame] | 95 | /* Add padding and return the message digest. */ |
| 96 | static int sha512_ssse3_final(struct shash_desc *desc, u8 *out) |
| 97 | { |
Ard Biesheuvel | e68410e | 2015-04-09 12:55:48 +0200 | [diff] [blame] | 98 | return sha512_ssse3_finup(desc, NULL, 0, out); |
Jussi Kivilinna | 340991e | 2013-05-21 17:10:44 +0300 | [diff] [blame] | 99 | } |
| 100 | |
tim | be6ec98 | 2015-09-16 16:35:53 -0700 | [diff] [blame] | 101 | static struct shash_alg sha512_ssse3_algs[] = { { |
Tim Chen | 87de457 | 2013-03-26 14:00:02 -0700 | [diff] [blame] | 102 | .digestsize = SHA512_DIGEST_SIZE, |
Ard Biesheuvel | e68410e | 2015-04-09 12:55:48 +0200 | [diff] [blame] | 103 | .init = sha512_base_init, |
Tim Chen | 87de457 | 2013-03-26 14:00:02 -0700 | [diff] [blame] | 104 | .update = sha512_ssse3_update, |
| 105 | .final = sha512_ssse3_final, |
Ard Biesheuvel | e68410e | 2015-04-09 12:55:48 +0200 | [diff] [blame] | 106 | .finup = sha512_ssse3_finup, |
Tim Chen | 87de457 | 2013-03-26 14:00:02 -0700 | [diff] [blame] | 107 | .descsize = sizeof(struct sha512_state), |
Tim Chen | 87de457 | 2013-03-26 14:00:02 -0700 | [diff] [blame] | 108 | .base = { |
| 109 | .cra_name = "sha512", |
| 110 | .cra_driver_name = "sha512-ssse3", |
| 111 | .cra_priority = 150, |
| 112 | .cra_flags = CRYPTO_ALG_TYPE_SHASH, |
| 113 | .cra_blocksize = SHA512_BLOCK_SIZE, |
| 114 | .cra_module = THIS_MODULE, |
| 115 | } |
Jussi Kivilinna | 340991e | 2013-05-21 17:10:44 +0300 | [diff] [blame] | 116 | }, { |
| 117 | .digestsize = SHA384_DIGEST_SIZE, |
Ard Biesheuvel | e68410e | 2015-04-09 12:55:48 +0200 | [diff] [blame] | 118 | .init = sha384_base_init, |
Jussi Kivilinna | 340991e | 2013-05-21 17:10:44 +0300 | [diff] [blame] | 119 | .update = sha512_ssse3_update, |
Ard Biesheuvel | e68410e | 2015-04-09 12:55:48 +0200 | [diff] [blame] | 120 | .final = sha512_ssse3_final, |
| 121 | .finup = sha512_ssse3_finup, |
Jussi Kivilinna | 340991e | 2013-05-21 17:10:44 +0300 | [diff] [blame] | 122 | .descsize = sizeof(struct sha512_state), |
Jussi Kivilinna | 340991e | 2013-05-21 17:10:44 +0300 | [diff] [blame] | 123 | .base = { |
| 124 | .cra_name = "sha384", |
| 125 | .cra_driver_name = "sha384-ssse3", |
| 126 | .cra_priority = 150, |
| 127 | .cra_flags = CRYPTO_ALG_TYPE_SHASH, |
| 128 | .cra_blocksize = SHA384_BLOCK_SIZE, |
| 129 | .cra_module = THIS_MODULE, |
| 130 | } |
| 131 | } }; |
Tim Chen | 87de457 | 2013-03-26 14:00:02 -0700 | [diff] [blame] | 132 | |
tim | be6ec98 | 2015-09-16 16:35:53 -0700 | [diff] [blame] | 133 | static int register_sha512_ssse3(void) |
| 134 | { |
| 135 | if (boot_cpu_has(X86_FEATURE_SSSE3)) |
| 136 | return crypto_register_shashes(sha512_ssse3_algs, |
| 137 | ARRAY_SIZE(sha512_ssse3_algs)); |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static void unregister_sha512_ssse3(void) |
| 142 | { |
| 143 | if (boot_cpu_has(X86_FEATURE_SSSE3)) |
| 144 | crypto_unregister_shashes(sha512_ssse3_algs, |
| 145 | ARRAY_SIZE(sha512_ssse3_algs)); |
| 146 | } |
| 147 | |
Tim Chen | 87de457 | 2013-03-26 14:00:02 -0700 | [diff] [blame] | 148 | #ifdef CONFIG_AS_AVX |
tim | be6ec98 | 2015-09-16 16:35:53 -0700 | [diff] [blame] | 149 | asmlinkage void sha512_transform_avx(u64 *digest, const char *data, |
| 150 | u64 rounds); |
| 151 | static bool avx_usable(void) |
Tim Chen | 87de457 | 2013-03-26 14:00:02 -0700 | [diff] [blame] | 152 | { |
Dave Hansen | d91cab7 | 2015-09-02 16:31:26 -0700 | [diff] [blame] | 153 | if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL)) { |
Borislav Petkov | da154e8 | 2016-04-04 22:24:56 +0200 | [diff] [blame] | 154 | if (boot_cpu_has(X86_FEATURE_AVX)) |
Ingo Molnar | c93b8a3 | 2015-04-28 10:11:24 +0200 | [diff] [blame] | 155 | pr_info("AVX detected but unusable.\n"); |
Tim Chen | 87de457 | 2013-03-26 14:00:02 -0700 | [diff] [blame] | 156 | return false; |
| 157 | } |
| 158 | |
| 159 | return true; |
| 160 | } |
tim | be6ec98 | 2015-09-16 16:35:53 -0700 | [diff] [blame] | 161 | |
| 162 | static int sha512_avx_update(struct shash_desc *desc, const u8 *data, |
| 163 | unsigned int len) |
| 164 | { |
| 165 | return sha512_update(desc, data, len, sha512_transform_avx); |
| 166 | } |
| 167 | |
| 168 | static int sha512_avx_finup(struct shash_desc *desc, const u8 *data, |
| 169 | unsigned int len, u8 *out) |
| 170 | { |
| 171 | return sha512_finup(desc, data, len, out, sha512_transform_avx); |
| 172 | } |
| 173 | |
| 174 | /* Add padding and return the message digest. */ |
| 175 | static int sha512_avx_final(struct shash_desc *desc, u8 *out) |
| 176 | { |
| 177 | return sha512_avx_finup(desc, NULL, 0, out); |
| 178 | } |
| 179 | |
| 180 | static struct shash_alg sha512_avx_algs[] = { { |
| 181 | .digestsize = SHA512_DIGEST_SIZE, |
| 182 | .init = sha512_base_init, |
| 183 | .update = sha512_avx_update, |
| 184 | .final = sha512_avx_final, |
| 185 | .finup = sha512_avx_finup, |
| 186 | .descsize = sizeof(struct sha512_state), |
| 187 | .base = { |
| 188 | .cra_name = "sha512", |
| 189 | .cra_driver_name = "sha512-avx", |
| 190 | .cra_priority = 160, |
| 191 | .cra_flags = CRYPTO_ALG_TYPE_SHASH, |
| 192 | .cra_blocksize = SHA512_BLOCK_SIZE, |
| 193 | .cra_module = THIS_MODULE, |
| 194 | } |
| 195 | }, { |
| 196 | .digestsize = SHA384_DIGEST_SIZE, |
| 197 | .init = sha384_base_init, |
| 198 | .update = sha512_avx_update, |
| 199 | .final = sha512_avx_final, |
| 200 | .finup = sha512_avx_finup, |
| 201 | .descsize = sizeof(struct sha512_state), |
| 202 | .base = { |
| 203 | .cra_name = "sha384", |
| 204 | .cra_driver_name = "sha384-avx", |
| 205 | .cra_priority = 160, |
| 206 | .cra_flags = CRYPTO_ALG_TYPE_SHASH, |
| 207 | .cra_blocksize = SHA384_BLOCK_SIZE, |
| 208 | .cra_module = THIS_MODULE, |
| 209 | } |
| 210 | } }; |
| 211 | |
| 212 | static int register_sha512_avx(void) |
| 213 | { |
| 214 | if (avx_usable()) |
| 215 | return crypto_register_shashes(sha512_avx_algs, |
| 216 | ARRAY_SIZE(sha512_avx_algs)); |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | static void unregister_sha512_avx(void) |
| 221 | { |
| 222 | if (avx_usable()) |
| 223 | crypto_unregister_shashes(sha512_avx_algs, |
| 224 | ARRAY_SIZE(sha512_avx_algs)); |
| 225 | } |
| 226 | #else |
| 227 | static inline int register_sha512_avx(void) { return 0; } |
| 228 | static inline void unregister_sha512_avx(void) { } |
| 229 | #endif |
| 230 | |
| 231 | #if defined(CONFIG_AS_AVX2) && defined(CONFIG_AS_AVX) |
| 232 | asmlinkage void sha512_transform_rorx(u64 *digest, const char *data, |
| 233 | u64 rounds); |
| 234 | |
| 235 | static int sha512_avx2_update(struct shash_desc *desc, const u8 *data, |
| 236 | unsigned int len) |
| 237 | { |
| 238 | return sha512_update(desc, data, len, sha512_transform_rorx); |
| 239 | } |
| 240 | |
| 241 | static int sha512_avx2_finup(struct shash_desc *desc, const u8 *data, |
| 242 | unsigned int len, u8 *out) |
| 243 | { |
| 244 | return sha512_finup(desc, data, len, out, sha512_transform_rorx); |
| 245 | } |
| 246 | |
| 247 | /* Add padding and return the message digest. */ |
| 248 | static int sha512_avx2_final(struct shash_desc *desc, u8 *out) |
| 249 | { |
| 250 | return sha512_avx2_finup(desc, NULL, 0, out); |
| 251 | } |
| 252 | |
| 253 | static struct shash_alg sha512_avx2_algs[] = { { |
| 254 | .digestsize = SHA512_DIGEST_SIZE, |
| 255 | .init = sha512_base_init, |
| 256 | .update = sha512_avx2_update, |
| 257 | .final = sha512_avx2_final, |
| 258 | .finup = sha512_avx2_finup, |
| 259 | .descsize = sizeof(struct sha512_state), |
| 260 | .base = { |
| 261 | .cra_name = "sha512", |
| 262 | .cra_driver_name = "sha512-avx2", |
| 263 | .cra_priority = 170, |
| 264 | .cra_flags = CRYPTO_ALG_TYPE_SHASH, |
| 265 | .cra_blocksize = SHA512_BLOCK_SIZE, |
| 266 | .cra_module = THIS_MODULE, |
| 267 | } |
| 268 | }, { |
| 269 | .digestsize = SHA384_DIGEST_SIZE, |
| 270 | .init = sha384_base_init, |
| 271 | .update = sha512_avx2_update, |
| 272 | .final = sha512_avx2_final, |
| 273 | .finup = sha512_avx2_finup, |
| 274 | .descsize = sizeof(struct sha512_state), |
| 275 | .base = { |
| 276 | .cra_name = "sha384", |
| 277 | .cra_driver_name = "sha384-avx2", |
| 278 | .cra_priority = 170, |
| 279 | .cra_flags = CRYPTO_ALG_TYPE_SHASH, |
| 280 | .cra_blocksize = SHA384_BLOCK_SIZE, |
| 281 | .cra_module = THIS_MODULE, |
| 282 | } |
| 283 | } }; |
| 284 | |
| 285 | static bool avx2_usable(void) |
| 286 | { |
| 287 | if (avx_usable() && boot_cpu_has(X86_FEATURE_AVX2) && |
| 288 | boot_cpu_has(X86_FEATURE_BMI2)) |
| 289 | return true; |
| 290 | |
| 291 | return false; |
| 292 | } |
| 293 | |
| 294 | static int register_sha512_avx2(void) |
| 295 | { |
| 296 | if (avx2_usable()) |
| 297 | return crypto_register_shashes(sha512_avx2_algs, |
| 298 | ARRAY_SIZE(sha512_avx2_algs)); |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | static void unregister_sha512_avx2(void) |
| 303 | { |
| 304 | if (avx2_usable()) |
| 305 | crypto_unregister_shashes(sha512_avx2_algs, |
| 306 | ARRAY_SIZE(sha512_avx2_algs)); |
| 307 | } |
| 308 | #else |
| 309 | static inline int register_sha512_avx2(void) { return 0; } |
| 310 | static inline void unregister_sha512_avx2(void) { } |
Tim Chen | 87de457 | 2013-03-26 14:00:02 -0700 | [diff] [blame] | 311 | #endif |
| 312 | |
| 313 | static int __init sha512_ssse3_mod_init(void) |
| 314 | { |
Tim Chen | 87de457 | 2013-03-26 14:00:02 -0700 | [diff] [blame] | 315 | |
tim | be6ec98 | 2015-09-16 16:35:53 -0700 | [diff] [blame] | 316 | if (register_sha512_ssse3()) |
| 317 | goto fail; |
| 318 | |
| 319 | if (register_sha512_avx()) { |
| 320 | unregister_sha512_ssse3(); |
| 321 | goto fail; |
Tim Chen | 87de457 | 2013-03-26 14:00:02 -0700 | [diff] [blame] | 322 | } |
Tim Chen | 87de457 | 2013-03-26 14:00:02 -0700 | [diff] [blame] | 323 | |
tim | be6ec98 | 2015-09-16 16:35:53 -0700 | [diff] [blame] | 324 | if (register_sha512_avx2()) { |
| 325 | unregister_sha512_avx(); |
| 326 | unregister_sha512_ssse3(); |
| 327 | goto fail; |
Tim Chen | 87de457 | 2013-03-26 14:00:02 -0700 | [diff] [blame] | 328 | } |
Tim Chen | 87de457 | 2013-03-26 14:00:02 -0700 | [diff] [blame] | 329 | |
tim | be6ec98 | 2015-09-16 16:35:53 -0700 | [diff] [blame] | 330 | return 0; |
| 331 | fail: |
Tim Chen | 87de457 | 2013-03-26 14:00:02 -0700 | [diff] [blame] | 332 | return -ENODEV; |
| 333 | } |
| 334 | |
| 335 | static void __exit sha512_ssse3_mod_fini(void) |
| 336 | { |
tim | be6ec98 | 2015-09-16 16:35:53 -0700 | [diff] [blame] | 337 | unregister_sha512_avx2(); |
| 338 | unregister_sha512_avx(); |
| 339 | unregister_sha512_ssse3(); |
Tim Chen | 87de457 | 2013-03-26 14:00:02 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | module_init(sha512_ssse3_mod_init); |
| 343 | module_exit(sha512_ssse3_mod_fini); |
| 344 | |
| 345 | MODULE_LICENSE("GPL"); |
| 346 | MODULE_DESCRIPTION("SHA512 Secure Hash Algorithm, Supplemental SSE3 accelerated"); |
| 347 | |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 348 | MODULE_ALIAS_CRYPTO("sha512"); |
Stephan Mueller | 1a445e8 | 2016-05-13 14:02:00 +0200 | [diff] [blame] | 349 | MODULE_ALIAS_CRYPTO("sha512-ssse3"); |
| 350 | MODULE_ALIAS_CRYPTO("sha512-avx"); |
| 351 | MODULE_ALIAS_CRYPTO("sha512-avx2"); |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 352 | MODULE_ALIAS_CRYPTO("sha384"); |
Stephan Mueller | 1a445e8 | 2016-05-13 14:02:00 +0200 | [diff] [blame] | 353 | MODULE_ALIAS_CRYPTO("sha384-ssse3"); |
| 354 | MODULE_ALIAS_CRYPTO("sha384-avx"); |
| 355 | MODULE_ALIAS_CRYPTO("sha384-avx2"); |