ghash_s390.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 "crypt_s390.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. int ret;
  50. if (dctx->bytes) {
  51. u8 *pos = buf + (GHASH_BLOCK_SIZE - dctx->bytes);
  52. n = min(srclen, dctx->bytes);
  53. dctx->bytes -= n;
  54. srclen -= n;
  55. memcpy(pos, src, n);
  56. src += n;
  57. if (!dctx->bytes) {
  58. ret = crypt_s390_kimd(KIMD_GHASH, dctx, buf,
  59. GHASH_BLOCK_SIZE);
  60. if (ret != GHASH_BLOCK_SIZE)
  61. return -EIO;
  62. }
  63. }
  64. n = srclen & ~(GHASH_BLOCK_SIZE - 1);
  65. if (n) {
  66. ret = crypt_s390_kimd(KIMD_GHASH, dctx, src, n);
  67. if (ret != n)
  68. return -EIO;
  69. src += n;
  70. srclen -= n;
  71. }
  72. if (srclen) {
  73. dctx->bytes = GHASH_BLOCK_SIZE - srclen;
  74. memcpy(buf, src, srclen);
  75. }
  76. return 0;
  77. }
  78. static int ghash_flush(struct ghash_desc_ctx *dctx)
  79. {
  80. u8 *buf = dctx->buffer;
  81. int ret;
  82. if (dctx->bytes) {
  83. u8 *pos = buf + (GHASH_BLOCK_SIZE - dctx->bytes);
  84. memset(pos, 0, dctx->bytes);
  85. ret = crypt_s390_kimd(KIMD_GHASH, dctx, buf, GHASH_BLOCK_SIZE);
  86. if (ret != GHASH_BLOCK_SIZE)
  87. return -EIO;
  88. dctx->bytes = 0;
  89. }
  90. return 0;
  91. }
  92. static int ghash_final(struct shash_desc *desc, u8 *dst)
  93. {
  94. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  95. int ret;
  96. ret = ghash_flush(dctx);
  97. if (!ret)
  98. memcpy(dst, dctx->icv, GHASH_BLOCK_SIZE);
  99. return ret;
  100. }
  101. static struct shash_alg ghash_alg = {
  102. .digestsize = GHASH_DIGEST_SIZE,
  103. .init = ghash_init,
  104. .update = ghash_update,
  105. .final = ghash_final,
  106. .setkey = ghash_setkey,
  107. .descsize = sizeof(struct ghash_desc_ctx),
  108. .base = {
  109. .cra_name = "ghash",
  110. .cra_driver_name = "ghash-s390",
  111. .cra_priority = CRYPT_S390_PRIORITY,
  112. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  113. .cra_blocksize = GHASH_BLOCK_SIZE,
  114. .cra_ctxsize = sizeof(struct ghash_ctx),
  115. .cra_module = THIS_MODULE,
  116. },
  117. };
  118. static int __init ghash_mod_init(void)
  119. {
  120. if (!crypt_s390_func_available(KIMD_GHASH,
  121. CRYPT_S390_MSA | CRYPT_S390_MSA4))
  122. return -EOPNOTSUPP;
  123. return crypto_register_shash(&ghash_alg);
  124. }
  125. static void __exit ghash_mod_exit(void)
  126. {
  127. crypto_unregister_shash(&ghash_alg);
  128. }
  129. module_cpu_feature_match(MSA, ghash_mod_init);
  130. module_exit(ghash_mod_exit);
  131. MODULE_ALIAS_CRYPTO("ghash");
  132. MODULE_LICENSE("GPL");
  133. MODULE_DESCRIPTION("GHASH Message Digest Algorithm, s390 implementation");