aboutsummaryrefslogtreecommitdiffstats
path: root/src/libkeccak/files.h
blob: f3279af196d2f0b1b82771e0c27534c3f35ec846 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
 * libkeccak – Keccak-family hashing library
 * 
 * Copyright © 2014  Mattias Andrée (maandree@member.fsf.org)
 * 
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef LIBKECCAK_FILES_H
#define LIBKECCAK_FILES_H  1


#include "../libkeccak.h"


/**
 * Calculate a Keccak-family hashsum of a file,
 * the content of the file is assumed non-sensitive
 * 
 * @param   fd       The file descriptor of the file to hash
 * @param   state    The hashing state, should not be initialised (memory leak otherwise)
 * @param   spec     Specifications for the hashing algorithm
 * @param   suffix   The data suffix, see `libkeccak_digest`
 * @param   hashsum  Output array for the hashsum, have an allocation size of
 *                   at least `(spec->output / 8) * sizeof(char)`, may be `NULL`
 * @return           Zero on success, -1 on error
 */
__attribute__((nonnull(2, 3)))
int libkeccak_generalised_sum_fd(int fd, libkeccak_state_t* restrict state,
				 const libkeccak_spec_t* restrict spec,
				 const char* restrict suffix, char* restrict hashsum);


/**
 * Calculate the Keccak hashsum of a file,
 * the content of the file is assumed non-sensitive
 * 
 * @param   fd       The file descriptor of the file to hash
 * @param   state    The hashing state, should not be initialised (memory leak otherwise)
 * @param   spec     Specifications for the hashing algorithm
 * @param   hashsum  Output array for the hashsum, have an allocation size of
 *                   at least `(spec->output / 8) * sizeof(char)`, may be `NULL`
 * @return           Zero on success, -1 on error
 */
static inline __attribute__((nonnull(2, 3), artificial, gnu_inline))
int libkeccak_keccaksum_fd(int fd, libkeccak_state_t* restrict state,
			   const libkeccak_spec_t* restrict spec, char* restrict hashsum)
{
  return libkeccak_generalised_sum_fd(fd, state, spec, NULL, hashsum);
}


/**
 * Calculate the SHA3 hashsum of a file,
 * the content of the file is assumed non-sensitive
 * 
 * @param   fd       The file descriptor of the file to hash
 * @param   state    The hashing state, should not be initialised (memory leak otherwise)
 * @param   output   The output size parameter for the hashing algorithm
 * @param   hashsum  Output array for the hashsum, have an allocation size of
 *                   at least `(output / 8) * sizeof(char)`, may be `NULL`
 * @return           Zero on success, -1 on error
 */
static inline __attribute__((nonnull(2), artificial, gnu_inline))
int libkeccak_sha3sum_fd(int fd, libkeccak_state_t* restrict state,
			 long output, char* restrict hashsum)
{
  libkeccak_spec_t spec;
  libkeccak_spec_sha3(&spec, output);
  return libkeccak_generalised_sum_fd(fd, state, &spec, LIBKECCAK_SHA3_SUFFIX, hashsum);
}


/**
 * Calculate the RawSHAKE hashsum of a file,
 * the content of the file is assumed non-sensitive
 * 
 * @param   fd            The file descriptor of the file to hash
 * @param   state         The hashing state, should not be initialised (memory leak otherwise)
 * @param   semicapacity  The semicapacity parameter for the hashing algorithm
 * @param   output        The output size parameter for the hashing algorithm
 * @param   hashsum       Output array for the hashsum, have an allocation size of
 *                        at least `(output / 8) * sizeof(char)`, may be `NULL`
 * @return                Zero on success, -1 on error
 */
static inline __attribute__((nonnull(2), artificial, gnu_inline))
int libkeccak_rawshakesum_fd(int fd, libkeccak_state_t* restrict state,
			     long semicapacity, long output, char* restrict hashsum)
{
  libkeccak_spec_t spec;
  libkeccak_spec_rawshake(&spec, semicapacity, output);
  return libkeccak_generalised_sum_fd(fd, state, &spec, LIBKECCAK_RAWSHAKE_SUFFIX, hashsum);
}


/**
 * Calculate the SHAKE hashsum of a file,
 * the content of the file is assumed non-sensitive
 * 
 * @param   fd            The file descriptor of the file to hash
 * @param   state         The hashing state, should not be initialised (memory leak otherwise)
 * @param   semicapacity  The semicapacity parameter for the hashing algorithm
 * @param   output        The output size parameter for the hashing algorithm
 * @param   hashsum       Output array for the hashsum, have an allocation size of
 *                        at least `(output / 8) * sizeof(char)`, may be `NULL`
 * @return                Zero on success, -1 on error
 */
static inline __attribute__((nonnull(2), artificial, gnu_inline))
int libkeccak_shakesum_fd(int fd, libkeccak_state_t* restrict state,
			  long semicapacity, long output, char* restrict hashsum)
{
  libkeccak_spec_t spec;
  libkeccak_spec_shake(&spec, semicapacity, output);
  return libkeccak_generalised_sum_fd(fd, state, &spec, LIBKECCAK_SHAKE_SUFFIX, hashsum);
}


#endif