-remove async ecc key generation, not needed
[oweals/gnunet.git] / src / util / crypto_crc.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2004, 2006 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19
20      For the actual CRC-32 code:
21      Copyright abandoned; this code is in the public domain.
22      Provided to GNUnet by peter@horizon.com
23 */
24
25 /**
26  * @file util/crypto_crc.c
27  * @brief implementation of CRC16 and CRC32
28  * @author Christian Grothoff
29  */
30 #include "platform.h"
31 #include "gnunet_common.h"
32 #include "gnunet_crypto_lib.h"
33
34 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
35
36 /* Avoid wasting space on 8-byte longs. */
37 #if UINT_MAX >= 0xffffffff
38 typedef unsigned int uLong;
39 #elif ULONG_MAX >= 0xffffffff
40 typedef unsigned long uLong;
41 #else
42 #error This compiler is not ANSI-compliant!
43 #endif
44
45 #define Z_NULL  0
46
47
48 #define POLYNOMIAL (uLong)0xedb88320
49 static uLong crc_table[256];
50
51 /*
52  * This routine writes each crc_table entry exactly once,
53  * with the correct final value.  Thus, it is safe to call
54  * even on a table that someone else is using concurrently.
55  */
56 static void
57 crc_init ()
58 {
59   static int once;
60   unsigned int i, j;
61   uLong h = 1;
62
63   if (once)
64     return;
65   once = 1;
66   crc_table[0] = 0;
67   for (i = 128; i; i >>= 1)
68   {
69     h = (h >> 1) ^ ((h & 1) ? POLYNOMIAL : 0);
70     /* h is now crc_table[i] */
71     for (j = 0; j < 256; j += 2 * i)
72       crc_table[i + j] = crc_table[j] ^ h;
73   }
74 }
75
76 /*
77  * This computes the standard preset and inverted CRC, as used
78  * by most networking standards.  Start by passing in an initial
79  * chaining value of 0, and then pass in the return value from the
80  * previous crc32() call.  The final return value is the CRC.
81  * Note that this is a little-endian CRC, which is best used with
82  * data transmitted lsbit-first, and it should, itself, be appended
83  * to data in little-endian byte and bit order to preserve the
84  * property of detecting all burst errors of length 32 bits or less.
85  */
86 static uLong
87 crc32 (uLong crc, const char *buf, size_t len)
88 {
89   crc_init ();
90   GNUNET_assert (crc_table[255] != 0);
91   crc ^= 0xffffffff;
92   while (len--)
93     crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
94   return crc ^ 0xffffffff;
95 }
96
97
98 /**
99  * Compute the CRC32 checksum for the first len bytes of the buffer.
100  *
101  * @param buf the data over which we're taking the CRC
102  * @param len the length of the buffer
103  * @return the resulting CRC32 checksum
104  */
105 int32_t
106 GNUNET_CRYPTO_crc32_n (const void *buf, size_t len)
107 {
108   uLong crc;
109
110   crc = crc32 (0L, Z_NULL, 0);
111   crc = crc32 (crc, (char *) buf, len);
112   return crc;
113 }
114
115
116 /**
117  * Perform an incremental step in a CRC16 (for TCP/IP) calculation.
118  *
119  * @param sum current sum, initially 0
120  * @param buf buffer to calculate CRC over (must be 16-bit aligned)
121  * @param len number of bytes in hdr, must be multiple of 2
122  * @return updated crc sum (must be subjected to GNUNET_CRYPTO_crc16_finish to get actual crc16)
123  */
124 uint32_t
125 GNUNET_CRYPTO_crc16_step (uint32_t sum, const void *buf, size_t len)
126 {
127   const uint16_t *hdr = buf;
128   for (; len >= 2; len -= 2)
129     sum += *(hdr++);
130   if (len == 1)
131     sum += (*hdr) & ntohs(0xFF00);
132   return sum;
133 }
134
135
136 /**
137  * Convert results from GNUNET_CRYPTO_crc16_step to final crc16.
138  *
139  * @param sum cummulative sum
140  * @return crc16 value
141  */
142 uint16_t
143 GNUNET_CRYPTO_crc16_finish (uint32_t sum)
144 {
145   sum = (sum >> 16) + (sum & 0xFFFF);
146   sum += (sum >> 16);
147
148   return ~sum;
149 }
150
151
152 /**
153  * Calculate the checksum of a buffer in one step.
154  *
155  * @param buf buffer to  calculate CRC over (must be 16-bit aligned)
156  * @param len number of bytes in hdr, must be multiple of 2
157  * @return crc16 value
158  */
159 uint16_t
160 GNUNET_CRYPTO_crc16_n (const void *buf, size_t len)
161 {
162   const uint16_t *hdr = buf;
163   uint32_t sum = GNUNET_CRYPTO_crc16_step (0, hdr, len);
164
165   return GNUNET_CRYPTO_crc16_finish (sum);
166 }
167
168
169
170 /* end of crypto_crc.c */