blob: a84e869ef900e21a7330d146188efa41a02fa301 [file] [log] [blame]
Sami Tolvanenf2f770d2015-04-03 18:03:40 +08001/*
2 * Glue code for the SHA256 Secure Hash Algorithm assembly implementation
3 * using optimized ARM assembler and NEON instructions.
4 *
5 * Copyright © 2015 Google Inc.
6 *
7 * This file is based on sha256_ssse3_glue.c:
8 * Copyright (C) 2013 Intel Corporation
9 * Author: Tim Chen <tim.c.chen@linux.intel.com>
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
14 * any later version.
15 *
16 */
17
18#include <crypto/internal/hash.h>
19#include <linux/crypto.h>
20#include <linux/init.h>
21#include <linux/module.h>
22#include <linux/mm.h>
23#include <linux/cryptohash.h>
24#include <linux/types.h>
25#include <linux/string.h>
26#include <crypto/sha.h>
Ard Biesheuvelb59e2ae2015-04-09 12:55:42 +020027#include <crypto/sha256_base.h>
Sami Tolvanenf2f770d2015-04-03 18:03:40 +080028#include <asm/simd.h>
29#include <asm/neon.h>
Ard Biesheuvelb59e2ae2015-04-09 12:55:42 +020030
Sami Tolvanenf2f770d2015-04-03 18:03:40 +080031#include "sha256_glue.h"
32
33asmlinkage void sha256_block_data_order(u32 *digest, const void *data,
Ard Biesheuvelb59e2ae2015-04-09 12:55:42 +020034 unsigned int num_blks);
Sami Tolvanenf2f770d2015-04-03 18:03:40 +080035
Ard Biesheuvelb59e2ae2015-04-09 12:55:42 +020036int crypto_sha256_arm_update(struct shash_desc *desc, const u8 *data,
37 unsigned int len)
Sami Tolvanenf2f770d2015-04-03 18:03:40 +080038{
Ard Biesheuvelb59e2ae2015-04-09 12:55:42 +020039 /* make sure casting to sha256_block_fn() is safe */
40 BUILD_BUG_ON(offsetof(struct sha256_state, state) != 0);
Sami Tolvanenf2f770d2015-04-03 18:03:40 +080041
Ard Biesheuvelb59e2ae2015-04-09 12:55:42 +020042 return sha256_base_do_update(desc, data, len,
43 (sha256_block_fn *)sha256_block_data_order);
Sami Tolvanenf2f770d2015-04-03 18:03:40 +080044}
Ard Biesheuvelb59e2ae2015-04-09 12:55:42 +020045EXPORT_SYMBOL(crypto_sha256_arm_update);
Sami Tolvanenf2f770d2015-04-03 18:03:40 +080046
Sami Tolvanenf2f770d2015-04-03 18:03:40 +080047static int sha256_final(struct shash_desc *desc, u8 *out)
48{
Ard Biesheuvelb59e2ae2015-04-09 12:55:42 +020049 sha256_base_do_finalize(desc,
50 (sha256_block_fn *)sha256_block_data_order);
51 return sha256_base_finish(desc, out);
Sami Tolvanenf2f770d2015-04-03 18:03:40 +080052}
53
Ard Biesheuvelb59e2ae2015-04-09 12:55:42 +020054int crypto_sha256_arm_finup(struct shash_desc *desc, const u8 *data,
55 unsigned int len, u8 *out)
Sami Tolvanenf2f770d2015-04-03 18:03:40 +080056{
Ard Biesheuvelb59e2ae2015-04-09 12:55:42 +020057 sha256_base_do_update(desc, data, len,
58 (sha256_block_fn *)sha256_block_data_order);
59 return sha256_final(desc, out);
Sami Tolvanenf2f770d2015-04-03 18:03:40 +080060}
Ard Biesheuvelb59e2ae2015-04-09 12:55:42 +020061EXPORT_SYMBOL(crypto_sha256_arm_finup);
Sami Tolvanenf2f770d2015-04-03 18:03:40 +080062
63static struct shash_alg algs[] = { {
64 .digestsize = SHA256_DIGEST_SIZE,
Ard Biesheuvelb59e2ae2015-04-09 12:55:42 +020065 .init = sha256_base_init,
66 .update = crypto_sha256_arm_update,
Sami Tolvanenf2f770d2015-04-03 18:03:40 +080067 .final = sha256_final,
Ard Biesheuvelb59e2ae2015-04-09 12:55:42 +020068 .finup = crypto_sha256_arm_finup,
Sami Tolvanenf2f770d2015-04-03 18:03:40 +080069 .descsize = sizeof(struct sha256_state),
Sami Tolvanenf2f770d2015-04-03 18:03:40 +080070 .base = {
71 .cra_name = "sha256",
72 .cra_driver_name = "sha256-asm",
73 .cra_priority = 150,
74 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
75 .cra_blocksize = SHA256_BLOCK_SIZE,
76 .cra_module = THIS_MODULE,
77 }
78}, {
79 .digestsize = SHA224_DIGEST_SIZE,
Ard Biesheuvelb59e2ae2015-04-09 12:55:42 +020080 .init = sha224_base_init,
81 .update = crypto_sha256_arm_update,
82 .final = sha256_final,
83 .finup = crypto_sha256_arm_finup,
Sami Tolvanenf2f770d2015-04-03 18:03:40 +080084 .descsize = sizeof(struct sha256_state),
Sami Tolvanenf2f770d2015-04-03 18:03:40 +080085 .base = {
86 .cra_name = "sha224",
87 .cra_driver_name = "sha224-asm",
88 .cra_priority = 150,
89 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
90 .cra_blocksize = SHA224_BLOCK_SIZE,
91 .cra_module = THIS_MODULE,
92 }
93} };
94
95static int __init sha256_mod_init(void)
96{
97 int res = crypto_register_shashes(algs, ARRAY_SIZE(algs));
98
99 if (res < 0)
100 return res;
101
102 if (IS_ENABLED(CONFIG_KERNEL_MODE_NEON) && cpu_has_neon()) {
103 res = crypto_register_shashes(sha256_neon_algs,
104 ARRAY_SIZE(sha256_neon_algs));
105
106 if (res < 0)
107 crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
108 }
109
110 return res;
111}
112
113static void __exit sha256_mod_fini(void)
114{
115 crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
116
117 if (IS_ENABLED(CONFIG_KERNEL_MODE_NEON) && cpu_has_neon())
118 crypto_unregister_shashes(sha256_neon_algs,
119 ARRAY_SIZE(sha256_neon_algs));
120}
121
122module_init(sha256_mod_init);
123module_exit(sha256_mod_fini);
124
125MODULE_LICENSE("GPL");
126MODULE_DESCRIPTION("SHA256 Secure Hash Algorithm (ARM), including NEON");
127
128MODULE_ALIAS_CRYPTO("sha256");