These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / drivers / staging / skein / skein_generic.c
1 /*
2  * Cryptographic API.
3  *
4  * Skein256 Hash Algorithm.
5  *
6  * Derived from cryptoapi implementation, adapted for in-place
7  * scatterlist interface.
8  *
9  * Copyright (c) Eric Rost <eric.rost@mybabylon.net>
10  *
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU General Public License as published by the Free
13  * Software Foundation; either version 2 of the License, or (at your option)
14  * any later version.
15  *
16  */
17 #include <linux/types.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <crypto/internal/hash.h>
21 #include "skein_base.h"
22
23 static int skein256_init(struct shash_desc *desc)
24 {
25         return skein_256_init((struct skein_256_ctx *)shash_desc_ctx(desc),
26                         SKEIN256_DIGEST_BIT_SIZE);
27 }
28
29 static int skein256_update(struct shash_desc *desc, const u8 *data,
30                         unsigned int len)
31 {
32         return skein_256_update((struct skein_256_ctx *)shash_desc_ctx(desc),
33                                 data, len);
34 }
35
36 static int skein256_final(struct shash_desc *desc, u8 *out)
37 {
38         return skein_256_final((struct skein_256_ctx *)shash_desc_ctx(desc),
39                                 out);
40 }
41
42 static int skein256_export(struct shash_desc *desc, void *out)
43 {
44         struct skein_256_ctx *sctx = shash_desc_ctx(desc);
45
46         memcpy(out, sctx, sizeof(*sctx));
47         return 0;
48 }
49
50 static int skein256_import(struct shash_desc *desc, const void *in)
51 {
52         struct skein_256_ctx *sctx = shash_desc_ctx(desc);
53
54         memcpy(sctx, in, sizeof(*sctx));
55         return 0;
56 }
57
58 static int skein512_init(struct shash_desc *desc)
59 {
60         return skein_512_init((struct skein_512_ctx *)shash_desc_ctx(desc),
61                                 SKEIN512_DIGEST_BIT_SIZE);
62 }
63
64 static int skein512_update(struct shash_desc *desc, const u8 *data,
65                         unsigned int len)
66 {
67         return skein_512_update((struct skein_512_ctx *)shash_desc_ctx(desc),
68                                 data, len);
69 }
70
71 static int skein512_final(struct shash_desc *desc, u8 *out)
72 {
73         return skein_512_final((struct skein_512_ctx *)shash_desc_ctx(desc),
74                                 out);
75 }
76
77 static int skein512_export(struct shash_desc *desc, void *out)
78 {
79         struct skein_512_ctx *sctx = shash_desc_ctx(desc);
80
81         memcpy(out, sctx, sizeof(*sctx));
82         return 0;
83 }
84
85 static int skein512_import(struct shash_desc *desc, const void *in)
86 {
87         struct skein_512_ctx *sctx = shash_desc_ctx(desc);
88
89         memcpy(sctx, in, sizeof(*sctx));
90         return 0;
91 }
92
93 static int skein1024_init(struct shash_desc *desc)
94 {
95         return skein_1024_init((struct skein_1024_ctx *)shash_desc_ctx(desc),
96                                 SKEIN1024_DIGEST_BIT_SIZE);
97 }
98
99 static int skein1024_update(struct shash_desc *desc, const u8 *data,
100                         unsigned int len)
101 {
102         return skein_1024_update((struct skein_1024_ctx *)shash_desc_ctx(desc),
103                                 data, len);
104 }
105
106 static int skein1024_final(struct shash_desc *desc, u8 *out)
107 {
108         return skein_1024_final((struct skein_1024_ctx *)shash_desc_ctx(desc),
109                         out);
110 }
111
112 static int skein1024_export(struct shash_desc *desc, void *out)
113 {
114         struct skein_1024_ctx *sctx = shash_desc_ctx(desc);
115
116         memcpy(out, sctx, sizeof(*sctx));
117         return 0;
118 }
119
120 static int skein1024_import(struct shash_desc *desc, const void *in)
121 {
122         struct skein_1024_ctx *sctx = shash_desc_ctx(desc);
123
124         memcpy(sctx, in, sizeof(*sctx));
125         return 0;
126 }
127
128 static struct shash_alg alg256 = {
129         .digestsize     =       (SKEIN256_DIGEST_BIT_SIZE / 8),
130         .init           =       skein256_init,
131         .update         =       skein256_update,
132         .final          =       skein256_final,
133         .export         =       skein256_export,
134         .import         =       skein256_import,
135         .descsize       =       sizeof(struct skein_256_ctx),
136         .statesize      =       sizeof(struct skein_256_ctx),
137         .base           =       {
138                 .cra_name               =       "skein256",
139                 .cra_driver_name        =       "skein",
140                 .cra_flags              =       CRYPTO_ALG_TYPE_SHASH,
141                 .cra_blocksize          =       SKEIN_256_BLOCK_BYTES,
142                 .cra_module             =       THIS_MODULE,
143         }
144 };
145
146 static struct shash_alg alg512 = {
147         .digestsize     =       (SKEIN512_DIGEST_BIT_SIZE / 8),
148         .init           =       skein512_init,
149         .update         =       skein512_update,
150         .final          =       skein512_final,
151         .export         =       skein512_export,
152         .import         =       skein512_import,
153         .descsize       =       sizeof(struct skein_512_ctx),
154         .statesize      =       sizeof(struct skein_512_ctx),
155         .base           =       {
156                 .cra_name               =       "skein512",
157                 .cra_driver_name        =       "skein",
158                 .cra_flags              =       CRYPTO_ALG_TYPE_SHASH,
159                 .cra_blocksize          =       SKEIN_512_BLOCK_BYTES,
160                 .cra_module             =       THIS_MODULE,
161         }
162 };
163
164 static struct shash_alg alg1024 = {
165         .digestsize     =       (SKEIN1024_DIGEST_BIT_SIZE / 8),
166         .init           =       skein1024_init,
167         .update         =       skein1024_update,
168         .final          =       skein1024_final,
169         .export         =       skein1024_export,
170         .import         =       skein1024_import,
171         .descsize       =       sizeof(struct skein_1024_ctx),
172         .statesize      =       sizeof(struct skein_1024_ctx),
173         .base           =       {
174                 .cra_name               =       "skein1024",
175                 .cra_driver_name        =       "skein",
176                 .cra_flags              =       CRYPTO_ALG_TYPE_SHASH,
177                 .cra_blocksize          =       SKEIN_1024_BLOCK_BYTES,
178                 .cra_module             =       THIS_MODULE,
179         }
180 };
181
182 static int __init skein_generic_init(void)
183 {
184         if (crypto_register_shash(&alg256))
185                 goto out;
186         if (crypto_register_shash(&alg512))
187                 goto unreg256;
188         if (crypto_register_shash(&alg1024))
189                 goto unreg512;
190
191         return 0;
192
193 unreg512:
194         crypto_unregister_shash(&alg512);
195 unreg256:
196         crypto_unregister_shash(&alg256);
197 out:
198         return -1;
199 }
200
201 static void __exit skein_generic_fini(void)
202 {
203         crypto_unregister_shash(&alg256);
204         crypto_unregister_shash(&alg512);
205         crypto_unregister_shash(&alg1024);
206 }
207
208 module_init(skein_generic_init);
209 module_exit(skein_generic_fini);
210
211 MODULE_LICENSE("GPL");
212 MODULE_DESCRIPTION("Skein Hash Algorithm");
213
214 MODULE_ALIAS("skein");