aead.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * AEAD: Authenticated Encryption with Associated Data
  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_AEAD_H
  13. #define _CRYPTO_AEAD_H
  14. #include <linux/crypto.h>
  15. #include <linux/kernel.h>
  16. #include <linux/slab.h>
  17. /**
  18. * struct aead_givcrypt_request - AEAD request with IV generation
  19. * @seq: Sequence number for IV generation
  20. * @giv: Space for generated IV
  21. * @areq: The AEAD request itself
  22. */
  23. struct aead_givcrypt_request {
  24. u64 seq;
  25. u8 *giv;
  26. struct aead_request areq;
  27. };
  28. static inline struct crypto_aead *aead_givcrypt_reqtfm(
  29. struct aead_givcrypt_request *req)
  30. {
  31. return crypto_aead_reqtfm(&req->areq);
  32. }
  33. static inline int crypto_aead_givencrypt(struct aead_givcrypt_request *req)
  34. {
  35. struct aead_tfm *crt = crypto_aead_crt(aead_givcrypt_reqtfm(req));
  36. return crt->givencrypt(req);
  37. };
  38. static inline int crypto_aead_givdecrypt(struct aead_givcrypt_request *req)
  39. {
  40. struct aead_tfm *crt = crypto_aead_crt(aead_givcrypt_reqtfm(req));
  41. return crt->givdecrypt(req);
  42. };
  43. static inline void aead_givcrypt_set_tfm(struct aead_givcrypt_request *req,
  44. struct crypto_aead *tfm)
  45. {
  46. req->areq.base.tfm = crypto_aead_tfm(tfm);
  47. }
  48. static inline struct aead_givcrypt_request *aead_givcrypt_alloc(
  49. struct crypto_aead *tfm, gfp_t gfp)
  50. {
  51. struct aead_givcrypt_request *req;
  52. req = kmalloc(sizeof(struct aead_givcrypt_request) +
  53. crypto_aead_reqsize(tfm), gfp);
  54. if (likely(req))
  55. aead_givcrypt_set_tfm(req, tfm);
  56. return req;
  57. }
  58. static inline void aead_givcrypt_free(struct aead_givcrypt_request *req)
  59. {
  60. kfree(req);
  61. }
  62. static inline void aead_givcrypt_set_callback(
  63. struct aead_givcrypt_request *req, u32 flags,
  64. crypto_completion_t complete, void *data)
  65. {
  66. aead_request_set_callback(&req->areq, flags, complete, data);
  67. }
  68. static inline void aead_givcrypt_set_crypt(struct aead_givcrypt_request *req,
  69. struct scatterlist *src,
  70. struct scatterlist *dst,
  71. unsigned int nbytes, void *iv)
  72. {
  73. aead_request_set_crypt(&req->areq, src, dst, nbytes, iv);
  74. }
  75. static inline void aead_givcrypt_set_assoc(struct aead_givcrypt_request *req,
  76. struct scatterlist *assoc,
  77. unsigned int assoclen)
  78. {
  79. aead_request_set_assoc(&req->areq, assoc, assoclen);
  80. }
  81. static inline void aead_givcrypt_set_giv(struct aead_givcrypt_request *req,
  82. u8 *giv, u64 seq)
  83. {
  84. req->giv = giv;
  85. req->seq = seq;
  86. }
  87. #endif /* _CRYPTO_AEAD_H */