6 * This module file is a wrapper to invoke the lib/crc32c routines.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/string.h>
17 #include <linux/crypto.h>
18 #include <linux/crc32c.h>
19 #include <linux/kernel.h>
21 #define CHKSUM_BLOCK_SIZE 32
22 #define CHKSUM_DIGEST_SIZE 4
30 * Steps through buffer one byte at at time, calculates reflected
34 static void chksum_init(struct crypto_tfm
*tfm
)
36 struct chksum_ctx
*mctx
= crypto_tfm_ctx(tfm
);
38 mctx
->crc
= mctx
->key
;
42 * Setting the seed allows arbitrary accumulators and flexible XOR policy
43 * If your algorithm starts with ~0, then XOR with ~0 before you set
46 static int chksum_setkey(struct crypto_tfm
*tfm
, const u8
*key
,
49 struct chksum_ctx
*mctx
= crypto_tfm_ctx(tfm
);
51 if (keylen
!= sizeof(mctx
->crc
)) {
52 tfm
->crt_flags
|= CRYPTO_TFM_RES_BAD_KEY_LEN
;
55 mctx
->key
= le32_to_cpu(*(__le32
*)key
);
59 static void chksum_update(struct crypto_tfm
*tfm
, const u8
*data
,
62 struct chksum_ctx
*mctx
= crypto_tfm_ctx(tfm
);
64 mctx
->crc
= crc32c(mctx
->crc
, data
, length
);
67 static void chksum_final(struct crypto_tfm
*tfm
, u8
*out
)
69 struct chksum_ctx
*mctx
= crypto_tfm_ctx(tfm
);
71 *(__le32
*)out
= ~cpu_to_le32(mctx
->crc
);
74 static int crc32c_cra_init(struct crypto_tfm
*tfm
)
76 struct chksum_ctx
*mctx
= crypto_tfm_ctx(tfm
);
82 static struct crypto_alg alg
= {
84 .cra_flags
= CRYPTO_ALG_TYPE_DIGEST
,
85 .cra_blocksize
= CHKSUM_BLOCK_SIZE
,
86 .cra_ctxsize
= sizeof(struct chksum_ctx
),
87 .cra_module
= THIS_MODULE
,
88 .cra_list
= LIST_HEAD_INIT(alg
.cra_list
),
89 .cra_init
= crc32c_cra_init
,
92 .dia_digestsize
= CHKSUM_DIGEST_SIZE
,
93 .dia_setkey
= chksum_setkey
,
94 .dia_init
= chksum_init
,
95 .dia_update
= chksum_update
,
96 .dia_final
= chksum_final
101 static int __init
init(void)
103 return crypto_register_alg(&alg
);
106 static void __exit
fini(void)
108 crypto_unregister_alg(&alg
);
114 MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
115 MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
116 MODULE_LICENSE("GPL");