summaryrefslogtreecommitdiffstats
path: root/include/crypto
diff options
context:
space:
mode:
authorHerbert Xu <herbert@gondor.apana.org.au>2016-07-12 01:17:31 -0400
committerHerbert Xu <herbert@gondor.apana.org.au>2016-07-18 05:35:36 -0400
commit4e6c3df4d729f85997cbf276bfa8ffd8579b8e77 (patch)
tree3293b9ab0a019308724c8b44430cadc0a3b4488d /include/crypto
parenteb9bc8e7afaa9f062105dad55ec1c0663d961bb3 (diff)
crypto: skcipher - Add low-level skcipher interface
This patch allows skcipher algorithms and instances to be created and registered with the crypto API. They are accessible through the top-level skcipher interface, along with ablkcipher/blkcipher algorithms and instances. This patch also introduces a new parameter called chunk size which is meant for ciphers such as CTR and CTS which ostensibly can handle arbitrary lengths, but still behave like block ciphers in that you can only process a partial block at the very end. For these ciphers the block size will continue to be set to 1 as it is now while the chunk size will be set to the underlying block size. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'include/crypto')
-rw-r--r--include/crypto/internal/skcipher.h87
-rw-r--r--include/crypto/skcipher.h130
2 files changed, 217 insertions, 0 deletions
diff --git a/include/crypto/internal/skcipher.h b/include/crypto/internal/skcipher.h
index 2cf7a61ece59..ce6619c339fe 100644
--- a/include/crypto/internal/skcipher.h
+++ b/include/crypto/internal/skcipher.h
@@ -19,12 +19,46 @@
19 19
20struct rtattr; 20struct rtattr;
21 21
22struct skcipher_instance {
23 void (*free)(struct skcipher_instance *inst);
24 union {
25 struct {
26 char head[offsetof(struct skcipher_alg, base)];
27 struct crypto_instance base;
28 } s;
29 struct skcipher_alg alg;
30 };
31};
32
22struct crypto_skcipher_spawn { 33struct crypto_skcipher_spawn {
23 struct crypto_spawn base; 34 struct crypto_spawn base;
24}; 35};
25 36
26extern const struct crypto_type crypto_givcipher_type; 37extern const struct crypto_type crypto_givcipher_type;
27 38
39static inline struct crypto_instance *skcipher_crypto_instance(
40 struct skcipher_instance *inst)
41{
42 return &inst->s.base;
43}
44
45static inline struct skcipher_instance *skcipher_alg_instance(
46 struct crypto_skcipher *skcipher)
47{
48 return container_of(crypto_skcipher_alg(skcipher),
49 struct skcipher_instance, alg);
50}
51
52static inline void *skcipher_instance_ctx(struct skcipher_instance *inst)
53{
54 return crypto_instance_ctx(skcipher_crypto_instance(inst));
55}
56
57static inline void skcipher_request_complete(struct skcipher_request *req, int err)
58{
59 req->base.complete(&req->base, err);
60}
61
28static inline void crypto_set_skcipher_spawn( 62static inline void crypto_set_skcipher_spawn(
29 struct crypto_skcipher_spawn *spawn, struct crypto_instance *inst) 63 struct crypto_skcipher_spawn *spawn, struct crypto_instance *inst)
30{ 64{
@@ -33,6 +67,8 @@ static inline void crypto_set_skcipher_spawn(
33 67
34int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name, 68int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name,
35 u32 type, u32 mask); 69 u32 type, u32 mask);
70int crypto_grab_skcipher2(struct crypto_skcipher_spawn *spawn,
71 const char *name, u32 type, u32 mask);
36 72
37struct crypto_alg *crypto_lookup_skcipher(const char *name, u32 type, u32 mask); 73struct crypto_alg *crypto_lookup_skcipher(const char *name, u32 type, u32 mask);
38 74
@@ -47,6 +83,12 @@ static inline struct crypto_alg *crypto_skcipher_spawn_alg(
47 return spawn->base.alg; 83 return spawn->base.alg;
48} 84}
49 85
86static inline struct skcipher_alg *crypto_spawn_skcipher_alg(
87 struct crypto_skcipher_spawn *spawn)
88{
89 return container_of(spawn->base.alg, struct skcipher_alg, base);
90}
91
50static inline struct crypto_ablkcipher *crypto_spawn_skcipher( 92static inline struct crypto_ablkcipher *crypto_spawn_skcipher(
51 struct crypto_skcipher_spawn *spawn) 93 struct crypto_skcipher_spawn *spawn)
52{ 94{
@@ -55,6 +97,25 @@ static inline struct crypto_ablkcipher *crypto_spawn_skcipher(
55 crypto_skcipher_mask(0))); 97 crypto_skcipher_mask(0)));
56} 98}
57 99
100static inline struct crypto_skcipher *crypto_spawn_skcipher2(
101 struct crypto_skcipher_spawn *spawn)
102{
103 return crypto_spawn_tfm2(&spawn->base);
104}
105
106static inline void crypto_skcipher_set_reqsize(
107 struct crypto_skcipher *skcipher, unsigned int reqsize)
108{
109 skcipher->reqsize = reqsize;
110}
111
112int crypto_register_skcipher(struct skcipher_alg *alg);
113void crypto_unregister_skcipher(struct skcipher_alg *alg);
114int crypto_register_skciphers(struct skcipher_alg *algs, int count);
115void crypto_unregister_skciphers(struct skcipher_alg *algs, int count);
116int skcipher_register_instance(struct crypto_template *tmpl,
117 struct skcipher_instance *inst);
118
58int skcipher_null_givencrypt(struct skcipher_givcrypt_request *req); 119int skcipher_null_givencrypt(struct skcipher_givcrypt_request *req);
59int skcipher_null_givdecrypt(struct skcipher_givcrypt_request *req); 120int skcipher_null_givdecrypt(struct skcipher_givcrypt_request *req);
60const char *crypto_default_geniv(const struct crypto_alg *alg); 121const char *crypto_default_geniv(const struct crypto_alg *alg);
@@ -122,5 +183,31 @@ static inline u32 skcipher_request_flags(struct skcipher_request *req)
122 return req->base.flags; 183 return req->base.flags;
123} 184}
124 185
186static inline unsigned int crypto_skcipher_alg_min_keysize(
187 struct skcipher_alg *alg)
188{
189 if ((alg->base.cra_flags & CRYPTO_ALG_TYPE_MASK) ==
190 CRYPTO_ALG_TYPE_BLKCIPHER)
191 return alg->base.cra_blkcipher.min_keysize;
192
193 if (alg->base.cra_ablkcipher.encrypt)
194 return alg->base.cra_ablkcipher.min_keysize;
195
196 return alg->min_keysize;
197}
198
199static inline unsigned int crypto_skcipher_alg_max_keysize(
200 struct skcipher_alg *alg)
201{
202 if ((alg->base.cra_flags & CRYPTO_ALG_TYPE_MASK) ==
203 CRYPTO_ALG_TYPE_BLKCIPHER)
204 return alg->base.cra_blkcipher.max_keysize;
205
206 if (alg->base.cra_ablkcipher.encrypt)
207 return alg->base.cra_ablkcipher.max_keysize;
208
209 return alg->max_keysize;
210}
211
125#endif /* _CRYPTO_INTERNAL_SKCIPHER_H */ 212#endif /* _CRYPTO_INTERNAL_SKCIPHER_H */
126 213
diff --git a/include/crypto/skcipher.h b/include/crypto/skcipher.h
index 0f987f50bb52..a381f57ea695 100644
--- a/include/crypto/skcipher.h
+++ b/include/crypto/skcipher.h
@@ -65,6 +65,75 @@ struct crypto_skcipher {
65 struct crypto_tfm base; 65 struct crypto_tfm base;
66}; 66};
67 67
68/**
69 * struct skcipher_alg - symmetric key cipher definition
70 * @min_keysize: Minimum key size supported by the transformation. This is the
71 * smallest key length supported by this transformation algorithm.
72 * This must be set to one of the pre-defined values as this is
73 * not hardware specific. Possible values for this field can be
74 * found via git grep "_MIN_KEY_SIZE" include/crypto/
75 * @max_keysize: Maximum key size supported by the transformation. This is the
76 * largest key length supported by this transformation algorithm.
77 * This must be set to one of the pre-defined values as this is
78 * not hardware specific. Possible values for this field can be
79 * found via git grep "_MAX_KEY_SIZE" include/crypto/
80 * @setkey: Set key for the transformation. This function is used to either
81 * program a supplied key into the hardware or store the key in the
82 * transformation context for programming it later. Note that this
83 * function does modify the transformation context. This function can
84 * be called multiple times during the existence of the transformation
85 * object, so one must make sure the key is properly reprogrammed into
86 * the hardware. This function is also responsible for checking the key
87 * length for validity. In case a software fallback was put in place in
88 * the @cra_init call, this function might need to use the fallback if
89 * the algorithm doesn't support all of the key sizes.
90 * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt
91 * the supplied scatterlist containing the blocks of data. The crypto
92 * API consumer is responsible for aligning the entries of the
93 * scatterlist properly and making sure the chunks are correctly
94 * sized. In case a software fallback was put in place in the
95 * @cra_init call, this function might need to use the fallback if
96 * the algorithm doesn't support all of the key sizes. In case the
97 * key was stored in transformation context, the key might need to be
98 * re-programmed into the hardware in this function. This function
99 * shall not modify the transformation context, as this function may
100 * be called in parallel with the same transformation object.
101 * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt
102 * and the conditions are exactly the same.
103 * @init: Initialize the cryptographic transformation object. This function
104 * is used to initialize the cryptographic transformation object.
105 * This function is called only once at the instantiation time, right
106 * after the transformation context was allocated. In case the
107 * cryptographic hardware has some special requirements which need to
108 * be handled by software, this function shall check for the precise
109 * requirement of the transformation and put any software fallbacks
110 * in place.
111 * @exit: Deinitialize the cryptographic transformation object. This is a
112 * counterpart to @init, used to remove various changes set in
113 * @init.
114 * @ivsize: IV size applicable for transformation. The consumer must provide an
115 * IV of exactly that size to perform the encrypt or decrypt operation.
116 * @chunksize: Equal to the block size except for stream ciphers such as
117 * CTR where it is set to the underlying block size.
118 *
119 * All fields except @ivsize are mandatory and must be filled.
120 */
121struct skcipher_alg {
122 int (*setkey)(struct crypto_skcipher *tfm, const u8 *key,
123 unsigned int keylen);
124 int (*encrypt)(struct skcipher_request *req);
125 int (*decrypt)(struct skcipher_request *req);
126 int (*init)(struct crypto_skcipher *tfm);
127 void (*exit)(struct crypto_skcipher *tfm);
128
129 unsigned int min_keysize;
130 unsigned int max_keysize;
131 unsigned int ivsize;
132 unsigned int chunksize;
133
134 struct crypto_alg base;
135};
136
68#define SKCIPHER_REQUEST_ON_STACK(name, tfm) \ 137#define SKCIPHER_REQUEST_ON_STACK(name, tfm) \
69 char __##name##_desc[sizeof(struct skcipher_request) + \ 138 char __##name##_desc[sizeof(struct skcipher_request) + \
70 crypto_skcipher_reqsize(tfm)] CRYPTO_MINALIGN_ATTR; \ 139 crypto_skcipher_reqsize(tfm)] CRYPTO_MINALIGN_ATTR; \
@@ -231,12 +300,43 @@ static inline int crypto_has_skcipher(const char *alg_name, u32 type,
231 crypto_skcipher_mask(mask)); 300 crypto_skcipher_mask(mask));
232} 301}
233 302
303/**
304 * crypto_has_skcipher2() - Search for the availability of an skcipher.
305 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
306 * skcipher
307 * @type: specifies the type of the skcipher
308 * @mask: specifies the mask for the skcipher
309 *
310 * Return: true when the skcipher is known to the kernel crypto API; false
311 * otherwise
312 */
313int crypto_has_skcipher2(const char *alg_name, u32 type, u32 mask);
314
234static inline const char *crypto_skcipher_driver_name( 315static inline const char *crypto_skcipher_driver_name(
235 struct crypto_skcipher *tfm) 316 struct crypto_skcipher *tfm)
236{ 317{
237 return crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm)); 318 return crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm));
238} 319}
239 320
321static inline struct skcipher_alg *crypto_skcipher_alg(
322 struct crypto_skcipher *tfm)
323{
324 return container_of(crypto_skcipher_tfm(tfm)->__crt_alg,
325 struct skcipher_alg, base);
326}
327
328static inline unsigned int crypto_skcipher_alg_ivsize(struct skcipher_alg *alg)
329{
330 if ((alg->base.cra_flags & CRYPTO_ALG_TYPE_MASK) ==
331 CRYPTO_ALG_TYPE_BLKCIPHER)
332 return alg->base.cra_blkcipher.ivsize;
333
334 if (alg->base.cra_ablkcipher.encrypt)
335 return alg->base.cra_ablkcipher.ivsize;
336
337 return alg->ivsize;
338}
339
240/** 340/**
241 * crypto_skcipher_ivsize() - obtain IV size 341 * crypto_skcipher_ivsize() - obtain IV size
242 * @tfm: cipher handle 342 * @tfm: cipher handle
@@ -251,6 +351,36 @@ static inline unsigned int crypto_skcipher_ivsize(struct crypto_skcipher *tfm)
251 return tfm->ivsize; 351 return tfm->ivsize;
252} 352}
253 353
354static inline unsigned int crypto_skcipher_alg_chunksize(
355 struct skcipher_alg *alg)
356{
357 if ((alg->base.cra_flags & CRYPTO_ALG_TYPE_MASK) ==
358 CRYPTO_ALG_TYPE_BLKCIPHER)
359 return alg->base.cra_blocksize;
360
361 if (alg->base.cra_ablkcipher.encrypt)
362 return alg->base.cra_blocksize;
363
364 return alg->chunksize;
365}
366
367/**
368 * crypto_skcipher_chunksize() - obtain chunk size
369 * @tfm: cipher handle
370 *
371 * The block size is set to one for ciphers such as CTR. However,
372 * you still need to provide incremental updates in multiples of
373 * the underlying block size as the IV does not have sub-block
374 * granularity. This is known in this API as the chunk size.
375 *
376 * Return: chunk size in bytes
377 */
378static inline unsigned int crypto_skcipher_chunksize(
379 struct crypto_skcipher *tfm)
380{
381 return crypto_skcipher_alg_chunksize(crypto_skcipher_alg(tfm));
382}
383
254/** 384/**
255 * crypto_skcipher_blocksize() - obtain block size of cipher 385 * crypto_skcipher_blocksize() - obtain block size of cipher
256 * @tfm: cipher handle 386 * @tfm: cipher handle