properly init sg in empty case
[cryptodev-linux.git] / cryptlib.h
blob6bec659ea12ed19311abb40fe8045628d4260262
1 #ifndef CRYPTLIB_H
2 # define CRYPTLIB_H
4 struct cipher_data {
5 int init; /* 0 uninitialized */
6 int blocksize;
7 int aead;
8 int stream;
9 int ivsize;
10 int alignmask;
11 struct {
12 /* block ciphers */
13 struct crypto_ablkcipher *s;
14 struct ablkcipher_request *request;
16 /* AEAD ciphers */
17 struct crypto_aead *as;
18 struct aead_request *arequest;
20 struct cryptodev_result *result;
21 uint8_t iv[EALG_MAX_BLOCK_LEN];
22 } async;
25 int cryptodev_cipher_init(struct cipher_data *out, const char *alg_name,
26 uint8_t *key, size_t keylen, int stream, int aead);
27 void cryptodev_cipher_deinit(struct cipher_data *cdata);
28 ssize_t cryptodev_cipher_decrypt(struct cipher_data *cdata,
29 const struct scatterlist *sg1,
30 struct scatterlist *sg2, size_t len);
31 ssize_t cryptodev_cipher_encrypt(struct cipher_data *cdata,
32 const struct scatterlist *sg1,
33 struct scatterlist *sg2, size_t len);
35 /* AEAD */
36 inline static void cryptodev_cipher_auth(struct cipher_data *cdata,
37 const struct scatterlist *sg1, size_t len)
39 aead_request_set_assoc(cdata->async.arequest,
40 (struct scatterlist *)sg1, len);
43 inline static void cryptodev_cipher_set_tag_size(struct cipher_data *cdata, int size)
45 if (likely(cdata->aead != 0))
46 crypto_aead_setauthsize(cdata->async.as, size);
49 inline static int cryptodev_cipher_get_tag_size(struct cipher_data *cdata)
51 if (likely(cdata->init && cdata->aead != 0))
52 return crypto_aead_authsize(cdata->async.as);
53 else
54 return 0;
57 inline static void cryptodev_cipher_set_iv(struct cipher_data *cdata,
58 void *iv, size_t iv_size)
60 memcpy(cdata->async.iv, iv, min(iv_size, sizeof(cdata->async.iv)));
63 inline static void cryptodev_cipher_get_iv(struct cipher_data *cdata,
64 void *iv, size_t iv_size)
66 memcpy(iv, cdata->async.iv, min(iv_size, sizeof(cdata->async.iv)));
69 /* Hash */
70 struct hash_data {
71 int init; /* 0 uninitialized */
72 int digestsize;
73 int alignmask;
74 struct {
75 struct crypto_ahash *s;
76 struct cryptodev_result *result;
77 struct ahash_request *request;
78 } async;
81 int cryptodev_hash_final(struct hash_data *hdata, void *output);
82 ssize_t cryptodev_hash_update(struct hash_data *hdata,
83 struct scatterlist *sg, size_t len);
84 int cryptodev_hash_reset(struct hash_data *hdata);
85 void cryptodev_hash_deinit(struct hash_data *hdata);
86 int cryptodev_hash_init(struct hash_data *hdata, const char *alg_name,
87 int hmac_mode, void *mackey, size_t mackeylen);
90 #endif