aes_s390.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Cryptographic API.
  3. *
  4. * s390 implementation of the AES Cipher Algorithm.
  5. *
  6. * s390 Version:
  7. * Copyright IBM Corp. 2005,2007
  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 <crypto/algapi.h>
  19. #include <linux/module.h>
  20. #include <linux/init.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. #define AES_KEYLEN_128 1
  27. #define AES_KEYLEN_192 2
  28. #define AES_KEYLEN_256 4
  29. static char keylen_flag = 0;
  30. struct s390_aes_ctx {
  31. u8 iv[AES_BLOCK_SIZE];
  32. u8 key[AES_MAX_KEY_SIZE];
  33. long enc;
  34. long dec;
  35. int key_len;
  36. };
  37. static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  38. unsigned int key_len)
  39. {
  40. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  41. u32 *flags = &tfm->crt_flags;
  42. switch (key_len) {
  43. case 16:
  44. if (!(keylen_flag & AES_KEYLEN_128))
  45. goto fail;
  46. break;
  47. case 24:
  48. if (!(keylen_flag & AES_KEYLEN_192))
  49. goto fail;
  50. break;
  51. case 32:
  52. if (!(keylen_flag & AES_KEYLEN_256))
  53. goto fail;
  54. break;
  55. default:
  56. goto fail;
  57. break;
  58. }
  59. sctx->key_len = key_len;
  60. memcpy(sctx->key, in_key, key_len);
  61. return 0;
  62. fail:
  63. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  64. return -EINVAL;
  65. }
  66. static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  67. {
  68. const struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  69. switch (sctx->key_len) {
  70. case 16:
  71. crypt_s390_km(KM_AES_128_ENCRYPT, &sctx->key, out, in,
  72. AES_BLOCK_SIZE);
  73. break;
  74. case 24:
  75. crypt_s390_km(KM_AES_192_ENCRYPT, &sctx->key, out, in,
  76. AES_BLOCK_SIZE);
  77. break;
  78. case 32:
  79. crypt_s390_km(KM_AES_256_ENCRYPT, &sctx->key, out, in,
  80. AES_BLOCK_SIZE);
  81. break;
  82. }
  83. }
  84. static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  85. {
  86. const struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  87. switch (sctx->key_len) {
  88. case 16:
  89. crypt_s390_km(KM_AES_128_DECRYPT, &sctx->key, out, in,
  90. AES_BLOCK_SIZE);
  91. break;
  92. case 24:
  93. crypt_s390_km(KM_AES_192_DECRYPT, &sctx->key, out, in,
  94. AES_BLOCK_SIZE);
  95. break;
  96. case 32:
  97. crypt_s390_km(KM_AES_256_DECRYPT, &sctx->key, out, in,
  98. AES_BLOCK_SIZE);
  99. break;
  100. }
  101. }
  102. static struct crypto_alg aes_alg = {
  103. .cra_name = "aes",
  104. .cra_driver_name = "aes-s390",
  105. .cra_priority = CRYPT_S390_PRIORITY,
  106. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  107. .cra_blocksize = AES_BLOCK_SIZE,
  108. .cra_ctxsize = sizeof(struct s390_aes_ctx),
  109. .cra_module = THIS_MODULE,
  110. .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
  111. .cra_u = {
  112. .cipher = {
  113. .cia_min_keysize = AES_MIN_KEY_SIZE,
  114. .cia_max_keysize = AES_MAX_KEY_SIZE,
  115. .cia_setkey = aes_set_key,
  116. .cia_encrypt = aes_encrypt,
  117. .cia_decrypt = aes_decrypt,
  118. }
  119. }
  120. };
  121. static int ecb_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  122. unsigned int key_len)
  123. {
  124. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  125. switch (key_len) {
  126. case 16:
  127. sctx->enc = KM_AES_128_ENCRYPT;
  128. sctx->dec = KM_AES_128_DECRYPT;
  129. break;
  130. case 24:
  131. sctx->enc = KM_AES_192_ENCRYPT;
  132. sctx->dec = KM_AES_192_DECRYPT;
  133. break;
  134. case 32:
  135. sctx->enc = KM_AES_256_ENCRYPT;
  136. sctx->dec = KM_AES_256_DECRYPT;
  137. break;
  138. }
  139. return aes_set_key(tfm, in_key, key_len);
  140. }
  141. static int ecb_aes_crypt(struct blkcipher_desc *desc, long func, void *param,
  142. struct blkcipher_walk *walk)
  143. {
  144. int ret = blkcipher_walk_virt(desc, walk);
  145. unsigned int nbytes;
  146. while ((nbytes = walk->nbytes)) {
  147. /* only use complete blocks */
  148. unsigned int n = nbytes & ~(AES_BLOCK_SIZE - 1);
  149. u8 *out = walk->dst.virt.addr;
  150. u8 *in = walk->src.virt.addr;
  151. ret = crypt_s390_km(func, param, out, in, n);
  152. BUG_ON((ret < 0) || (ret != n));
  153. nbytes &= AES_BLOCK_SIZE - 1;
  154. ret = blkcipher_walk_done(desc, walk, nbytes);
  155. }
  156. return ret;
  157. }
  158. static int ecb_aes_encrypt(struct blkcipher_desc *desc,
  159. struct scatterlist *dst, struct scatterlist *src,
  160. unsigned int nbytes)
  161. {
  162. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  163. struct blkcipher_walk walk;
  164. blkcipher_walk_init(&walk, dst, src, nbytes);
  165. return ecb_aes_crypt(desc, sctx->enc, sctx->key, &walk);
  166. }
  167. static int ecb_aes_decrypt(struct blkcipher_desc *desc,
  168. struct scatterlist *dst, struct scatterlist *src,
  169. unsigned int nbytes)
  170. {
  171. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  172. struct blkcipher_walk walk;
  173. blkcipher_walk_init(&walk, dst, src, nbytes);
  174. return ecb_aes_crypt(desc, sctx->dec, sctx->key, &walk);
  175. }
  176. static struct crypto_alg ecb_aes_alg = {
  177. .cra_name = "ecb(aes)",
  178. .cra_driver_name = "ecb-aes-s390",
  179. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  180. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  181. .cra_blocksize = AES_BLOCK_SIZE,
  182. .cra_ctxsize = sizeof(struct s390_aes_ctx),
  183. .cra_type = &crypto_blkcipher_type,
  184. .cra_module = THIS_MODULE,
  185. .cra_list = LIST_HEAD_INIT(ecb_aes_alg.cra_list),
  186. .cra_u = {
  187. .blkcipher = {
  188. .min_keysize = AES_MIN_KEY_SIZE,
  189. .max_keysize = AES_MAX_KEY_SIZE,
  190. .setkey = ecb_aes_set_key,
  191. .encrypt = ecb_aes_encrypt,
  192. .decrypt = ecb_aes_decrypt,
  193. }
  194. }
  195. };
  196. static int cbc_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  197. unsigned int key_len)
  198. {
  199. struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
  200. switch (key_len) {
  201. case 16:
  202. sctx->enc = KMC_AES_128_ENCRYPT;
  203. sctx->dec = KMC_AES_128_DECRYPT;
  204. break;
  205. case 24:
  206. sctx->enc = KMC_AES_192_ENCRYPT;
  207. sctx->dec = KMC_AES_192_DECRYPT;
  208. break;
  209. case 32:
  210. sctx->enc = KMC_AES_256_ENCRYPT;
  211. sctx->dec = KMC_AES_256_DECRYPT;
  212. break;
  213. }
  214. return aes_set_key(tfm, in_key, key_len);
  215. }
  216. static int cbc_aes_crypt(struct blkcipher_desc *desc, long func, void *param,
  217. struct blkcipher_walk *walk)
  218. {
  219. int ret = blkcipher_walk_virt(desc, walk);
  220. unsigned int nbytes = walk->nbytes;
  221. if (!nbytes)
  222. goto out;
  223. memcpy(param, walk->iv, AES_BLOCK_SIZE);
  224. do {
  225. /* only use complete blocks */
  226. unsigned int n = nbytes & ~(AES_BLOCK_SIZE - 1);
  227. u8 *out = walk->dst.virt.addr;
  228. u8 *in = walk->src.virt.addr;
  229. ret = crypt_s390_kmc(func, param, out, in, n);
  230. BUG_ON((ret < 0) || (ret != n));
  231. nbytes &= AES_BLOCK_SIZE - 1;
  232. ret = blkcipher_walk_done(desc, walk, nbytes);
  233. } while ((nbytes = walk->nbytes));
  234. memcpy(walk->iv, param, AES_BLOCK_SIZE);
  235. out:
  236. return ret;
  237. }
  238. static int cbc_aes_encrypt(struct blkcipher_desc *desc,
  239. struct scatterlist *dst, struct scatterlist *src,
  240. unsigned int nbytes)
  241. {
  242. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  243. struct blkcipher_walk walk;
  244. blkcipher_walk_init(&walk, dst, src, nbytes);
  245. return cbc_aes_crypt(desc, sctx->enc, sctx->iv, &walk);
  246. }
  247. static int cbc_aes_decrypt(struct blkcipher_desc *desc,
  248. struct scatterlist *dst, struct scatterlist *src,
  249. unsigned int nbytes)
  250. {
  251. struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
  252. struct blkcipher_walk walk;
  253. blkcipher_walk_init(&walk, dst, src, nbytes);
  254. return cbc_aes_crypt(desc, sctx->dec, sctx->iv, &walk);
  255. }
  256. static struct crypto_alg cbc_aes_alg = {
  257. .cra_name = "cbc(aes)",
  258. .cra_driver_name = "cbc-aes-s390",
  259. .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
  260. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  261. .cra_blocksize = AES_BLOCK_SIZE,
  262. .cra_ctxsize = sizeof(struct s390_aes_ctx),
  263. .cra_type = &crypto_blkcipher_type,
  264. .cra_module = THIS_MODULE,
  265. .cra_list = LIST_HEAD_INIT(cbc_aes_alg.cra_list),
  266. .cra_u = {
  267. .blkcipher = {
  268. .min_keysize = AES_MIN_KEY_SIZE,
  269. .max_keysize = AES_MAX_KEY_SIZE,
  270. .ivsize = AES_BLOCK_SIZE,
  271. .setkey = cbc_aes_set_key,
  272. .encrypt = cbc_aes_encrypt,
  273. .decrypt = cbc_aes_decrypt,
  274. }
  275. }
  276. };
  277. static int __init aes_init(void)
  278. {
  279. int ret;
  280. if (crypt_s390_func_available(KM_AES_128_ENCRYPT))
  281. keylen_flag |= AES_KEYLEN_128;
  282. if (crypt_s390_func_available(KM_AES_192_ENCRYPT))
  283. keylen_flag |= AES_KEYLEN_192;
  284. if (crypt_s390_func_available(KM_AES_256_ENCRYPT))
  285. keylen_flag |= AES_KEYLEN_256;
  286. if (!keylen_flag)
  287. return -EOPNOTSUPP;
  288. /* z9 109 and z9 BC/EC only support 128 bit key length */
  289. if (keylen_flag == AES_KEYLEN_128)
  290. printk(KERN_INFO
  291. "aes_s390: hardware acceleration only available for"
  292. "128 bit keys\n");
  293. ret = crypto_register_alg(&aes_alg);
  294. if (ret)
  295. goto aes_err;
  296. ret = crypto_register_alg(&ecb_aes_alg);
  297. if (ret)
  298. goto ecb_aes_err;
  299. ret = crypto_register_alg(&cbc_aes_alg);
  300. if (ret)
  301. goto cbc_aes_err;
  302. out:
  303. return ret;
  304. cbc_aes_err:
  305. crypto_unregister_alg(&ecb_aes_alg);
  306. ecb_aes_err:
  307. crypto_unregister_alg(&aes_alg);
  308. aes_err:
  309. goto out;
  310. }
  311. static void __exit aes_fini(void)
  312. {
  313. crypto_unregister_alg(&cbc_aes_alg);
  314. crypto_unregister_alg(&ecb_aes_alg);
  315. crypto_unregister_alg(&aes_alg);
  316. }
  317. module_init(aes_init);
  318. module_exit(aes_fini);
  319. MODULE_ALIAS("aes");
  320. MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
  321. MODULE_LICENSE("GPL");