glue_helper.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Shared glue code for 128bit block ciphers
  3. */
  4. #ifndef _CRYPTO_GLUE_HELPER_H
  5. #define _CRYPTO_GLUE_HELPER_H
  6. #include <linux/kernel.h>
  7. #include <linux/crypto.h>
  8. #include <asm/i387.h>
  9. #include <crypto/b128ops.h>
  10. typedef void (*common_glue_func_t)(void *ctx, u8 *dst, const u8 *src);
  11. typedef void (*common_glue_cbc_func_t)(void *ctx, u128 *dst, const u128 *src);
  12. typedef void (*common_glue_ctr_func_t)(void *ctx, u128 *dst, const u128 *src,
  13. le128 *iv);
  14. typedef void (*common_glue_xts_func_t)(void *ctx, u128 *dst, const u128 *src,
  15. le128 *iv);
  16. #define GLUE_FUNC_CAST(fn) ((common_glue_func_t)(fn))
  17. #define GLUE_CBC_FUNC_CAST(fn) ((common_glue_cbc_func_t)(fn))
  18. #define GLUE_CTR_FUNC_CAST(fn) ((common_glue_ctr_func_t)(fn))
  19. #define GLUE_XTS_FUNC_CAST(fn) ((common_glue_xts_func_t)(fn))
  20. struct common_glue_func_entry {
  21. unsigned int num_blocks; /* number of blocks that @fn will process */
  22. union {
  23. common_glue_func_t ecb;
  24. common_glue_cbc_func_t cbc;
  25. common_glue_ctr_func_t ctr;
  26. common_glue_xts_func_t xts;
  27. } fn_u;
  28. };
  29. struct common_glue_ctx {
  30. unsigned int num_funcs;
  31. int fpu_blocks_limit; /* -1 means fpu not needed at all */
  32. /*
  33. * First funcs entry must have largest num_blocks and last funcs entry
  34. * must have num_blocks == 1!
  35. */
  36. struct common_glue_func_entry funcs[];
  37. };
  38. static inline bool glue_fpu_begin(unsigned int bsize, int fpu_blocks_limit,
  39. struct blkcipher_desc *desc,
  40. bool fpu_enabled, unsigned int nbytes)
  41. {
  42. if (likely(fpu_blocks_limit < 0))
  43. return false;
  44. if (fpu_enabled)
  45. return true;
  46. /*
  47. * Vector-registers are only used when chunk to be processed is large
  48. * enough, so do not enable FPU until it is necessary.
  49. */
  50. if (nbytes < bsize * (unsigned int)fpu_blocks_limit)
  51. return false;
  52. if (desc) {
  53. /* prevent sleeping if FPU is in use */
  54. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  55. }
  56. kernel_fpu_begin();
  57. return true;
  58. }
  59. static inline void glue_fpu_end(bool fpu_enabled)
  60. {
  61. if (fpu_enabled)
  62. kernel_fpu_end();
  63. }
  64. static inline void le128_to_be128(be128 *dst, const le128 *src)
  65. {
  66. dst->a = cpu_to_be64(le64_to_cpu(src->a));
  67. dst->b = cpu_to_be64(le64_to_cpu(src->b));
  68. }
  69. static inline void be128_to_le128(le128 *dst, const be128 *src)
  70. {
  71. dst->a = cpu_to_le64(be64_to_cpu(src->a));
  72. dst->b = cpu_to_le64(be64_to_cpu(src->b));
  73. }
  74. static inline void le128_inc(le128 *i)
  75. {
  76. u64 a = le64_to_cpu(i->a);
  77. u64 b = le64_to_cpu(i->b);
  78. b++;
  79. if (!b)
  80. a++;
  81. i->a = cpu_to_le64(a);
  82. i->b = cpu_to_le64(b);
  83. }
  84. static inline void le128_gf128mul_x_ble(le128 *dst, const le128 *src)
  85. {
  86. u64 a = le64_to_cpu(src->a);
  87. u64 b = le64_to_cpu(src->b);
  88. u64 _tt = ((s64)a >> 63) & 0x87;
  89. dst->a = cpu_to_le64((a << 1) ^ (b >> 63));
  90. dst->b = cpu_to_le64((b << 1) ^ _tt);
  91. }
  92. extern int glue_ecb_crypt_128bit(const struct common_glue_ctx *gctx,
  93. struct blkcipher_desc *desc,
  94. struct scatterlist *dst,
  95. struct scatterlist *src, unsigned int nbytes);
  96. extern int glue_cbc_encrypt_128bit(const common_glue_func_t fn,
  97. struct blkcipher_desc *desc,
  98. struct scatterlist *dst,
  99. struct scatterlist *src,
  100. unsigned int nbytes);
  101. extern int glue_cbc_decrypt_128bit(const struct common_glue_ctx *gctx,
  102. struct blkcipher_desc *desc,
  103. struct scatterlist *dst,
  104. struct scatterlist *src,
  105. unsigned int nbytes);
  106. extern int glue_ctr_crypt_128bit(const struct common_glue_ctx *gctx,
  107. struct blkcipher_desc *desc,
  108. struct scatterlist *dst,
  109. struct scatterlist *src, unsigned int nbytes);
  110. extern int glue_xts_crypt_128bit(const struct common_glue_ctx *gctx,
  111. struct blkcipher_desc *desc,
  112. struct scatterlist *dst,
  113. struct scatterlist *src, unsigned int nbytes,
  114. common_glue_func_t tweak_fn, void *tweak_ctx,
  115. void *crypt_ctx);
  116. extern void glue_xts_crypt_128bit_one(void *ctx, u128 *dst, const u128 *src,
  117. le128 *iv, common_glue_func_t fn);
  118. #endif /* _CRYPTO_GLUE_HELPER_H */