blob: a0d956b529498c84ea39f2888e63878d143adc6c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Cryptographic API.
3 *
4 * Null algorithms, aka Much Ado About Nothing.
5 *
6 * These are needed for IPsec, and may be useful in general for
7 * testing & debugging.
8 *
9 * The null cipher is compliant with RFC2410.
10 *
11 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 */
19#include <linux/init.h>
20#include <linux/module.h>
21#include <linux/mm.h>
22#include <asm/scatterlist.h>
23#include <linux/crypto.h>
Patrick McHardyd0856002005-05-16 21:53:41 -070024#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#define NULL_KEY_SIZE 0
27#define NULL_BLOCK_SIZE 1
28#define NULL_DIGEST_SIZE 0
29
Herbert Xu6c2bb982006-05-16 22:09:29 +100030static int null_compress(struct crypto_tfm *tfm, const u8 *src,
31 unsigned int slen, u8 *dst, unsigned int *dlen)
Patrick McHardyd0856002005-05-16 21:53:41 -070032{
33 if (slen > *dlen)
34 return -EINVAL;
35 memcpy(dst, src, slen);
36 *dlen = slen;
37 return 0;
38}
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Herbert Xu6c2bb982006-05-16 22:09:29 +100040static void null_init(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{ }
42
Herbert Xu6c2bb982006-05-16 22:09:29 +100043static void null_update(struct crypto_tfm *tfm, const u8 *data,
44 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{ }
46
Herbert Xu6c2bb982006-05-16 22:09:29 +100047static void null_final(struct crypto_tfm *tfm, u8 *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{ }
49
Herbert Xu6c2bb982006-05-16 22:09:29 +100050static int null_setkey(struct crypto_tfm *tfm, const u8 *key,
51 unsigned int keylen, u32 *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{ return 0; }
53
Herbert Xu6c2bb982006-05-16 22:09:29 +100054static void null_crypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
Patrick McHardyd0856002005-05-16 21:53:41 -070055{
56 memcpy(dst, src, NULL_BLOCK_SIZE);
57}
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59static struct crypto_alg compress_null = {
60 .cra_name = "compress_null",
61 .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
62 .cra_blocksize = NULL_BLOCK_SIZE,
63 .cra_ctxsize = 0,
64 .cra_module = THIS_MODULE,
65 .cra_list = LIST_HEAD_INIT(compress_null.cra_list),
66 .cra_u = { .compress = {
67 .coa_compress = null_compress,
Patrick McHardyd0856002005-05-16 21:53:41 -070068 .coa_decompress = null_compress } }
Linus Torvalds1da177e2005-04-16 15:20:36 -070069};
70
71static struct crypto_alg digest_null = {
72 .cra_name = "digest_null",
73 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
74 .cra_blocksize = NULL_BLOCK_SIZE,
75 .cra_ctxsize = 0,
76 .cra_module = THIS_MODULE,
77 .cra_list = LIST_HEAD_INIT(digest_null.cra_list),
78 .cra_u = { .digest = {
79 .dia_digestsize = NULL_DIGEST_SIZE,
80 .dia_init = null_init,
81 .dia_update = null_update,
82 .dia_final = null_final } }
83};
84
85static struct crypto_alg cipher_null = {
86 .cra_name = "cipher_null",
87 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
88 .cra_blocksize = NULL_BLOCK_SIZE,
89 .cra_ctxsize = 0,
90 .cra_module = THIS_MODULE,
91 .cra_list = LIST_HEAD_INIT(cipher_null.cra_list),
92 .cra_u = { .cipher = {
93 .cia_min_keysize = NULL_KEY_SIZE,
94 .cia_max_keysize = NULL_KEY_SIZE,
95 .cia_setkey = null_setkey,
Patrick McHardyd0856002005-05-16 21:53:41 -070096 .cia_encrypt = null_crypt,
97 .cia_decrypt = null_crypt } }
Linus Torvalds1da177e2005-04-16 15:20:36 -070098};
99
100MODULE_ALIAS("compress_null");
101MODULE_ALIAS("digest_null");
102MODULE_ALIAS("cipher_null");
103
104static int __init init(void)
105{
106 int ret = 0;
107
108 ret = crypto_register_alg(&cipher_null);
109 if (ret < 0)
110 goto out;
111
112 ret = crypto_register_alg(&digest_null);
113 if (ret < 0) {
114 crypto_unregister_alg(&cipher_null);
115 goto out;
116 }
117
118 ret = crypto_register_alg(&compress_null);
119 if (ret < 0) {
120 crypto_unregister_alg(&digest_null);
121 crypto_unregister_alg(&cipher_null);
122 goto out;
123 }
124
125out:
126 return ret;
127}
128
129static void __exit fini(void)
130{
131 crypto_unregister_alg(&compress_null);
132 crypto_unregister_alg(&digest_null);
133 crypto_unregister_alg(&cipher_null);
134}
135
136module_init(init);
137module_exit(fini);
138
139MODULE_LICENSE("GPL");
140MODULE_DESCRIPTION("Null Cryptographic Algorithms");