Let md5sum and all sha*sum utilities use Linux kernel cryptographic API via the AF_ALG address family.
Speed gain depends on the CPU type: Xeon E3-1265L V2: $ truncate -s 2GB 2g.bin $ time sha1sum 2g.bin 752ef2367f479e79e4f0cded9c270c2890506ab0 2g.bin real 0m4.829s user 0m4.437s sys 0m0.391s $ time ./sha1sum-afalg 2g.bin 752ef2367f479e79e4f0cded9c270c2890506ab0 2g.bin real 0m3.164s user 0m0.000s sys 0m3.162s Xeon E3-1240 v6: $ time sha1sum disk.qcow2 47be301f86c71c20eae4ccc5bab4c02e09e729a4 disk.qcow2 real 0m28.390s user 0m13.352s sys 0m1.376s $ time ./sha1sum-afalg disk.qcow2 47be301f86c71c20eae4ccc5bab4c02e09e729a4 disk.qcow2 real 0m8.373s user 0m0.052s sys 0m8.308s Marvell Armada 8040 - MACCHIATOBin Dual shot: $ dd if=/dev/zero bs=1G count=10 |time -p sha1sum a0b6e2ca4e28360a929943e8eb966f703a69dc44 2g.bin real 0m49.390s user 0m46.852s sys 0m2.076s $ dd if=/dev/zero bs=1G count=10 |time -p ./sha1sum-afalg a0b6e2ca4e28360a929943e8eb966f703a69dc44 2g.bin real 0m15.104s user 0m0.052s sys 0m15.008s Correctness of the implementation was tested with the following script: algos='md5sum sha1sum sha224sum sha256sum sha384sum sha512sum' for alg in $algos; do echo -n "Checking $alg..." for bs in 1 31 512 1K 4K 1M; do for count in 0 1 1234 4096; do hash1=$(dd status=none if=/dev/zero bs=$bs count=$count |$alg) hash2=$(dd status=none if=/dev/zero bs=$bs count=$count |src/$alg) [ "$hash1" = "$hash2" ] || exit 1 done done echo " ok" done Changes from v2: * add copyright nore in af_alg.c * check for AF_ALG in sys/socket.h to avoid build failures when unavailable * don't include unneeded header files in af_alg.h * revert wrong Include directive in module description * revert unneeded 'Hey Emacs!' blocks * use correct GNU indentation * prefer size_t over int to denote memory segments * avoid possible overflow by checking arguments size * return -EIO if sendfile() returns a short read/write count * fix a file descriptor leak when bind() returns error Matteo Croce (4): sha1sum: use AF_ALG when available sha256sum: use kernel crypto API sha512sum: use kernel crypto API md5sum: use kernel crypto API lib/af_alg.c | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/af_alg.h | 51 +++++++++++++++++++++ lib/md5.c | 20 +++++++- lib/sha1.c | 19 +++++++- lib/sha256.c | 34 +++++++++++++- lib/sha512.c | 34 +++++++++++++- modules/crypto/md5 | 4 +- modules/crypto/sha1 | 4 +- modules/crypto/sha256 | 4 +- modules/crypto/sha512 | 4 +- 10 files changed, 288 insertions(+), 10 deletions(-) create mode 100644 lib/af_alg.c create mode 100644 lib/af_alg.h -- 2.14.3