1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * The MORUS-1280 Authenticated-Encryption Algorithm
4  *   Common glue skeleton -- header file
5  *
6  * Copyright (c) 2016-2018 Ondrej Mosnacek <omosnacek@gmail.com>
7  * Copyright (C) 2017-2018 Red Hat, Inc. All rights reserved.
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by the Free
11  * Software Foundation; either version 2 of the License, or (at your option)
12  * any later version.
13  */
14 
15 #ifndef _CRYPTO_MORUS1280_GLUE_H
16 #define _CRYPTO_MORUS1280_GLUE_H
17 
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <crypto/algapi.h>
21 #include <crypto/aead.h>
22 #include <crypto/morus_common.h>
23 
24 #define MORUS1280_WORD_SIZE 8
25 #define MORUS1280_BLOCK_SIZE (MORUS_BLOCK_WORDS * MORUS1280_WORD_SIZE)
26 
27 struct morus1280_block {
28 	u8 bytes[MORUS1280_BLOCK_SIZE];
29 };
30 
31 struct morus1280_glue_ops {
32 	void (*init)(void *state, const void *key, const void *iv);
33 	void (*ad)(void *state, const void *data, unsigned int length);
34 	void (*enc)(void *state, const void *src, void *dst, unsigned int length);
35 	void (*dec)(void *state, const void *src, void *dst, unsigned int length);
36 	void (*enc_tail)(void *state, const void *src, void *dst, unsigned int length);
37 	void (*dec_tail)(void *state, const void *src, void *dst, unsigned int length);
38 	void (*final)(void *state, void *tag_xor, u64 assoclen, u64 cryptlen);
39 };
40 
41 struct morus1280_ctx {
42 	const struct morus1280_glue_ops *ops;
43 	struct morus1280_block key;
44 };
45 
46 void crypto_morus1280_glue_init_ops(struct crypto_aead *aead,
47 				    const struct morus1280_glue_ops *ops);
48 int crypto_morus1280_glue_setkey(struct crypto_aead *aead, const u8 *key,
49 				 unsigned int keylen);
50 int crypto_morus1280_glue_setauthsize(struct crypto_aead *tfm,
51 				      unsigned int authsize);
52 int crypto_morus1280_glue_encrypt(struct aead_request *req);
53 int crypto_morus1280_glue_decrypt(struct aead_request *req);
54 
55 int cryptd_morus1280_glue_setkey(struct crypto_aead *aead, const u8 *key,
56 				 unsigned int keylen);
57 int cryptd_morus1280_glue_setauthsize(struct crypto_aead *aead,
58 				      unsigned int authsize);
59 int cryptd_morus1280_glue_encrypt(struct aead_request *req);
60 int cryptd_morus1280_glue_decrypt(struct aead_request *req);
61 int cryptd_morus1280_glue_init_tfm(struct crypto_aead *aead);
62 void cryptd_morus1280_glue_exit_tfm(struct crypto_aead *aead);
63 
64 #define MORUS1280_DECLARE_ALGS(id, driver_name, priority) \
65 	static const struct morus1280_glue_ops crypto_morus1280_##id##_ops = {\
66 		.init = crypto_morus1280_##id##_init, \
67 		.ad = crypto_morus1280_##id##_ad, \
68 		.enc = crypto_morus1280_##id##_enc, \
69 		.enc_tail = crypto_morus1280_##id##_enc_tail, \
70 		.dec = crypto_morus1280_##id##_dec, \
71 		.dec_tail = crypto_morus1280_##id##_dec_tail, \
72 		.final = crypto_morus1280_##id##_final, \
73 	}; \
74 	\
75 	static int crypto_morus1280_##id##_init_tfm(struct crypto_aead *tfm) \
76 	{ \
77 		crypto_morus1280_glue_init_ops(tfm, &crypto_morus1280_##id##_ops); \
78 		return 0; \
79 	} \
80 	\
81 	static void crypto_morus1280_##id##_exit_tfm(struct crypto_aead *tfm) \
82 	{ \
83 	} \
84 	\
85 	struct aead_alg crypto_morus1280_##id##_algs[] = {\
86 		{ \
87 			.setkey = crypto_morus1280_glue_setkey, \
88 			.setauthsize = crypto_morus1280_glue_setauthsize, \
89 			.encrypt = crypto_morus1280_glue_encrypt, \
90 			.decrypt = crypto_morus1280_glue_decrypt, \
91 			.init = crypto_morus1280_##id##_init_tfm, \
92 			.exit = crypto_morus1280_##id##_exit_tfm, \
93 			\
94 			.ivsize = MORUS_NONCE_SIZE, \
95 			.maxauthsize = MORUS_MAX_AUTH_SIZE, \
96 			.chunksize = MORUS1280_BLOCK_SIZE, \
97 			\
98 			.base = { \
99 				.cra_flags = CRYPTO_ALG_INTERNAL, \
100 				.cra_blocksize = 1, \
101 				.cra_ctxsize = sizeof(struct morus1280_ctx), \
102 				.cra_alignmask = 0, \
103 				\
104 				.cra_name = "__morus1280", \
105 				.cra_driver_name = "__"driver_name, \
106 				\
107 				.cra_module = THIS_MODULE, \
108 			} \
109 		}, { \
110 			.setkey = cryptd_morus1280_glue_setkey, \
111 			.setauthsize = cryptd_morus1280_glue_setauthsize, \
112 			.encrypt = cryptd_morus1280_glue_encrypt, \
113 			.decrypt = cryptd_morus1280_glue_decrypt, \
114 			.init = cryptd_morus1280_glue_init_tfm, \
115 			.exit = cryptd_morus1280_glue_exit_tfm, \
116 			\
117 			.ivsize = MORUS_NONCE_SIZE, \
118 			.maxauthsize = MORUS_MAX_AUTH_SIZE, \
119 			.chunksize = MORUS1280_BLOCK_SIZE, \
120 			\
121 			.base = { \
122 				.cra_flags = CRYPTO_ALG_ASYNC, \
123 				.cra_blocksize = 1, \
124 				.cra_ctxsize = sizeof(struct crypto_aead *), \
125 				.cra_alignmask = 0, \
126 				\
127 				.cra_priority = priority, \
128 				\
129 				.cra_name = "morus1280", \
130 				.cra_driver_name = driver_name, \
131 				\
132 				.cra_module = THIS_MODULE, \
133 			} \
134 		} \
135 	}
136 
137 #endif /* _CRYPTO_MORUS1280_GLUE_H */
138