Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Cryptographic API. |
| 3 | * |
| 4 | * Glue code for the SHA256 Secure Hash Algorithm assembler |
| 5 | * implementation using supplemental SSE3 / AVX / AVX2 instructions. |
| 6 | * |
| 7 | * This file is based on sha256_generic.c |
| 8 | * |
| 9 | * Copyright (C) 2013 Intel Corporation. |
| 10 | * |
| 11 | * Author: |
| 12 | * Tim Chen <tim.c.chen@linux.intel.com> |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify it |
| 15 | * under the terms of the GNU General Public License as published by the Free |
| 16 | * Software Foundation; either version 2 of the License, or (at your option) |
| 17 | * any later version. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 21 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 22 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 23 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 24 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 25 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 | * SOFTWARE. |
| 27 | */ |
| 28 | |
| 29 | |
| 30 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 31 | |
| 32 | #include <crypto/internal/hash.h> |
Eric Biggers | f2abe0d | 2019-03-12 22:12:48 -0700 | [diff] [blame] | 33 | #include <crypto/internal/simd.h> |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 34 | #include <linux/init.h> |
| 35 | #include <linux/module.h> |
| 36 | #include <linux/mm.h> |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 37 | #include <linux/types.h> |
Eric Biggers | a24d22b | 2020-11-12 21:20:21 -0800 | [diff] [blame] | 38 | #include <crypto/sha2.h> |
Ard Biesheuvel | 1631030 | 2015-04-09 12:55:47 +0200 | [diff] [blame] | 39 | #include <crypto/sha256_base.h> |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 40 | #include <linux/string.h> |
Eric Biggers | f2abe0d | 2019-03-12 22:12:48 -0700 | [diff] [blame] | 41 | #include <asm/simd.h> |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 42 | |
Kees Cook | 41419a2 | 2020-01-14 19:57:29 -0800 | [diff] [blame] | 43 | asmlinkage void sha256_transform_ssse3(struct sha256_state *state, |
| 44 | const u8 *data, int blocks); |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 45 | |
Hans de Goede | eb7d6ba | 2019-09-01 22:35:27 +0200 | [diff] [blame] | 46 | static int _sha256_update(struct shash_desc *desc, const u8 *data, |
Kees Cook | 41419a2 | 2020-01-14 19:57:29 -0800 | [diff] [blame] | 47 | unsigned int len, sha256_block_fn *sha256_xform) |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 48 | { |
| 49 | struct sha256_state *sctx = shash_desc_ctx(desc); |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 50 | |
Eric Biggers | f2abe0d | 2019-03-12 22:12:48 -0700 | [diff] [blame] | 51 | if (!crypto_simd_usable() || |
Ard Biesheuvel | 1631030 | 2015-04-09 12:55:47 +0200 | [diff] [blame] | 52 | (sctx->count % SHA256_BLOCK_SIZE) + len < SHA256_BLOCK_SIZE) |
| 53 | return crypto_sha256_update(desc, data, len); |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 54 | |
Kees Cook | 41419a2 | 2020-01-14 19:57:29 -0800 | [diff] [blame] | 55 | /* |
| 56 | * Make sure struct sha256_state begins directly with the SHA256 |
| 57 | * 256-bit internal state, as this is what the asm functions expect. |
| 58 | */ |
Ard Biesheuvel | 1631030 | 2015-04-09 12:55:47 +0200 | [diff] [blame] | 59 | BUILD_BUG_ON(offsetof(struct sha256_state, state) != 0); |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 60 | |
Ard Biesheuvel | 1631030 | 2015-04-09 12:55:47 +0200 | [diff] [blame] | 61 | kernel_fpu_begin(); |
Kees Cook | 41419a2 | 2020-01-14 19:57:29 -0800 | [diff] [blame] | 62 | sha256_base_do_update(desc, data, len, sha256_xform); |
Ard Biesheuvel | 1631030 | 2015-04-09 12:55:47 +0200 | [diff] [blame] | 63 | kernel_fpu_end(); |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 64 | |
Ard Biesheuvel | 1631030 | 2015-04-09 12:55:47 +0200 | [diff] [blame] | 65 | return 0; |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 66 | } |
| 67 | |
tim | 5dda42f | 2015-09-16 16:35:23 -0700 | [diff] [blame] | 68 | static int sha256_finup(struct shash_desc *desc, const u8 *data, |
Kees Cook | 41419a2 | 2020-01-14 19:57:29 -0800 | [diff] [blame] | 69 | unsigned int len, u8 *out, sha256_block_fn *sha256_xform) |
Ard Biesheuvel | 1631030 | 2015-04-09 12:55:47 +0200 | [diff] [blame] | 70 | { |
Eric Biggers | f2abe0d | 2019-03-12 22:12:48 -0700 | [diff] [blame] | 71 | if (!crypto_simd_usable()) |
Ard Biesheuvel | 1631030 | 2015-04-09 12:55:47 +0200 | [diff] [blame] | 72 | return crypto_sha256_finup(desc, data, len, out); |
| 73 | |
| 74 | kernel_fpu_begin(); |
| 75 | if (len) |
Kees Cook | 41419a2 | 2020-01-14 19:57:29 -0800 | [diff] [blame] | 76 | sha256_base_do_update(desc, data, len, sha256_xform); |
| 77 | sha256_base_do_finalize(desc, sha256_xform); |
Ard Biesheuvel | 1631030 | 2015-04-09 12:55:47 +0200 | [diff] [blame] | 78 | kernel_fpu_end(); |
| 79 | |
| 80 | return sha256_base_finish(desc, out); |
| 81 | } |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 82 | |
tim | 5dda42f | 2015-09-16 16:35:23 -0700 | [diff] [blame] | 83 | static int sha256_ssse3_update(struct shash_desc *desc, const u8 *data, |
| 84 | unsigned int len) |
| 85 | { |
Hans de Goede | eb7d6ba | 2019-09-01 22:35:27 +0200 | [diff] [blame] | 86 | return _sha256_update(desc, data, len, sha256_transform_ssse3); |
tim | 5dda42f | 2015-09-16 16:35:23 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static int sha256_ssse3_finup(struct shash_desc *desc, const u8 *data, |
| 90 | unsigned int len, u8 *out) |
| 91 | { |
| 92 | return sha256_finup(desc, data, len, out, sha256_transform_ssse3); |
| 93 | } |
| 94 | |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 95 | /* Add padding and return the message digest. */ |
| 96 | static int sha256_ssse3_final(struct shash_desc *desc, u8 *out) |
| 97 | { |
Ard Biesheuvel | 1631030 | 2015-04-09 12:55:47 +0200 | [diff] [blame] | 98 | return sha256_ssse3_finup(desc, NULL, 0, out); |
Jussi Kivilinna | a710f76 | 2013-05-21 17:10:49 +0300 | [diff] [blame] | 99 | } |
| 100 | |
tim | 5dda42f | 2015-09-16 16:35:23 -0700 | [diff] [blame] | 101 | static struct shash_alg sha256_ssse3_algs[] = { { |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 102 | .digestsize = SHA256_DIGEST_SIZE, |
Ard Biesheuvel | 1631030 | 2015-04-09 12:55:47 +0200 | [diff] [blame] | 103 | .init = sha256_base_init, |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 104 | .update = sha256_ssse3_update, |
| 105 | .final = sha256_ssse3_final, |
Ard Biesheuvel | 1631030 | 2015-04-09 12:55:47 +0200 | [diff] [blame] | 106 | .finup = sha256_ssse3_finup, |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 107 | .descsize = sizeof(struct sha256_state), |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 108 | .base = { |
| 109 | .cra_name = "sha256", |
| 110 | .cra_driver_name = "sha256-ssse3", |
| 111 | .cra_priority = 150, |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 112 | .cra_blocksize = SHA256_BLOCK_SIZE, |
| 113 | .cra_module = THIS_MODULE, |
| 114 | } |
Jussi Kivilinna | a710f76 | 2013-05-21 17:10:49 +0300 | [diff] [blame] | 115 | }, { |
| 116 | .digestsize = SHA224_DIGEST_SIZE, |
Ard Biesheuvel | 1631030 | 2015-04-09 12:55:47 +0200 | [diff] [blame] | 117 | .init = sha224_base_init, |
Jussi Kivilinna | a710f76 | 2013-05-21 17:10:49 +0300 | [diff] [blame] | 118 | .update = sha256_ssse3_update, |
Ard Biesheuvel | 1631030 | 2015-04-09 12:55:47 +0200 | [diff] [blame] | 119 | .final = sha256_ssse3_final, |
| 120 | .finup = sha256_ssse3_finup, |
Jussi Kivilinna | a710f76 | 2013-05-21 17:10:49 +0300 | [diff] [blame] | 121 | .descsize = sizeof(struct sha256_state), |
Jussi Kivilinna | a710f76 | 2013-05-21 17:10:49 +0300 | [diff] [blame] | 122 | .base = { |
| 123 | .cra_name = "sha224", |
| 124 | .cra_driver_name = "sha224-ssse3", |
| 125 | .cra_priority = 150, |
Jussi Kivilinna | a710f76 | 2013-05-21 17:10:49 +0300 | [diff] [blame] | 126 | .cra_blocksize = SHA224_BLOCK_SIZE, |
| 127 | .cra_module = THIS_MODULE, |
| 128 | } |
| 129 | } }; |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 130 | |
tim | 5dda42f | 2015-09-16 16:35:23 -0700 | [diff] [blame] | 131 | static int register_sha256_ssse3(void) |
| 132 | { |
| 133 | if (boot_cpu_has(X86_FEATURE_SSSE3)) |
| 134 | return crypto_register_shashes(sha256_ssse3_algs, |
| 135 | ARRAY_SIZE(sha256_ssse3_algs)); |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static void unregister_sha256_ssse3(void) |
| 140 | { |
| 141 | if (boot_cpu_has(X86_FEATURE_SSSE3)) |
| 142 | crypto_unregister_shashes(sha256_ssse3_algs, |
| 143 | ARRAY_SIZE(sha256_ssse3_algs)); |
| 144 | } |
| 145 | |
Kees Cook | 41419a2 | 2020-01-14 19:57:29 -0800 | [diff] [blame] | 146 | asmlinkage void sha256_transform_avx(struct sha256_state *state, |
| 147 | const u8 *data, int blocks); |
tim | 5dda42f | 2015-09-16 16:35:23 -0700 | [diff] [blame] | 148 | |
| 149 | static int sha256_avx_update(struct shash_desc *desc, const u8 *data, |
| 150 | unsigned int len) |
| 151 | { |
Hans de Goede | eb7d6ba | 2019-09-01 22:35:27 +0200 | [diff] [blame] | 152 | return _sha256_update(desc, data, len, sha256_transform_avx); |
tim | 5dda42f | 2015-09-16 16:35:23 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static int sha256_avx_finup(struct shash_desc *desc, const u8 *data, |
| 156 | unsigned int len, u8 *out) |
| 157 | { |
| 158 | return sha256_finup(desc, data, len, out, sha256_transform_avx); |
| 159 | } |
| 160 | |
| 161 | static int sha256_avx_final(struct shash_desc *desc, u8 *out) |
| 162 | { |
| 163 | return sha256_avx_finup(desc, NULL, 0, out); |
| 164 | } |
| 165 | |
| 166 | static struct shash_alg sha256_avx_algs[] = { { |
| 167 | .digestsize = SHA256_DIGEST_SIZE, |
| 168 | .init = sha256_base_init, |
| 169 | .update = sha256_avx_update, |
| 170 | .final = sha256_avx_final, |
| 171 | .finup = sha256_avx_finup, |
| 172 | .descsize = sizeof(struct sha256_state), |
| 173 | .base = { |
| 174 | .cra_name = "sha256", |
| 175 | .cra_driver_name = "sha256-avx", |
| 176 | .cra_priority = 160, |
tim | 5dda42f | 2015-09-16 16:35:23 -0700 | [diff] [blame] | 177 | .cra_blocksize = SHA256_BLOCK_SIZE, |
| 178 | .cra_module = THIS_MODULE, |
| 179 | } |
| 180 | }, { |
| 181 | .digestsize = SHA224_DIGEST_SIZE, |
| 182 | .init = sha224_base_init, |
| 183 | .update = sha256_avx_update, |
| 184 | .final = sha256_avx_final, |
| 185 | .finup = sha256_avx_finup, |
| 186 | .descsize = sizeof(struct sha256_state), |
| 187 | .base = { |
| 188 | .cra_name = "sha224", |
| 189 | .cra_driver_name = "sha224-avx", |
| 190 | .cra_priority = 160, |
tim | 5dda42f | 2015-09-16 16:35:23 -0700 | [diff] [blame] | 191 | .cra_blocksize = SHA224_BLOCK_SIZE, |
| 192 | .cra_module = THIS_MODULE, |
| 193 | } |
| 194 | } }; |
| 195 | |
| 196 | static bool avx_usable(void) |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 197 | { |
Dave Hansen | d91cab7 | 2015-09-02 16:31:26 -0700 | [diff] [blame] | 198 | if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL)) { |
Borislav Petkov | da154e8 | 2016-04-04 22:24:56 +0200 | [diff] [blame] | 199 | if (boot_cpu_has(X86_FEATURE_AVX)) |
Ingo Molnar | 70d51eb | 2015-04-28 10:11:24 +0200 | [diff] [blame] | 200 | pr_info("AVX detected but unusable.\n"); |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 201 | return false; |
| 202 | } |
| 203 | |
| 204 | return true; |
| 205 | } |
tim | 5dda42f | 2015-09-16 16:35:23 -0700 | [diff] [blame] | 206 | |
| 207 | static int register_sha256_avx(void) |
| 208 | { |
| 209 | if (avx_usable()) |
| 210 | return crypto_register_shashes(sha256_avx_algs, |
| 211 | ARRAY_SIZE(sha256_avx_algs)); |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | static void unregister_sha256_avx(void) |
| 216 | { |
| 217 | if (avx_usable()) |
| 218 | crypto_unregister_shashes(sha256_avx_algs, |
| 219 | ARRAY_SIZE(sha256_avx_algs)); |
| 220 | } |
| 221 | |
Kees Cook | 41419a2 | 2020-01-14 19:57:29 -0800 | [diff] [blame] | 222 | asmlinkage void sha256_transform_rorx(struct sha256_state *state, |
| 223 | const u8 *data, int blocks); |
tim | 5dda42f | 2015-09-16 16:35:23 -0700 | [diff] [blame] | 224 | |
| 225 | static int sha256_avx2_update(struct shash_desc *desc, const u8 *data, |
| 226 | unsigned int len) |
| 227 | { |
Hans de Goede | eb7d6ba | 2019-09-01 22:35:27 +0200 | [diff] [blame] | 228 | return _sha256_update(desc, data, len, sha256_transform_rorx); |
tim | 5dda42f | 2015-09-16 16:35:23 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static int sha256_avx2_finup(struct shash_desc *desc, const u8 *data, |
| 232 | unsigned int len, u8 *out) |
| 233 | { |
| 234 | return sha256_finup(desc, data, len, out, sha256_transform_rorx); |
| 235 | } |
| 236 | |
| 237 | static int sha256_avx2_final(struct shash_desc *desc, u8 *out) |
| 238 | { |
| 239 | return sha256_avx2_finup(desc, NULL, 0, out); |
| 240 | } |
| 241 | |
| 242 | static struct shash_alg sha256_avx2_algs[] = { { |
| 243 | .digestsize = SHA256_DIGEST_SIZE, |
| 244 | .init = sha256_base_init, |
| 245 | .update = sha256_avx2_update, |
| 246 | .final = sha256_avx2_final, |
| 247 | .finup = sha256_avx2_finup, |
| 248 | .descsize = sizeof(struct sha256_state), |
| 249 | .base = { |
| 250 | .cra_name = "sha256", |
| 251 | .cra_driver_name = "sha256-avx2", |
| 252 | .cra_priority = 170, |
tim | 5dda42f | 2015-09-16 16:35:23 -0700 | [diff] [blame] | 253 | .cra_blocksize = SHA256_BLOCK_SIZE, |
| 254 | .cra_module = THIS_MODULE, |
| 255 | } |
| 256 | }, { |
| 257 | .digestsize = SHA224_DIGEST_SIZE, |
| 258 | .init = sha224_base_init, |
| 259 | .update = sha256_avx2_update, |
| 260 | .final = sha256_avx2_final, |
| 261 | .finup = sha256_avx2_finup, |
| 262 | .descsize = sizeof(struct sha256_state), |
| 263 | .base = { |
| 264 | .cra_name = "sha224", |
| 265 | .cra_driver_name = "sha224-avx2", |
| 266 | .cra_priority = 170, |
tim | 5dda42f | 2015-09-16 16:35:23 -0700 | [diff] [blame] | 267 | .cra_blocksize = SHA224_BLOCK_SIZE, |
| 268 | .cra_module = THIS_MODULE, |
| 269 | } |
| 270 | } }; |
| 271 | |
| 272 | static bool avx2_usable(void) |
| 273 | { |
| 274 | if (avx_usable() && boot_cpu_has(X86_FEATURE_AVX2) && |
| 275 | boot_cpu_has(X86_FEATURE_BMI2)) |
| 276 | return true; |
| 277 | |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | static int register_sha256_avx2(void) |
| 282 | { |
| 283 | if (avx2_usable()) |
| 284 | return crypto_register_shashes(sha256_avx2_algs, |
| 285 | ARRAY_SIZE(sha256_avx2_algs)); |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | static void unregister_sha256_avx2(void) |
| 290 | { |
| 291 | if (avx2_usable()) |
| 292 | crypto_unregister_shashes(sha256_avx2_algs, |
| 293 | ARRAY_SIZE(sha256_avx2_algs)); |
| 294 | } |
| 295 | |
tim | 5dda42f | 2015-09-16 16:35:23 -0700 | [diff] [blame] | 296 | #ifdef CONFIG_AS_SHA256_NI |
Kees Cook | 41419a2 | 2020-01-14 19:57:29 -0800 | [diff] [blame] | 297 | asmlinkage void sha256_ni_transform(struct sha256_state *digest, |
| 298 | const u8 *data, int rounds); |
tim | 5dda42f | 2015-09-16 16:35:23 -0700 | [diff] [blame] | 299 | |
| 300 | static int sha256_ni_update(struct shash_desc *desc, const u8 *data, |
| 301 | unsigned int len) |
| 302 | { |
Hans de Goede | eb7d6ba | 2019-09-01 22:35:27 +0200 | [diff] [blame] | 303 | return _sha256_update(desc, data, len, sha256_ni_transform); |
tim | 5dda42f | 2015-09-16 16:35:23 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | static int sha256_ni_finup(struct shash_desc *desc, const u8 *data, |
| 307 | unsigned int len, u8 *out) |
| 308 | { |
| 309 | return sha256_finup(desc, data, len, out, sha256_ni_transform); |
| 310 | } |
| 311 | |
| 312 | static int sha256_ni_final(struct shash_desc *desc, u8 *out) |
| 313 | { |
| 314 | return sha256_ni_finup(desc, NULL, 0, out); |
| 315 | } |
| 316 | |
| 317 | static struct shash_alg sha256_ni_algs[] = { { |
| 318 | .digestsize = SHA256_DIGEST_SIZE, |
| 319 | .init = sha256_base_init, |
| 320 | .update = sha256_ni_update, |
| 321 | .final = sha256_ni_final, |
| 322 | .finup = sha256_ni_finup, |
| 323 | .descsize = sizeof(struct sha256_state), |
| 324 | .base = { |
| 325 | .cra_name = "sha256", |
| 326 | .cra_driver_name = "sha256-ni", |
| 327 | .cra_priority = 250, |
tim | 5dda42f | 2015-09-16 16:35:23 -0700 | [diff] [blame] | 328 | .cra_blocksize = SHA256_BLOCK_SIZE, |
| 329 | .cra_module = THIS_MODULE, |
| 330 | } |
| 331 | }, { |
| 332 | .digestsize = SHA224_DIGEST_SIZE, |
| 333 | .init = sha224_base_init, |
| 334 | .update = sha256_ni_update, |
| 335 | .final = sha256_ni_final, |
| 336 | .finup = sha256_ni_finup, |
| 337 | .descsize = sizeof(struct sha256_state), |
| 338 | .base = { |
| 339 | .cra_name = "sha224", |
| 340 | .cra_driver_name = "sha224-ni", |
| 341 | .cra_priority = 250, |
tim | 5dda42f | 2015-09-16 16:35:23 -0700 | [diff] [blame] | 342 | .cra_blocksize = SHA224_BLOCK_SIZE, |
| 343 | .cra_module = THIS_MODULE, |
| 344 | } |
| 345 | } }; |
| 346 | |
| 347 | static int register_sha256_ni(void) |
| 348 | { |
| 349 | if (boot_cpu_has(X86_FEATURE_SHA_NI)) |
| 350 | return crypto_register_shashes(sha256_ni_algs, |
| 351 | ARRAY_SIZE(sha256_ni_algs)); |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | static void unregister_sha256_ni(void) |
| 356 | { |
| 357 | if (boot_cpu_has(X86_FEATURE_SHA_NI)) |
| 358 | crypto_unregister_shashes(sha256_ni_algs, |
| 359 | ARRAY_SIZE(sha256_ni_algs)); |
| 360 | } |
| 361 | |
| 362 | #else |
| 363 | static inline int register_sha256_ni(void) { return 0; } |
| 364 | static inline void unregister_sha256_ni(void) { } |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 365 | #endif |
| 366 | |
| 367 | static int __init sha256_ssse3_mod_init(void) |
| 368 | { |
tim | 5dda42f | 2015-09-16 16:35:23 -0700 | [diff] [blame] | 369 | if (register_sha256_ssse3()) |
| 370 | goto fail; |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 371 | |
tim | 5dda42f | 2015-09-16 16:35:23 -0700 | [diff] [blame] | 372 | if (register_sha256_avx()) { |
| 373 | unregister_sha256_ssse3(); |
| 374 | goto fail; |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 375 | } |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 376 | |
tim | 5dda42f | 2015-09-16 16:35:23 -0700 | [diff] [blame] | 377 | if (register_sha256_avx2()) { |
| 378 | unregister_sha256_avx(); |
| 379 | unregister_sha256_ssse3(); |
| 380 | goto fail; |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 381 | } |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 382 | |
tim | 5dda42f | 2015-09-16 16:35:23 -0700 | [diff] [blame] | 383 | if (register_sha256_ni()) { |
| 384 | unregister_sha256_avx2(); |
| 385 | unregister_sha256_avx(); |
| 386 | unregister_sha256_ssse3(); |
| 387 | goto fail; |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 388 | } |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 389 | |
tim | 5dda42f | 2015-09-16 16:35:23 -0700 | [diff] [blame] | 390 | return 0; |
| 391 | fail: |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 392 | return -ENODEV; |
| 393 | } |
| 394 | |
| 395 | static void __exit sha256_ssse3_mod_fini(void) |
| 396 | { |
tim | 5dda42f | 2015-09-16 16:35:23 -0700 | [diff] [blame] | 397 | unregister_sha256_ni(); |
| 398 | unregister_sha256_avx2(); |
| 399 | unregister_sha256_avx(); |
| 400 | unregister_sha256_ssse3(); |
Tim Chen | 8275d1a | 2013-03-26 13:59:17 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | module_init(sha256_ssse3_mod_init); |
| 404 | module_exit(sha256_ssse3_mod_fini); |
| 405 | |
| 406 | MODULE_LICENSE("GPL"); |
| 407 | MODULE_DESCRIPTION("SHA256 Secure Hash Algorithm, Supplemental SSE3 accelerated"); |
| 408 | |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 409 | MODULE_ALIAS_CRYPTO("sha256"); |
Stephan Mueller | 1a445e8 | 2016-05-13 14:02:00 +0200 | [diff] [blame] | 410 | MODULE_ALIAS_CRYPTO("sha256-ssse3"); |
| 411 | MODULE_ALIAS_CRYPTO("sha256-avx"); |
| 412 | MODULE_ALIAS_CRYPTO("sha256-avx2"); |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 413 | MODULE_ALIAS_CRYPTO("sha224"); |
Stephan Mueller | 1a445e8 | 2016-05-13 14:02:00 +0200 | [diff] [blame] | 414 | MODULE_ALIAS_CRYPTO("sha224-ssse3"); |
| 415 | MODULE_ALIAS_CRYPTO("sha224-avx"); |
| 416 | MODULE_ALIAS_CRYPTO("sha224-avx2"); |
| 417 | #ifdef CONFIG_AS_SHA256_NI |
| 418 | MODULE_ALIAS_CRYPTO("sha256-ni"); |
| 419 | MODULE_ALIAS_CRYPTO("sha224-ni"); |
| 420 | #endif |