bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH v2 1/4] sha1sum: use AF_ALG when available


From: Matteo Croce
Subject: Re: [PATCH v2 1/4] sha1sum: use AF_ALG when available
Date: Thu, 26 Apr 2018 00:14:49 +0200

On Wed, Apr 25, 2018 at 2:25 PM, Dmitry V. Levin <address@hidden> wrote:
> On Wed, Apr 25, 2018 at 01:26:08PM +0200, Matteo Croce wrote:
> [...]
>> +#include <config.h>
>> +
>> +#include <sys/socket.h>
>> +
>> +#ifdef AF_ALG
>> +
>> +#include <unistd.h>
>> +#include <string.h>
>> +#include <linux/if_alg.h>
>> +#include <sys/stat.h>
>> +#include <sys/sendfile.h>
>> +
>> +#include "af_alg.h"
>> +
>> +/* from linux/include/linux/fs.h: (INT_MAX & PAGE_MASK) */
>> +#define MAX_RW_COUNT 0x7FFFF000
>> +#define BLOCKSIZE 32768
>> +
>> +int
>> +afalg_stream (FILE * stream, void *resblock, const char *alg, ssize_t 
>> hashlen)
>> +{
>> +  struct sockaddr_alg salg = {
>
> As struct sockaddr_alg is not provided by glibc,
> you'd have to check for HAVE_LINUX_IF_ALG_H after all.
>

Did you mean check for HAVE_LINUX_IF_ALG_H instead of AF_ALG?
That would work without even including sys/socket.h.

> [...]
>> +  /* if file is a regular file, attempt sendfile() to pipe the data */
>> +  if (!fstat (fileno (stream), &st) && S_ISREG (st.st_mode) &&
>> +      st.st_size <= MAX_RW_COUNT)
>> +    {
>> +      if (sendfile (ofd, fileno (stream), NULL, st.st_size) != st.st_size)
>
> Given that sendfile has offset argument, wouldn't it be better
> to use a sendfile loop for handling large regular files?
>

It would be nice to call sendfile multiple times, unfortunately after
every write the kernel calculates the hash unless you set the MSG_MORE
flag in the send(), but sendfile doesn't allow to inform the kernel
that we're sending more data later. From
Documentation/crypto/userspace-if.rst

Using the send() system call, the application provides the data that
should be processed with the message digest. The send system call allows
the following flags to be specified:

-  MSG_MORE: If this flag is set, the send system call acts like a
   message digest update function where the final hash is not yet
   calculated. If the flag is not set, the send system call calculates
   the final message digest immediately.

Cheers,
-- 
Matteo Croce
per aspera ad upstream



reply via email to

[Prev in Thread] Current Thread [Next in Thread]