application: Use printerr for runtime errors
[glib.git] / glib / ghmac.c
blob1da1b7f186741a7e113a16a9370199a0bd18d5f7
1 /* ghmac.h - data hashing functions
3 * Copyright (C) 2011 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Stef Walter <stefw@collabora.co.uk>
23 #include "config.h"
25 #include <string.h>
27 #include "ghmac.h"
29 #include "glib/galloca.h"
30 #include "gatomic.h"
31 #include "gslice.h"
32 #include "gmem.h"
33 #include "gstrfuncs.h"
34 #include "gtestutils.h"
35 #include "gtypes.h"
36 #include "glibintl.h"
39 /**
40 * SECTION:hmac
41 * @title: Secure HMAC Digests
42 * @short_description: computes the HMAC for data
44 * HMACs should be used when producing a cookie or hash based on data
45 * and a key. Simple mechanisms for using SHA1 and other algorithms to
46 * digest a key and data together are vulnerable to various security
47 * issues. <ulink url="http://en.wikipedia.org/wiki/HMAC">HMAC</ulink>
48 * uses algorithms like SHA1 in a secure way to produce a digest of a
49 * key and data.
51 * Both the key and data are arbitrary byte arrays of bytes or characters.
53 * Support for HMAC Digests has been added in GLib 2.30.
56 struct _GHmac
58 int ref_count;
59 GChecksumType digest_type;
60 GChecksum *digesti;
61 GChecksum *digesto;
64 /**
65 * g_hmac_new:
66 * @digest_type: the desired type of digest
67 * @key: (array length=key_len): the key for the HMAC
68 * @key_len: the length of the keys
70 * Creates a new #GHmac, using the digest algorithm @digest_type.
71 * If the @digest_type is not known, %NULL is returned.
72 * A #GHmac can be used to compute the HMAC of a key and an
73 * arbitrary binary blob, using different hashing algorithms.
75 * A #GHmac works by feeding a binary blob through g_hmac_update()
76 * until the data is complete; the digest can then be extracted
77 * using g_hmac_get_string(), which will return the checksum as a
78 * hexadecimal string; or g_hmac_get_digest(), which will return a
79 * array of raw bytes. Once either g_hmac_get_string() or
80 * g_hmac_get_digest() have been called on a #GHmac, the HMAC
81 * will be closed and it won't be possible to call g_hmac_update()
82 * on it anymore.
84 * Return value: the newly created #GHmac, or %NULL.
85 * Use g_hmac_unref() to free the memory allocated by it.
87 * Since: 2.30
89 GHmac *
90 g_hmac_new (GChecksumType digest_type,
91 const guchar *key,
92 gsize key_len)
94 GChecksum *checksum;
95 GHmac *hmac;
96 guchar *buffer;
97 guchar *pad;
98 gsize i, len;
99 gsize block_size;
101 checksum = g_checksum_new (digest_type);
102 g_return_val_if_fail (checksum != NULL, NULL);
104 switch (digest_type)
106 case G_CHECKSUM_MD5:
107 case G_CHECKSUM_SHA1:
108 block_size = 64; /* RFC 2104 */
109 break;
110 case G_CHECKSUM_SHA256:
111 block_size = 64; /* RFC draft-kelly-ipsec-ciph-sha2-01 */
112 break;
113 default:
114 g_return_val_if_reached (NULL);
117 hmac = g_slice_new0 (GHmac);
118 hmac->ref_count = 1;
119 hmac->digest_type = digest_type;
120 hmac->digesti = checksum;
121 hmac->digesto = g_checksum_new (digest_type);
123 buffer = g_alloca (block_size);
124 pad = g_alloca (block_size);
126 memset (buffer, 0, block_size);
128 /* If the key is too long, hash it */
129 if (key_len > block_size)
131 len = block_size;
132 g_checksum_update (hmac->digesti, key, key_len);
133 g_checksum_get_digest (hmac->digesti, buffer, &len);
134 g_checksum_reset (hmac->digesti);
137 /* Otherwise pad it with zeros */
138 else
140 memcpy (buffer, key, key_len);
143 /* First pad */
144 for (i = 0; i < block_size; i++)
145 pad[i] = 0x36 ^ buffer[i]; /* ipad value */
146 g_checksum_update (hmac->digesti, pad, block_size);
148 /* Second pad */
149 for (i = 0; i < block_size; i++)
150 pad[i] = 0x5c ^ buffer[i]; /* opad value */
151 g_checksum_update (hmac->digesto, pad, block_size);
153 return hmac;
157 * g_hmac_copy:
158 * @hmac: the #GHmac to copy
160 * Copies a #GHmac. If @hmac has been closed, by calling
161 * g_hmac_get_string() or g_hmac_get_digest(), the copied
162 * HMAC will be closed as well.
164 * Return value: the copy of the passed #GHmac. Use g_hmac_unref()
165 * when finished using it.
167 * Since: 2.30
169 GHmac *
170 g_hmac_copy (const GHmac *hmac)
172 GHmac *copy;
174 g_return_val_if_fail (hmac != NULL, NULL);
176 copy = g_slice_new (GHmac);
177 copy->ref_count = 1;
178 copy->digest_type = hmac->digest_type;
179 copy->digesti = g_checksum_copy (hmac->digesti);
180 copy->digesto = g_checksum_copy (hmac->digesto);
182 return copy;
186 * g_hmac_ref:
187 * @hmac: a valid #GHmac
189 * Atomically increments the reference count of @hmac by one.
191 * This function is MT-safe and may be called from any thread.
193 * Return value: the passed in #GHmac.
195 * Since: 2.30
197 GHmac *
198 g_hmac_ref (GHmac *hmac)
200 g_return_val_if_fail (hmac != NULL, NULL);
202 g_atomic_int_inc (&hmac->ref_count);
204 return hmac;
208 * g_hmac_unref:
209 * @hmac: a #GHmac
211 * Atomically decrements the reference count of @hmac by one.
213 * If the reference count drops to 0, all keys and values will be
214 * destroyed, and all memory allocated by the hash table is released.
215 * This function is MT-safe and may be called from any thread.
216 * Frees the memory allocated for @hmac.
218 * Since: 2.30
220 void
221 g_hmac_unref (GHmac *hmac)
223 g_return_if_fail (hmac != NULL);
225 if (g_atomic_int_dec_and_test (&hmac->ref_count))
227 g_checksum_free (hmac->digesti);
228 g_checksum_free (hmac->digesto);
229 g_slice_free (GHmac, hmac);
234 * g_hmac_update:
235 * @hmac: a #GHmac
236 * @data: (array length=length): buffer used to compute the checksum
237 * @length: size of the buffer, or -1 if it is a nul-terminated string
239 * Feeds @data into an existing #GHmac.
241 * The HMAC must still be open, that is g_hmac_get_string() or
242 * g_hmac_get_digest() must not have been called on @hmac.
244 * Since: 2.30
246 void
247 g_hmac_update (GHmac *hmac,
248 const guchar *data,
249 gssize length)
251 g_return_if_fail (hmac != NULL);
252 g_return_if_fail (length == 0 || data != NULL);
254 g_checksum_update (hmac->digesti, data, length);
258 * g_hmac_get_string:
259 * @hmac: a #GHmac
261 * Gets the HMAC as an hexadecimal string.
263 * Once this function has been called the #GHmac can no longer be
264 * updated with g_hmac_update().
266 * The hexadecimal characters will be lower case.
268 * Return value: the hexadecimal representation of the HMAC. The
269 * returned string is owned by the HMAC and should not be modified
270 * or freed.
272 * Since: 2.30
274 const gchar *
275 g_hmac_get_string (GHmac *hmac)
277 guint8 *buffer;
278 gsize digest_len;
280 g_return_val_if_fail (hmac != NULL, NULL);
282 digest_len = g_checksum_type_get_length (hmac->digest_type);
283 buffer = g_alloca (digest_len);
285 /* This is only called for its side-effect of updating hmac->digesto... */
286 g_hmac_get_digest (hmac, buffer, &digest_len);
287 /* ... because we get the string from the checksum rather than
288 * stringifying buffer ourselves
290 return g_checksum_get_string (hmac->digesto);
294 * g_hmac_get_digest:
295 * @hmac: a #GHmac
296 * @buffer: output buffer
297 * @digest_len: an inout parameter. The caller initializes it to the
298 * size of @buffer. After the call it contains the length of the digest
300 * Gets the digest from @checksum as a raw binary array and places it
301 * into @buffer. The size of the digest depends on the type of checksum.
303 * Once this function has been called, the #GHmac is closed and can
304 * no longer be updated with g_checksum_update().
306 * Since: 2.30
308 void
309 g_hmac_get_digest (GHmac *hmac,
310 guint8 *buffer,
311 gsize *digest_len)
313 gsize len;
315 g_return_if_fail (hmac != NULL);
317 len = g_checksum_type_get_length (hmac->digest_type);
318 g_return_if_fail (*digest_len >= len);
320 /* Use the same buffer, because we can :) */
321 g_checksum_get_digest (hmac->digesti, buffer, &len);
322 g_checksum_update (hmac->digesto, buffer, len);
323 g_checksum_get_digest (hmac->digesto, buffer, digest_len);
327 * g_compute_hmac_for_data:
328 * @digest_type: a #GChecksumType to use for the HMAC
329 * @key: (array length=key_len): the key to use in the HMAC
330 * @key_len: the length of the key
331 * @data: binary blob to compute the HMAC of
332 * @length: length of @data
334 * Computes the HMAC for a binary @data of @length. This is a
335 * convenience wrapper for g_hmac_new(), g_hmac_get_string()
336 * and g_hmac_unref().
338 * The hexadecimal string returned will be in lower case.
340 * Return value: the HMAC of the binary data as a string in hexadecimal.
341 * The returned string should be freed with g_free() when done using it.
343 * Since: 2.30
345 gchar *
346 g_compute_hmac_for_data (GChecksumType digest_type,
347 const guchar *key,
348 gsize key_len,
349 const guchar *data,
350 gsize length)
352 GHmac *hmac;
353 gchar *retval;
355 g_return_val_if_fail (length == 0 || data != NULL, NULL);
357 hmac = g_hmac_new (digest_type, key, key_len);
358 if (!hmac)
359 return NULL;
361 g_hmac_update (hmac, data, length);
362 retval = g_strdup (g_hmac_get_string (hmac));
363 g_hmac_unref (hmac);
365 return retval;
369 * g_compute_hmac_for_string:
370 * @digest_type: a #GChecksumType to use for the HMAC
371 * @key: (array length=key_len): the key to use in the HMAC
372 * @key_len: the length of the key
373 * @str: the string to compute the HMAC for
374 * @length: the length of the string, or -1 if the string is nul-terminated
376 * Computes the HMAC for a string.
378 * The hexadecimal string returned will be in lower case.
380 * Return value: the HMAC as a hexadecimal string.
381 * The returned string should be freed with g_free()
382 * when done using it.
384 * Since: 2.30
386 gchar *
387 g_compute_hmac_for_string (GChecksumType digest_type,
388 const guchar *key,
389 gsize key_len,
390 const gchar *str,
391 gssize length)
393 g_return_val_if_fail (length == 0 || str != NULL, NULL);
395 if (length < 0)
396 length = strlen (str);
398 return g_compute_hmac_for_data (digest_type, key, key_len,
399 (const guchar *) str, length);