glue_helper.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. u128 *iv);
  14. #define GLUE_FUNC_CAST(fn) ((common_glue_func_t)(fn))
  15. #define GLUE_CBC_FUNC_CAST(fn) ((common_glue_cbc_func_t)(fn))
  16. #define GLUE_CTR_FUNC_CAST(fn) ((common_glue_ctr_func_t)(fn))
  17. struct common_glue_func_entry {
  18. unsigned int num_blocks; /* number of blocks that @fn will process */
  19. union {
  20. common_glue_func_t ecb;
  21. common_glue_cbc_func_t cbc;
  22. common_glue_ctr_func_t ctr;
  23. } fn_u;
  24. };
  25. struct common_glue_ctx {
  26. unsigned int num_funcs;
  27. int fpu_blocks_limit; /* -1 means fpu not needed at all */
  28. /*
  29. * First funcs entry must have largest num_blocks and last funcs entry
  30. * must have num_blocks == 1!
  31. */
  32. struct common_glue_func_entry funcs[];
  33. };
  34. static inline bool glue_fpu_begin(unsigned int bsize, int fpu_blocks_limit,
  35. struct blkcipher_desc *desc,
  36. bool fpu_enabled, unsigned int nbytes)
  37. {
  38. if (likely(fpu_blocks_limit < 0))
  39. return false;
  40. if (fpu_enabled)
  41. return true;
  42. /*
  43. * Vector-registers are only used when chunk to be processed is large
  44. * enough, so do not enable FPU until it is necessary.
  45. */
  46. if (nbytes < bsize * (unsigned int)fpu_blocks_limit)
  47. return false;
  48. if (desc) {
  49. /* prevent sleeping if FPU is in use */
  50. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  51. }
  52. kernel_fpu_begin();
  53. return true;
  54. }
  55. static inline void glue_fpu_end(bool fpu_enabled)
  56. {
  57. if (fpu_enabled)
  58. kernel_fpu_end();
  59. }
  60. static inline void u128_to_be128(be128 *dst, const u128 *src)
  61. {
  62. dst->a = cpu_to_be64(src->a);
  63. dst->b = cpu_to_be64(src->b);
  64. }
  65. static inline void be128_to_u128(u128 *dst, const be128 *src)
  66. {
  67. dst->a = be64_to_cpu(src->a);
  68. dst->b = be64_to_cpu(src->b);
  69. }
  70. static inline void u128_inc(u128 *i)
  71. {
  72. i->b++;
  73. if (!i->b)
  74. i->a++;
  75. }
  76. extern int glue_ecb_crypt_128bit(const struct common_glue_ctx *gctx,
  77. struct blkcipher_desc *desc,
  78. struct scatterlist *dst,
  79. struct scatterlist *src, unsigned int nbytes);
  80. extern int glue_cbc_encrypt_128bit(const common_glue_func_t fn,
  81. struct blkcipher_desc *desc,
  82. struct scatterlist *dst,
  83. struct scatterlist *src,
  84. unsigned int nbytes);
  85. extern int glue_cbc_decrypt_128bit(const struct common_glue_ctx *gctx,
  86. struct blkcipher_desc *desc,
  87. struct scatterlist *dst,
  88. struct scatterlist *src,
  89. unsigned int nbytes);
  90. extern int glue_ctr_crypt_128bit(const struct common_glue_ctx *gctx,
  91. struct blkcipher_desc *desc,
  92. struct scatterlist *dst,
  93. struct scatterlist *src, unsigned int nbytes);
  94. #endif /* _CRYPTO_GLUE_HELPER_H */