blob: f090c3c2745bf17ddac07652a97b2e02b2336265 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Cryptographic API.
3 *
Jan Glauberc1e26e12006-01-06 00:19:17 -08004 * s390 implementation of the DES Cipher Algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Copyright (c) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Author(s): Thomas Spatzier (tspat@de.ibm.com)
8 *
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 */
16#include <linux/init.h>
17#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/crypto.h>
Jan Glauberc1357832006-01-14 13:20:53 -080019
Jan Glauberc1e26e12006-01-06 00:19:17 -080020#include "crypt_s390.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "crypto_des.h"
22
23#define DES_BLOCK_SIZE 8
24#define DES_KEY_SIZE 8
25
26#define DES3_128_KEY_SIZE (2 * DES_KEY_SIZE)
27#define DES3_128_BLOCK_SIZE DES_BLOCK_SIZE
28
29#define DES3_192_KEY_SIZE (3 * DES_KEY_SIZE)
30#define DES3_192_BLOCK_SIZE DES_BLOCK_SIZE
31
Jan Glauberc1e26e12006-01-06 00:19:17 -080032struct crypt_s390_des_ctx {
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 u8 iv[DES_BLOCK_SIZE];
34 u8 key[DES_KEY_SIZE];
35};
36
Jan Glauberc1e26e12006-01-06 00:19:17 -080037struct crypt_s390_des3_128_ctx {
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 u8 iv[DES_BLOCK_SIZE];
39 u8 key[DES3_128_KEY_SIZE];
40};
41
Jan Glauberc1e26e12006-01-06 00:19:17 -080042struct crypt_s390_des3_192_ctx {
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 u8 iv[DES_BLOCK_SIZE];
44 u8 key[DES3_192_KEY_SIZE];
45};
46
Jan Glauberc1357832006-01-14 13:20:53 -080047static int des_setkey(void *ctx, const u8 *key, unsigned int keylen,
48 u32 *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Jan Glauberc1357832006-01-14 13:20:53 -080050 struct crypt_s390_des_ctx *dctx = ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 int ret;
52
Jan Glauberc1357832006-01-14 13:20:53 -080053 /* test if key is valid (not a weak key) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 ret = crypto_des_check_key(key, keylen, flags);
Jan Glauberc1357832006-01-14 13:20:53 -080055 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 memcpy(dctx->key, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 return ret;
58}
59
Jan Glauberc1357832006-01-14 13:20:53 -080060static void des_encrypt(void *ctx, u8 *dst, const u8 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Jan Glauberc1357832006-01-14 13:20:53 -080062 struct crypt_s390_des_ctx *dctx = ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Jan Glauberc1e26e12006-01-06 00:19:17 -080064 crypt_s390_km(KM_DEA_ENCRYPT, dctx->key, dst, src, DES_BLOCK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
Jan Glauberc1357832006-01-14 13:20:53 -080067static void des_decrypt(void *ctx, u8 *dst, const u8 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Jan Glauberc1357832006-01-14 13:20:53 -080069 struct crypt_s390_des_ctx *dctx = ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Jan Glauberc1e26e12006-01-06 00:19:17 -080071 crypt_s390_km(KM_DEA_DECRYPT, dctx->key, dst, src, DES_BLOCK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
74static struct crypto_alg des_alg = {
75 .cra_name = "des",
76 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
77 .cra_blocksize = DES_BLOCK_SIZE,
Jan Glauberc1e26e12006-01-06 00:19:17 -080078 .cra_ctxsize = sizeof(struct crypt_s390_des_ctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 .cra_module = THIS_MODULE,
80 .cra_list = LIST_HEAD_INIT(des_alg.cra_list),
Jan Glauberc1357832006-01-14 13:20:53 -080081 .cra_u = {
82 .cipher = {
83 .cia_min_keysize = DES_KEY_SIZE,
84 .cia_max_keysize = DES_KEY_SIZE,
85 .cia_setkey = des_setkey,
86 .cia_encrypt = des_encrypt,
87 .cia_decrypt = des_decrypt
88 }
89 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070090};
91
92/*
93 * RFC2451:
94 *
95 * For DES-EDE3, there is no known need to reject weak or
96 * complementation keys. Any weakness is obviated by the use of
97 * multiple keys.
98 *
99 * However, if the two independent 64-bit keys are equal,
100 * then the DES3 operation is simply the same as DES.
101 * Implementers MUST reject keys that exhibit this property.
102 *
103 */
Jan Glauberc1357832006-01-14 13:20:53 -0800104static int des3_128_setkey(void *ctx, const u8 *key, unsigned int keylen,
105 u32 *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 int i, ret;
Jan Glauberc1357832006-01-14 13:20:53 -0800108 struct crypt_s390_des3_128_ctx *dctx = ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 const u8* temp_key = key;
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
113 return -EINVAL;
114 }
Jan Glauberc1357832006-01-14 13:20:53 -0800115 for (i = 0; i < 2; i++, temp_key += DES_KEY_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
117 if (ret < 0)
118 return ret;
119 }
120 memcpy(dctx->key, key, keylen);
121 return 0;
122}
123
Jan Glauberc1357832006-01-14 13:20:53 -0800124static void des3_128_encrypt(void *ctx, u8 *dst, const u8 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Jan Glauberc1357832006-01-14 13:20:53 -0800126 struct crypt_s390_des3_128_ctx *dctx = ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Jan Glauberc1e26e12006-01-06 00:19:17 -0800128 crypt_s390_km(KM_TDEA_128_ENCRYPT, dctx->key, dst, (void*)src,
Jan Glauberc1357832006-01-14 13:20:53 -0800129 DES3_128_BLOCK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Jan Glauberc1357832006-01-14 13:20:53 -0800132static void des3_128_decrypt(void *ctx, u8 *dst, const u8 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Jan Glauberc1357832006-01-14 13:20:53 -0800134 struct crypt_s390_des3_128_ctx *dctx = ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Jan Glauberc1e26e12006-01-06 00:19:17 -0800136 crypt_s390_km(KM_TDEA_128_DECRYPT, dctx->key, dst, (void*)src,
Jan Glauberc1357832006-01-14 13:20:53 -0800137 DES3_128_BLOCK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140static struct crypto_alg des3_128_alg = {
141 .cra_name = "des3_ede128",
142 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
143 .cra_blocksize = DES3_128_BLOCK_SIZE,
Jan Glauberc1e26e12006-01-06 00:19:17 -0800144 .cra_ctxsize = sizeof(struct crypt_s390_des3_128_ctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 .cra_module = THIS_MODULE,
146 .cra_list = LIST_HEAD_INIT(des3_128_alg.cra_list),
Jan Glauberc1357832006-01-14 13:20:53 -0800147 .cra_u = {
148 .cipher = {
149 .cia_min_keysize = DES3_128_KEY_SIZE,
150 .cia_max_keysize = DES3_128_KEY_SIZE,
151 .cia_setkey = des3_128_setkey,
152 .cia_encrypt = des3_128_encrypt,
153 .cia_decrypt = des3_128_decrypt
154 }
155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156};
157
158/*
159 * RFC2451:
160 *
161 * For DES-EDE3, there is no known need to reject weak or
162 * complementation keys. Any weakness is obviated by the use of
163 * multiple keys.
164 *
165 * However, if the first two or last two independent 64-bit keys are
166 * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
167 * same as DES. Implementers MUST reject keys that exhibit this
168 * property.
169 *
170 */
Jan Glauberc1357832006-01-14 13:20:53 -0800171static int des3_192_setkey(void *ctx, const u8 *key, unsigned int keylen,
172 u32 *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 int i, ret;
Jan Glauberc1357832006-01-14 13:20:53 -0800175 struct crypt_s390_des3_192_ctx *dctx = ctx;
176 const u8* temp_key = key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
179 memcmp(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],
Jan Glauberc1357832006-01-14 13:20:53 -0800180 DES_KEY_SIZE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
183 return -EINVAL;
184 }
185 for (i = 0; i < 3; i++, temp_key += DES_KEY_SIZE) {
186 ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
Jan Glauberc1357832006-01-14 13:20:53 -0800187 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
190 memcpy(dctx->key, key, keylen);
191 return 0;
192}
193
Jan Glauberc1357832006-01-14 13:20:53 -0800194static void des3_192_encrypt(void *ctx, u8 *dst, const u8 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Jan Glauberc1357832006-01-14 13:20:53 -0800196 struct crypt_s390_des3_192_ctx *dctx = ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Jan Glauberc1e26e12006-01-06 00:19:17 -0800198 crypt_s390_km(KM_TDEA_192_ENCRYPT, dctx->key, dst, (void*)src,
Jan Glauberc1357832006-01-14 13:20:53 -0800199 DES3_192_BLOCK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Jan Glauberc1357832006-01-14 13:20:53 -0800202static void des3_192_decrypt(void *ctx, u8 *dst, const u8 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Jan Glauberc1357832006-01-14 13:20:53 -0800204 struct crypt_s390_des3_192_ctx *dctx = ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Jan Glauberc1e26e12006-01-06 00:19:17 -0800206 crypt_s390_km(KM_TDEA_192_DECRYPT, dctx->key, dst, (void*)src,
Jan Glauberc1357832006-01-14 13:20:53 -0800207 DES3_192_BLOCK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
210static struct crypto_alg des3_192_alg = {
211 .cra_name = "des3_ede",
212 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
213 .cra_blocksize = DES3_192_BLOCK_SIZE,
Jan Glauberc1e26e12006-01-06 00:19:17 -0800214 .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 .cra_module = THIS_MODULE,
216 .cra_list = LIST_HEAD_INIT(des3_192_alg.cra_list),
Jan Glauberc1357832006-01-14 13:20:53 -0800217 .cra_u = {
218 .cipher = {
219 .cia_min_keysize = DES3_192_KEY_SIZE,
220 .cia_max_keysize = DES3_192_KEY_SIZE,
221 .cia_setkey = des3_192_setkey,
222 .cia_encrypt = des3_192_encrypt,
223 .cia_decrypt = des3_192_decrypt
224 }
225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226};
227
Jan Glauberc1357832006-01-14 13:20:53 -0800228static int init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Jan Glauberc1357832006-01-14 13:20:53 -0800230 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Jan Glauberc1e26e12006-01-06 00:19:17 -0800232 if (!crypt_s390_func_available(KM_DEA_ENCRYPT) ||
233 !crypt_s390_func_available(KM_TDEA_128_ENCRYPT) ||
Jan Glauberc1357832006-01-14 13:20:53 -0800234 !crypt_s390_func_available(KM_TDEA_192_ENCRYPT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Jan Glauberc1357832006-01-14 13:20:53 -0800237 ret |= (crypto_register_alg(&des_alg) == 0) ? 0:1;
238 ret |= (crypto_register_alg(&des3_128_alg) == 0) ? 0:2;
239 ret |= (crypto_register_alg(&des3_192_alg) == 0) ? 0:4;
240 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 crypto_unregister_alg(&des3_192_alg);
242 crypto_unregister_alg(&des3_128_alg);
243 crypto_unregister_alg(&des_alg);
244 return -EEXIST;
245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return 0;
247}
248
Jan Glauberc1357832006-01-14 13:20:53 -0800249static void __exit fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 crypto_unregister_alg(&des3_192_alg);
252 crypto_unregister_alg(&des3_128_alg);
253 crypto_unregister_alg(&des_alg);
254}
255
256module_init(init);
257module_exit(fini);
258
259MODULE_ALIAS("des");
260MODULE_ALIAS("des3_ede");
261
262MODULE_LICENSE("GPL");
263MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");