syslog-ng source
slog.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Airbus Commercial Aircraft
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * As an additional exemption you are allowed to compile & link against the
19  * OpenSSL libraries as published by the OpenSSL project. See the file
20  * COPYING for details.
21  *
22  */
23 
24 #ifndef SLOG_H_INCLUDED
25 #define SLOG_H_INCLUDED 1
26 
27 #define AES_BLOCKSIZE 16
28 #define IV_LENGTH 12
29 #define KEY_LENGTH 32
30 #define CMAC_LENGTH 16
31 #define KEY_ERROR 0x20
32 #define KEY_FILE_ERROR 0x20
33 #define KEY_READ_ERROR 0x21
34 #define KEY_WRITE_ERROR 0x22
35 #define COLON 1
36 #define BLANK 1
37 #define COUNTER_LENGTH 12 // We use an 8 byte counter resulting in 12 byte BASE64 encoding
38 #define CTR_LEN_SIMPLE 20 // This is for the string representation of 8 byte (=2^64) counters
39 
40 // These are arbitrary constants (with mean) Hamming distance.
41 #define IPAD 0x36
42 #define OPAD 0x5C
43 #define EPAD 0x6A
44 
45 // Buffer size for import and verification
46 #define MIN_BUF_SIZE 10
47 #define MAX_BUF_SIZE 1073741823 // INT_MAX/2
48 #define DEF_BUF_SIZE 1000
49 
50 // Error message in case of invalid file
51 #define FILE_ERROR "Invalid path or non existing regular file: "
52 
53 // Structure for command line arguments of template and utilities
54 typedef struct
55 {
56  char *longname;
57  char shortname;
58  char *description;
59  char *type;
60  char *arg;
61 } SLogOptions;
62 
63 // Dump contents of an array on STDOUT, byte by byte, converting to hex.
64 void outputByteBuffer(unsigned char *buf, int length);
65 
66 
67 void evolveKey(unsigned char *key);
68 
69 /*
70  * Encrypts plaintext
71  *
72  * 1. Parameter: pointer to plaintext (input)
73  * 2. Parameter: length of plaintext (input)
74  * 3. Parameter: pointer to key (input)
75  * 4. Parameter: pointer to IV (input, nonce of length IV_LENGTH)
76  * 5. Parameter: pointer to ciphertext (output)
77  * 6. Parameter: pointer to tag (output)
78  *
79  * Note: Caller must take care of memory management.
80  *
81  * Return:
82  * Length of ciphertext (>0)
83  * 0 on error
84  */
85 int sLogEncrypt(unsigned char *plaintext, int plaintext_len,
86  unsigned char *key, unsigned char *iv,
87  unsigned char *ciphertext, unsigned char *tag);
88 /*
89  * Decrypt ciphertext and verify integrity
90  *
91  * 1. Parameter: Pointer to ciphertext (input)
92  * 2. Parameter: Ciphertext length (input)
93  * 3. Parameter: Pointer to integrity tag (input)
94  * 4. Parameter: Pointer to IV (input)
95  * 5. Parameter: Pointer to plaintext (output)
96  *
97  * Note: Caller must take care of memory management.
98  *
99  * Return:
100  * >0 success
101  * -1 in case verification fails
102  * 0 on error
103  */
104 int sLogDecrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *tag, unsigned char *key,
105  unsigned char *iv,
106  unsigned char *plaintext);
107 
108 /*
109  * Compute AES256 CMAC of input
110  *
111  *
112  * 1. Parameter: Pointer to key (input)
113  * 2. Parameter: Pointer to input (input)
114  * 3. Parameter: Input length (input)
115  * 4. Parameter: Pointer to output (output)
116  * 5. Parameter: Length of output (output)
117  * 6. Parameter: Capacity of output buffer (input)
118  *
119  * If Parameter 5 == 0, there was an error.
120  *
121  * Note: Caller must take care of memory management.
122  */
123 void cmac(unsigned char *key, const void *input, gsize length, unsigned char *out, gsize *outlen, gsize out_capacity);
124 
125 
126 gchar *convertToBase64(unsigned char *input, gsize len);
127 guchar *convertToBin(char *input, gsize *outLen);
128 
129 /*
130  * Derive key = evolve key multiple times
131  *
132  * 1. Parameter: Pointer to destination key (output)
133  * 2. Parameter: Number of times current key should be evolved (input)
134  * 3. Parameter: Pointer to current key (input)
135  *
136  *
137  * Note: Caller must take care of memory management.
138  */
139 void deriveKey(unsigned char *dst, guint64 index, guint64 currentKey);
140 
141 /*
142  * Create a new encrypted log entry
143  *
144  * This function creates a new encrypted log entry updates the corresponding MAC accordingly
145  *
146  * 1. Parameter: Number of log entries (for enumerating the entries in the log file)
147  * 2. Parameter: The original log message
148  * 3. Parameter: The current encryption key
149  * 4. Parameter: The current MAC
150  * 5. Parameter: The resulting encrypted log entry
151  * 6. Parameter: The newly updated MAC
152  * 7. Parameter: The capacity of the newly updated MAC buffer
153  */
154 void sLogEntry(guint64 numberOfLogEntries, GString *text, unsigned char *key, unsigned char *inputBigMac,
155  GString *output, unsigned char *outputBigMac, gsize outputBigMac_capacity);
156 
157 /*
158  * Generate a master key
159  *
160  * This unique master key requires 32 bytes of storage.
161  * The caller has to allocate this memory.
162  *
163  * Return:
164  * 1 on success
165  * 0 on error
166  */
167 int generateMasterKey(guchar *masterkey);
168 
169 /*
170  * Generate a host key based on a previously created master key
171  *
172  * 1. Parameter: Master key
173  * 2. Parameter: Host MAC address
174  * 3. Parameter: Host S/N
175  *
176  * The specific unique host key k_0 is k_0 = H(master key|| MAC address || S/N)
177  * and requires 48 bytes of storage. Additional 8 bytes need to be allocated to store
178  * the serial number of the host key. The caller has to allocate this memory.
179  *
180  * Return:
181  * 1 on success
182  * 0 on error
183  */
184 int deriveHostKey(guchar *masterkey, gchar *macAddr, gchar *serial, guchar *hostkey);
185 
186 int readBigMAC(gchar *filename, char *outputBuffer);
187 int writeBigMAC(gchar *filename, char *outputBuffer);
188 
189 /*
190  * Read key from file
191  *
192  * Return:
193  * 1 on success
194  * 0 on error
195  */
196 int readKey(char *destKey, guint64 *destCounter, gchar *keypath);
197 
198 /*
199  * Write key to file
200  *
201  * Return:
202  * 1 on success
203  * 0 on error
204  */
205 int writeKey(char *key, guint64 counter, gchar *keypath);
206 
207 /*
208  * Verify the integrity of an existing log file
209  *
210  * Return:
211  * 1 on success
212  * 0 on error
213  */
214 int fileVerify(unsigned char *key, char *inputFileName, char *outputFileName, unsigned char *bigMac,
215  guint64 entriesInFile, int chunkLength);
216 
217 int initVerify(guint64 entriesInFile, unsigned char *key, guint64 *nextLogEntry, guint64 *startingEntry,
218  GString **input, GHashTable **tab);
219 
220 int iterateBuffer(guint64 entriesInBuffer, GString **input, guint64 *nextLogEntry, unsigned char *key,
221  unsigned char *keyZero, guint keyNumber, GString **output, guint64 *numberOfLogEntries, unsigned char *cmac_tag,
222  gsize cmac_tag_capacity, GHashTable *tab);
223 
224 int finalizeVerify(guint64 startingEntry, guint64 entriesInFile, unsigned char *bigMac, unsigned char *cmac_tag,
225  GHashTable *tab);
226 
227 int iterativeFileVerify(unsigned char *previousMAC, unsigned char *previousKey, char *inputFileName,
228  unsigned char *currentMAC, char *outputFileName, guint64 entriesInFile, int chunkLength, guint64 keyNumber);
229 
230 void deriveEncSubKey(unsigned char *mainKey, unsigned char *encKey);
231 void deriveMACSubKey(unsigned char *mainKey, unsigned char *MACKey);
232 void PRF(unsigned char *key, unsigned char *originalInput, guint64 inputLength, unsigned char *output,
233  guint64 outputLength);
234 
235 // Print usage message and clean up
236 int slog_usage(GOptionContext *ctx, GOptionGroup *grp, GString *errormsg);
237 
238 /*
239  * Callback function to check whether a command line argument represents a valid file name
240  *
241  * Return:
242  * TRUE on success
243  * FALSE on error
244  */
245 gboolean validFileNameArg(const gchar *option_name, const gchar *value, gpointer data, GError **error);
246 
247 #endif
void deriveKey(unsigned char *dst, guint64 index, guint64 currentKey)
Definition: slog.c:397
void deriveMACSubKey(unsigned char *mainKey, unsigned char *MACKey)
Definition: slog.c:93
void sLogEntry(guint64 numberOfLogEntries, GString *text, unsigned char *key, unsigned char *inputBigMac, GString *output, unsigned char *outputBigMac, gsize outputBigMac_capacity)
Definition: slog.c:306
void PRF(unsigned char *key, unsigned char *originalInput, guint64 inputLength, unsigned char *output, guint64 outputLength)
Definition: slog.c:490
int iterativeFileVerify(unsigned char *previousMAC, unsigned char *previousKey, char *inputFileName, unsigned char *currentMAC, char *outputFileName, guint64 entriesInFile, int chunkLength, guint64 keyNumber)
Definition: slog.c:1291
guchar * convertToBin(char *input, gsize *outLen)
Definition: slog.c:405
int finalizeVerify(guint64 startingEntry, guint64 entriesInFile, unsigned char *bigMac, unsigned char *cmac_tag, GHashTable *tab)
Definition: slog.c:1186
void deriveEncSubKey(unsigned char *mainKey, unsigned char *encKey)
Definition: slog.c:88
int iterateBuffer(guint64 entriesInBuffer, GString **input, guint64 *nextLogEntry, unsigned char *key, unsigned char *keyZero, guint keyNumber, GString **output, guint64 *numberOfLogEntries, unsigned char *cmac_tag, gsize cmac_tag_capacity, GHashTable *tab)
Definition: slog.c:1010
int slog_usage(GOptionContext *ctx, GOptionGroup *grp, GString *errormsg)
Definition: slog.c:1940
void evolveKey(unsigned char *key)
Definition: slog.c:470
void outputByteBuffer(unsigned char *buf, int length)
int readBigMAC(gchar *filename, char *outputBuffer)
Definition: slog.c:690
int fileVerify(unsigned char *key, char *inputFileName, char *outputFileName, unsigned char *bigMac, guint64 entriesInFile, int chunkLength)
Definition: slog.c:1601
void cmac(unsigned char *key, const void *input, gsize length, unsigned char *out, gsize *outlen, gsize out_capacity)
Definition: slog.c:430
int deriveHostKey(guchar *masterkey, gchar *macAddr, gchar *serial, guchar *hostkey)
Definition: slog.c:549
gchar * convertToBase64(unsigned char *input, gsize len)
Definition: slog.c:410
int readKey(char *destKey, guint64 *destCounter, gchar *keypath)
Definition: slog.c:787
int initVerify(guint64 entriesInFile, unsigned char *key, guint64 *nextLogEntry, guint64 *startingEntry, GString **input, GHashTable **tab)
Definition: slog.c:1234
int sLogDecrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *tag, unsigned char *key, unsigned char *iv, unsigned char *plaintext)
Definition: slog.c:214
int sLogEncrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *iv, unsigned char *ciphertext, unsigned char *tag)
Definition: slog.c:117
int writeBigMAC(gchar *filename, char *outputBuffer)
Definition: slog.c:592
gboolean validFileNameArg(const gchar *option_name, const gchar *value, gpointer data, GError **error)
Definition: slog.c:1961
int generateMasterKey(guchar *masterkey)
Definition: slog.c:529
int writeKey(char *key, guint64 counter, gchar *keypath)
Definition: slog.c:909
Definition: slog.h:55
char * description
Definition: slog.h:58
char * type
Definition: slog.h:59
char * longname
Definition: slog.h:56
char * arg
Definition: slog.h:60
char shortname
Definition: slog.h:57
GString * value
Definition: test_decode.c:28
struct tm key
Definition: cache.c:63