Blame view

crypto/crc32c.c 2.6 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  /* 
   * Cryptographic API.
   *
   * CRC32C chksum
   *
   * This module file is a wrapper to invoke the lib/crc32c routines.
   *
   * This program is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License as published by the Free
   * Software Foundation; either version 2 of the License, or (at your option) 
   * any later version.
   *
   */
  #include <linux/init.h>
  #include <linux/module.h>
  #include <linux/string.h>
  #include <linux/crypto.h>
  #include <linux/crc32c.h>
25cdbcd9e   Herbert Xu   [CRYPTO] crc32c: ...
19
  #include <linux/kernel.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
21
22
23
24
25
  
  #define CHKSUM_BLOCK_SIZE	32
  #define CHKSUM_DIGEST_SIZE	4
  
  struct chksum_ctx {
  	u32 crc;
25cdbcd9e   Herbert Xu   [CRYPTO] crc32c: ...
26
  	u32 key;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
27
28
29
30
31
32
  };
  
  /*
   * Steps through buffer one byte at at time, calculates reflected 
   * crc using table.
   */
6c2bb98bc   Herbert Xu   [CRYPTO] all: Pas...
33
  static void chksum_init(struct crypto_tfm *tfm)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34
  {
6c2bb98bc   Herbert Xu   [CRYPTO] all: Pas...
35
  	struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
36

25cdbcd9e   Herbert Xu   [CRYPTO] crc32c: ...
37
  	mctx->crc = mctx->key;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
38
39
40
41
42
43
44
  }
  
  /*
   * Setting the seed allows arbitrary accumulators and flexible XOR policy
   * If your algorithm starts with ~0, then XOR with ~0 before you set
   * the seed.
   */
6c2bb98bc   Herbert Xu   [CRYPTO] all: Pas...
45
  static int chksum_setkey(struct crypto_tfm *tfm, const u8 *key,
560c06ae1   Herbert Xu   [CRYPTO] api: Get...
46
  			 unsigned int keylen)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
47
  {
6c2bb98bc   Herbert Xu   [CRYPTO] all: Pas...
48
  	struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
49
50
  
  	if (keylen != sizeof(mctx->crc)) {
560c06ae1   Herbert Xu   [CRYPTO] api: Get...
51
  		tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
52
53
  		return -EINVAL;
  	}
25cdbcd9e   Herbert Xu   [CRYPTO] crc32c: ...
54
  	mctx->key = le32_to_cpu(*(__le32 *)key);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
55
56
  	return 0;
  }
6c2bb98bc   Herbert Xu   [CRYPTO] all: Pas...
57
58
  static void chksum_update(struct crypto_tfm *tfm, const u8 *data,
  			  unsigned int length)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59
  {
6c2bb98bc   Herbert Xu   [CRYPTO] all: Pas...
60
  	struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
61

25cdbcd9e   Herbert Xu   [CRYPTO] crc32c: ...
62
  	mctx->crc = crc32c(mctx->crc, data, length);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
63
  }
6c2bb98bc   Herbert Xu   [CRYPTO] all: Pas...
64
  static void chksum_final(struct crypto_tfm *tfm, u8 *out)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
65
  {
6c2bb98bc   Herbert Xu   [CRYPTO] all: Pas...
66
  	struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
67
  	
25cdbcd9e   Herbert Xu   [CRYPTO] crc32c: ...
68
69
70
71
72
73
74
75
76
  	*(__le32 *)out = ~cpu_to_le32(mctx->crc);
  }
  
  static int crc32c_cra_init(struct crypto_tfm *tfm)
  {
  	struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
  
  	mctx->key = ~0;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
77
78
79
80
81
82
83
84
85
  }
  
  static struct crypto_alg alg = {
  	.cra_name	=	"crc32c",
  	.cra_flags	=	CRYPTO_ALG_TYPE_DIGEST,
  	.cra_blocksize	=	CHKSUM_BLOCK_SIZE,
  	.cra_ctxsize	=	sizeof(struct chksum_ctx),
  	.cra_module	=	THIS_MODULE,
  	.cra_list	=	LIST_HEAD_INIT(alg.cra_list),
25cdbcd9e   Herbert Xu   [CRYPTO] crc32c: ...
86
  	.cra_init	=	crc32c_cra_init,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
87
88
89
90
91
92
93
94
95
96
  	.cra_u		=	{
  		.digest = {
  			 .dia_digestsize=	CHKSUM_DIGEST_SIZE,
  			 .dia_setkey	=	chksum_setkey,
  			 .dia_init   	= 	chksum_init,
  			 .dia_update 	=	chksum_update,
  			 .dia_final  	=	chksum_final
  		 }
  	}
  };
3af5b90bd   Kamalesh Babulal   [CRYPTO] all: Cle...
97
  static int __init crc32c_mod_init(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
98
99
100
  {
  	return crypto_register_alg(&alg);
  }
3af5b90bd   Kamalesh Babulal   [CRYPTO] all: Cle...
101
  static void __exit crc32c_mod_fini(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
102
103
104
  {
  	crypto_unregister_alg(&alg);
  }
3af5b90bd   Kamalesh Babulal   [CRYPTO] all: Cle...
105
106
  module_init(crc32c_mod_init);
  module_exit(crc32c_mod_fini);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
107
108
109
110
  
  MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
  MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
  MODULE_LICENSE("GPL");