4 * s390 implementation of the AES Cipher Algorithm.
7 * Copyright (C) 2005 IBM Deutschland GmbH, IBM Corporation
8 * Author(s): Jan Glauber (jang@de.ibm.com)
10 * Derived from "crypto/aes.c"
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your option)
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/crypto.h>
22 #include "crypt_s390.h"
24 #define AES_MIN_KEY_SIZE 16
25 #define AES_MAX_KEY_SIZE 32
27 /* data block size for all key lengths */
28 #define AES_BLOCK_SIZE 16
35 u8 iv[AES_BLOCK_SIZE];
36 u8 key[AES_MAX_KEY_SIZE];
40 static int aes_set_key(void *ctx, const u8 *in_key, unsigned int key_len,
43 struct s390_aes_ctx *sctx = ctx;
60 /* invalid key length */
65 sctx->key_len = key_len;
66 memcpy(sctx->key, in_key, key_len);
69 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
73 static void aes_encrypt(void *ctx, u8 *out, const u8 *in)
75 const struct s390_aes_ctx *sctx = ctx;
77 switch (sctx->key_len) {
79 crypt_s390_km(KM_AES_128_ENCRYPT, &sctx->key, out, in,
83 crypt_s390_km(KM_AES_192_ENCRYPT, &sctx->key, out, in,
87 crypt_s390_km(KM_AES_256_ENCRYPT, &sctx->key, out, in,
93 static void aes_decrypt(void *ctx, u8 *out, const u8 *in)
95 const struct s390_aes_ctx *sctx = ctx;
97 switch (sctx->key_len) {
99 crypt_s390_km(KM_AES_128_DECRYPT, &sctx->key, out, in,
103 crypt_s390_km(KM_AES_192_DECRYPT, &sctx->key, out, in,
107 crypt_s390_km(KM_AES_256_DECRYPT, &sctx->key, out, in,
113 static unsigned int aes_encrypt_ecb(const struct cipher_desc *desc, u8 *out,
114 const u8 *in, unsigned int nbytes)
116 struct s390_aes_ctx *sctx = crypto_tfm_ctx(desc->tfm);
119 /* only use complete blocks */
120 nbytes &= ~(AES_BLOCK_SIZE - 1);
122 switch (sctx->key_len) {
124 ret = crypt_s390_km(KM_AES_128_ENCRYPT, &sctx->key, out, in, nbytes);
125 BUG_ON((ret < 0) || (ret != nbytes));
128 ret = crypt_s390_km(KM_AES_192_ENCRYPT, &sctx->key, out, in, nbytes);
129 BUG_ON((ret < 0) || (ret != nbytes));
132 ret = crypt_s390_km(KM_AES_256_ENCRYPT, &sctx->key, out, in, nbytes);
133 BUG_ON((ret < 0) || (ret != nbytes));
139 static unsigned int aes_decrypt_ecb(const struct cipher_desc *desc, u8 *out,
140 const u8 *in, unsigned int nbytes)
142 struct s390_aes_ctx *sctx = crypto_tfm_ctx(desc->tfm);
145 /* only use complete blocks */
146 nbytes &= ~(AES_BLOCK_SIZE - 1);
148 switch (sctx->key_len) {
150 ret = crypt_s390_km(KM_AES_128_DECRYPT, &sctx->key, out, in, nbytes);
151 BUG_ON((ret < 0) || (ret != nbytes));
154 ret = crypt_s390_km(KM_AES_192_DECRYPT, &sctx->key, out, in, nbytes);
155 BUG_ON((ret < 0) || (ret != nbytes));
158 ret = crypt_s390_km(KM_AES_256_DECRYPT, &sctx->key, out, in, nbytes);
159 BUG_ON((ret < 0) || (ret != nbytes));
165 static unsigned int aes_encrypt_cbc(const struct cipher_desc *desc, u8 *out,
166 const u8 *in, unsigned int nbytes)
168 struct s390_aes_ctx *sctx = crypto_tfm_ctx(desc->tfm);
171 /* only use complete blocks */
172 nbytes &= ~(AES_BLOCK_SIZE - 1);
174 memcpy(&sctx->iv, desc->info, AES_BLOCK_SIZE);
175 switch (sctx->key_len) {
177 ret = crypt_s390_kmc(KMC_AES_128_ENCRYPT, &sctx->iv, out, in, nbytes);
178 BUG_ON((ret < 0) || (ret != nbytes));
181 ret = crypt_s390_kmc(KMC_AES_192_ENCRYPT, &sctx->iv, out, in, nbytes);
182 BUG_ON((ret < 0) || (ret != nbytes));
185 ret = crypt_s390_kmc(KMC_AES_256_ENCRYPT, &sctx->iv, out, in, nbytes);
186 BUG_ON((ret < 0) || (ret != nbytes));
189 memcpy(desc->info, &sctx->iv, AES_BLOCK_SIZE);
194 static unsigned int aes_decrypt_cbc(const struct cipher_desc *desc, u8 *out,
195 const u8 *in, unsigned int nbytes)
197 struct s390_aes_ctx *sctx = crypto_tfm_ctx(desc->tfm);
200 /* only use complete blocks */
201 nbytes &= ~(AES_BLOCK_SIZE - 1);
203 memcpy(&sctx->iv, desc->info, AES_BLOCK_SIZE);
204 switch (sctx->key_len) {
206 ret = crypt_s390_kmc(KMC_AES_128_DECRYPT, &sctx->iv, out, in, nbytes);
207 BUG_ON((ret < 0) || (ret != nbytes));
210 ret = crypt_s390_kmc(KMC_AES_192_DECRYPT, &sctx->iv, out, in, nbytes);
211 BUG_ON((ret < 0) || (ret != nbytes));
214 ret = crypt_s390_kmc(KMC_AES_256_DECRYPT, &sctx->iv, out, in, nbytes);
215 BUG_ON((ret < 0) || (ret != nbytes));
222 static struct crypto_alg aes_alg = {
224 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
225 .cra_blocksize = AES_BLOCK_SIZE,
226 .cra_ctxsize = sizeof(struct s390_aes_ctx),
227 .cra_module = THIS_MODULE,
228 .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
231 .cia_min_keysize = AES_MIN_KEY_SIZE,
232 .cia_max_keysize = AES_MAX_KEY_SIZE,
233 .cia_setkey = aes_set_key,
234 .cia_encrypt = aes_encrypt,
235 .cia_decrypt = aes_decrypt,
236 .cia_encrypt_ecb = aes_encrypt_ecb,
237 .cia_decrypt_ecb = aes_decrypt_ecb,
238 .cia_encrypt_cbc = aes_encrypt_cbc,
239 .cia_decrypt_cbc = aes_decrypt_cbc,
244 static int __init aes_init(void)
248 if (crypt_s390_func_available(KM_AES_128_ENCRYPT))
250 if (crypt_s390_func_available(KM_AES_192_ENCRYPT))
252 if (crypt_s390_func_available(KM_AES_256_ENCRYPT))
255 if (!has_aes_128 && !has_aes_192 && !has_aes_256)
258 ret = crypto_register_alg(&aes_alg);
260 printk(KERN_INFO "crypt_s390: aes_s390 couldn't be loaded.\n");
264 static void __exit aes_fini(void)
266 crypto_unregister_alg(&aes_alg);
269 module_init(aes_init);
270 module_exit(aes_fini);
274 MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
275 MODULE_LICENSE("GPL");