skcipher.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Symmetric key ciphers.
  3. *
  4. * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. *
  11. */
  12. #ifndef _CRYPTO_SKCIPHER_H
  13. #define _CRYPTO_SKCIPHER_H
  14. #include <linux/crypto.h>
  15. #include <linux/kernel.h>
  16. #include <linux/slab.h>
  17. /**
  18. * struct skcipher_givcrypt_request - Crypto request with IV generation
  19. * @seq: Sequence number for IV generation
  20. * @giv: Space for generated IV
  21. * @creq: The crypto request itself
  22. */
  23. struct skcipher_givcrypt_request {
  24. u64 seq;
  25. u8 *giv;
  26. struct ablkcipher_request creq;
  27. };
  28. static inline struct crypto_ablkcipher *skcipher_givcrypt_reqtfm(
  29. struct skcipher_givcrypt_request *req)
  30. {
  31. return crypto_ablkcipher_reqtfm(&req->creq);
  32. }
  33. static inline int crypto_skcipher_givencrypt(
  34. struct skcipher_givcrypt_request *req)
  35. {
  36. struct ablkcipher_tfm *crt =
  37. crypto_ablkcipher_crt(skcipher_givcrypt_reqtfm(req));
  38. return crt->givencrypt(req);
  39. };
  40. static inline int crypto_skcipher_givdecrypt(
  41. struct skcipher_givcrypt_request *req)
  42. {
  43. struct ablkcipher_tfm *crt =
  44. crypto_ablkcipher_crt(skcipher_givcrypt_reqtfm(req));
  45. return crt->givdecrypt(req);
  46. };
  47. static inline void skcipher_givcrypt_set_tfm(
  48. struct skcipher_givcrypt_request *req, struct crypto_ablkcipher *tfm)
  49. {
  50. req->creq.base.tfm = crypto_ablkcipher_tfm(tfm);
  51. }
  52. static inline struct skcipher_givcrypt_request *skcipher_givcrypt_cast(
  53. struct crypto_async_request *req)
  54. {
  55. return container_of(ablkcipher_request_cast(req),
  56. struct skcipher_givcrypt_request, creq);
  57. }
  58. static inline struct skcipher_givcrypt_request *skcipher_givcrypt_alloc(
  59. struct crypto_ablkcipher *tfm, gfp_t gfp)
  60. {
  61. struct skcipher_givcrypt_request *req;
  62. req = kmalloc(sizeof(struct skcipher_givcrypt_request) +
  63. crypto_ablkcipher_reqsize(tfm), gfp);
  64. if (likely(req))
  65. skcipher_givcrypt_set_tfm(req, tfm);
  66. return req;
  67. }
  68. static inline void skcipher_givcrypt_free(struct skcipher_givcrypt_request *req)
  69. {
  70. kfree(req);
  71. }
  72. static inline void skcipher_givcrypt_set_callback(
  73. struct skcipher_givcrypt_request *req, u32 flags,
  74. crypto_completion_t compl, void *data)
  75. {
  76. ablkcipher_request_set_callback(&req->creq, flags, compl, data);
  77. }
  78. static inline void skcipher_givcrypt_set_crypt(
  79. struct skcipher_givcrypt_request *req,
  80. struct scatterlist *src, struct scatterlist *dst,
  81. unsigned int nbytes, void *iv)
  82. {
  83. ablkcipher_request_set_crypt(&req->creq, src, dst, nbytes, iv);
  84. }
  85. static inline void skcipher_givcrypt_set_giv(
  86. struct skcipher_givcrypt_request *req, u8 *giv, u64 seq)
  87. {
  88. req->giv = giv;
  89. req->seq = seq;
  90. }
  91. #endif /* _CRYPTO_SKCIPHER_H */