lib/af_alg.c


Log

Author Commit Date CI Message
Bruno Haible 013b9158 2018-06-25T04:18:30 af_alg: Fail in continuable manner on Linux/powerpc64le. Reported by Assaf Gordon <assafgordon@gmail.com> in <https://lists.gnu.org/archive/html/coreutils/2018-06/msg00034.html>. * lib/af_alg.c (afalg_stream): On non-seekable streams, try a single- byte send() as the first round.
Bruno Haible c08a99b5 2018-06-25T00:41:03 af_alg: Fix state of stream after sendfile() succeeds. * lib/af_alg.c (afalg_stream): Invoke fflush and lseek, to ensure that the stream is correctly positioned afterwards. * modules/crypto/af_alg (Depends-on): Add fflush. * tests/test-digest.h (test_digest_on_files): Verify that after the operation the stream is positioned at end of file.
Pádraig Brady a79bb41b 2018-06-24T15:15:16 maint: clarify comments about sticky EOF * lib/af_alg.c: Be more direct that we can't assume stickiness of EOF for portability reasons. * lib/md5.c: Clarify that this isn't just a glibc issue. * lib/sha1.c: Likewise. * lib/sha256.c: Likewise. * lib/sha512.c: Likewise.
Bruno Haible 20841b57 2018-06-25T00:25:31 af_alg: Comment and style improvements. * lib/af_alg.c (alg_socket): Use 'size_t' as index into a string. (afalg_buffer, afalg_stream): Improve comments.
Pádraig Brady 20c91856 2018-06-24T01:46:10 af_alg: avoid hangs when reading from streams * lib/af_alg.c (afalg_stream): Don't assume EOF is sticky, and thus avoid doing a fread() when feof() is set. * lib/md5.c: Ensure feof() is called before fread(). * lib/sha1.c: Likewise. * lib/sha256.c: Likewise. * lib/sha512.c: Likewise.
Pádraig Brady 42085232 2018-06-24T01:29:55 af_alg: fix error handling when hash not returned * lib/af_alg.c (afalg_stream): Handle the case where we've successfully written data to the kernel in the read/write loop, but the kernel doesn't respond with the hash.
Paul Eggert e133742d 2018-05-09T18:28:04 af_alg: fix my typo in afalg_buffer * lib/af_alg.c (afalg_buffer): Fix typo I recently introduced. (afalg_stream): Simplify and avoid the need for a runtime test at the end.
Paul Eggert cb17397c 2018-05-09T18:07:29 af_alg: recover better from crypto failures * lib/af_alg.c (afalg_stream): Recover from crypto failures if the input stream is seekable, by repositioning the stream back to where it was, possibly by just calling sendfile with an offset arg. This lets us return -EAFNOSUPPORT instead of -EIO in some cases, which lets our callers try again with user-mode code. * modules/crypto/af_alg (Depends-on): Depend on fseeko and ftello instead of on fflush and lseek.
Paul Eggert a03d9294 2018-05-09T12:04:37 af_alg: distiguish I/O errors better * lib/af_alg.c (afalg_buffer, afalg_stream): Return -EAFNOSUPPORT, not -EIO, if it’s OK for the caller to try again with user-mode code. (afalg_stream) [!_WIN32 || __CYGWIN__]: Return -EIO (not possibly some other error number) if fflush fails, as the caller should not try again that case.
Paul Eggert be7754a0 2018-05-09T11:53:03 af_alg: avoid gotos * lib/af_alg.c (afalg_buffer, afalg_stream): Rewrite to avoid gotos, as they were a source of unreliability and made the code a bit harder to follow.
Paul Eggert 842c754d 2018-05-09T11:34:28 af_alg: don’t leak file descriptors into children * lib/af_alg.c (alg_socket): Use SOCK_CLOEXEC when creating sockets. This code should be compiled only on recent GNU/Linux platforms so we shouldn’t have to also depend on the accept4 module.
Paul Eggert 9d991bcb 2018-05-09T11:16:59 af_alg: coalesce socket creation * lib/af_alg.c (alg_socket): New function. (afalg_buffer, afalg_stream): Use it. This avoids some code duplication and gotos.
Paul Eggert 3850248f 2018-05-09T11:05:12 af_alg: fix file descriptor leak * lib/af_alg.c (afalg_stream): Close leak.
Paul Eggert 0d896776 2018-05-09T10:38:17 af_alg: Pacify --enable-gcc-warnings on GCC 8 * lib/af_alg.c (afalg_buffer, afalg_stream): Reorder local decls and checking to pacify gcc -Wjump-misses-init on GCC 8.
Paul Eggert 0d10473b 2018-05-07T00:25:57 af_alg: Pacify --enable-gcc-warnings Problem reported by Assaf Gordon in: https://lists.gnu.org/r/bug-gnulib/2018-05/msg00041.html * lib/af_alg.c (afalg_buffer): Move local decls to pacify gcc -Wjump-misses-init. * lib/sha512.c (shaxxx_stream): Now static.
Bruno Haible 55efbb11 2018-05-06T17:04:25 af_alg: Add ability to use Linux kernel crypto API on data in memory. * lib/af_alg.h (afalg_buffer): New declaration. * lib/af_alg.c (afalg_buffer): New function.
Bruno Haible 902d630a 2018-05-06T15:19:44 af_alg: Fix a resource leak. * lib/af_alg.c (afalg_stream): Close socket before returning -EINVAL. New local variable 'result'.
Bruno Haible 22d459bb 2018-05-06T13:30:24 af_alg: Fix bug with streams that are not at position 0. * lib/af_alg.c (afalg_stream): Before sendfile, invoke fflush. Don't assume that the stream is positioned at position 0. * lib/af_alg.h (afalg_stream): Mention restriction regarding the state of the stream. * lib/md5.h (md5_stream): Likewise. * lib/sha1.h (sha1_stream): Likewise. * lib/sha256.h (sha256_stream, sha224_stream): Likewise. * lib/sha512.h (sha512_stream, sha384_stream): Likewise. * modules/crypto/af_alg (Depends-on): Add fflush, lseek.
Bruno Haible fff36252 2018-05-06T12:23:55 af_alg: Add configure option to enable/disable use of Linux crypto API. Suggested by Assaf Gordon <assafgordon@gmail.com>. * m4/af_alg.m4 (gl_AF_ALG): Add AC_ARG_WITH invocation. Define C macro USE_LINUX_CRYPTO_API. * lib/af_alg.h: Test USE_LINUX_CRYPTO_API, not HAVE_LINUX_IF_ALG_H. * lib/af_alg.c: Likewise.
Bruno Haible 9366d9d9 2018-05-06T02:39:38 af_alg: Improve comments. * lib/af_alg.c (afalg_stream): Improve comment about kernel bug.
Bruno Haible 459221a8 2018-05-05T20:27:33 af_alg: New module. * lib/af_alg.h: Test HAVE_* macro through '#if', not '#ifdef'. * lib/af_alg.c: Include "af_alg.h" before the other header files. * lib/md5.c: Include "af_alg.h" unconditionally. (md5_stream): Invoke afalg_stream unconditionally. * lib/sha1.c: Include "af_alg.h" unconditionally. (sha1_stream): Invoke afalg_stream unconditionally. * lib/sha256.c: Include "af_alg.h" unconditionally. (sha256_stream, sha224_stream): Invoke afalg_stream unconditionally. * lib/sha512.c: Include "af_alg.h" unconditionally. (sha512_stream, sha384_stream): Invoke afalg_stream unconditionally. * m4/af_alg.m4: Renamed from m4/linux-if-alg.m4. (gl_AF_ALG): Renamed from gl_LINUX_IF_ALG_H. * modules/crypto/af_alg: New file. * modules/crypto/md5 (Files): Remove files that are now in the 'crypto/af_alg' module. (Depends-on): Add crypto/af_alg. (configure.ac): Remove gl_LINUX_IF_ALG_H invocation. (Makefile.am): Don't mention af_alg.c here. * modules/crypto/sha1 (Files): Remove files that are now in the 'crypto/af_alg' module. (Depends-on): Add crypto/af_alg. (configure.ac): Remove gl_LINUX_IF_ALG_H invocation. (Makefile.am): Don't mention af_alg.c here. * modules/crypto/sha256 (Files): Remove files that are now in the 'crypto/af_alg' module. (Depends-on): Add crypto/af_alg. (configure.ac): Remove gl_LINUX_IF_ALG_H invocation. (Makefile.am): Don't mention af_alg.c here. * modules/crypto/sha512 (Files): Remove files that are now in the 'crypto/af_alg' module. (Depends-on): Add crypto/af_alg. (configure.ac): Remove gl_LINUX_IF_ALG_H invocation. (Makefile.am): Don't mention af_alg.c here.
Paul Eggert 3929fb20 2018-05-05T11:08:08 af_alg: minor style improvements * lib/af_alg.c (afalg_stream): Prefer C99 style decl-after-statement, since we’re already assuming C99. Clarify by strengthening the bind test and omit unnecessary assignment.
Bruno Haible ba847c6e 2018-05-05T20:02:58 af_alg: Fix bug on empty files. * lib/af_alg.c (afalg_stream): Ignore the kernel's result if the input stream is empty.
Paul Eggert a4fd2b6f 2018-05-05T10:27:38 sys-limits.h: new file for crypto and safe I/O * lib/af_alg.c: Include sys-limits.h. (MAX_RW_COUNT): Remove. Use replaced by SYS_BUFSIZE_MAX. (afalg_stream): Also reject negative sizes for sendfile; they should not happen and the code is a bit cleaner and faster this way. * lib/safe-read.c: Include sys-limits.h. (BUGGY_READ_MAXIMUM): Remove. All uses replaced by SYS_BUFSIZE_MAX. * lib/sys-limits.h: New file, with values and commentary derived from the old safe-read.c and from GNU Emacs sysdep.c. * modules/crypto/md5, modules/crypto/sha1, modules/crypto/sha256: * modules/crypto/sha512, modules/safe-read, modules/safe-write: Add lib/sys-limits.h to Files section.
Bruno Haible abe288ce 2018-05-05T18:13:16 af_alg: Improve function signature. * lib/af_alg.h (afalg_stream): Swap second and third argument. * lib/af_alg.c (afalg_stream): Likewise. * lib/md5.c, lib/sha1.c, lib/sha256.c, lib/sha512.c: Callers changed.
Matteo Croce 7074a586 2018-04-28T15:32:55 sha1sum: use AF_ALG when available Linux supports accessing kernel crypto API via AF_ALG since version 2.6.38. Coreutils uses libcrypto when available and fallbacks to generic C implementation of various hashing functions. Add a generic afalg_stream() function which uses AF_ALG to calculate the hash of a stream and use sendfile() when possible (regular file with size less or equal than 0x7ffff000 (2,147,479,552) bytes, AKA MAX_RW_COUNT). Use afalg_stream() only in sha1sum for now, but other hashes are possible. The speed gain really depends on the CPU type, on systems which doesn't use libcrypto ranges from ~10% to 320%. This is a test on a Intel(R) Xeon(R) CPU E3-1265L V2 and Debian stretch: $ 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 Signed-off-by: Matteo Croce <mcroce@redhat.com>