diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/include/gnunet_crypto_lib.h | 34 | ||||
-rw-r--r-- | src/util/crypto_crc.c | 58 |
2 files changed, 90 insertions, 2 deletions
diff --git a/src/include/gnunet_crypto_lib.h b/src/include/gnunet_crypto_lib.h index 76806d432..0be9611ba 100644 --- a/src/include/gnunet_crypto_lib.h +++ b/src/include/gnunet_crypto_lib.h | |||
@@ -233,6 +233,40 @@ struct GNUNET_CRYPTO_AuthKey | |||
233 | void | 233 | void |
234 | GNUNET_CRYPTO_seed_weak_random (int32_t seed); | 234 | GNUNET_CRYPTO_seed_weak_random (int32_t seed); |
235 | 235 | ||
236 | |||
237 | /** | ||
238 | * Perform an incremental step in a CRC16 (for TCP/IP) calculation. | ||
239 | * | ||
240 | * @param sum current sum, initially 0 | ||
241 | * @param hdr buffer to calculate CRC over (must be 16-bit aligned) | ||
242 | * @param len number of bytes in hdr, must be multiple of 2 | ||
243 | * @return updated crc sum (must be subjected to GNUNET_CRYPTO_crc16_finish to get actual crc16) | ||
244 | */ | ||
245 | uint32_t | ||
246 | GNUNET_CRYPTO_crc16_step (uint32_t sum, uint16_t * hdr, size_t len); | ||
247 | |||
248 | |||
249 | /** | ||
250 | * Convert results from GNUNET_CRYPTO_crc16_step to final crc16. | ||
251 | * | ||
252 | * @param sum cummulative sum | ||
253 | * @return crc16 value | ||
254 | */ | ||
255 | uint16_t | ||
256 | GNUNET_CRYPTO_crc16_finish (uint32_t sum); | ||
257 | |||
258 | |||
259 | /** | ||
260 | * Calculate the checksum of a buffer in one step. | ||
261 | * | ||
262 | * @param hdr buffer to calculate CRC over (must be 16-bit aligned) | ||
263 | * @param len number of bytes in hdr, must be multiple of 2 | ||
264 | * @return crc16 value | ||
265 | */ | ||
266 | uint16_t | ||
267 | GNUNET_CRYPTO_crc16_n (uint16_t *hdr, size_t len); | ||
268 | |||
269 | |||
236 | /** | 270 | /** |
237 | * Compute the CRC32 checksum for the first len | 271 | * Compute the CRC32 checksum for the first len |
238 | * bytes of the buffer. | 272 | * bytes of the buffer. |
diff --git a/src/util/crypto_crc.c b/src/util/crypto_crc.c index 698d8d450..c26541457 100644 --- a/src/util/crypto_crc.c +++ b/src/util/crypto_crc.c | |||
@@ -17,14 +17,14 @@ | |||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
18 | Boston, MA 02111-1307, USA. | 18 | Boston, MA 02111-1307, USA. |
19 | 19 | ||
20 | For the actual CRC code: | 20 | For the actual CRC-32 code: |
21 | Copyright abandoned; this code is in the public domain. | 21 | Copyright abandoned; this code is in the public domain. |
22 | Provided to GNUnet by peter@horizon.com | 22 | Provided to GNUnet by peter@horizon.com |
23 | */ | 23 | */ |
24 | 24 | ||
25 | /** | 25 | /** |
26 | * @file util/crypto_crc.c | 26 | * @file util/crypto_crc.c |
27 | * @brief implementation of CRC32 | 27 | * @brief implementation of CRC16 and CRC32 |
28 | * @author Christian Grothoff | 28 | * @author Christian Grothoff |
29 | */ | 29 | */ |
30 | 30 | ||
@@ -113,4 +113,58 @@ GNUNET_CRYPTO_crc32_n (const void *buf, size_t len) | |||
113 | return crc; | 113 | return crc; |
114 | } | 114 | } |
115 | 115 | ||
116 | |||
117 | |||
118 | /** | ||
119 | * Perform an incremental step in a CRC16 (for TCP/IP) calculation. | ||
120 | * | ||
121 | * @param sum current sum, initially 0 | ||
122 | * @param hdr buffer to calculate CRC over (must be 16-bit aligned) | ||
123 | * @param len number of bytes in hdr, must be multiple of 2 | ||
124 | * @return updated crc sum (must be subjected to GNUNET_CRYPTO_crc16_finish to get actual crc16) | ||
125 | */ | ||
126 | uint32_t | ||
127 | GNUNET_CRYPTO_crc16_step (uint32_t sum, uint16_t * hdr, size_t len) | ||
128 | { | ||
129 | GNUNET_assert (0 == (len & 1)); | ||
130 | for (; len >= 2; len -= 2) | ||
131 | sum += *(hdr++); | ||
132 | if (len == 1) | ||
133 | sum += *((unsigned char *) hdr); | ||
134 | return sum; | ||
135 | } | ||
136 | |||
137 | /** | ||
138 | * Convert results from GNUNET_CRYPTO_crc16_step to final crc16. | ||
139 | * | ||
140 | * @param sum cummulative sum | ||
141 | * @return crc16 value | ||
142 | */ | ||
143 | uint16_t | ||
144 | GNUNET_CRYPTO_crc16_finish (uint32_t sum) | ||
145 | { | ||
146 | sum = (sum >> 16) + (sum & 0xFFFF); | ||
147 | sum += (sum >> 16); | ||
148 | |||
149 | return ~sum; | ||
150 | } | ||
151 | |||
152 | |||
153 | /** | ||
154 | * Calculate the checksum of a buffer in one step. | ||
155 | * | ||
156 | * @param hdr buffer to calculate CRC over (must be 16-bit aligned) | ||
157 | * @param len number of bytes in hdr, must be multiple of 2 | ||
158 | * @return crc16 value | ||
159 | */ | ||
160 | uint16_t | ||
161 | GNUNET_CRYPTO_crc16_n (uint16_t *hdr, size_t len) | ||
162 | { | ||
163 | uint32_t sum = GNUNET_CRYPTO_crc16_step (0, hdr, len); | ||
164 | |||
165 | return GNUNET_CRYPTO_crc16_finish (sum); | ||
166 | } | ||
167 | |||
168 | |||
169 | |||
116 | /* end of crypto_crc.c */ | 170 | /* end of crypto_crc.c */ |