| 1 | |
| 2 | |
| 3 | * Crypto API support for SHA-3
|
| 4 | * (https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf)
|
| 5 |
|
| 6 | #include <crypto/internal/hash.h>
|
| 7 | #include <crypto/sha3.h>
|
| 8 | #include <linux/kernel.h>
|
| 9 | #include <linux/module.h>
|
| 10 |
|
| 11 | #define SHA3_CTX(desc) ((struct sha3_ctx *)shash_desc_ctx(desc))
|
| 12 |
|
| 13 | static int crypto_sha3_224_init(struct shash_desc *desc)
|
| 14 | {
|
| 15 | sha3_224_init(SHA3_CTX(desc));
|
| 16 | return 0;
|
| 17 | }
|
| 18 |
|
| 19 | static int crypto_sha3_256_init(struct shash_desc *desc)
|
| 20 | {
|
| 21 | sha3_256_init(SHA3_CTX(desc));
|
| 22 | return 0;
|
| 23 | }
|
| 24 |
|
| 25 | static int crypto_sha3_384_init(struct shash_desc *desc)
|
| 26 | {
|
| 27 | sha3_384_init(SHA3_CTX(desc));
|
| 28 | return 0;
|
| 29 | }
|
| 30 |
|
| 31 | static int crypto_sha3_512_init(struct shash_desc *desc)
|
| 32 | {
|
| 33 | sha3_512_init(SHA3_CTX(desc));
|
| 34 | return 0;
|
| 35 | }
|
| 36 |
|
| 37 | static int crypto_sha3_update(struct shash_desc *desc, const u8 *data,
|
| 38 | unsigned int len)
|
| 39 | {
|
| 40 | sha3_update(SHA3_CTX(desc), data, len);
|
| 41 | return 0;
|
| 42 | }
|
| 43 |
|
| 44 | static int crypto_sha3_final(struct shash_desc *desc, u8 *out)
|
| 45 | {
|
| 46 | sha3_final(SHA3_CTX(desc), out);
|
| 47 | return 0;
|
| 48 | }
|
| 49 |
|
| 50 | static int crypto_sha3_224_digest(struct shash_desc *desc,
|
| 51 | const u8 *data, unsigned int len, u8 *out)
|
| 52 | {
|
| 53 | sha3_224(data, len, out);
|
| 54 | return 0;
|
| 55 | }
|
| 56 |
|
| 57 | static int crypto_sha3_256_digest(struct shash_desc *desc,
|
| 58 | const u8 *data, unsigned int len, u8 *out)
|
| 59 | {
|
| 60 | sha3_256(data, len, out);
|
| 61 | return 0;
|
| 62 | }
|
| 63 |
|
| 64 | static int crypto_sha3_384_digest(struct shash_desc *desc,
|
| 65 | const u8 *data, unsigned int len, u8 *out)
|
| 66 | {
|
| 67 | sha3_384(data, len, out);
|
| 68 | return 0;
|
| 69 | }
|
| 70 |
|
| 71 | static int crypto_sha3_512_digest(struct shash_desc *desc,
|
| 72 | const u8 *data, unsigned int len, u8 *out)
|
| 73 | {
|
| 74 | sha3_512(data, len, out);
|
| 75 | return 0;
|
| 76 | }
|
| 77 |
|
| 78 | static int crypto_sha3_export_core(struct shash_desc *desc, void *out)
|
| 79 | {
|
| 80 | memcpy(out, SHA3_CTX(desc), sizeof(struct sha3_ctx));
|
| 81 | return 0;
|
| 82 | }
|
| 83 |
|
| 84 | static int crypto_sha3_import_core(struct shash_desc *desc, const void *in)
|
| 85 | {
|
| 86 | memcpy(SHA3_CTX(desc), in, sizeof(struct sha3_ctx));
|
| 87 | return 0;
|
| 88 | }
|
| 89 |
|
| 90 | static struct shash_alg algs[] = { {
|
| 91 | .digestsize = SHA3_224_DIGEST_SIZE,
|
| 92 | .init = crypto_sha3_224_init,
|
| 93 | .update = crypto_sha3_update,
|
| 94 | .final = crypto_sha3_final,
|
| 95 | .digest = crypto_sha3_224_digest,
|
| 96 | .export_core = crypto_sha3_export_core,
|
| 97 | .import_core = crypto_sha3_import_core,
|
| 98 | .descsize = sizeof(struct sha3_ctx),
|
| 99 | .base.cra_name = "sha3-224",
|
| 100 | .base.cra_driver_name = "sha3-224-lib",
|
| 101 | .base.cra_blocksize = SHA3_224_BLOCK_SIZE,
|
| 102 | .base.cra_module = THIS_MODULE,
|
| 103 | }, {
|
| 104 | .digestsize = SHA3_256_DIGEST_SIZE,
|
| 105 | .init = crypto_sha3_256_init,
|
| 106 | .update = crypto_sha3_update,
|
| 107 | .final = crypto_sha3_final,
|
| 108 | .digest = crypto_sha3_256_digest,
|
| 109 | .export_core = crypto_sha3_export_core,
|
| 110 | .import_core = crypto_sha3_import_core,
|
| 111 | .descsize = sizeof(struct sha3_ctx),
|
| 112 | .base.cra_name = "sha3-256",
|
| 113 | .base.cra_driver_name = "sha3-256-lib",
|
| 114 | .base.cra_blocksize = SHA3_256_BLOCK_SIZE,
|
| 115 | .base.cra_module = THIS_MODULE,
|
| 116 | }, {
|
| 117 | .digestsize = SHA3_384_DIGEST_SIZE,
|
| 118 | .init = crypto_sha3_384_init,
|
| 119 | .update = crypto_sha3_update,
|
| 120 | .final = crypto_sha3_final,
|
| 121 | .digest = crypto_sha3_384_digest,
|
| 122 | .export_core = crypto_sha3_export_core,
|
| 123 | .import_core = crypto_sha3_import_core,
|
| 124 | .descsize = sizeof(struct sha3_ctx),
|
| 125 | .base.cra_name = "sha3-384",
|
| 126 | .base.cra_driver_name = "sha3-384-lib",
|
| 127 | .base.cra_blocksize = SHA3_384_BLOCK_SIZE,
|
| 128 | .base.cra_module = THIS_MODULE,
|
| 129 | }, {
|
| 130 | .digestsize = SHA3_512_DIGEST_SIZE,
|
| 131 | .init = crypto_sha3_512_init,
|
| 132 | .update = crypto_sha3_update,
|
| 133 | .final = crypto_sha3_final,
|
| 134 | .digest = crypto_sha3_512_digest,
|
| 135 | .export_core = crypto_sha3_export_core,
|
| 136 | .import_core = crypto_sha3_import_core,
|
| 137 | .descsize = sizeof(struct sha3_ctx),
|
| 138 | .base.cra_name = "sha3-512",
|
| 139 | .base.cra_driver_name = "sha3-512-lib",
|
| 140 | .base.cra_blocksize = SHA3_512_BLOCK_SIZE,
|
| 141 | .base.cra_module = THIS_MODULE,
|
| 142 | } };
|
| 143 |
|
| 144 | static int __init crypto_sha3_mod_init(void)
|
| 145 | {
|
| 146 | return crypto_register_shashes(algs, ARRAY_SIZE(algs));
|
| 147 | }
|
| 148 | module_init(crypto_sha3_mod_init);
|
| 149 |
|
| 150 | static void __exit crypto_sha3_mod_exit(void)
|
| 151 | {
|
| 152 | crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
|
| 153 | }
|
| 154 | module_exit(crypto_sha3_mod_exit);
|
| 155 |
|
| 156 | MODULE_LICENSE("GPL");
|
| 157 | MODULE_DESCRIPTION("Crypto API support for SHA-3");
|
| 158 |
|
| 159 | MODULE_ALIAS_CRYPTO("sha3-224");
|
| 160 | MODULE_ALIAS_CRYPTO("sha3-224-lib");
|
| 161 | MODULE_ALIAS_CRYPTO("sha3-256");
|
| 162 | MODULE_ALIAS_CRYPTO("sha3-256-lib");
|
| 163 | MODULE_ALIAS_CRYPTO("sha3-384");
|
| 164 | MODULE_ALIAS_CRYPTO("sha3-384-lib");
|
| 165 | MODULE_ALIAS_CRYPTO("sha3-512");
|
| 166 | MODULE_ALIAS_CRYPTO("sha3-512-lib");
|