diff options
Diffstat (limited to 'crypto/sha256_generic.c')
| -rw-r--r-- | crypto/sha256_generic.c | 100 |
1 files changed, 54 insertions, 46 deletions
diff --git a/crypto/sha256_generic.c b/crypto/sha256_generic.c index 6349d8339d37..c48459ebf05b 100644 --- a/crypto/sha256_generic.c +++ b/crypto/sha256_generic.c | |||
| @@ -25,12 +25,6 @@ | |||
| 25 | #include <crypto/sha.h> | 25 | #include <crypto/sha.h> |
| 26 | #include <asm/byteorder.h> | 26 | #include <asm/byteorder.h> |
| 27 | 27 | ||
| 28 | struct sha256_ctx { | ||
| 29 | u32 count[2]; | ||
| 30 | u32 state[8]; | ||
| 31 | u8 buf[128]; | ||
| 32 | }; | ||
| 33 | |||
| 34 | static inline u32 Ch(u32 x, u32 y, u32 z) | 28 | static inline u32 Ch(u32 x, u32 y, u32 z) |
| 35 | { | 29 | { |
| 36 | return z ^ (x & (y ^ z)); | 30 | return z ^ (x & (y ^ z)); |
| @@ -222,7 +216,7 @@ static void sha256_transform(u32 *state, const u8 *input) | |||
| 222 | 216 | ||
| 223 | static int sha224_init(struct shash_desc *desc) | 217 | static int sha224_init(struct shash_desc *desc) |
| 224 | { | 218 | { |
| 225 | struct sha256_ctx *sctx = shash_desc_ctx(desc); | 219 | struct sha256_state *sctx = shash_desc_ctx(desc); |
| 226 | sctx->state[0] = SHA224_H0; | 220 | sctx->state[0] = SHA224_H0; |
| 227 | sctx->state[1] = SHA224_H1; | 221 | sctx->state[1] = SHA224_H1; |
| 228 | sctx->state[2] = SHA224_H2; | 222 | sctx->state[2] = SHA224_H2; |
| @@ -231,15 +225,14 @@ static int sha224_init(struct shash_desc *desc) | |||
| 231 | sctx->state[5] = SHA224_H5; | 225 | sctx->state[5] = SHA224_H5; |
| 232 | sctx->state[6] = SHA224_H6; | 226 | sctx->state[6] = SHA224_H6; |
| 233 | sctx->state[7] = SHA224_H7; | 227 | sctx->state[7] = SHA224_H7; |
| 234 | sctx->count[0] = 0; | 228 | sctx->count = 0; |
| 235 | sctx->count[1] = 0; | ||
| 236 | 229 | ||
| 237 | return 0; | 230 | return 0; |
| 238 | } | 231 | } |
| 239 | 232 | ||
| 240 | static int sha256_init(struct shash_desc *desc) | 233 | static int sha256_init(struct shash_desc *desc) |
| 241 | { | 234 | { |
| 242 | struct sha256_ctx *sctx = shash_desc_ctx(desc); | 235 | struct sha256_state *sctx = shash_desc_ctx(desc); |
| 243 | sctx->state[0] = SHA256_H0; | 236 | sctx->state[0] = SHA256_H0; |
| 244 | sctx->state[1] = SHA256_H1; | 237 | sctx->state[1] = SHA256_H1; |
| 245 | sctx->state[2] = SHA256_H2; | 238 | sctx->state[2] = SHA256_H2; |
| @@ -248,7 +241,7 @@ static int sha256_init(struct shash_desc *desc) | |||
| 248 | sctx->state[5] = SHA256_H5; | 241 | sctx->state[5] = SHA256_H5; |
| 249 | sctx->state[6] = SHA256_H6; | 242 | sctx->state[6] = SHA256_H6; |
| 250 | sctx->state[7] = SHA256_H7; | 243 | sctx->state[7] = SHA256_H7; |
| 251 | sctx->count[0] = sctx->count[1] = 0; | 244 | sctx->count = 0; |
| 252 | 245 | ||
| 253 | return 0; | 246 | return 0; |
| 254 | } | 247 | } |
| @@ -256,58 +249,54 @@ static int sha256_init(struct shash_desc *desc) | |||
| 256 | static int sha256_update(struct shash_desc *desc, const u8 *data, | 249 | static int sha256_update(struct shash_desc *desc, const u8 *data, |
| 257 | unsigned int len) | 250 | unsigned int len) |
| 258 | { | 251 | { |
| 259 | struct sha256_ctx *sctx = shash_desc_ctx(desc); | 252 | struct sha256_state *sctx = shash_desc_ctx(desc); |
| 260 | unsigned int i, index, part_len; | 253 | unsigned int partial, done; |
| 261 | 254 | const u8 *src; | |
| 262 | /* Compute number of bytes mod 128 */ | 255 | |
| 263 | index = (unsigned int)((sctx->count[0] >> 3) & 0x3f); | 256 | partial = sctx->count & 0x3f; |
| 264 | 257 | sctx->count += len; | |
| 265 | /* Update number of bits */ | 258 | done = 0; |
| 266 | if ((sctx->count[0] += (len << 3)) < (len << 3)) { | 259 | src = data; |
| 267 | sctx->count[1]++; | 260 | |
| 268 | sctx->count[1] += (len >> 29); | 261 | if ((partial + len) > 63) { |
| 269 | } | 262 | if (partial) { |
| 270 | 263 | done = -partial; | |
| 271 | part_len = 64 - index; | 264 | memcpy(sctx->buf + partial, data, done + 64); |
| 272 | 265 | src = sctx->buf; | |
| 273 | /* Transform as many times as possible. */ | 266 | } |
| 274 | if (len >= part_len) { | 267 | |
| 275 | memcpy(&sctx->buf[index], data, part_len); | 268 | do { |
| 276 | sha256_transform(sctx->state, sctx->buf); | 269 | sha256_transform(sctx->state, src); |
| 277 | 270 | done += 64; | |
| 278 | for (i = part_len; i + 63 < len; i += 64) | 271 | src = data + done; |
| 279 | sha256_transform(sctx->state, &data[i]); | 272 | } while (done + 63 < len); |
| 280 | index = 0; | 273 | |
| 281 | } else { | 274 | partial = 0; |
| 282 | i = 0; | ||
| 283 | } | 275 | } |
| 284 | 276 | memcpy(sctx->buf + partial, src, len - done); | |
| 285 | /* Buffer remaining input */ | ||
| 286 | memcpy(&sctx->buf[index], &data[i], len-i); | ||
| 287 | 277 | ||
| 288 | return 0; | 278 | return 0; |
| 289 | } | 279 | } |
| 290 | 280 | ||
| 291 | static int sha256_final(struct shash_desc *desc, u8 *out) | 281 | static int sha256_final(struct shash_desc *desc, u8 *out) |
| 292 | { | 282 | { |
| 293 | struct sha256_ctx *sctx = shash_desc_ctx(desc); | 283 | struct sha256_state *sctx = shash_desc_ctx(desc); |
| 294 | __be32 *dst = (__be32 *)out; | 284 | __be32 *dst = (__be32 *)out; |
| 295 | __be32 bits[2]; | 285 | __be64 bits; |
| 296 | unsigned int index, pad_len; | 286 | unsigned int index, pad_len; |
| 297 | int i; | 287 | int i; |
| 298 | static const u8 padding[64] = { 0x80, }; | 288 | static const u8 padding[64] = { 0x80, }; |
| 299 | 289 | ||
| 300 | /* Save number of bits */ | 290 | /* Save number of bits */ |
| 301 | bits[1] = cpu_to_be32(sctx->count[0]); | 291 | bits = cpu_to_be64(sctx->count << 3); |
| 302 | bits[0] = cpu_to_be32(sctx->count[1]); | ||
| 303 | 292 | ||
| 304 | /* Pad out to 56 mod 64. */ | 293 | /* Pad out to 56 mod 64. */ |
| 305 | index = (sctx->count[0] >> 3) & 0x3f; | 294 | index = sctx->count & 0x3f; |
| 306 | pad_len = (index < 56) ? (56 - index) : ((64+56) - index); | 295 | pad_len = (index < 56) ? (56 - index) : ((64+56) - index); |
| 307 | sha256_update(desc, padding, pad_len); | 296 | sha256_update(desc, padding, pad_len); |
| 308 | 297 | ||
| 309 | /* Append length (before padding) */ | 298 | /* Append length (before padding) */ |
| 310 | sha256_update(desc, (const u8 *)bits, sizeof(bits)); | 299 | sha256_update(desc, (const u8 *)&bits, sizeof(bits)); |
| 311 | 300 | ||
| 312 | /* Store state in digest */ | 301 | /* Store state in digest */ |
| 313 | for (i = 0; i < 8; i++) | 302 | for (i = 0; i < 8; i++) |
| @@ -331,12 +320,31 @@ static int sha224_final(struct shash_desc *desc, u8 *hash) | |||
| 331 | return 0; | 320 | return 0; |
| 332 | } | 321 | } |
| 333 | 322 | ||
| 323 | static int sha256_export(struct shash_desc *desc, void *out) | ||
| 324 | { | ||
| 325 | struct sha256_state *sctx = shash_desc_ctx(desc); | ||
| 326 | |||
| 327 | memcpy(out, sctx, sizeof(*sctx)); | ||
| 328 | return 0; | ||
| 329 | } | ||
| 330 | |||
| 331 | static int sha256_import(struct shash_desc *desc, const void *in) | ||
| 332 | { | ||
| 333 | struct sha256_state *sctx = shash_desc_ctx(desc); | ||
| 334 | |||
| 335 | memcpy(sctx, in, sizeof(*sctx)); | ||
| 336 | return 0; | ||
| 337 | } | ||
| 338 | |||
| 334 | static struct shash_alg sha256 = { | 339 | static struct shash_alg sha256 = { |
| 335 | .digestsize = SHA256_DIGEST_SIZE, | 340 | .digestsize = SHA256_DIGEST_SIZE, |
| 336 | .init = sha256_init, | 341 | .init = sha256_init, |
| 337 | .update = sha256_update, | 342 | .update = sha256_update, |
| 338 | .final = sha256_final, | 343 | .final = sha256_final, |
| 339 | .descsize = sizeof(struct sha256_ctx), | 344 | .export = sha256_export, |
| 345 | .import = sha256_import, | ||
| 346 | .descsize = sizeof(struct sha256_state), | ||
| 347 | .statesize = sizeof(struct sha256_state), | ||
| 340 | .base = { | 348 | .base = { |
| 341 | .cra_name = "sha256", | 349 | .cra_name = "sha256", |
| 342 | .cra_driver_name= "sha256-generic", | 350 | .cra_driver_name= "sha256-generic", |
| @@ -351,7 +359,7 @@ static struct shash_alg sha224 = { | |||
| 351 | .init = sha224_init, | 359 | .init = sha224_init, |
| 352 | .update = sha256_update, | 360 | .update = sha256_update, |
| 353 | .final = sha224_final, | 361 | .final = sha224_final, |
| 354 | .descsize = sizeof(struct sha256_ctx), | 362 | .descsize = sizeof(struct sha256_state), |
| 355 | .base = { | 363 | .base = { |
| 356 | .cra_name = "sha224", | 364 | .cra_name = "sha224", |
| 357 | .cra_driver_name= "sha224-generic", | 365 | .cra_driver_name= "sha224-generic", |
