ghash_s390.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Cryptographic API.
  3. *
  4. * s390 implementation of the GHASH algorithm for GCM (Galois/Counter Mode).
  5. *
  6. * Copyright IBM Corp. 2011
  7. * Author(s): Gerald Schaefer <gerald.schaefer@de.ibm.com>
  8. */
  9. #include <crypto/internal/hash.h>
  10. #include <linux/module.h>
  11. #include <linux/cpufeature.h>
  12. #include <asm/cpacf.h>
  13. #define GHASH_BLOCK_SIZE 16
  14. #define GHASH_DIGEST_SIZE 16
  15. struct ghash_ctx {
  16. u8 key[GHASH_BLOCK_SIZE];
  17. };
  18. struct ghash_desc_ctx {
  19. u8 icv[GHASH_BLOCK_SIZE];
  20. u8 key[GHASH_BLOCK_SIZE];
  21. u8 buffer[GHASH_BLOCK_SIZE];
  22. u32 bytes;
  23. };
  24. static int ghash_init(struct shash_desc *desc)
  25. {
  26. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  27. struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
  28. memset(dctx, 0, sizeof(*dctx));
  29. memcpy(dctx->key, ctx->key, GHASH_BLOCK_SIZE);
  30. return 0;
  31. }
  32. static int ghash_setkey(struct crypto_shash *tfm,
  33. const u8 *key, unsigned int keylen)
  34. {
  35. struct ghash_ctx *ctx = crypto_shash_ctx(tfm);
  36. if (keylen != GHASH_BLOCK_SIZE) {
  37. crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  38. return -EINVAL;
  39. }
  40. memcpy(ctx->key, key, GHASH_BLOCK_SIZE);
  41. return 0;
  42. }
  43. static int ghash_update(struct shash_desc *desc,
  44. const u8 *src, unsigned int srclen)
  45. {
  46. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  47. unsigned int n;
  48. u8 *buf = dctx->buffer;
  49. if (dctx->bytes) {
  50. u8 *pos = buf + (GHASH_BLOCK_SIZE - dctx->bytes);
  51. n = min(srclen, dctx->bytes);
  52. dctx->bytes -= n;
  53. srclen -= n;
  54. memcpy(pos, src, n);
  55. src += n;
  56. if (!dctx->bytes) {
  57. cpacf_kimd(CPACF_KIMD_GHASH, dctx, buf,
  58. GHASH_BLOCK_SIZE);
  59. }
  60. }
  61. n = srclen & ~(GHASH_BLOCK_SIZE - 1);
  62. if (n) {
  63. cpacf_kimd(CPACF_KIMD_GHASH, dctx, src, n);
  64. src += n;
  65. srclen -= n;
  66. }
  67. if (srclen) {
  68. dctx->bytes = GHASH_BLOCK_SIZE - srclen;
  69. memcpy(buf, src, srclen);
  70. }
  71. return 0;
  72. }
  73. static int ghash_flush(struct ghash_desc_ctx *dctx)
  74. {
  75. u8 *buf = dctx->buffer;
  76. if (dctx->bytes) {
  77. u8 *pos = buf + (GHASH_BLOCK_SIZE - dctx->bytes);
  78. memset(pos, 0, dctx->bytes);
  79. cpacf_kimd(CPACF_KIMD_GHASH, dctx, buf, GHASH_BLOCK_SIZE);
  80. dctx->bytes = 0;
  81. }
  82. return 0;
  83. }
  84. static int ghash_final(struct shash_desc *desc, u8 *dst)
  85. {
  86. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  87. int ret;
  88. ret = ghash_flush(dctx);
  89. if (!ret)
  90. memcpy(dst, dctx->icv, GHASH_BLOCK_SIZE);
  91. return ret;
  92. }
  93. static struct shash_alg ghash_alg = {
  94. .digestsize = GHASH_DIGEST_SIZE,
  95. .init = ghash_init,
  96. .update = ghash_update,
  97. .final = ghash_final,
  98. .setkey = ghash_setkey,
  99. .descsize = sizeof(struct ghash_desc_ctx),
  100. .base = {
  101. .cra_name = "ghash",
  102. .cra_driver_name = "ghash-s390",
  103. .cra_priority = 300,
  104. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  105. .cra_blocksize = GHASH_BLOCK_SIZE,
  106. .cra_ctxsize = sizeof(struct ghash_ctx),
  107. .cra_module = THIS_MODULE,
  108. },
  109. };
  110. static int __init ghash_mod_init(void)
  111. {
  112. if (!cpacf_query_func(CPACF_KIMD, CPACF_KIMD_GHASH))
  113. return -EOPNOTSUPP;
  114. return crypto_register_shash(&ghash_alg);
  115. }
  116. static void __exit ghash_mod_exit(void)
  117. {
  118. crypto_unregister_shash(&ghash_alg);
  119. }
  120. module_cpu_feature_match(MSA, ghash_mod_init);
  121. module_exit(ghash_mod_exit);
  122. MODULE_ALIAS_CRYPTO("ghash");
  123. MODULE_LICENSE("GPL");
  124. MODULE_DESCRIPTION("GHASH Message Digest Algorithm, s390 implementation");