suricata
util-crypt.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2012 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Roliers Jean-Paul <popof.fpn@gmail.co>
22  *
23  * Implements cryptographic functions.
24  * Based on the libtomcrypt library ( http://libtom.org/?page=features&newsitems=5&whatfile=crypt )
25  */
26 
27 #ifndef UTIL_CRYPT_H_
28 #define UTIL_CRYPT_H_
29 
30 #include "suricata-common.h"
31 
32 /* Ratio of output bytes to input bytes for Base64 Encoding is 4:3, hence the
33  * required output bytes are 4 * ceil(input_len / 3) and an additional byte
34  * for storing the NULL pointer.
35  * */
36 #define BASE64_BUFFER_SIZE(x) ((4 * ((x) + 2) / 3) + 1)
37 
38 typedef enum {
42 
46 
47 } CryptId;
48 
49 #ifndef HAVE_NSS
50 
51 #define SHA1_LENGTH 20
52 
53 #define LOAD32H(x, y) \
54  { x = ((unsigned long)((y)[0] & 255)<<24) | \
55  ((unsigned long)((y)[1] & 255)<<16) | \
56  ((unsigned long)((y)[2] & 255)<<8) | \
57  ((unsigned long)((y)[3] & 255)); }
58 
59 #define STORE64H(x, y) \
60  { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
61  (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
62  (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
63  (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }
64 
65 #define STORE32H(x, y) \
66  { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \
67  (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); }
68 
69 #define ROL(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
70 #define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
71 #ifndef MIN
72 #define MIN(x, y) ( ((x)<(y))?(x):(y) )
73 #endif
74 
75 typedef struct Sha1State_ {
76  uint64_t length;
77  uint32_t state[5], curlen;
78  unsigned char buf[64];
80 
81 typedef union HashState_ {
82  char dummy[1];
84  void *data;
86 
87 #endif /* don't HAVE_NSS */
88 
89 int ComputeSHA1(const uint8_t * inbuf, size_t inbuf_len,
90  uint8_t *outbuf, size_t outbuf_len);
91 int Base64Encode(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen);
92 
93 #endif /* UTIL_CRYPT_H_ */
Sha1State
struct Sha1State_ Sha1State
HashState_::sha1
Sha1State sha1
Definition: util-crypt.h:83
HashState_
Definition: util-crypt.h:81
SC_BASE64_OVERFLOW
@ SC_BASE64_OVERFLOW
Definition: util-crypt.h:45
SC_BASE64_INVALID_ARG
@ SC_BASE64_INVALID_ARG
Definition: util-crypt.h:44
CryptId
CryptId
Definition: util-crypt.h:38
SC_SHA_1_NOK
@ SC_SHA_1_NOK
Definition: util-crypt.h:40
SC_BASE64_OK
@ SC_BASE64_OK
Definition: util-crypt.h:43
Sha1State_
Definition: util-crypt.h:75
SC_SHA_1_OK
@ SC_SHA_1_OK
Definition: util-crypt.h:39
Sha1State_::curlen
uint32_t curlen
Definition: util-crypt.h:77
SC_SHA_1_INVALID_ARG
@ SC_SHA_1_INVALID_ARG
Definition: util-crypt.h:41
suricata-common.h
HashState_::data
void * data
Definition: util-crypt.h:84
HashState_::dummy
char dummy[1]
Definition: util-crypt.h:82
Sha1State_::length
uint64_t length
Definition: util-crypt.h:76
ComputeSHA1
int ComputeSHA1(const uint8_t *inbuf, size_t inbuf_len, uint8_t *outbuf, size_t outbuf_len)
calculate SHA1 hash
Definition: util-crypt.c:230
HashState
union HashState_ HashState
Sha1State_::state
uint32_t state[5]
Definition: util-crypt.h:77
Sha1State_::buf
unsigned char buf[64]
Definition: util-crypt.h:78
Base64Encode
int Base64Encode(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen)
Definition: util-crypt.c:272