6 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
16 #include <linux/errno.h>
17 #include <linux/highmem.h>
18 #include <linux/module.h>
19 #include <linux/scatterlist.h>
22 #include "scatterwalk.h"
24 static int init(struct hash_desc
*desc
)
26 struct crypto_tfm
*tfm
= crypto_hash_tfm(desc
->tfm
);
28 tfm
->__crt_alg
->cra_digest
.dia_init(tfm
);
32 static int update(struct hash_desc
*desc
,
33 struct scatterlist
*sg
, unsigned int nbytes
)
35 struct crypto_tfm
*tfm
= crypto_hash_tfm(desc
->tfm
);
36 unsigned int alignmask
= crypto_tfm_alg_alignmask(tfm
);
42 struct page
*pg
= sg
->page
;
43 unsigned int offset
= sg
->offset
;
44 unsigned int l
= sg
->length
;
46 if (unlikely(l
> nbytes
))
51 unsigned int bytes_from_page
= min(l
, ((unsigned int)
54 char *src
= crypto_kmap(pg
, 0);
55 char *p
= src
+ offset
;
57 if (unlikely(offset
& alignmask
)) {
59 alignmask
+ 1 - (offset
& alignmask
);
60 bytes
= min(bytes
, bytes_from_page
);
61 tfm
->__crt_alg
->cra_digest
.dia_update(tfm
, p
,
64 bytes_from_page
-= bytes
;
67 tfm
->__crt_alg
->cra_digest
.dia_update(tfm
, p
,
69 crypto_kunmap(src
, 0);
70 crypto_yield(desc
->flags
);
84 static int final(struct hash_desc
*desc
, u8
*out
)
86 struct crypto_tfm
*tfm
= crypto_hash_tfm(desc
->tfm
);
87 unsigned long alignmask
= crypto_tfm_alg_alignmask(tfm
);
88 struct digest_alg
*digest
= &tfm
->__crt_alg
->cra_digest
;
90 if (unlikely((unsigned long)out
& alignmask
)) {
91 unsigned long align
= alignmask
+ 1;
92 unsigned long addr
= (unsigned long)crypto_tfm_ctx(tfm
);
93 u8
*dst
= (u8
*)ALIGN(addr
, align
) +
94 ALIGN(tfm
->__crt_alg
->cra_ctxsize
, align
);
96 digest
->dia_final(tfm
, dst
);
97 memcpy(out
, dst
, digest
->dia_digestsize
);
99 digest
->dia_final(tfm
, out
);
104 static int nosetkey(struct crypto_hash
*tfm
, const u8
*key
, unsigned int keylen
)
106 crypto_hash_clear_flags(tfm
, CRYPTO_TFM_RES_MASK
);
110 static int setkey(struct crypto_hash
*hash
, const u8
*key
, unsigned int keylen
)
112 struct crypto_tfm
*tfm
= crypto_hash_tfm(hash
);
114 crypto_hash_clear_flags(hash
, CRYPTO_TFM_RES_MASK
);
115 return tfm
->__crt_alg
->cra_digest
.dia_setkey(tfm
, key
, keylen
);
118 static int digest(struct hash_desc
*desc
,
119 struct scatterlist
*sg
, unsigned int nbytes
, u8
*out
)
122 update(desc
, sg
, nbytes
);
123 return final(desc
, out
);
126 int crypto_init_digest_flags(struct crypto_tfm
*tfm
, u32 flags
)
128 return flags
? -EINVAL
: 0;
131 int crypto_init_digest_ops(struct crypto_tfm
*tfm
)
133 struct hash_tfm
*ops
= &tfm
->crt_hash
;
134 struct digest_alg
*dalg
= &tfm
->__crt_alg
->cra_digest
;
136 if (dalg
->dia_digestsize
> crypto_tfm_alg_blocksize(tfm
))
140 ops
->update
= update
;
142 ops
->digest
= digest
;
143 ops
->setkey
= dalg
->dia_setkey
? setkey
: nosetkey
;
144 ops
->digestsize
= dalg
->dia_digestsize
;
149 void crypto_exit_digest_ops(struct crypto_tfm
*tfm
)