Blame lib/af_alg.h

Packit 8f70b4
/* af_alg.h - Compute message digests from file streams and buffers.
Packit 8f70b4
   Copyright (C) 2018 Free Software Foundation, Inc.
Packit 8f70b4
Packit 8f70b4
   This program is free software; you can redistribute it and/or modify it
Packit 8f70b4
   under the terms of the GNU General Public License as published by the
Packit 8f70b4
   Free Software Foundation; either version 3, or (at your option) any
Packit 8f70b4
   later version.
Packit 8f70b4
Packit 8f70b4
   This program is distributed in the hope that it will be useful,
Packit 8f70b4
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8f70b4
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 8f70b4
   GNU General Public License for more details.
Packit 8f70b4
Packit 8f70b4
   You should have received a copy of the GNU General Public License
Packit 8f70b4
   along with this program; if not, see <https://www.gnu.org/licenses/>.  */
Packit 8f70b4
Packit 8f70b4
/* Written by Matteo Croce <mcroce@redhat.com>, 2018.
Packit 8f70b4
   Documentation by Bruno Haible <bruno@clisp.org>, 2018.  */
Packit 8f70b4
Packit 8f70b4
/* Declare specific functions for computing message digests
Packit 8f70b4
   using the Linux kernel crypto API, if available.  This kernel API gives
Packit 8f70b4
   access to specialized crypto instructions (that would also be available
Packit 8f70b4
   in user space) or to crypto devices (not directly available in user space).
Packit 8f70b4
Packit 8f70b4
   For a more complete set of facilities that use the Linux kernel crypto API,
Packit 8f70b4
   look at libkcapi.  */
Packit 8f70b4
Packit 8f70b4
#ifndef AF_ALG_H
Packit 8f70b4
# define AF_ALG_H 1
Packit 8f70b4
Packit 8f70b4
# include <stdio.h>
Packit 8f70b4
# include <errno.h>
Packit 8f70b4
Packit 8f70b4
# ifdef __cplusplus
Packit 8f70b4
extern "C" {
Packit 8f70b4
# endif
Packit 8f70b4
Packit 8f70b4
# if USE_LINUX_CRYPTO_API
Packit 8f70b4
Packit 8f70b4
/* Compute a message digest of a memory region.
Packit 8f70b4
Packit 8f70b4
   The memory region starts at BUFFER and is LEN bytes long.
Packit 8f70b4
Packit 8f70b4
   ALG is the message digest algorithm; see the file /proc/crypto.
Packit 8f70b4
Packit 8f70b4
   RESBLOCK points to a block of HASHLEN bytes, for the result.
Packit 8f70b4
   HASHLEN must be the length of the message digest, in bytes, in particular:
Packit 8f70b4
Packit 8f70b4
      alg    | hashlen
Packit 8f70b4
      -------+--------
Packit 8f70b4
      md5    | 16
Packit 8f70b4
      sha1   | 20
Packit 8f70b4
      sha224 | 28
Packit 8f70b4
      sha256 | 32
Packit 8f70b4
      sha384 | 48
Packit 8f70b4
      sha512 | 64
Packit 8f70b4
Packit 8f70b4
   If successful, fill RESBLOCK and return 0.
Packit 8f70b4
   Upon failure, return a negated error number.  */
Packit 8f70b4
int
Packit 8f70b4
afalg_buffer (const char *buffer, size_t len, const char *alg,
Packit 8f70b4
              void *resblock, ssize_t hashlen);
Packit 8f70b4
Packit 8f70b4
/* Compute a message digest of data read from STREAM.
Packit 8f70b4
Packit 8f70b4
   STREAM is an open file stream.  The last operation on STREAM should
Packit 8f70b4
   not be 'ungetc', and if STREAM is also open for writing it should
Packit 8f70b4
   have been fflushed since its last write.  Read from the current
Packit 8f70b4
   position to the end of STREAM.  Handle regular files efficently.
Packit 8f70b4
Packit 8f70b4
   ALG is the message digest algorithm; see the file /proc/crypto.
Packit 8f70b4
Packit 8f70b4
   RESBLOCK points to a block of HASHLEN bytes, for the result.
Packit 8f70b4
   HASHLEN must be the length of the message digest, in bytes, in particular:
Packit 8f70b4
Packit 8f70b4
      alg    | hashlen
Packit 8f70b4
      -------+--------
Packit 8f70b4
      md5    | 16
Packit 8f70b4
      sha1   | 20
Packit 8f70b4
      sha224 | 28
Packit 8f70b4
      sha256 | 32
Packit 8f70b4
      sha384 | 48
Packit 8f70b4
      sha512 | 64
Packit 8f70b4
Packit 8f70b4
   If successful, fill RESBLOCK and return 0.
Packit 8f70b4
   Upon failure, return a negated error number.
Packit 8f70b4
   Unless returning 0 or -EIO, restore STREAM's file position so that
Packit 8f70b4
   the caller can fall back on some other method.  */
Packit 8f70b4
int
Packit 8f70b4
afalg_stream (FILE *stream, const char *alg,
Packit 8f70b4
              void *resblock, ssize_t hashlen);
Packit 8f70b4
Packit 8f70b4
# else
Packit 8f70b4
Packit 8f70b4
static inline int
Packit 8f70b4
afalg_buffer (const char *buffer, size_t len, const char *alg,
Packit 8f70b4
              void *resblock, ssize_t hashlen)
Packit 8f70b4
{
Packit 8f70b4
  return -EAFNOSUPPORT;
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
static inline int
Packit 8f70b4
afalg_stream (FILE *stream, const char *alg,
Packit 8f70b4
              void *resblock, ssize_t hashlen)
Packit 8f70b4
{
Packit 8f70b4
  return -EAFNOSUPPORT;
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
# endif
Packit 8f70b4
Packit 8f70b4
# ifdef __cplusplus
Packit 8f70b4
}
Packit 8f70b4
# endif
Packit 8f70b4
Packit 8f70b4
#endif /* AF_ALG_H */