mountlist: Use Linux code on Android.
[gnulib.git] / lib / af_alg.h
blob877369a7fbb6596112851e7037daebd8c7fa1077
1 /* af_alg.h - Compute message digests from file streams and buffers.
2 Copyright (C) 2018-2019 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any
7 later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Matteo Croce <mcroce@redhat.com>, 2018.
18 Documentation by Bruno Haible <bruno@clisp.org>, 2018. */
20 /* Declare specific functions for computing message digests
21 using the Linux kernel crypto API, if available. This kernel API gives
22 access to specialized crypto instructions (that would also be available
23 in user space) or to crypto devices (not directly available in user space).
25 For a more complete set of facilities that use the Linux kernel crypto API,
26 look at libkcapi. */
28 #ifndef AF_ALG_H
29 # define AF_ALG_H 1
31 # include <stdio.h>
32 # include <errno.h>
34 # ifdef __cplusplus
35 extern "C" {
36 # endif
38 # if USE_LINUX_CRYPTO_API
40 /* Compute a message digest of a memory region.
42 The memory region starts at BUFFER and is LEN bytes long.
44 ALG is the message digest algorithm; see the file /proc/crypto.
46 RESBLOCK points to a block of HASHLEN bytes, for the result.
47 HASHLEN must be the length of the message digest, in bytes, in particular:
49 alg | hashlen
50 -------+--------
51 md5 | 16
52 sha1 | 20
53 sha224 | 28
54 sha256 | 32
55 sha384 | 48
56 sha512 | 64
58 If successful, fill RESBLOCK and return 0.
59 Upon failure, return a negated error number. */
60 int
61 afalg_buffer (const char *buffer, size_t len, const char *alg,
62 void *resblock, ssize_t hashlen);
64 /* Compute a message digest of data read from STREAM.
66 STREAM is an open file stream. The last operation on STREAM should
67 not be 'ungetc', and if STREAM is also open for writing it should
68 have been fflushed since its last write. Read from the current
69 position to the end of STREAM. Handle regular files efficiently.
71 ALG is the message digest algorithm; see the file /proc/crypto.
73 RESBLOCK points to a block of HASHLEN bytes, for the result.
74 HASHLEN must be the length of the message digest, in bytes, in particular:
76 alg | hashlen
77 -------+--------
78 md5 | 16
79 sha1 | 20
80 sha224 | 28
81 sha256 | 32
82 sha384 | 48
83 sha512 | 64
85 If successful, fill RESBLOCK and return 0.
86 Upon failure, return a negated error number.
87 Unless returning 0 or -EIO, restore STREAM's file position so that
88 the caller can fall back on some other method. */
89 int
90 afalg_stream (FILE *stream, const char *alg,
91 void *resblock, ssize_t hashlen);
93 # else
95 static inline int
96 afalg_buffer (const char *buffer, size_t len, const char *alg,
97 void *resblock, ssize_t hashlen)
99 return -EAFNOSUPPORT;
102 static inline int
103 afalg_stream (FILE *stream, const char *alg,
104 void *resblock, ssize_t hashlen)
106 return -EAFNOSUPPORT;
109 # endif
111 # ifdef __cplusplus
113 # endif
115 #endif /* AF_ALG_H */