aes_s390.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Cryptographic API.
  3. *
  4. * s390 implementation of the AES Cipher Algorithm.
  5. *
  6. * s390 Version:
  7. * Copyright (C) 2005 IBM Deutschland GmbH, IBM Corporation
  8. * Author(s): Jan Glauber (jang@de.ibm.com)
  9. *
  10. * Derived from "crypto/aes.c"
  11. *
  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)
  15. * any later version.
  16. *
  17. */
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/crypto.h>
  21. #include "crypt_s390.h"
  22. #define AES_MIN_KEY_SIZE 16
  23. #define AES_MAX_KEY_SIZE 32
  24. /* data block size for all key lengths */
  25. #define AES_BLOCK_SIZE 16
  26. int has_aes_128 = 0;
  27. int has_aes_192 = 0;
  28. int has_aes_256 = 0;
  29. struct s390_aes_ctx {
  30. u8 iv[AES_BLOCK_SIZE];
  31. u8 key[AES_MAX_KEY_SIZE];
  32. int key_len;
  33. };
  34. static int aes_set_key(void *ctx, const u8 *in_key, unsigned int key_len,
  35. u32 *flags)
  36. {
  37. struct s390_aes_ctx *sctx = ctx;
  38. switch (key_len) {
  39. case 16:
  40. if (!has_aes_128)
  41. goto fail;
  42. break;
  43. case 24:
  44. if (!has_aes_192)
  45. goto fail;
  46. break;
  47. case 32:
  48. if (!has_aes_256)
  49. goto fail;
  50. break;
  51. default:
  52. /* invalid key length */
  53. goto fail;
  54. break;
  55. }
  56. sctx->key_len = key_len;
  57. memcpy(sctx->key, in_key, key_len);
  58. return 0;
  59. fail:
  60. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  61. return -EINVAL;
  62. }
  63. static void aes_encrypt(void *ctx, u8 *out, const u8 *in)
  64. {
  65. const struct s390_aes_ctx *sctx = ctx;
  66. switch (sctx->key_len) {
  67. case 16:
  68. crypt_s390_km(KM_AES_128_ENCRYPT, &sctx->key, out, in,
  69. AES_BLOCK_SIZE);
  70. break;
  71. case 24:
  72. crypt_s390_km(KM_AES_192_ENCRYPT, &sctx->key, out, in,
  73. AES_BLOCK_SIZE);
  74. break;
  75. case 32:
  76. crypt_s390_km(KM_AES_256_ENCRYPT, &sctx->key, out, in,
  77. AES_BLOCK_SIZE);
  78. break;
  79. }
  80. }
  81. static void aes_decrypt(void *ctx, u8 *out, const u8 *in)
  82. {
  83. const struct s390_aes_ctx *sctx = ctx;
  84. switch (sctx->key_len) {
  85. case 16:
  86. crypt_s390_km(KM_AES_128_DECRYPT, &sctx->key, out, in,
  87. AES_BLOCK_SIZE);
  88. break;
  89. case 24:
  90. crypt_s390_km(KM_AES_192_DECRYPT, &sctx->key, out, in,
  91. AES_BLOCK_SIZE);
  92. break;
  93. case 32:
  94. crypt_s390_km(KM_AES_256_DECRYPT, &sctx->key, out, in,
  95. AES_BLOCK_SIZE);
  96. break;
  97. }
  98. }
  99. static unsigned int aes_encrypt_ecb(const struct cipher_desc *desc, u8 *out,
  100. const u8 *in, unsigned int nbytes)
  101. {
  102. struct s390_aes_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  103. switch (sctx->key_len) {
  104. case 16:
  105. crypt_s390_km(KM_AES_128_ENCRYPT, &sctx->key, out, in, nbytes);
  106. break;
  107. case 24:
  108. crypt_s390_km(KM_AES_192_ENCRYPT, &sctx->key, out, in, nbytes);
  109. break;
  110. case 32:
  111. crypt_s390_km(KM_AES_256_ENCRYPT, &sctx->key, out, in, nbytes);
  112. break;
  113. }
  114. return nbytes & ~(AES_BLOCK_SIZE - 1);
  115. }
  116. static unsigned int aes_decrypt_ecb(const struct cipher_desc *desc, u8 *out,
  117. const u8 *in, unsigned int nbytes)
  118. {
  119. struct s390_aes_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  120. switch (sctx->key_len) {
  121. case 16:
  122. crypt_s390_km(KM_AES_128_DECRYPT, &sctx->key, out, in, nbytes);
  123. break;
  124. case 24:
  125. crypt_s390_km(KM_AES_192_DECRYPT, &sctx->key, out, in, nbytes);
  126. break;
  127. case 32:
  128. crypt_s390_km(KM_AES_256_DECRYPT, &sctx->key, out, in, nbytes);
  129. break;
  130. }
  131. return nbytes & ~(AES_BLOCK_SIZE - 1);
  132. }
  133. static unsigned int aes_encrypt_cbc(const struct cipher_desc *desc, u8 *out,
  134. const u8 *in, unsigned int nbytes)
  135. {
  136. struct s390_aes_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  137. memcpy(&sctx->iv, desc->info, AES_BLOCK_SIZE);
  138. switch (sctx->key_len) {
  139. case 16:
  140. crypt_s390_kmc(KMC_AES_128_ENCRYPT, &sctx->iv, out, in, nbytes);
  141. break;
  142. case 24:
  143. crypt_s390_kmc(KMC_AES_192_ENCRYPT, &sctx->iv, out, in, nbytes);
  144. break;
  145. case 32:
  146. crypt_s390_kmc(KMC_AES_256_ENCRYPT, &sctx->iv, out, in, nbytes);
  147. break;
  148. }
  149. memcpy(desc->info, &sctx->iv, AES_BLOCK_SIZE);
  150. return nbytes & ~(AES_BLOCK_SIZE - 1);
  151. }
  152. static unsigned int aes_decrypt_cbc(const struct cipher_desc *desc, u8 *out,
  153. const u8 *in, unsigned int nbytes)
  154. {
  155. struct s390_aes_ctx *sctx = crypto_tfm_ctx(desc->tfm);
  156. memcpy(&sctx->iv, desc->info, AES_BLOCK_SIZE);
  157. switch (sctx->key_len) {
  158. case 16:
  159. crypt_s390_kmc(KMC_AES_128_DECRYPT, &sctx->iv, out, in, nbytes);
  160. break;
  161. case 24:
  162. crypt_s390_kmc(KMC_AES_192_DECRYPT, &sctx->iv, out, in, nbytes);
  163. break;
  164. case 32:
  165. crypt_s390_kmc(KMC_AES_256_DECRYPT, &sctx->iv, out, in, nbytes);
  166. break;
  167. }
  168. return nbytes & ~(AES_BLOCK_SIZE - 1);
  169. }
  170. static struct crypto_alg aes_alg = {
  171. .cra_name = "aes",
  172. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  173. .cra_blocksize = AES_BLOCK_SIZE,
  174. .cra_ctxsize = sizeof(struct s390_aes_ctx),
  175. .cra_module = THIS_MODULE,
  176. .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
  177. .cra_u = {
  178. .cipher = {
  179. .cia_min_keysize = AES_MIN_KEY_SIZE,
  180. .cia_max_keysize = AES_MAX_KEY_SIZE,
  181. .cia_setkey = aes_set_key,
  182. .cia_encrypt = aes_encrypt,
  183. .cia_decrypt = aes_decrypt,
  184. .cia_encrypt_ecb = aes_encrypt_ecb,
  185. .cia_decrypt_ecb = aes_decrypt_ecb,
  186. .cia_encrypt_cbc = aes_encrypt_cbc,
  187. .cia_decrypt_cbc = aes_decrypt_cbc,
  188. }
  189. }
  190. };
  191. static int __init aes_init(void)
  192. {
  193. int ret;
  194. if (crypt_s390_func_available(KM_AES_128_ENCRYPT))
  195. has_aes_128 = 1;
  196. if (crypt_s390_func_available(KM_AES_192_ENCRYPT))
  197. has_aes_192 = 1;
  198. if (crypt_s390_func_available(KM_AES_256_ENCRYPT))
  199. has_aes_256 = 1;
  200. if (!has_aes_128 && !has_aes_192 && !has_aes_256)
  201. return -ENOSYS;
  202. ret = crypto_register_alg(&aes_alg);
  203. if (ret != 0)
  204. printk(KERN_INFO "crypt_s390: aes_s390 couldn't be loaded.\n");
  205. return ret;
  206. }
  207. static void __exit aes_fini(void)
  208. {
  209. crypto_unregister_alg(&aes_alg);
  210. }
  211. module_init(aes_init);
  212. module_exit(aes_fini);
  213. MODULE_ALIAS("aes");
  214. MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
  215. MODULE_LICENSE("GPL");