staging: crypto: skein: cleanup >80 character lines
[linux-2.6/btrfs-unstable.git] / drivers / staging / skein / skeinApi.c
blobf0015d5b10f5ff6ef2cb51a3396d3e0191a94e7d
1 /*
2 Copyright (c) 2010 Werner Dittmann
4 Permission is hereby granted, free of charge, to any person
5 obtaining a copy of this software and associated documentation
6 files (the "Software"), to deal in the Software without
7 restriction, including without limitation the rights to use,
8 copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following
11 conditions:
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 OTHER DEALINGS IN THE SOFTWARE.
27 #include <linux/string.h>
28 #include <skeinApi.h>
30 int skeinCtxPrepare(struct skein_ctx *ctx, enum skein_size size)
32 Skein_Assert(ctx && size, SKEIN_FAIL);
34 memset(ctx , 0, sizeof(struct skein_ctx));
35 ctx->skeinSize = size;
37 return SKEIN_SUCCESS;
40 int skeinInit(struct skein_ctx *ctx, size_t hashBitLen)
42 int ret = SKEIN_FAIL;
43 size_t Xlen = 0;
44 u64 *X = NULL;
45 u64 treeInfo = SKEIN_CFG_TREE_INFO_SEQUENTIAL;
47 Skein_Assert(ctx, SKEIN_FAIL);
49 * The following two lines rely of the fact that the real Skein
50 * contexts are a union in out context and thus have tha maximum
51 * memory available. The beauty of C :-) .
53 X = ctx->m.s256.X;
54 Xlen = ctx->skeinSize/8;
56 * If size is the same and hash bit length is zero then reuse
57 * the save chaining variables.
59 switch (ctx->skeinSize) {
60 case Skein256:
61 ret = Skein_256_InitExt(&ctx->m.s256, hashBitLen,
62 treeInfo, NULL, 0);
63 break;
64 case Skein512:
65 ret = Skein_512_InitExt(&ctx->m.s512, hashBitLen,
66 treeInfo, NULL, 0);
67 break;
68 case Skein1024:
69 ret = Skein1024_InitExt(&ctx->m.s1024, hashBitLen,
70 treeInfo, NULL, 0);
71 break;
74 if (ret == SKEIN_SUCCESS) {
76 * Save chaining variables for this combination of size and
77 * hashBitLen
79 memcpy(ctx->XSave, X, Xlen);
81 return ret;
84 int skeinMacInit(struct skein_ctx *ctx, const u8 *key, size_t keyLen,
85 size_t hashBitLen)
87 int ret = SKEIN_FAIL;
88 u64 *X = NULL;
89 size_t Xlen = 0;
90 u64 treeInfo = SKEIN_CFG_TREE_INFO_SEQUENTIAL;
92 Skein_Assert(ctx, SKEIN_FAIL);
94 X = ctx->m.s256.X;
95 Xlen = ctx->skeinSize/8;
97 Skein_Assert(hashBitLen, SKEIN_BAD_HASHLEN);
99 switch (ctx->skeinSize) {
100 case Skein256:
101 ret = Skein_256_InitExt(&ctx->m.s256, hashBitLen,
102 treeInfo,
103 (const u8 *)key, keyLen);
105 break;
106 case Skein512:
107 ret = Skein_512_InitExt(&ctx->m.s512, hashBitLen,
108 treeInfo,
109 (const u8 *)key, keyLen);
110 break;
111 case Skein1024:
112 ret = Skein1024_InitExt(&ctx->m.s1024, hashBitLen,
113 treeInfo,
114 (const u8 *)key, keyLen);
116 break;
118 if (ret == SKEIN_SUCCESS) {
120 * Save chaining variables for this combination of key,
121 * keyLen, hashBitLen
123 memcpy(ctx->XSave, X, Xlen);
125 return ret;
128 void skeinReset(struct skein_ctx *ctx)
130 size_t Xlen = 0;
131 u64 *X = NULL;
134 * The following two lines rely of the fact that the real Skein
135 * contexts are a union in out context and thus have tha maximum
136 * memory available. The beautiy of C :-) .
138 X = ctx->m.s256.X;
139 Xlen = ctx->skeinSize/8;
140 /* Restore the chaing variable, reset byte counter */
141 memcpy(X, ctx->XSave, Xlen);
143 /* Setup context to process the message */
144 Skein_Start_New_Type(&ctx->m, MSG);
147 int skeinUpdate(struct skein_ctx *ctx, const u8 *msg,
148 size_t msgByteCnt)
150 int ret = SKEIN_FAIL;
151 Skein_Assert(ctx, SKEIN_FAIL);
153 switch (ctx->skeinSize) {
154 case Skein256:
155 ret = Skein_256_Update(&ctx->m.s256, (const u8 *)msg,
156 msgByteCnt);
157 break;
158 case Skein512:
159 ret = Skein_512_Update(&ctx->m.s512, (const u8 *)msg,
160 msgByteCnt);
161 break;
162 case Skein1024:
163 ret = Skein1024_Update(&ctx->m.s1024, (const u8 *)msg,
164 msgByteCnt);
165 break;
167 return ret;
171 int skeinUpdateBits(struct skein_ctx *ctx, const u8 *msg,
172 size_t msgBitCnt)
175 * I've used the bit pad implementation from skein_test.c (see NIST CD)
176 * and modified it to use the convenience functions and added some
177 * pointer arithmetic.
179 size_t length;
180 u8 mask;
181 u8 *up;
184 * only the final Update() call is allowed do partial bytes, else
185 * assert an error
187 Skein_Assert((ctx->m.h.T[1] & SKEIN_T1_FLAG_BIT_PAD) == 0 ||
188 msgBitCnt == 0, SKEIN_FAIL);
190 /* if number of bits is a multiple of bytes - that's easy */
191 if ((msgBitCnt & 0x7) == 0) {
192 return skeinUpdate(ctx, msg, msgBitCnt >> 3);
194 skeinUpdate(ctx, msg, (msgBitCnt >> 3) + 1);
197 * The next line rely on the fact that the real Skein contexts
198 * are a union in our context. After the addition the pointer points to
199 * Skein's real partial block buffer.
200 * If this layout ever changes we have to adapt this as well.
202 up = (u8 *)ctx->m.s256.X + ctx->skeinSize / 8;
204 /* set tweak flag for the skeinFinal call */
205 Skein_Set_Bit_Pad_Flag(ctx->m.h);
207 /* now "pad" the final partial byte the way NIST likes */
208 /* get the bCnt value (same location for all block sizes) */
209 length = ctx->m.h.bCnt;
210 /* internal sanity check: there IS a partial byte in the buffer! */
211 Skein_assert(length != 0);
212 /* partial byte bit mask */
213 mask = (u8) (1u << (7 - (msgBitCnt & 7)));
214 /* apply bit padding on final byte (in the buffer) */
215 up[length-1] = (u8)((up[length-1] & (0-mask))|mask);
217 return SKEIN_SUCCESS;
220 int skeinFinal(struct skein_ctx *ctx, u8 *hash)
222 int ret = SKEIN_FAIL;
223 Skein_Assert(ctx, SKEIN_FAIL);
225 switch (ctx->skeinSize) {
226 case Skein256:
227 ret = Skein_256_Final(&ctx->m.s256, (u8 *)hash);
228 break;
229 case Skein512:
230 ret = Skein_512_Final(&ctx->m.s512, (u8 *)hash);
231 break;
232 case Skein1024:
233 ret = Skein1024_Final(&ctx->m.s1024, (u8 *)hash);
234 break;
236 return ret;