Skip to content

Commit 21dda37

Browse files
committed
crypto: crct10dif - expose arch-optimized lib function
Now that crc_t10dif_update() may be directly optimized for each architecture, make the shash driver for crct10dif register a crct10dif-$arch algorithm that uses it, instead of only crct10dif-generic which uses crc_t10dif_generic(). The result is that architecture-optimized crct10dif will remain available through the shash API once the architectures implement crc_t10dif_arch() instead of the shash API. Reviewed-by: Ard Biesheuvel <[email protected]> Reviewed-by: Martin K. Petersen <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Eric Biggers <[email protected]>
1 parent 0961c3b commit 21dda37

File tree

2 files changed

+65
-18
lines changed

2 files changed

+65
-18
lines changed

crypto/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ obj-$(CONFIG_CRYPTO_CRC32) += crc32_generic.o
158158
CFLAGS_crc32c_generic.o += -DARCH=$(ARCH)
159159
CFLAGS_crc32_generic.o += -DARCH=$(ARCH)
160160
obj-$(CONFIG_CRYPTO_CRCT10DIF) += crct10dif_generic.o
161+
CFLAGS_crct10dif_generic.o += -DARCH=$(ARCH)
161162
obj-$(CONFIG_CRYPTO_CRC64_ROCKSOFT) += crc64_rocksoft_generic.o
162163
obj-$(CONFIG_CRYPTO_AUTHENC) += authenc.o authencesn.o
163164
obj-$(CONFIG_CRYPTO_LZO) += lzo.o lzo-rle.o

crypto/crct10dif_generic.c

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ static int chksum_update(struct shash_desc *desc, const u8 *data,
5757
return 0;
5858
}
5959

60+
static int chksum_update_arch(struct shash_desc *desc, const u8 *data,
61+
unsigned int length)
62+
{
63+
struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
64+
65+
ctx->crc = crc_t10dif_update(ctx->crc, data, length);
66+
return 0;
67+
}
68+
6069
static int chksum_final(struct shash_desc *desc, u8 *out)
6170
{
6271
struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
@@ -71,6 +80,13 @@ static int __chksum_finup(__u16 crc, const u8 *data, unsigned int len, u8 *out)
7180
return 0;
7281
}
7382

83+
static int __chksum_finup_arch(__u16 crc, const u8 *data, unsigned int len,
84+
u8 *out)
85+
{
86+
*(__u16 *)out = crc_t10dif_update(crc, data, len);
87+
return 0;
88+
}
89+
7490
static int chksum_finup(struct shash_desc *desc, const u8 *data,
7591
unsigned int len, u8 *out)
7692
{
@@ -79,37 +95,67 @@ static int chksum_finup(struct shash_desc *desc, const u8 *data,
7995
return __chksum_finup(ctx->crc, data, len, out);
8096
}
8197

98+
static int chksum_finup_arch(struct shash_desc *desc, const u8 *data,
99+
unsigned int len, u8 *out)
100+
{
101+
struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
102+
103+
return __chksum_finup_arch(ctx->crc, data, len, out);
104+
}
105+
82106
static int chksum_digest(struct shash_desc *desc, const u8 *data,
83107
unsigned int length, u8 *out)
84108
{
85109
return __chksum_finup(0, data, length, out);
86110
}
87111

88-
static struct shash_alg alg = {
89-
.digestsize = CRC_T10DIF_DIGEST_SIZE,
90-
.init = chksum_init,
91-
.update = chksum_update,
92-
.final = chksum_final,
93-
.finup = chksum_finup,
94-
.digest = chksum_digest,
95-
.descsize = sizeof(struct chksum_desc_ctx),
96-
.base = {
97-
.cra_name = "crct10dif",
98-
.cra_driver_name = "crct10dif-generic",
99-
.cra_priority = 100,
100-
.cra_blocksize = CRC_T10DIF_BLOCK_SIZE,
101-
.cra_module = THIS_MODULE,
102-
}
103-
};
112+
static int chksum_digest_arch(struct shash_desc *desc, const u8 *data,
113+
unsigned int length, u8 *out)
114+
{
115+
return __chksum_finup_arch(0, data, length, out);
116+
}
117+
118+
static struct shash_alg algs[] = {{
119+
.digestsize = CRC_T10DIF_DIGEST_SIZE,
120+
.init = chksum_init,
121+
.update = chksum_update,
122+
.final = chksum_final,
123+
.finup = chksum_finup,
124+
.digest = chksum_digest,
125+
.descsize = sizeof(struct chksum_desc_ctx),
126+
.base.cra_name = "crct10dif",
127+
.base.cra_driver_name = "crct10dif-generic",
128+
.base.cra_priority = 100,
129+
.base.cra_blocksize = CRC_T10DIF_BLOCK_SIZE,
130+
.base.cra_module = THIS_MODULE,
131+
}, {
132+
.digestsize = CRC_T10DIF_DIGEST_SIZE,
133+
.init = chksum_init,
134+
.update = chksum_update_arch,
135+
.final = chksum_final,
136+
.finup = chksum_finup_arch,
137+
.digest = chksum_digest_arch,
138+
.descsize = sizeof(struct chksum_desc_ctx),
139+
.base.cra_name = "crct10dif",
140+
.base.cra_driver_name = "crct10dif-" __stringify(ARCH),
141+
.base.cra_priority = 150,
142+
.base.cra_blocksize = CRC_T10DIF_BLOCK_SIZE,
143+
.base.cra_module = THIS_MODULE,
144+
}};
145+
146+
static int num_algs;
104147

105148
static int __init crct10dif_mod_init(void)
106149
{
107-
return crypto_register_shash(&alg);
150+
/* register the arch flavor only if it differs from the generic one */
151+
num_algs = 1 + crc_t10dif_is_optimized();
152+
153+
return crypto_register_shashes(algs, num_algs);
108154
}
109155

110156
static void __exit crct10dif_mod_fini(void)
111157
{
112-
crypto_unregister_shash(&alg);
158+
crypto_unregister_shashes(algs, num_algs);
113159
}
114160

115161
subsys_initcall(crct10dif_mod_init);

0 commit comments

Comments
 (0)