poly1305-glue.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * OpenSSL/Cryptogams accelerated Poly1305 transform for MIPS
  4. *
  5. * Copyright (C) 2019 Linaro Ltd. <[email protected]>
  6. */
  7. #include <asm/unaligned.h>
  8. #include <crypto/algapi.h>
  9. #include <crypto/internal/hash.h>
  10. #include <crypto/internal/poly1305.h>
  11. #include <linux/cpufeature.h>
  12. #include <linux/crypto.h>
  13. #include <linux/module.h>
  14. asmlinkage void poly1305_init_mips(void *state, const u8 *key);
  15. asmlinkage void poly1305_blocks_mips(void *state, const u8 *src, u32 len, u32 hibit);
  16. asmlinkage void poly1305_emit_mips(void *state, u8 *digest, const u32 *nonce);
  17. void poly1305_init_arch(struct poly1305_desc_ctx *dctx, const u8 key[POLY1305_KEY_SIZE])
  18. {
  19. poly1305_init_mips(&dctx->h, key);
  20. dctx->s[0] = get_unaligned_le32(key + 16);
  21. dctx->s[1] = get_unaligned_le32(key + 20);
  22. dctx->s[2] = get_unaligned_le32(key + 24);
  23. dctx->s[3] = get_unaligned_le32(key + 28);
  24. dctx->buflen = 0;
  25. }
  26. EXPORT_SYMBOL(poly1305_init_arch);
  27. static int mips_poly1305_init(struct shash_desc *desc)
  28. {
  29. struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
  30. dctx->buflen = 0;
  31. dctx->rset = 0;
  32. dctx->sset = false;
  33. return 0;
  34. }
  35. static void mips_poly1305_blocks(struct poly1305_desc_ctx *dctx, const u8 *src,
  36. u32 len, u32 hibit)
  37. {
  38. if (unlikely(!dctx->sset)) {
  39. if (!dctx->rset) {
  40. poly1305_init_mips(&dctx->h, src);
  41. src += POLY1305_BLOCK_SIZE;
  42. len -= POLY1305_BLOCK_SIZE;
  43. dctx->rset = 1;
  44. }
  45. if (len >= POLY1305_BLOCK_SIZE) {
  46. dctx->s[0] = get_unaligned_le32(src + 0);
  47. dctx->s[1] = get_unaligned_le32(src + 4);
  48. dctx->s[2] = get_unaligned_le32(src + 8);
  49. dctx->s[3] = get_unaligned_le32(src + 12);
  50. src += POLY1305_BLOCK_SIZE;
  51. len -= POLY1305_BLOCK_SIZE;
  52. dctx->sset = true;
  53. }
  54. if (len < POLY1305_BLOCK_SIZE)
  55. return;
  56. }
  57. len &= ~(POLY1305_BLOCK_SIZE - 1);
  58. poly1305_blocks_mips(&dctx->h, src, len, hibit);
  59. }
  60. static int mips_poly1305_update(struct shash_desc *desc, const u8 *src,
  61. unsigned int len)
  62. {
  63. struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
  64. if (unlikely(dctx->buflen)) {
  65. u32 bytes = min(len, POLY1305_BLOCK_SIZE - dctx->buflen);
  66. memcpy(dctx->buf + dctx->buflen, src, bytes);
  67. src += bytes;
  68. len -= bytes;
  69. dctx->buflen += bytes;
  70. if (dctx->buflen == POLY1305_BLOCK_SIZE) {
  71. mips_poly1305_blocks(dctx, dctx->buf, POLY1305_BLOCK_SIZE, 1);
  72. dctx->buflen = 0;
  73. }
  74. }
  75. if (likely(len >= POLY1305_BLOCK_SIZE)) {
  76. mips_poly1305_blocks(dctx, src, len, 1);
  77. src += round_down(len, POLY1305_BLOCK_SIZE);
  78. len %= POLY1305_BLOCK_SIZE;
  79. }
  80. if (unlikely(len)) {
  81. dctx->buflen = len;
  82. memcpy(dctx->buf, src, len);
  83. }
  84. return 0;
  85. }
  86. void poly1305_update_arch(struct poly1305_desc_ctx *dctx, const u8 *src,
  87. unsigned int nbytes)
  88. {
  89. if (unlikely(dctx->buflen)) {
  90. u32 bytes = min(nbytes, POLY1305_BLOCK_SIZE - dctx->buflen);
  91. memcpy(dctx->buf + dctx->buflen, src, bytes);
  92. src += bytes;
  93. nbytes -= bytes;
  94. dctx->buflen += bytes;
  95. if (dctx->buflen == POLY1305_BLOCK_SIZE) {
  96. poly1305_blocks_mips(&dctx->h, dctx->buf,
  97. POLY1305_BLOCK_SIZE, 1);
  98. dctx->buflen = 0;
  99. }
  100. }
  101. if (likely(nbytes >= POLY1305_BLOCK_SIZE)) {
  102. unsigned int len = round_down(nbytes, POLY1305_BLOCK_SIZE);
  103. poly1305_blocks_mips(&dctx->h, src, len, 1);
  104. src += len;
  105. nbytes %= POLY1305_BLOCK_SIZE;
  106. }
  107. if (unlikely(nbytes)) {
  108. dctx->buflen = nbytes;
  109. memcpy(dctx->buf, src, nbytes);
  110. }
  111. }
  112. EXPORT_SYMBOL(poly1305_update_arch);
  113. void poly1305_final_arch(struct poly1305_desc_ctx *dctx, u8 *dst)
  114. {
  115. if (unlikely(dctx->buflen)) {
  116. dctx->buf[dctx->buflen++] = 1;
  117. memset(dctx->buf + dctx->buflen, 0,
  118. POLY1305_BLOCK_SIZE - dctx->buflen);
  119. poly1305_blocks_mips(&dctx->h, dctx->buf, POLY1305_BLOCK_SIZE, 0);
  120. }
  121. poly1305_emit_mips(&dctx->h, dst, dctx->s);
  122. *dctx = (struct poly1305_desc_ctx){};
  123. }
  124. EXPORT_SYMBOL(poly1305_final_arch);
  125. static int mips_poly1305_final(struct shash_desc *desc, u8 *dst)
  126. {
  127. struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
  128. if (unlikely(!dctx->sset))
  129. return -ENOKEY;
  130. poly1305_final_arch(dctx, dst);
  131. return 0;
  132. }
  133. static struct shash_alg mips_poly1305_alg = {
  134. .init = mips_poly1305_init,
  135. .update = mips_poly1305_update,
  136. .final = mips_poly1305_final,
  137. .digestsize = POLY1305_DIGEST_SIZE,
  138. .descsize = sizeof(struct poly1305_desc_ctx),
  139. .base.cra_name = "poly1305",
  140. .base.cra_driver_name = "poly1305-mips",
  141. .base.cra_priority = 200,
  142. .base.cra_blocksize = POLY1305_BLOCK_SIZE,
  143. .base.cra_module = THIS_MODULE,
  144. };
  145. static int __init mips_poly1305_mod_init(void)
  146. {
  147. return IS_REACHABLE(CONFIG_CRYPTO_HASH) ?
  148. crypto_register_shash(&mips_poly1305_alg) : 0;
  149. }
  150. static void __exit mips_poly1305_mod_exit(void)
  151. {
  152. if (IS_REACHABLE(CONFIG_CRYPTO_HASH))
  153. crypto_unregister_shash(&mips_poly1305_alg);
  154. }
  155. module_init(mips_poly1305_mod_init);
  156. module_exit(mips_poly1305_mod_exit);
  157. MODULE_LICENSE("GPL v2");
  158. MODULE_ALIAS_CRYPTO("poly1305");
  159. MODULE_ALIAS_CRYPTO("poly1305-mips");