Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 1 | /* |
| 2 | * AEAD: Authenticated Encryption with Associated Data |
| 3 | * |
Herbert Xu | b0d955b | 2015-08-14 15:30:41 +0800 | [diff] [blame] | 4 | * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au> |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 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 | |
| 13 | #ifndef _CRYPTO_INTERNAL_AEAD_H |
| 14 | #define _CRYPTO_INTERNAL_AEAD_H |
| 15 | |
| 16 | #include <crypto/aead.h> |
| 17 | #include <crypto/algapi.h> |
Herbert Xu | f5d8660 | 2015-06-18 14:00:49 +0800 | [diff] [blame] | 18 | #include <linux/stddef.h> |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 19 | #include <linux/types.h> |
| 20 | |
| 21 | struct rtattr; |
| 22 | |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 23 | struct aead_instance { |
Herbert Xu | ba75e15 | 2015-07-09 07:17:17 +0800 | [diff] [blame] | 24 | void (*free)(struct aead_instance *inst); |
Herbert Xu | f5d8660 | 2015-06-18 14:00:49 +0800 | [diff] [blame] | 25 | union { |
| 26 | struct { |
| 27 | char head[offsetof(struct aead_alg, base)]; |
| 28 | struct crypto_instance base; |
| 29 | } s; |
| 30 | struct aead_alg alg; |
| 31 | }; |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 32 | }; |
| 33 | |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 34 | struct crypto_aead_spawn { |
| 35 | struct crypto_spawn base; |
| 36 | }; |
| 37 | |
Herbert Xu | 2c11a3f | 2015-07-08 12:15:14 +0800 | [diff] [blame] | 38 | struct aead_queue { |
| 39 | struct crypto_queue base; |
| 40 | }; |
| 41 | |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 42 | static inline void *crypto_aead_ctx(struct crypto_aead *tfm) |
| 43 | { |
| 44 | return crypto_tfm_ctx(&tfm->base); |
| 45 | } |
| 46 | |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 47 | static inline struct crypto_instance *aead_crypto_instance( |
| 48 | struct aead_instance *inst) |
| 49 | { |
| 50 | return container_of(&inst->alg.base, struct crypto_instance, alg); |
| 51 | } |
| 52 | |
| 53 | static inline struct aead_instance *aead_instance(struct crypto_instance *inst) |
| 54 | { |
| 55 | return container_of(&inst->alg, struct aead_instance, alg.base); |
| 56 | } |
| 57 | |
Herbert Xu | 5c98d62 | 2015-05-28 22:07:55 +0800 | [diff] [blame] | 58 | static inline struct aead_instance *aead_alg_instance(struct crypto_aead *aead) |
| 59 | { |
Herbert Xu | b0d955b | 2015-08-14 15:30:41 +0800 | [diff] [blame] | 60 | return aead_instance(crypto_tfm_alg_instance(&aead->base)); |
Herbert Xu | 5c98d62 | 2015-05-28 22:07:55 +0800 | [diff] [blame] | 61 | } |
| 62 | |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 63 | static inline void *aead_instance_ctx(struct aead_instance *inst) |
| 64 | { |
| 65 | return crypto_instance_ctx(aead_crypto_instance(inst)); |
| 66 | } |
| 67 | |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 68 | static inline void *aead_request_ctx(struct aead_request *req) |
| 69 | { |
| 70 | return req->__ctx; |
| 71 | } |
| 72 | |
| 73 | static inline void aead_request_complete(struct aead_request *req, int err) |
| 74 | { |
| 75 | req->base.complete(&req->base, err); |
| 76 | } |
| 77 | |
| 78 | static inline u32 aead_request_flags(struct aead_request *req) |
| 79 | { |
| 80 | return req->base.flags; |
| 81 | } |
| 82 | |
Tadeusz Struk | 53a0bd7 | 2016-02-01 11:17:30 -0800 | [diff] [blame] | 83 | static inline struct aead_request *aead_request_cast( |
| 84 | struct crypto_async_request *req) |
| 85 | { |
| 86 | return container_of(req, struct aead_request, base); |
| 87 | } |
| 88 | |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 89 | static inline void crypto_set_aead_spawn( |
| 90 | struct crypto_aead_spawn *spawn, struct crypto_instance *inst) |
| 91 | { |
| 92 | crypto_set_spawn(&spawn->base, inst); |
| 93 | } |
| 94 | |
Herbert Xu | d29ce98 | 2007-12-12 19:24:27 +0800 | [diff] [blame] | 95 | int crypto_grab_aead(struct crypto_aead_spawn *spawn, const char *name, |
| 96 | u32 type, u32 mask); |
| 97 | |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 98 | static inline void crypto_drop_aead(struct crypto_aead_spawn *spawn) |
| 99 | { |
| 100 | crypto_drop_spawn(&spawn->base); |
| 101 | } |
| 102 | |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 103 | static inline struct aead_alg *crypto_spawn_aead_alg( |
| 104 | struct crypto_aead_spawn *spawn) |
| 105 | { |
| 106 | return container_of(spawn->base.alg, struct aead_alg, base); |
| 107 | } |
| 108 | |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 109 | static inline struct crypto_aead *crypto_spawn_aead( |
| 110 | struct crypto_aead_spawn *spawn) |
| 111 | { |
Herbert Xu | 5d1d65f | 2015-05-11 17:48:12 +0800 | [diff] [blame] | 112 | return crypto_spawn_tfm2(&spawn->base); |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 113 | } |
| 114 | |
Herbert Xu | 21b70134 | 2015-05-11 17:47:52 +0800 | [diff] [blame] | 115 | static inline void crypto_aead_set_reqsize(struct crypto_aead *aead, |
| 116 | unsigned int reqsize) |
| 117 | { |
Herbert Xu | b0d955b | 2015-08-14 15:30:41 +0800 | [diff] [blame] | 118 | aead->reqsize = reqsize; |
Herbert Xu | 21b70134 | 2015-05-11 17:47:52 +0800 | [diff] [blame] | 119 | } |
| 120 | |
Herbert Xu | 30e4c01 | 2015-05-22 16:30:48 +0800 | [diff] [blame] | 121 | static inline unsigned int crypto_aead_alg_maxauthsize(struct aead_alg *alg) |
| 122 | { |
Herbert Xu | b0d955b | 2015-08-14 15:30:41 +0800 | [diff] [blame] | 123 | return alg->maxauthsize; |
Herbert Xu | 30e4c01 | 2015-05-22 16:30:48 +0800 | [diff] [blame] | 124 | } |
| 125 | |
Herbert Xu | f569525 | 2015-05-21 15:11:05 +0800 | [diff] [blame] | 126 | static inline unsigned int crypto_aead_maxauthsize(struct crypto_aead *aead) |
| 127 | { |
Herbert Xu | 30e4c01 | 2015-05-22 16:30:48 +0800 | [diff] [blame] | 128 | return crypto_aead_alg_maxauthsize(crypto_aead_alg(aead)); |
Herbert Xu | f569525 | 2015-05-21 15:11:05 +0800 | [diff] [blame] | 129 | } |
| 130 | |
Herbert Xu | 2c11a3f | 2015-07-08 12:15:14 +0800 | [diff] [blame] | 131 | static inline void aead_init_queue(struct aead_queue *queue, |
| 132 | unsigned int max_qlen) |
| 133 | { |
| 134 | crypto_init_queue(&queue->base, max_qlen); |
| 135 | } |
| 136 | |
| 137 | static inline int aead_enqueue_request(struct aead_queue *queue, |
| 138 | struct aead_request *request) |
| 139 | { |
| 140 | return crypto_enqueue_request(&queue->base, &request->base); |
| 141 | } |
| 142 | |
| 143 | static inline struct aead_request *aead_dequeue_request( |
| 144 | struct aead_queue *queue) |
| 145 | { |
| 146 | struct crypto_async_request *req; |
| 147 | |
| 148 | req = crypto_dequeue_request(&queue->base); |
| 149 | |
| 150 | return req ? container_of(req, struct aead_request, base) : NULL; |
| 151 | } |
| 152 | |
| 153 | static inline struct aead_request *aead_get_backlog(struct aead_queue *queue) |
| 154 | { |
| 155 | struct crypto_async_request *req; |
| 156 | |
| 157 | req = crypto_get_backlog(&queue->base); |
| 158 | |
| 159 | return req ? container_of(req, struct aead_request, base) : NULL; |
| 160 | } |
| 161 | |
Herbert Xu | 7a530aa | 2016-07-12 13:17:33 +0800 | [diff] [blame] | 162 | static inline unsigned int crypto_aead_alg_chunksize(struct aead_alg *alg) |
| 163 | { |
| 164 | return alg->chunksize; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * crypto_aead_chunksize() - obtain chunk size |
| 169 | * @tfm: cipher handle |
| 170 | * |
| 171 | * The block size is set to one for ciphers such as CCM. However, |
| 172 | * you still need to provide incremental updates in multiples of |
| 173 | * the underlying block size as the IV does not have sub-block |
| 174 | * granularity. This is known in this API as the chunk size. |
| 175 | * |
| 176 | * Return: chunk size in bytes |
| 177 | */ |
| 178 | static inline unsigned int crypto_aead_chunksize(struct crypto_aead *tfm) |
| 179 | { |
| 180 | return crypto_aead_alg_chunksize(crypto_aead_alg(tfm)); |
| 181 | } |
| 182 | |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 183 | int crypto_register_aead(struct aead_alg *alg); |
Herbert Xu | 4361536 | 2015-05-28 22:07:57 +0800 | [diff] [blame] | 184 | void crypto_unregister_aead(struct aead_alg *alg); |
Herbert Xu | caab946 | 2015-05-28 22:07:59 +0800 | [diff] [blame] | 185 | int crypto_register_aeads(struct aead_alg *algs, int count); |
| 186 | void crypto_unregister_aeads(struct aead_alg *algs, int count); |
Herbert Xu | 63293c6 | 2015-05-21 15:11:08 +0800 | [diff] [blame] | 187 | int aead_register_instance(struct crypto_template *tmpl, |
| 188 | struct aead_instance *inst); |
| 189 | |
Herbert Xu | 5b6d2d7 | 2007-12-12 19:23:36 +0800 | [diff] [blame] | 190 | #endif /* _CRYPTO_INTERNAL_AEAD_H */ |
| 191 | |