glue_helper.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #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 le128_to_be128(be128 *dst, const le128 *src)
  61. {
  62. dst->a = cpu_to_be64(le64_to_cpu(src->a));
  63. dst->b = cpu_to_be64(le64_to_cpu(src->b));
  64. }
  65. static inline void be128_to_le128(le128 *dst, const be128 *src)
  66. {
  67. dst->a = cpu_to_le64(be64_to_cpu(src->a));
  68. dst->b = cpu_to_le64(be64_to_cpu(src->b));
  69. }
  70. static inline void le128_inc(le128 *i)
  71. {
  72. u64 a = le64_to_cpu(i->a);
  73. u64 b = le64_to_cpu(i->b);
  74. b++;
  75. if (!b)
  76. a++;
  77. i->a = cpu_to_le64(a);
  78. i->b = cpu_to_le64(b);
  79. }
  80. extern int glue_ecb_crypt_128bit(const struct common_glue_ctx *gctx,
  81. struct blkcipher_desc *desc,
  82. struct scatterlist *dst,
  83. struct scatterlist *src, unsigned int nbytes);
  84. extern int glue_cbc_encrypt_128bit(const common_glue_func_t fn,
  85. struct blkcipher_desc *desc,
  86. struct scatterlist *dst,
  87. struct scatterlist *src,
  88. unsigned int nbytes);
  89. extern int glue_cbc_decrypt_128bit(const struct common_glue_ctx *gctx,
  90. struct blkcipher_desc *desc,
  91. struct scatterlist *dst,
  92. struct scatterlist *src,
  93. unsigned int nbytes);
  94. extern int glue_ctr_crypt_128bit(const struct common_glue_ctx *gctx,
  95. struct blkcipher_desc *desc,
  96. struct scatterlist *dst,
  97. struct scatterlist *src, unsigned int nbytes);
  98. #endif /* _CRYPTO_GLUE_HELPER_H */