suricata
util-crypt.c
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  * Implementation of function using NSS is not linked with libtomcrypt.
27  */
28 
29 #include "suricata-common.h"
30 #include "suricata.h"
31 #include "util-crypt.h"
32 
33 static const char *b64codes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
34 
35 int Base64Encode(const unsigned char *in, unsigned long inlen,
36  unsigned char *out, unsigned long *outlen)
37 {
38  unsigned long i, len2, leven;
39  unsigned char *p;
40  if(in == NULL || out == NULL || outlen == NULL)
41  {
42  return SC_BASE64_INVALID_ARG;
43  }
44  /* valid output size ? */
45  len2 = 4 * ((inlen + 2) / 3);
46  if (*outlen < len2 + 1) {
47  *outlen = len2 + 1;
48  return SC_BASE64_OVERFLOW;
49  }
50  p = out;
51  leven = 3*(inlen / 3);
52  for (i = 0; i < leven; i += 3) {
53  *p++ = b64codes[(in[0] >> 2) & 0x3F];
54  *p++ = b64codes[(((in[0] & 3) << 4) + (in[1] >> 4)) & 0x3F];
55  *p++ = b64codes[(((in[1] & 0xf) << 2) + (in[2] >> 6)) & 0x3F];
56  *p++ = b64codes[in[2] & 0x3F];
57  in += 3;
58  }
59  /* Pad it if necessary... */
60  if (i < inlen) {
61  unsigned a = in[0];
62  unsigned b = (i+1 < inlen) ? in[1] : 0;
63 
64  *p++ = b64codes[(a >> 2) & 0x3F];
65  *p++ = b64codes[(((a & 3) << 4) + (b >> 4)) & 0x3F];
66  *p++ = (i+1 < inlen) ? b64codes[(((b & 0xf) << 2)) & 0x3F] : '=';
67  *p++ = '=';
68  }
69  /* append a NULL byte */
70  *p = '\0';
71  /* return ok */
72  *outlen = p - out;
73  return SC_BASE64_OK;
74 }
SC_BASE64_OVERFLOW
@ SC_BASE64_OVERFLOW
Definition: util-crypt.h:41
SC_BASE64_INVALID_ARG
@ SC_BASE64_INVALID_ARG
Definition: util-crypt.h:40
SC_BASE64_OK
@ SC_BASE64_OK
Definition: util-crypt.h:39
Base64Encode
int Base64Encode(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen)
Definition: util-crypt.c:35
util-crypt.h
suricata-common.h
suricata.h