Commit e2a7ce4e185a94462698cc0e5192495ee3d22a2f
1 parent
8267adab94
crypto: sha1_generic - Add export/import support
This patch adds export/import support to sha1_generic. The exported type is defined by struct sha1_state, which is basically the entire descriptor state of sha1_generic. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Showing 2 changed files with 33 additions and 16 deletions Side-by-side Diff
crypto/sha1_generic.c
... | ... | @@ -25,31 +25,21 @@ |
25 | 25 | #include <crypto/sha.h> |
26 | 26 | #include <asm/byteorder.h> |
27 | 27 | |
28 | -struct sha1_ctx { | |
29 | - u64 count; | |
30 | - u32 state[5]; | |
31 | - u8 buffer[64]; | |
32 | -}; | |
33 | - | |
34 | 28 | static int sha1_init(struct shash_desc *desc) |
35 | 29 | { |
36 | - struct sha1_ctx *sctx = shash_desc_ctx(desc); | |
30 | + struct sha1_state *sctx = shash_desc_ctx(desc); | |
37 | 31 | |
38 | - static const struct sha1_ctx initstate = { | |
39 | - 0, | |
40 | - { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 }, | |
41 | - { 0, } | |
32 | + *sctx = (struct sha1_state){ | |
33 | + .state = { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 }, | |
42 | 34 | }; |
43 | 35 | |
44 | - *sctx = initstate; | |
45 | - | |
46 | 36 | return 0; |
47 | 37 | } |
48 | 38 | |
49 | 39 | static int sha1_update(struct shash_desc *desc, const u8 *data, |
50 | 40 | unsigned int len) |
51 | 41 | { |
52 | - struct sha1_ctx *sctx = shash_desc_ctx(desc); | |
42 | + struct sha1_state *sctx = shash_desc_ctx(desc); | |
53 | 43 | unsigned int partial, done; |
54 | 44 | const u8 *src; |
55 | 45 | |
... | ... | @@ -85,7 +75,7 @@ |
85 | 75 | /* Add padding and return the message digest. */ |
86 | 76 | static int sha1_final(struct shash_desc *desc, u8 *out) |
87 | 77 | { |
88 | - struct sha1_ctx *sctx = shash_desc_ctx(desc); | |
78 | + struct sha1_state *sctx = shash_desc_ctx(desc); | |
89 | 79 | __be32 *dst = (__be32 *)out; |
90 | 80 | u32 i, index, padlen; |
91 | 81 | __be64 bits; |
92 | 82 | |
... | ... | @@ -111,12 +101,31 @@ |
111 | 101 | return 0; |
112 | 102 | } |
113 | 103 | |
104 | +static int sha1_export(struct shash_desc *desc, void *out) | |
105 | +{ | |
106 | + struct sha1_state *sctx = shash_desc_ctx(desc); | |
107 | + | |
108 | + memcpy(out, sctx, sizeof(*sctx)); | |
109 | + return 0; | |
110 | +} | |
111 | + | |
112 | +static int sha1_import(struct shash_desc *desc, const void *in) | |
113 | +{ | |
114 | + struct sha1_state *sctx = shash_desc_ctx(desc); | |
115 | + | |
116 | + memcpy(sctx, in, sizeof(*sctx)); | |
117 | + return 0; | |
118 | +} | |
119 | + | |
114 | 120 | static struct shash_alg alg = { |
115 | 121 | .digestsize = SHA1_DIGEST_SIZE, |
116 | 122 | .init = sha1_init, |
117 | 123 | .update = sha1_update, |
118 | 124 | .final = sha1_final, |
119 | - .descsize = sizeof(struct sha1_ctx), | |
125 | + .export = sha1_export, | |
126 | + .import = sha1_import, | |
127 | + .descsize = sizeof(struct sha1_state), | |
128 | + .statesize = sizeof(struct sha1_state), | |
120 | 129 | .base = { |
121 | 130 | .cra_name = "sha1", |
122 | 131 | .cra_driver_name= "sha1-generic", |
include/crypto/sha.h
... | ... | @@ -5,6 +5,8 @@ |
5 | 5 | #ifndef _CRYPTO_SHA_H |
6 | 6 | #define _CRYPTO_SHA_H |
7 | 7 | |
8 | +#include <linux/types.h> | |
9 | + | |
8 | 10 | #define SHA1_DIGEST_SIZE 20 |
9 | 11 | #define SHA1_BLOCK_SIZE 64 |
10 | 12 | |
... | ... | @@ -61,6 +63,12 @@ |
61 | 63 | #define SHA512_H5 0x9b05688c2b3e6c1fULL |
62 | 64 | #define SHA512_H6 0x1f83d9abfb41bd6bULL |
63 | 65 | #define SHA512_H7 0x5be0cd19137e2179ULL |
66 | + | |
67 | +struct sha1_state { | |
68 | + u64 count; | |
69 | + u32 state[SHA1_DIGEST_SIZE / 4]; | |
70 | + u8 buffer[SHA1_BLOCK_SIZE]; | |
71 | +}; | |
64 | 72 | |
65 | 73 | #endif |