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);
118 switch (sctx->key_len) {
120 crypt_s390_km(KM_AES_128_ENCRYPT, &sctx->key, out, in, nbytes);
123 crypt_s390_km(KM_AES_192_ENCRYPT, &sctx->key, out, in, nbytes);
126 crypt_s390_km(KM_AES_256_ENCRYPT, &sctx->key, out, in, nbytes);
129 return nbytes & ~(AES_BLOCK_SIZE - 1);
132 static unsigned int aes_decrypt_ecb(const struct cipher_desc *desc, u8 *out,
133 const u8 *in, unsigned int nbytes)
135 struct s390_aes_ctx *sctx = crypto_tfm_ctx(desc->tfm);
137 switch (sctx->key_len) {
139 crypt_s390_km(KM_AES_128_DECRYPT, &sctx->key, out, in, nbytes);
142 crypt_s390_km(KM_AES_192_DECRYPT, &sctx->key, out, in, nbytes);
145 crypt_s390_km(KM_AES_256_DECRYPT, &sctx->key, out, in, nbytes);
148 return nbytes & ~(AES_BLOCK_SIZE - 1);
151 static unsigned int aes_encrypt_cbc(const struct cipher_desc *desc, u8 *out,
152 const u8 *in, unsigned int nbytes)
154 struct s390_aes_ctx *sctx = crypto_tfm_ctx(desc->tfm);
156 memcpy(&sctx->iv, desc->info, AES_BLOCK_SIZE);
157 switch (sctx->key_len) {
159 crypt_s390_kmc(KMC_AES_128_ENCRYPT, &sctx->iv, out, in, nbytes);
162 crypt_s390_kmc(KMC_AES_192_ENCRYPT, &sctx->iv, out, in, nbytes);
165 crypt_s390_kmc(KMC_AES_256_ENCRYPT, &sctx->iv, out, in, nbytes);
168 memcpy(desc->info, &sctx->iv, AES_BLOCK_SIZE);
170 return nbytes & ~(AES_BLOCK_SIZE - 1);
173 static unsigned int aes_decrypt_cbc(const struct cipher_desc *desc, u8 *out,
174 const u8 *in, unsigned int nbytes)
176 struct s390_aes_ctx *sctx = crypto_tfm_ctx(desc->tfm);
178 memcpy(&sctx->iv, desc->info, AES_BLOCK_SIZE);
179 switch (sctx->key_len) {
181 crypt_s390_kmc(KMC_AES_128_DECRYPT, &sctx->iv, out, in, nbytes);
184 crypt_s390_kmc(KMC_AES_192_DECRYPT, &sctx->iv, out, in, nbytes);
187 crypt_s390_kmc(KMC_AES_256_DECRYPT, &sctx->iv, out, in, nbytes);
190 return nbytes & ~(AES_BLOCK_SIZE - 1);
194 static struct crypto_alg aes_alg = {
196 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
197 .cra_blocksize = AES_BLOCK_SIZE,
198 .cra_ctxsize = sizeof(struct s390_aes_ctx),
199 .cra_module = THIS_MODULE,
200 .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
203 .cia_min_keysize = AES_MIN_KEY_SIZE,
204 .cia_max_keysize = AES_MAX_KEY_SIZE,
205 .cia_setkey = aes_set_key,
206 .cia_encrypt = aes_encrypt,
207 .cia_decrypt = aes_decrypt,
208 .cia_encrypt_ecb = aes_encrypt_ecb,
209 .cia_decrypt_ecb = aes_decrypt_ecb,
210 .cia_encrypt_cbc = aes_encrypt_cbc,
211 .cia_decrypt_cbc = aes_decrypt_cbc,
216 static int __init aes_init(void)
220 if (crypt_s390_func_available(KM_AES_128_ENCRYPT))
222 if (crypt_s390_func_available(KM_AES_192_ENCRYPT))
224 if (crypt_s390_func_available(KM_AES_256_ENCRYPT))
227 if (!has_aes_128 && !has_aes_192 && !has_aes_256)
230 ret = crypto_register_alg(&aes_alg);
232 printk(KERN_INFO "crypt_s390: aes_s390 couldn't be loaded.\n");
236 static void __exit aes_fini(void)
238 crypto_unregister_alg(&aes_alg);
241 module_init(aes_init);
242 module_exit(aes_fini);
246 MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
247 MODULE_LICENSE("GPL");