blob: 3fcf6e887e87d7280a9765450b237e9933af035a [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
30static int null_compress(void *ctx, const u8 *src, unsigned int slen,
31 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
40static void null_init(void *ctx)
41{ }
42
43static void null_update(void *ctx, const u8 *data, unsigned int len)
44{ }
45
46static void null_final(void *ctx, u8 *out)
47{ }
48
49static int null_setkey(void *ctx, const u8 *key,
50 unsigned int keylen, u32 *flags)
51{ return 0; }
52
Patrick McHardyd0856002005-05-16 21:53:41 -070053static void null_crypt(void *ctx, u8 *dst, const u8 *src)
54{
55 memcpy(dst, src, NULL_BLOCK_SIZE);
56}
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58static struct crypto_alg compress_null = {
59 .cra_name = "compress_null",
60 .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
61 .cra_blocksize = NULL_BLOCK_SIZE,
62 .cra_ctxsize = 0,
63 .cra_module = THIS_MODULE,
64 .cra_list = LIST_HEAD_INIT(compress_null.cra_list),
65 .cra_u = { .compress = {
66 .coa_compress = null_compress,
Patrick McHardyd0856002005-05-16 21:53:41 -070067 .coa_decompress = null_compress } }
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
70static struct crypto_alg digest_null = {
71 .cra_name = "digest_null",
72 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
73 .cra_blocksize = NULL_BLOCK_SIZE,
74 .cra_ctxsize = 0,
75 .cra_module = THIS_MODULE,
76 .cra_list = LIST_HEAD_INIT(digest_null.cra_list),
77 .cra_u = { .digest = {
78 .dia_digestsize = NULL_DIGEST_SIZE,
79 .dia_init = null_init,
80 .dia_update = null_update,
81 .dia_final = null_final } }
82};
83
84static struct crypto_alg cipher_null = {
85 .cra_name = "cipher_null",
86 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
87 .cra_blocksize = NULL_BLOCK_SIZE,
88 .cra_ctxsize = 0,
89 .cra_module = THIS_MODULE,
90 .cra_list = LIST_HEAD_INIT(cipher_null.cra_list),
91 .cra_u = { .cipher = {
92 .cia_min_keysize = NULL_KEY_SIZE,
93 .cia_max_keysize = NULL_KEY_SIZE,
94 .cia_setkey = null_setkey,
Patrick McHardyd0856002005-05-16 21:53:41 -070095 .cia_encrypt = null_crypt,
96 .cia_decrypt = null_crypt } }
Linus Torvalds1da177e2005-04-16 15:20:36 -070097};
98
99MODULE_ALIAS("compress_null");
100MODULE_ALIAS("digest_null");
101MODULE_ALIAS("cipher_null");
102
103static int __init init(void)
104{
105 int ret = 0;
106
107 ret = crypto_register_alg(&cipher_null);
108 if (ret < 0)
109 goto out;
110
111 ret = crypto_register_alg(&digest_null);
112 if (ret < 0) {
113 crypto_unregister_alg(&cipher_null);
114 goto out;
115 }
116
117 ret = crypto_register_alg(&compress_null);
118 if (ret < 0) {
119 crypto_unregister_alg(&digest_null);
120 crypto_unregister_alg(&cipher_null);
121 goto out;
122 }
123
124out:
125 return ret;
126}
127
128static void __exit fini(void)
129{
130 crypto_unregister_alg(&compress_null);
131 crypto_unregister_alg(&digest_null);
132 crypto_unregister_alg(&cipher_null);
133}
134
135module_init(init);
136module_exit(fini);
137
138MODULE_LICENSE("GPL");
139MODULE_DESCRIPTION("Null Cryptographic Algorithms");