1 /* af_alg.h - Compute message digests from file streams and buffers. 2 Copyright (C) 2018-2023 Free Software Foundation, Inc. 3 4 This file is free software: you can redistribute it and/or modify 5 it under the terms of the GNU Lesser General Public License as 6 published by the Free Software Foundation; either version 2.1 of the 7 License, or (at your option) any later version. 8 9 This file 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 Lesser General Public License for more details. 13 14 You should have received a copy of the GNU Lesser General Public License 15 along with this program. If not, see <https://www.gnu.org/licenses/>. */ 16 17 /* Written by Matteo Croce <mcroce@redhat.com>, 2018. 18 Documentation by Bruno Haible <bruno@clisp.org>, 2018. */ 19 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). 24 25 For a more complete set of facilities that use the Linux kernel crypto API, 26 look at libkcapi. */ 27 28 #ifndef AF_ALG_H 29 # define AF_ALG_H 1 30 31 # include <stdio.h> 32 # include <errno.h> 33 34 # ifdef __cplusplus 35 extern "C" { 36 # endif 37 38 # if USE_LINUX_CRYPTO_API 39 40 /* Compute a message digest of a memory region. 41 42 The memory region starts at BUFFER and is LEN bytes long. 43 44 ALG is the message digest algorithm; see the file /proc/crypto. 45 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: 48 49 alg | hashlen 50 -------+-------- 51 md5 | 16 52 sha1 | 20 53 sha224 | 28 54 sha256 | 32 55 sha384 | 48 56 sha512 | 64 57 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); 63 64 /* Compute a message digest of data read from STREAM. 65 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. 70 71 ALG is the message digest algorithm; see the file /proc/crypto. 72 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: 75 76 alg | hashlen 77 -------+-------- 78 md5 | 16 79 sha1 | 20 80 sha224 | 28 81 sha256 | 32 82 sha384 | 48 83 sha512 | 64 84 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); 92 93 # else 94 95 static inline int 96 afalg_buffer (const char *buffer, size_t len, const char *alg, 97 void *resblock, ssize_t hashlen) 98 { 99 return -EAFNOSUPPORT; 100 } 101 102 static inline int 103 afalg_stream (FILE *stream, const char *alg, 104 void *resblock, ssize_t hashlen) 105 { 106 return -EAFNOSUPPORT; 107 } 108 109 # endif 110 111 # ifdef __cplusplus 112 } 113 # endif 114 115 #endif /* AF_ALG_H */