2 * Symmetric key ciphers.
4 * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au>
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)
13 #ifndef _CRYPTO_SKCIPHER_H
14 #define _CRYPTO_SKCIPHER_H
16 #include <linux/crypto.h>
18 struct crypto_skcipher;
19 struct skcipher_request;
22 struct crypto_alg base;
25 int crypto_register_skcipher(struct skcipher_alg *alg);
27 struct crypto_skcipher {
28 int (*setkey)(struct crypto_skcipher *tfm, const u8 *key,
30 int (*encrypt)(struct skcipher_request *req);
31 int (*decrypt)(struct skcipher_request *req);
36 struct crypto_tfm base;
39 struct crypto_sync_skcipher {
40 struct crypto_skcipher base;
43 struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name,
46 static inline struct crypto_sync_skcipher *
47 crypto_alloc_sync_skcipher(const char *alg_name, u32 type, u32 mask)
49 return (void *) crypto_alloc_skcipher(alg_name, type, mask);
52 static inline void crypto_free_skcipher(struct crypto_skcipher *tfm)
57 static inline void crypto_free_sync_skcipher(struct crypto_sync_skcipher *tfm)
59 crypto_free_skcipher(&tfm->base);
62 struct skcipher_request {
66 struct scatterlist *src;
67 struct scatterlist *dst;
69 struct crypto_tfm *tfm;
72 #define MAX_SYNC_SKCIPHER_REQSIZE 384
73 #define SYNC_SKCIPHER_REQUEST_ON_STACK(name, tfm) \
74 char __##name##_desc[sizeof(struct skcipher_request) + \
75 MAX_SYNC_SKCIPHER_REQSIZE + \
76 (!(sizeof((struct crypto_sync_skcipher *)1 == \
78 ] CRYPTO_MINALIGN_ATTR; \
79 struct skcipher_request *name = (void *)__##name##_desc
81 static inline int crypto_skcipher_setkey(struct crypto_skcipher *tfm,
82 const u8 *key, unsigned int keylen)
84 return tfm->setkey(tfm, key, keylen);
87 static inline struct crypto_skcipher *crypto_skcipher_reqtfm(
88 struct skcipher_request *req)
90 return container_of(req->tfm, struct crypto_skcipher, base);
93 static inline int crypto_skcipher_encrypt(struct skcipher_request *req)
95 return crypto_skcipher_reqtfm(req)->encrypt(req);
98 static inline int crypto_skcipher_decrypt(struct skcipher_request *req)
100 return crypto_skcipher_reqtfm(req)->decrypt(req);
103 static inline void skcipher_request_set_tfm(struct skcipher_request *req,
104 struct crypto_skcipher *tfm)
106 req->tfm = &tfm->base;
109 static inline void skcipher_request_set_sync_tfm(struct skcipher_request *req,
110 struct crypto_sync_skcipher *tfm)
112 skcipher_request_set_tfm(req, &tfm->base);
115 static inline void skcipher_request_set_crypt(
116 struct skcipher_request *req,
117 struct scatterlist *src, struct scatterlist *dst,
118 unsigned int cryptlen, void *iv)
122 req->cryptlen = cryptlen;
126 #endif /* _CRYPTO_SKCIPHER_H */