ghash_s390.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 "crypt_s390.h"
  12. #define GHASH_BLOCK_SIZE 16
  13. #define GHASH_DIGEST_SIZE 16
  14. struct ghash_ctx {
  15. u8 icv[16];
  16. u8 key[16];
  17. };
  18. struct ghash_desc_ctx {
  19. u8 buffer[GHASH_BLOCK_SIZE];
  20. u32 bytes;
  21. };
  22. static int ghash_init(struct shash_desc *desc)
  23. {
  24. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  25. memset(dctx, 0, sizeof(*dctx));
  26. return 0;
  27. }
  28. static int ghash_setkey(struct crypto_shash *tfm,
  29. const u8 *key, unsigned int keylen)
  30. {
  31. struct ghash_ctx *ctx = crypto_shash_ctx(tfm);
  32. if (keylen != GHASH_BLOCK_SIZE) {
  33. crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  34. return -EINVAL;
  35. }
  36. memcpy(ctx->key, key, GHASH_BLOCK_SIZE);
  37. memset(ctx->icv, 0, GHASH_BLOCK_SIZE);
  38. return 0;
  39. }
  40. static int ghash_update(struct shash_desc *desc,
  41. const u8 *src, unsigned int srclen)
  42. {
  43. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  44. struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
  45. unsigned int n;
  46. u8 *buf = dctx->buffer;
  47. int ret;
  48. if (dctx->bytes) {
  49. u8 *pos = buf + (GHASH_BLOCK_SIZE - dctx->bytes);
  50. n = min(srclen, dctx->bytes);
  51. dctx->bytes -= n;
  52. srclen -= n;
  53. memcpy(pos, src, n);
  54. src += n;
  55. if (!dctx->bytes) {
  56. ret = crypt_s390_kimd(KIMD_GHASH, ctx, buf,
  57. GHASH_BLOCK_SIZE);
  58. BUG_ON(ret != GHASH_BLOCK_SIZE);
  59. }
  60. }
  61. n = srclen & ~(GHASH_BLOCK_SIZE - 1);
  62. if (n) {
  63. ret = crypt_s390_kimd(KIMD_GHASH, ctx, src, n);
  64. BUG_ON(ret != n);
  65. src += n;
  66. srclen -= n;
  67. }
  68. if (srclen) {
  69. dctx->bytes = GHASH_BLOCK_SIZE - srclen;
  70. memcpy(buf, src, srclen);
  71. }
  72. return 0;
  73. }
  74. static void ghash_flush(struct ghash_ctx *ctx, struct ghash_desc_ctx *dctx)
  75. {
  76. u8 *buf = dctx->buffer;
  77. int ret;
  78. if (dctx->bytes) {
  79. u8 *pos = buf + (GHASH_BLOCK_SIZE - dctx->bytes);
  80. memset(pos, 0, dctx->bytes);
  81. ret = crypt_s390_kimd(KIMD_GHASH, ctx, buf, GHASH_BLOCK_SIZE);
  82. BUG_ON(ret != GHASH_BLOCK_SIZE);
  83. }
  84. dctx->bytes = 0;
  85. }
  86. static int ghash_final(struct shash_desc *desc, u8 *dst)
  87. {
  88. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  89. struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
  90. ghash_flush(ctx, dctx);
  91. memcpy(dst, ctx->icv, GHASH_BLOCK_SIZE);
  92. return 0;
  93. }
  94. static struct shash_alg ghash_alg = {
  95. .digestsize = GHASH_DIGEST_SIZE,
  96. .init = ghash_init,
  97. .update = ghash_update,
  98. .final = ghash_final,
  99. .setkey = ghash_setkey,
  100. .descsize = sizeof(struct ghash_desc_ctx),
  101. .base = {
  102. .cra_name = "ghash",
  103. .cra_driver_name = "ghash-s390",
  104. .cra_priority = CRYPT_S390_PRIORITY,
  105. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  106. .cra_blocksize = GHASH_BLOCK_SIZE,
  107. .cra_ctxsize = sizeof(struct ghash_ctx),
  108. .cra_module = THIS_MODULE,
  109. .cra_list = LIST_HEAD_INIT(ghash_alg.base.cra_list),
  110. },
  111. };
  112. static int __init ghash_mod_init(void)
  113. {
  114. if (!crypt_s390_func_available(KIMD_GHASH,
  115. CRYPT_S390_MSA | CRYPT_S390_MSA4))
  116. return -EOPNOTSUPP;
  117. return crypto_register_shash(&ghash_alg);
  118. }
  119. static void __exit ghash_mod_exit(void)
  120. {
  121. crypto_unregister_shash(&ghash_alg);
  122. }
  123. module_init(ghash_mod_init);
  124. module_exit(ghash_mod_exit);
  125. MODULE_ALIAS("ghash");
  126. MODULE_LICENSE("GPL");
  127. MODULE_DESCRIPTION("GHASH Message Digest Algorithm, s390 implementation");