aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/staging/crypto/cryptodev/cryptlib.h
blob: 074428467cb3049f3f3b12bf9f296e8008683aa9 (plain)
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
#ifndef CRYPTLIB_H
# define CRYPTLIB_H

struct cipher_data {
	int init; /* 0 uninitialized */
	int blocksize;
	int aead;
	int stream;
	int ivsize;
	int alignmask;
	struct {
		/* block ciphers */
		struct crypto_ablkcipher *s;
		struct ablkcipher_request *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);
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 */
	aead_request_set_assoc(cdata->async.arequest, len ? sg1 : NULL, len);
}

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);


#endif