1212#include <linux/kernel.h>
1313#include <linux/module.h>
1414
15+ /*
16+ * Export and import functions. crypto_shash wants a particular format that
17+ * matches that used by some legacy drivers. It currently is the same as the
18+ * library SHA context, except the value in bytecount must be block-aligned and
19+ * the remainder must be stored in an extra u8 appended to the struct.
20+ */
21+
22+ #define SHA1_SHASH_STATE_SIZE (sizeof(struct sha1_ctx) + 1)
23+ static_assert (sizeof (struct sha1_ctx ) == sizeof (struct sha1_state ));
24+ static_assert (offsetof(struct sha1_ctx , state ) == offsetof(struct sha1_state , state ));
25+ static_assert (offsetof(struct sha1_ctx , bytecount ) == offsetof(struct sha1_state , count ));
26+ static_assert (offsetof(struct sha1_ctx , buf ) == offsetof(struct sha1_state , buffer ));
27+
28+ static int __crypto_sha1_export (const struct sha1_ctx * ctx0 , void * out )
29+ {
30+ struct sha1_ctx ctx = * ctx0 ;
31+ unsigned int partial ;
32+ u8 * p = out ;
33+
34+ partial = ctx .bytecount % SHA1_BLOCK_SIZE ;
35+ ctx .bytecount -= partial ;
36+ memcpy (p , & ctx , sizeof (ctx ));
37+ p += sizeof (ctx );
38+ * p = partial ;
39+ return 0 ;
40+ }
41+
42+ static int __crypto_sha1_import (struct sha1_ctx * ctx , const void * in )
43+ {
44+ const u8 * p = in ;
45+
46+ memcpy (ctx , p , sizeof (* ctx ));
47+ p += sizeof (* ctx );
48+ ctx -> bytecount += * p ;
49+ return 0 ;
50+ }
51+
1552const u8 sha1_zero_message_hash [SHA1_DIGEST_SIZE ] = {
1653 0xda , 0x39 , 0xa3 , 0xee , 0x5e , 0x6b , 0x4b , 0x0d ,
1754 0x32 , 0x55 , 0xbf , 0xef , 0x95 , 0x60 , 0x18 , 0x90 ,
@@ -47,6 +84,16 @@ static int crypto_sha1_digest(struct shash_desc *desc,
4784 return 0 ;
4885}
4986
87+ static int crypto_sha1_export (struct shash_desc * desc , void * out )
88+ {
89+ return __crypto_sha1_export (SHA1_CTX (desc ), out );
90+ }
91+
92+ static int crypto_sha1_import (struct shash_desc * desc , const void * in )
93+ {
94+ return __crypto_sha1_import (SHA1_CTX (desc ), in );
95+ }
96+
5097#define HMAC_SHA1_KEY (tfm ) ((struct hmac_sha1_key *)crypto_shash_ctx(tfm))
5198#define HMAC_SHA1_CTX (desc ) ((struct hmac_sha1_ctx *)shash_desc_ctx(desc))
5299
@@ -83,6 +130,19 @@ static int crypto_hmac_sha1_digest(struct shash_desc *desc,
83130 return 0 ;
84131}
85132
133+ static int crypto_hmac_sha1_export (struct shash_desc * desc , void * out )
134+ {
135+ return __crypto_sha1_export (& HMAC_SHA1_CTX (desc )-> sha_ctx , out );
136+ }
137+
138+ static int crypto_hmac_sha1_import (struct shash_desc * desc , const void * in )
139+ {
140+ struct hmac_sha1_ctx * ctx = HMAC_SHA1_CTX (desc );
141+
142+ ctx -> ostate = HMAC_SHA1_KEY (desc -> tfm )-> ostate ;
143+ return __crypto_sha1_import (& ctx -> sha_ctx , in );
144+ }
145+
86146static struct shash_alg algs [] = {
87147 {
88148 .base .cra_name = "sha1" ,
@@ -95,7 +155,10 @@ static struct shash_alg algs[] = {
95155 .update = crypto_sha1_update ,
96156 .final = crypto_sha1_final ,
97157 .digest = crypto_sha1_digest ,
158+ .export = crypto_sha1_export ,
159+ .import = crypto_sha1_import ,
98160 .descsize = sizeof (struct sha1_ctx ),
161+ .statesize = SHA1_SHASH_STATE_SIZE ,
99162 },
100163 {
101164 .base .cra_name = "hmac(sha1)" ,
@@ -110,7 +173,10 @@ static struct shash_alg algs[] = {
110173 .update = crypto_hmac_sha1_update ,
111174 .final = crypto_hmac_sha1_final ,
112175 .digest = crypto_hmac_sha1_digest ,
176+ .export = crypto_hmac_sha1_export ,
177+ .import = crypto_hmac_sha1_import ,
113178 .descsize = sizeof (struct hmac_sha1_ctx ),
179+ .statesize = SHA1_SHASH_STATE_SIZE ,
114180 },
115181};
116182
0 commit comments