blob: e33f32b54c080392f1cf1620fdf4ff86a083b041 [file] [log] [blame]
Jan Glauberbf754ae2006-01-06 00:19:18 -08001/*
2 * Cryptographic API.
3 *
4 * s390 implementation of the AES Cipher Algorithm.
5 *
6 * s390 Version:
Jan Glauber86aa9fc2007-02-05 21:18:14 +01007 * Copyright IBM Corp. 2005,2007
Jan Glauberbf754ae2006-01-06 00:19:18 -08008 * Author(s): Jan Glauber (jang@de.ibm.com)
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +11009 * Sebastian Siewior (sebastian@breakpoint.cc> SW-Fallback
Jan Glauberbf754ae2006-01-06 00:19:18 -080010 *
Sebastian Siewiorf8246af2007-10-05 16:52:01 +080011 * Derived from "crypto/aes_generic.c"
Jan Glauberbf754ae2006-01-06 00:19:18 -080012 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
16 * any later version.
17 *
18 */
19
Sebastian Siewior89e126542007-10-17 23:18:57 +080020#include <crypto/aes.h>
Herbert Xua9e62fa2006-08-21 21:39:24 +100021#include <crypto/algapi.h>
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +110022#include <linux/err.h>
Jan Glauberbf754ae2006-01-06 00:19:18 -080023#include <linux/module.h>
24#include <linux/init.h>
Jan Glauberbf754ae2006-01-06 00:19:18 -080025#include "crypt_s390.h"
26
Jan Glauber86aa9fc2007-02-05 21:18:14 +010027#define AES_KEYLEN_128 1
28#define AES_KEYLEN_192 2
29#define AES_KEYLEN_256 4
30
31static char keylen_flag = 0;
Jan Glauberbf754ae2006-01-06 00:19:18 -080032
33struct s390_aes_ctx {
34 u8 iv[AES_BLOCK_SIZE];
35 u8 key[AES_MAX_KEY_SIZE];
Herbert Xua9e62fa2006-08-21 21:39:24 +100036 long enc;
37 long dec;
Jan Glauberbf754ae2006-01-06 00:19:18 -080038 int key_len;
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +110039 union {
40 struct crypto_blkcipher *blk;
41 struct crypto_cipher *cip;
42 } fallback;
Jan Glauberbf754ae2006-01-06 00:19:18 -080043};
44
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +110045/*
46 * Check if the key_len is supported by the HW.
47 * Returns 0 if it is, a positive number if it is not and software fallback is
48 * required or a negative number in case the key size is not valid
49 */
50static int need_fallback(unsigned int key_len)
51{
52 switch (key_len) {
53 case 16:
54 if (!(keylen_flag & AES_KEYLEN_128))
55 return 1;
56 break;
57 case 24:
58 if (!(keylen_flag & AES_KEYLEN_192))
59 return 1;
60 break;
61 case 32:
62 if (!(keylen_flag & AES_KEYLEN_256))
63 return 1;
64 break;
65 default:
66 return -1;
67 break;
68 }
69 return 0;
70}
71
72static int setkey_fallback_cip(struct crypto_tfm *tfm, const u8 *in_key,
73 unsigned int key_len)
74{
75 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
76 int ret;
77
78 sctx->fallback.blk->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
79 sctx->fallback.blk->base.crt_flags |= (tfm->crt_flags &
80 CRYPTO_TFM_REQ_MASK);
81
82 ret = crypto_cipher_setkey(sctx->fallback.cip, in_key, key_len);
83 if (ret) {
84 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
85 tfm->crt_flags |= (sctx->fallback.blk->base.crt_flags &
86 CRYPTO_TFM_RES_MASK);
87 }
88 return ret;
89}
90
Herbert Xu6c2bb982006-05-16 22:09:29 +100091static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
Herbert Xu560c06a2006-08-13 14:16:39 +100092 unsigned int key_len)
Jan Glauberbf754ae2006-01-06 00:19:18 -080093{
Herbert Xu6c2bb982006-05-16 22:09:29 +100094 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
Herbert Xu560c06a2006-08-13 14:16:39 +100095 u32 *flags = &tfm->crt_flags;
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +110096 int ret;
Jan Glauberbf754ae2006-01-06 00:19:18 -080097
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +110098 ret = need_fallback(key_len);
99 if (ret < 0) {
100 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
101 return -EINVAL;
Jan Glauberbf754ae2006-01-06 00:19:18 -0800102 }
103
104 sctx->key_len = key_len;
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100105 if (!ret) {
106 memcpy(sctx->key, in_key, key_len);
107 return 0;
108 }
109
110 return setkey_fallback_cip(tfm, in_key, key_len);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800111}
112
Herbert Xu6c2bb982006-05-16 22:09:29 +1000113static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
Jan Glauberbf754ae2006-01-06 00:19:18 -0800114{
Herbert Xu6c2bb982006-05-16 22:09:29 +1000115 const struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800116
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100117 if (unlikely(need_fallback(sctx->key_len))) {
118 crypto_cipher_encrypt_one(sctx->fallback.cip, out, in);
119 return;
120 }
121
Jan Glauberbf754ae2006-01-06 00:19:18 -0800122 switch (sctx->key_len) {
123 case 16:
124 crypt_s390_km(KM_AES_128_ENCRYPT, &sctx->key, out, in,
125 AES_BLOCK_SIZE);
126 break;
127 case 24:
128 crypt_s390_km(KM_AES_192_ENCRYPT, &sctx->key, out, in,
129 AES_BLOCK_SIZE);
130 break;
131 case 32:
132 crypt_s390_km(KM_AES_256_ENCRYPT, &sctx->key, out, in,
133 AES_BLOCK_SIZE);
134 break;
135 }
136}
137
Herbert Xu6c2bb982006-05-16 22:09:29 +1000138static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
Jan Glauberbf754ae2006-01-06 00:19:18 -0800139{
Herbert Xu6c2bb982006-05-16 22:09:29 +1000140 const struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800141
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100142 if (unlikely(need_fallback(sctx->key_len))) {
143 crypto_cipher_decrypt_one(sctx->fallback.cip, out, in);
144 return;
145 }
146
Jan Glauberbf754ae2006-01-06 00:19:18 -0800147 switch (sctx->key_len) {
148 case 16:
149 crypt_s390_km(KM_AES_128_DECRYPT, &sctx->key, out, in,
150 AES_BLOCK_SIZE);
151 break;
152 case 24:
153 crypt_s390_km(KM_AES_192_DECRYPT, &sctx->key, out, in,
154 AES_BLOCK_SIZE);
155 break;
156 case 32:
157 crypt_s390_km(KM_AES_256_DECRYPT, &sctx->key, out, in,
158 AES_BLOCK_SIZE);
159 break;
160 }
161}
162
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100163static int fallback_init_cip(struct crypto_tfm *tfm)
164{
165 const char *name = tfm->__crt_alg->cra_name;
166 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
167
168 sctx->fallback.cip = crypto_alloc_cipher(name, 0,
169 CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
170
171 if (IS_ERR(sctx->fallback.cip)) {
172 printk(KERN_ERR "Error allocating fallback algo %s\n", name);
173 return PTR_ERR(sctx->fallback.blk);
174 }
175
176 return 0;
177}
178
179static void fallback_exit_cip(struct crypto_tfm *tfm)
180{
181 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
182
183 crypto_free_cipher(sctx->fallback.cip);
184 sctx->fallback.cip = NULL;
185}
Jan Glauberbf754ae2006-01-06 00:19:18 -0800186
187static struct crypto_alg aes_alg = {
188 .cra_name = "aes",
Herbert Xu65b75c32006-08-21 21:18:50 +1000189 .cra_driver_name = "aes-s390",
190 .cra_priority = CRYPT_S390_PRIORITY,
Jan Glauberf67d1362007-05-04 18:47:47 +0200191 .cra_flags = CRYPTO_ALG_TYPE_CIPHER |
192 CRYPTO_ALG_NEED_FALLBACK,
Jan Glauberbf754ae2006-01-06 00:19:18 -0800193 .cra_blocksize = AES_BLOCK_SIZE,
194 .cra_ctxsize = sizeof(struct s390_aes_ctx),
195 .cra_module = THIS_MODULE,
196 .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100197 .cra_init = fallback_init_cip,
198 .cra_exit = fallback_exit_cip,
Jan Glauberbf754ae2006-01-06 00:19:18 -0800199 .cra_u = {
200 .cipher = {
201 .cia_min_keysize = AES_MIN_KEY_SIZE,
202 .cia_max_keysize = AES_MAX_KEY_SIZE,
203 .cia_setkey = aes_set_key,
204 .cia_encrypt = aes_encrypt,
205 .cia_decrypt = aes_decrypt,
Jan Glauberbf754ae2006-01-06 00:19:18 -0800206 }
207 }
208};
209
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100210static int setkey_fallback_blk(struct crypto_tfm *tfm, const u8 *key,
211 unsigned int len)
212{
213 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
214 unsigned int ret;
215
216 sctx->fallback.blk->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
217 sctx->fallback.blk->base.crt_flags |= (tfm->crt_flags &
218 CRYPTO_TFM_REQ_MASK);
219
220 ret = crypto_blkcipher_setkey(sctx->fallback.blk, key, len);
221 if (ret) {
222 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
223 tfm->crt_flags |= (sctx->fallback.blk->base.crt_flags &
224 CRYPTO_TFM_RES_MASK);
225 }
226 return ret;
227}
228
229static int fallback_blk_dec(struct blkcipher_desc *desc,
230 struct scatterlist *dst, struct scatterlist *src,
231 unsigned int nbytes)
232{
233 unsigned int ret;
234 struct crypto_blkcipher *tfm;
235 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
236
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100237 tfm = desc->tfm;
238 desc->tfm = sctx->fallback.blk;
239
Sebastian Siewior2d74d402007-12-10 15:49:41 +0800240 ret = crypto_blkcipher_decrypt_iv(desc, dst, src, nbytes);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100241
242 desc->tfm = tfm;
243 return ret;
244}
245
246static int fallback_blk_enc(struct blkcipher_desc *desc,
247 struct scatterlist *dst, struct scatterlist *src,
248 unsigned int nbytes)
249{
250 unsigned int ret;
251 struct crypto_blkcipher *tfm;
252 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
253
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100254 tfm = desc->tfm;
255 desc->tfm = sctx->fallback.blk;
256
Sebastian Siewior2d74d402007-12-10 15:49:41 +0800257 ret = crypto_blkcipher_encrypt_iv(desc, dst, src, nbytes);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100258
259 desc->tfm = tfm;
260 return ret;
261}
262
Herbert Xua9e62fa2006-08-21 21:39:24 +1000263static int ecb_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
264 unsigned int key_len)
265{
266 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100267 int ret;
268
269 ret = need_fallback(key_len);
270 if (ret > 0) {
271 sctx->key_len = key_len;
272 return setkey_fallback_blk(tfm, in_key, key_len);
273 }
Herbert Xua9e62fa2006-08-21 21:39:24 +1000274
275 switch (key_len) {
276 case 16:
277 sctx->enc = KM_AES_128_ENCRYPT;
278 sctx->dec = KM_AES_128_DECRYPT;
279 break;
280 case 24:
281 sctx->enc = KM_AES_192_ENCRYPT;
282 sctx->dec = KM_AES_192_DECRYPT;
283 break;
284 case 32:
285 sctx->enc = KM_AES_256_ENCRYPT;
286 sctx->dec = KM_AES_256_DECRYPT;
287 break;
288 }
289
290 return aes_set_key(tfm, in_key, key_len);
291}
292
293static int ecb_aes_crypt(struct blkcipher_desc *desc, long func, void *param,
294 struct blkcipher_walk *walk)
295{
296 int ret = blkcipher_walk_virt(desc, walk);
297 unsigned int nbytes;
298
299 while ((nbytes = walk->nbytes)) {
300 /* only use complete blocks */
301 unsigned int n = nbytes & ~(AES_BLOCK_SIZE - 1);
302 u8 *out = walk->dst.virt.addr;
303 u8 *in = walk->src.virt.addr;
304
305 ret = crypt_s390_km(func, param, out, in, n);
306 BUG_ON((ret < 0) || (ret != n));
307
308 nbytes &= AES_BLOCK_SIZE - 1;
309 ret = blkcipher_walk_done(desc, walk, nbytes);
310 }
311
312 return ret;
313}
314
315static int ecb_aes_encrypt(struct blkcipher_desc *desc,
316 struct scatterlist *dst, struct scatterlist *src,
317 unsigned int nbytes)
318{
319 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
320 struct blkcipher_walk walk;
321
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100322 if (unlikely(need_fallback(sctx->key_len)))
323 return fallback_blk_enc(desc, dst, src, nbytes);
324
Herbert Xua9e62fa2006-08-21 21:39:24 +1000325 blkcipher_walk_init(&walk, dst, src, nbytes);
326 return ecb_aes_crypt(desc, sctx->enc, sctx->key, &walk);
327}
328
329static int ecb_aes_decrypt(struct blkcipher_desc *desc,
330 struct scatterlist *dst, struct scatterlist *src,
331 unsigned int nbytes)
332{
333 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
334 struct blkcipher_walk walk;
335
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100336 if (unlikely(need_fallback(sctx->key_len)))
337 return fallback_blk_dec(desc, dst, src, nbytes);
338
Herbert Xua9e62fa2006-08-21 21:39:24 +1000339 blkcipher_walk_init(&walk, dst, src, nbytes);
340 return ecb_aes_crypt(desc, sctx->dec, sctx->key, &walk);
341}
342
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100343static int fallback_init_blk(struct crypto_tfm *tfm)
344{
345 const char *name = tfm->__crt_alg->cra_name;
346 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
347
348 sctx->fallback.blk = crypto_alloc_blkcipher(name, 0,
349 CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
350
351 if (IS_ERR(sctx->fallback.blk)) {
352 printk(KERN_ERR "Error allocating fallback algo %s\n", name);
353 return PTR_ERR(sctx->fallback.blk);
354 }
355
356 return 0;
357}
358
359static void fallback_exit_blk(struct crypto_tfm *tfm)
360{
361 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
362
363 crypto_free_blkcipher(sctx->fallback.blk);
364 sctx->fallback.blk = NULL;
365}
366
Herbert Xua9e62fa2006-08-21 21:39:24 +1000367static struct crypto_alg ecb_aes_alg = {
368 .cra_name = "ecb(aes)",
369 .cra_driver_name = "ecb-aes-s390",
370 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
Jan Glauberf67d1362007-05-04 18:47:47 +0200371 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
372 CRYPTO_ALG_NEED_FALLBACK,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000373 .cra_blocksize = AES_BLOCK_SIZE,
374 .cra_ctxsize = sizeof(struct s390_aes_ctx),
375 .cra_type = &crypto_blkcipher_type,
376 .cra_module = THIS_MODULE,
377 .cra_list = LIST_HEAD_INIT(ecb_aes_alg.cra_list),
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100378 .cra_init = fallback_init_blk,
379 .cra_exit = fallback_exit_blk,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000380 .cra_u = {
381 .blkcipher = {
382 .min_keysize = AES_MIN_KEY_SIZE,
383 .max_keysize = AES_MAX_KEY_SIZE,
384 .setkey = ecb_aes_set_key,
385 .encrypt = ecb_aes_encrypt,
386 .decrypt = ecb_aes_decrypt,
387 }
388 }
389};
390
391static int cbc_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
392 unsigned int key_len)
393{
394 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100395 int ret;
396
397 ret = need_fallback(key_len);
398 if (ret > 0) {
399 sctx->key_len = key_len;
400 return setkey_fallback_blk(tfm, in_key, key_len);
401 }
Herbert Xua9e62fa2006-08-21 21:39:24 +1000402
403 switch (key_len) {
404 case 16:
405 sctx->enc = KMC_AES_128_ENCRYPT;
406 sctx->dec = KMC_AES_128_DECRYPT;
407 break;
408 case 24:
409 sctx->enc = KMC_AES_192_ENCRYPT;
410 sctx->dec = KMC_AES_192_DECRYPT;
411 break;
412 case 32:
413 sctx->enc = KMC_AES_256_ENCRYPT;
414 sctx->dec = KMC_AES_256_DECRYPT;
415 break;
416 }
417
418 return aes_set_key(tfm, in_key, key_len);
419}
420
421static int cbc_aes_crypt(struct blkcipher_desc *desc, long func, void *param,
422 struct blkcipher_walk *walk)
423{
424 int ret = blkcipher_walk_virt(desc, walk);
425 unsigned int nbytes = walk->nbytes;
426
427 if (!nbytes)
428 goto out;
429
430 memcpy(param, walk->iv, AES_BLOCK_SIZE);
431 do {
432 /* only use complete blocks */
433 unsigned int n = nbytes & ~(AES_BLOCK_SIZE - 1);
434 u8 *out = walk->dst.virt.addr;
435 u8 *in = walk->src.virt.addr;
436
437 ret = crypt_s390_kmc(func, param, out, in, n);
438 BUG_ON((ret < 0) || (ret != n));
439
440 nbytes &= AES_BLOCK_SIZE - 1;
441 ret = blkcipher_walk_done(desc, walk, nbytes);
442 } while ((nbytes = walk->nbytes));
443 memcpy(walk->iv, param, AES_BLOCK_SIZE);
444
445out:
446 return ret;
447}
448
449static int cbc_aes_encrypt(struct blkcipher_desc *desc,
450 struct scatterlist *dst, struct scatterlist *src,
451 unsigned int nbytes)
452{
453 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
454 struct blkcipher_walk walk;
455
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100456 if (unlikely(need_fallback(sctx->key_len)))
457 return fallback_blk_enc(desc, dst, src, nbytes);
458
Herbert Xua9e62fa2006-08-21 21:39:24 +1000459 blkcipher_walk_init(&walk, dst, src, nbytes);
460 return cbc_aes_crypt(desc, sctx->enc, sctx->iv, &walk);
461}
462
463static int cbc_aes_decrypt(struct blkcipher_desc *desc,
464 struct scatterlist *dst, struct scatterlist *src,
465 unsigned int nbytes)
466{
467 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
468 struct blkcipher_walk walk;
469
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100470 if (unlikely(need_fallback(sctx->key_len)))
471 return fallback_blk_dec(desc, dst, src, nbytes);
472
Herbert Xua9e62fa2006-08-21 21:39:24 +1000473 blkcipher_walk_init(&walk, dst, src, nbytes);
474 return cbc_aes_crypt(desc, sctx->dec, sctx->iv, &walk);
475}
476
477static struct crypto_alg cbc_aes_alg = {
478 .cra_name = "cbc(aes)",
479 .cra_driver_name = "cbc-aes-s390",
480 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
Jan Glauberf67d1362007-05-04 18:47:47 +0200481 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
482 CRYPTO_ALG_NEED_FALLBACK,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000483 .cra_blocksize = AES_BLOCK_SIZE,
484 .cra_ctxsize = sizeof(struct s390_aes_ctx),
485 .cra_type = &crypto_blkcipher_type,
486 .cra_module = THIS_MODULE,
487 .cra_list = LIST_HEAD_INIT(cbc_aes_alg.cra_list),
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100488 .cra_init = fallback_init_blk,
489 .cra_exit = fallback_exit_blk,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000490 .cra_u = {
491 .blkcipher = {
492 .min_keysize = AES_MIN_KEY_SIZE,
493 .max_keysize = AES_MAX_KEY_SIZE,
494 .ivsize = AES_BLOCK_SIZE,
495 .setkey = cbc_aes_set_key,
496 .encrypt = cbc_aes_encrypt,
497 .decrypt = cbc_aes_decrypt,
498 }
499 }
500};
501
Heiko Carstens9f7819c2008-04-17 07:46:17 +0200502static int __init aes_s390_init(void)
Jan Glauberbf754ae2006-01-06 00:19:18 -0800503{
504 int ret;
505
506 if (crypt_s390_func_available(KM_AES_128_ENCRYPT))
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100507 keylen_flag |= AES_KEYLEN_128;
Jan Glauberbf754ae2006-01-06 00:19:18 -0800508 if (crypt_s390_func_available(KM_AES_192_ENCRYPT))
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100509 keylen_flag |= AES_KEYLEN_192;
Jan Glauberbf754ae2006-01-06 00:19:18 -0800510 if (crypt_s390_func_available(KM_AES_256_ENCRYPT))
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100511 keylen_flag |= AES_KEYLEN_256;
Jan Glauberbf754ae2006-01-06 00:19:18 -0800512
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100513 if (!keylen_flag)
514 return -EOPNOTSUPP;
515
516 /* z9 109 and z9 BC/EC only support 128 bit key length */
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100517 if (keylen_flag == AES_KEYLEN_128)
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100518 printk(KERN_INFO
Joe Perchescfe73812008-01-26 14:11:09 +0100519 "aes_s390: hardware acceleration only available for "
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100520 "128 bit keys\n");
Jan Glauberbf754ae2006-01-06 00:19:18 -0800521
522 ret = crypto_register_alg(&aes_alg);
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100523 if (ret)
Herbert Xua9e62fa2006-08-21 21:39:24 +1000524 goto aes_err;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000525
526 ret = crypto_register_alg(&ecb_aes_alg);
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100527 if (ret)
Herbert Xua9e62fa2006-08-21 21:39:24 +1000528 goto ecb_aes_err;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000529
530 ret = crypto_register_alg(&cbc_aes_alg);
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100531 if (ret)
Herbert Xua9e62fa2006-08-21 21:39:24 +1000532 goto cbc_aes_err;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000533
534out:
Jan Glauberbf754ae2006-01-06 00:19:18 -0800535 return ret;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000536
537cbc_aes_err:
538 crypto_unregister_alg(&ecb_aes_alg);
539ecb_aes_err:
540 crypto_unregister_alg(&aes_alg);
541aes_err:
542 goto out;
Jan Glauberbf754ae2006-01-06 00:19:18 -0800543}
544
Heiko Carstens9f7819c2008-04-17 07:46:17 +0200545static void __exit aes_s390_fini(void)
Jan Glauberbf754ae2006-01-06 00:19:18 -0800546{
Herbert Xua9e62fa2006-08-21 21:39:24 +1000547 crypto_unregister_alg(&cbc_aes_alg);
548 crypto_unregister_alg(&ecb_aes_alg);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800549 crypto_unregister_alg(&aes_alg);
550}
551
Heiko Carstens9f7819c2008-04-17 07:46:17 +0200552module_init(aes_s390_init);
553module_exit(aes_s390_fini);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800554
555MODULE_ALIAS("aes");
556
557MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
558MODULE_LICENSE("GPL");