forked from cryptodev-linux/cryptodev-linux
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcryptlib.h
More file actions
148 lines (128 loc) · 4.32 KB
/
cryptlib.h
File metadata and controls
148 lines (128 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#ifndef CRYPTLIB_H
# define CRYPTLIB_H
#include <linux/version.h>
struct cryptodev_result {
struct completion completion;
int err;
};
#include "cipherapi.h"
struct cipher_data {
int init; /* 0 uninitialized */
int blocksize;
int aead;
int stream;
int ivsize;
int alignmask;
struct {
/* block ciphers */
cryptodev_crypto_blkcipher_t *s;
cryptodev_blkcipher_request_t *request;
/* AEAD ciphers */
struct crypto_aead *as;
struct aead_request *arequest;
struct cryptodev_result result;
uint8_t iv[EALG_MAX_BLOCK_LEN];
} async;
};
int cryptodev_cipher_init(struct cipher_data *out, const char *alg_name,
uint8_t *key, size_t keylen, int stream, int aead);
void cryptodev_cipher_deinit(struct cipher_data *cdata);
int cryptodev_get_cipher_key(uint8_t *key, struct session_op *sop, int aead);
int cryptodev_get_cipher_keylen(unsigned int *keylen, struct session_op *sop,
int aead);
ssize_t cryptodev_cipher_decrypt(struct cipher_data *cdata,
const struct scatterlist *sg1,
struct scatterlist *sg2, size_t len);
ssize_t cryptodev_cipher_encrypt(struct cipher_data *cdata,
const struct scatterlist *sg1,
struct scatterlist *sg2, size_t len);
/* AEAD */
static inline void cryptodev_cipher_auth(struct cipher_data *cdata,
struct scatterlist *sg1, size_t len)
{
/* for some reason we _have_ to call that even for zero length sgs */
#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 3, 0))
aead_request_set_assoc(cdata->async.arequest, len ? sg1 : NULL, len);
#else
aead_request_set_ad(cdata->async.arequest, len);
#endif
}
static inline void cryptodev_cipher_set_tag_size(struct cipher_data *cdata, int size)
{
if (likely(cdata->aead != 0))
crypto_aead_setauthsize(cdata->async.as, size);
}
static inline int cryptodev_cipher_get_tag_size(struct cipher_data *cdata)
{
if (likely(cdata->init && cdata->aead != 0))
return crypto_aead_authsize(cdata->async.as);
else
return 0;
}
static inline void cryptodev_cipher_set_iv(struct cipher_data *cdata,
void *iv, size_t iv_size)
{
memcpy(cdata->async.iv, iv, min(iv_size, sizeof(cdata->async.iv)));
}
static inline void cryptodev_cipher_get_iv(struct cipher_data *cdata,
void *iv, size_t iv_size)
{
memcpy(iv, cdata->async.iv, min(iv_size, sizeof(cdata->async.iv)));
}
/* Hash */
struct hash_data {
int init; /* 0 uninitialized */
int digestsize;
int alignmask;
struct {
struct crypto_ahash *s;
struct cryptodev_result result;
struct ahash_request *request;
} async;
};
int cryptodev_hash_final(struct hash_data *hdata, void *output);
ssize_t cryptodev_hash_update(struct hash_data *hdata,
struct scatterlist *sg, size_t len);
int cryptodev_hash_reset(struct hash_data *hdata);
void cryptodev_hash_deinit(struct hash_data *hdata);
int cryptodev_hash_init(struct hash_data *hdata, const char *alg_name,
int hmac_mode, void *mackey, size_t mackeylen);
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29))
int cryptodev_hash_copy(struct hash_data *dst, struct hash_data *src);
#endif
/* Compression */
struct compr_data {
int init; /* 0 uninitialized */
int alignmask;
struct crypto_comp *tfm;
u8 *srcbuf;
u8 *dstbuf;
int slowpath_warned;
uint32_t numchunks;
uint32_t chunklens[CRYPTODEV_COMP_MAX_CHUNKS];
uint32_t chunkdlens[CRYPTODEV_COMP_MAX_CHUNKS];
int chunkrets[CRYPTODEV_COMP_MAX_CHUNKS];
};
void cryptodev_compr_deinit(struct compr_data *cdata);
int cryptodev_compr_init(struct compr_data *cdata, const char *alg_name);
ssize_t cryptodev_compr_compress(struct compr_data *cdata,
const struct scatterlist *src, struct scatterlist *dst,
unsigned int slen, unsigned int dlen);
ssize_t cryptodev_compr_decompress(struct compr_data *cdata,
const struct scatterlist *src, struct scatterlist *dst,
unsigned int slen, unsigned int dlen);
static inline void cryptodev_compr_set_chunks(struct compr_data *comprdata,
size_t numchunks, const uint32_t *chunklens, const uint32_t *chunkdlens)
{
comprdata->numchunks = numchunks;
memcpy(comprdata->chunklens, chunklens, numchunks * sizeof(uint32_t));
memcpy(comprdata->chunkdlens, chunkdlens, numchunks * sizeof(uint32_t));
}
static inline void cryptodev_compr_get_chunkdlens(
const struct compr_data *comprdata,
uint32_t *chunkdlens, int *chunkrets)
{
memcpy(chunkdlens, comprdata->chunkdlens, comprdata->numchunks * sizeof(uint32_t));
memcpy(chunkrets, comprdata->chunkrets, comprdata->numchunks * sizeof(int));
}
#endif