Blame glib/ghmac.c

Packit ae235b
/* ghmac.h - data hashing functions
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2011  Collabora Ltd.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public License
Packit ae235b
 * along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Stef Walter <stefw@collabora.co.uk>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
#include "ghmac.h"
Packit ae235b
Packit ae235b
#include "glib/galloca.h"
Packit ae235b
#include "gatomic.h"
Packit ae235b
#include "gslice.h"
Packit ae235b
#include "gmem.h"
Packit ae235b
#include "gstrfuncs.h"
Packit ae235b
#include "gtestutils.h"
Packit ae235b
#include "gtypes.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:hmac
Packit ae235b
 * @title: Secure HMAC Digests
Packit ae235b
 * @short_description: computes the HMAC for data
Packit ae235b
 *
Packit ae235b
 * HMACs should be used when producing a cookie or hash based on data
Packit ae235b
 * and a key. Simple mechanisms for using SHA1 and other algorithms to
Packit ae235b
 * digest a key and data together are vulnerable to various security
Packit ae235b
 * issues.
Packit ae235b
 * [HMAC](http://en.wikipedia.org/wiki/HMAC)
Packit ae235b
 * uses algorithms like SHA1 in a secure way to produce a digest of a
Packit ae235b
 * key and data.
Packit ae235b
 *
Packit ae235b
 * Both the key and data are arbitrary byte arrays of bytes or characters.
Packit ae235b
 *
Packit ae235b
 * Support for HMAC Digests has been added in GLib 2.30, and support for SHA-512
Packit ae235b
 * in GLib 2.42. Support for SHA-384 was added in GLib 2.52.
Packit ae235b
 */
Packit ae235b
Packit ae235b
struct _GHmac
Packit ae235b
{
Packit ae235b
  int ref_count;
Packit ae235b
  GChecksumType digest_type;
Packit ae235b
  GChecksum *digesti;
Packit ae235b
  GChecksum *digesto;
Packit ae235b
};
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hmac_new:
Packit ae235b
 * @digest_type: the desired type of digest
Packit ae235b
 * @key: (array length=key_len): the key for the HMAC
Packit ae235b
 * @key_len: the length of the keys
Packit ae235b
 *
Packit ae235b
 * Creates a new #GHmac, using the digest algorithm @digest_type.
Packit ae235b
 * If the @digest_type is not known, %NULL is returned.
Packit ae235b
 * A #GHmac can be used to compute the HMAC of a key and an
Packit ae235b
 * arbitrary binary blob, using different hashing algorithms.
Packit ae235b
 *
Packit ae235b
 * A #GHmac works by feeding a binary blob through g_hmac_update()
Packit ae235b
 * until the data is complete; the digest can then be extracted
Packit ae235b
 * using g_hmac_get_string(), which will return the checksum as a
Packit ae235b
 * hexadecimal string; or g_hmac_get_digest(), which will return a
Packit ae235b
 * array of raw bytes. Once either g_hmac_get_string() or
Packit ae235b
 * g_hmac_get_digest() have been called on a #GHmac, the HMAC
Packit ae235b
 * will be closed and it won't be possible to call g_hmac_update()
Packit ae235b
 * on it anymore.
Packit ae235b
 *
Packit ae235b
 * Support for digests of type %G_CHECKSUM_SHA512 has been added in GLib 2.42.
Packit ae235b
 * Support for %G_CHECKSUM_SHA384 was added in GLib 2.52.
Packit ae235b
 *
Packit ae235b
 * Returns: the newly created #GHmac, or %NULL.
Packit ae235b
 *   Use g_hmac_unref() to free the memory allocated by it.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
GHmac *
Packit ae235b
g_hmac_new (GChecksumType  digest_type,
Packit ae235b
            const guchar  *key,
Packit ae235b
            gsize          key_len)
Packit ae235b
{
Packit ae235b
  GChecksum *checksum;
Packit ae235b
  GHmac *hmac;
Packit ae235b
  guchar *buffer;
Packit ae235b
  guchar *pad;
Packit ae235b
  gsize i, len;
Packit ae235b
  gsize block_size;
Packit ae235b
Packit ae235b
  checksum = g_checksum_new (digest_type);
Packit ae235b
  g_return_val_if_fail (checksum != NULL, NULL);
Packit ae235b
Packit ae235b
  switch (digest_type)
Packit ae235b
    {
Packit ae235b
    case G_CHECKSUM_MD5:
Packit ae235b
    case G_CHECKSUM_SHA1:
Packit ae235b
      block_size = 64; /* RFC 2104 */
Packit ae235b
      break;
Packit ae235b
    case G_CHECKSUM_SHA256:
Packit ae235b
      block_size = 64; /* RFC 4868 */
Packit ae235b
      break;
Packit ae235b
    case G_CHECKSUM_SHA384:
Packit ae235b
    case G_CHECKSUM_SHA512:
Packit ae235b
      block_size = 128; /* RFC 4868 */
Packit ae235b
      break;
Packit ae235b
    default:
Packit ae235b
      g_return_val_if_reached (NULL);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  hmac = g_slice_new0 (GHmac);
Packit ae235b
  hmac->ref_count = 1;
Packit ae235b
  hmac->digest_type = digest_type;
Packit ae235b
  hmac->digesti = checksum;
Packit ae235b
  hmac->digesto = g_checksum_new (digest_type);
Packit ae235b
Packit ae235b
  buffer = g_alloca (block_size);
Packit ae235b
  pad = g_alloca (block_size);
Packit ae235b
Packit ae235b
  memset (buffer, 0, block_size);
Packit ae235b
Packit ae235b
  /* If the key is too long, hash it */
Packit ae235b
  if (key_len > block_size)
Packit ae235b
    {
Packit ae235b
      len = block_size;
Packit ae235b
      g_checksum_update (hmac->digesti, key, key_len);
Packit ae235b
      g_checksum_get_digest (hmac->digesti, buffer, &len;;
Packit ae235b
      g_checksum_reset (hmac->digesti);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Otherwise pad it with zeros */
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      memcpy (buffer, key, key_len);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* First pad */
Packit ae235b
  for (i = 0; i < block_size; i++)
Packit ae235b
    pad[i] = 0x36 ^ buffer[i]; /* ipad value */
Packit ae235b
  g_checksum_update (hmac->digesti, pad, block_size);
Packit ae235b
Packit ae235b
  /* Second pad */
Packit ae235b
  for (i = 0; i < block_size; i++)
Packit ae235b
    pad[i] = 0x5c ^ buffer[i]; /* opad value */
Packit ae235b
  g_checksum_update (hmac->digesto, pad, block_size);
Packit ae235b
Packit ae235b
  return hmac;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hmac_copy:
Packit ae235b
 * @hmac: the #GHmac to copy
Packit ae235b
 *
Packit ae235b
 * Copies a #GHmac. If @hmac has been closed, by calling
Packit ae235b
 * g_hmac_get_string() or g_hmac_get_digest(), the copied
Packit ae235b
 * HMAC will be closed as well.
Packit ae235b
 *
Packit ae235b
 * Returns: the copy of the passed #GHmac. Use g_hmac_unref()
Packit ae235b
 *   when finished using it.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
GHmac *
Packit ae235b
g_hmac_copy (const GHmac *hmac)
Packit ae235b
{
Packit ae235b
  GHmac *copy;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (hmac != NULL, NULL);
Packit ae235b
Packit ae235b
  copy = g_slice_new (GHmac);
Packit ae235b
  copy->ref_count = 1;
Packit ae235b
  copy->digest_type = hmac->digest_type;
Packit ae235b
  copy->digesti = g_checksum_copy (hmac->digesti);
Packit ae235b
  copy->digesto = g_checksum_copy (hmac->digesto);
Packit ae235b
Packit ae235b
  return copy;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hmac_ref:
Packit ae235b
 * @hmac: a valid #GHmac
Packit ae235b
 *
Packit ae235b
 * Atomically increments the reference count of @hmac by one.
Packit ae235b
 *
Packit ae235b
 * This function is MT-safe and may be called from any thread.
Packit ae235b
 *
Packit ae235b
 * Returns: the passed in #GHmac.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 **/
Packit ae235b
GHmac *
Packit ae235b
g_hmac_ref (GHmac *hmac)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (hmac != NULL, NULL);
Packit ae235b
Packit ae235b
  g_atomic_int_inc (&hmac->ref_count);
Packit ae235b
Packit ae235b
  return hmac;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hmac_unref:
Packit ae235b
 * @hmac: a #GHmac
Packit ae235b
 *
Packit ae235b
 * Atomically decrements the reference count of @hmac by one.
Packit ae235b
 *
Packit ae235b
 * If the reference count drops to 0, all keys and values will be
Packit ae235b
 * destroyed, and all memory allocated by the hash table is released.
Packit ae235b
 * This function is MT-safe and may be called from any thread.
Packit ae235b
 * Frees the memory allocated for @hmac.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_hmac_unref (GHmac *hmac)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (hmac != NULL);
Packit ae235b
Packit ae235b
  if (g_atomic_int_dec_and_test (&hmac->ref_count))
Packit ae235b
    {
Packit ae235b
      g_checksum_free (hmac->digesti);
Packit ae235b
      g_checksum_free (hmac->digesto);
Packit ae235b
      g_slice_free (GHmac, hmac);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hmac_update:
Packit ae235b
 * @hmac: a #GHmac
Packit ae235b
 * @data: (array length=length): buffer used to compute the checksum
Packit ae235b
 * @length: size of the buffer, or -1 if it is a nul-terminated string
Packit ae235b
 *
Packit ae235b
 * Feeds @data into an existing #GHmac.
Packit ae235b
 *
Packit ae235b
 * The HMAC must still be open, that is g_hmac_get_string() or
Packit ae235b
 * g_hmac_get_digest() must not have been called on @hmac.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_hmac_update (GHmac        *hmac,
Packit ae235b
               const guchar *data,
Packit ae235b
               gssize        length)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (hmac != NULL);
Packit ae235b
  g_return_if_fail (length == 0 || data != NULL);
Packit ae235b
Packit ae235b
  g_checksum_update (hmac->digesti, data, length);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hmac_get_string:
Packit ae235b
 * @hmac: a #GHmac
Packit ae235b
 *
Packit ae235b
 * Gets the HMAC as an hexadecimal string.
Packit ae235b
 *
Packit ae235b
 * Once this function has been called the #GHmac can no longer be
Packit ae235b
 * updated with g_hmac_update().
Packit ae235b
 *
Packit ae235b
 * The hexadecimal characters will be lower case.
Packit ae235b
 *
Packit ae235b
 * Returns: the hexadecimal representation of the HMAC. The
Packit ae235b
 *   returned string is owned by the HMAC and should not be modified
Packit ae235b
 *   or freed.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
const gchar *
Packit ae235b
g_hmac_get_string (GHmac *hmac)
Packit ae235b
{
Packit ae235b
  guint8 *buffer;
Packit ae235b
  gsize digest_len;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (hmac != NULL, NULL);
Packit ae235b
Packit ae235b
  digest_len = g_checksum_type_get_length (hmac->digest_type);
Packit ae235b
  buffer = g_alloca (digest_len);
Packit ae235b
Packit ae235b
  /* This is only called for its side-effect of updating hmac->digesto... */
Packit ae235b
  g_hmac_get_digest (hmac, buffer, &digest_len);
Packit ae235b
  /* ... because we get the string from the checksum rather than
Packit ae235b
   * stringifying buffer ourselves
Packit ae235b
   */
Packit ae235b
  return g_checksum_get_string (hmac->digesto);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hmac_get_digest:
Packit ae235b
 * @hmac: a #GHmac
Packit ae235b
 * @buffer: output buffer
Packit ae235b
 * @digest_len: an inout parameter. The caller initializes it to the
Packit ae235b
 *   size of @buffer. After the call it contains the length of the digest
Packit ae235b
 *
Packit ae235b
 * Gets the digest from @checksum as a raw binary array and places it
Packit ae235b
 * into @buffer. The size of the digest depends on the type of checksum.
Packit ae235b
 *
Packit ae235b
 * Once this function has been called, the #GHmac is closed and can
Packit ae235b
 * no longer be updated with g_checksum_update().
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_hmac_get_digest (GHmac  *hmac,
Packit ae235b
                   guint8 *buffer,
Packit ae235b
                   gsize  *digest_len)
Packit ae235b
{
Packit ae235b
  gsize len;
Packit ae235b
Packit ae235b
  g_return_if_fail (hmac != NULL);
Packit ae235b
Packit ae235b
  len = g_checksum_type_get_length (hmac->digest_type);
Packit ae235b
  g_return_if_fail (*digest_len >= len);
Packit ae235b
Packit ae235b
  /* Use the same buffer, because we can :) */
Packit ae235b
  g_checksum_get_digest (hmac->digesti, buffer, &len;;
Packit ae235b
  g_checksum_update (hmac->digesto, buffer, len);
Packit ae235b
  g_checksum_get_digest (hmac->digesto, buffer, digest_len);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_compute_hmac_for_data:
Packit ae235b
 * @digest_type: a #GChecksumType to use for the HMAC
Packit ae235b
 * @key: (array length=key_len): the key to use in the HMAC
Packit ae235b
 * @key_len: the length of the key
Packit ae235b
 * @data: (array length=length): binary blob to compute the HMAC of
Packit ae235b
 * @length: length of @data
Packit ae235b
 *
Packit ae235b
 * Computes the HMAC for a binary @data of @length. This is a
Packit ae235b
 * convenience wrapper for g_hmac_new(), g_hmac_get_string()
Packit ae235b
 * and g_hmac_unref().
Packit ae235b
 *
Packit ae235b
 * The hexadecimal string returned will be in lower case.
Packit ae235b
 *
Packit ae235b
 * Returns: the HMAC of the binary data as a string in hexadecimal.
Packit ae235b
 *   The returned string should be freed with g_free() when done using it.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_compute_hmac_for_data (GChecksumType  digest_type,
Packit ae235b
                         const guchar  *key,
Packit ae235b
                         gsize          key_len,
Packit ae235b
                         const guchar  *data,
Packit ae235b
                         gsize          length)
Packit ae235b
{
Packit ae235b
  GHmac *hmac;
Packit ae235b
  gchar *retval;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (length == 0 || data != NULL, NULL);
Packit ae235b
Packit ae235b
  hmac = g_hmac_new (digest_type, key, key_len);
Packit ae235b
  if (!hmac)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  g_hmac_update (hmac, data, length);
Packit ae235b
  retval = g_strdup (g_hmac_get_string (hmac));
Packit ae235b
  g_hmac_unref (hmac);
Packit ae235b
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_compute_hmac_for_bytes:
Packit ae235b
 * @digest_type: a #GChecksumType to use for the HMAC
Packit ae235b
 * @key: the key to use in the HMAC
Packit ae235b
 * @data: binary blob to compute the HMAC of
Packit ae235b
 *
Packit ae235b
 * Computes the HMAC for a binary @data. This is a
Packit ae235b
 * convenience wrapper for g_hmac_new(), g_hmac_get_string()
Packit ae235b
 * and g_hmac_unref().
Packit ae235b
 *
Packit ae235b
 * The hexadecimal string returned will be in lower case.
Packit ae235b
 *
Packit ae235b
 * Returns: the HMAC of the binary data as a string in hexadecimal.
Packit ae235b
 *   The returned string should be freed with g_free() when done using it.
Packit ae235b
 *
Packit ae235b
 * Since: 2.50
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_compute_hmac_for_bytes (GChecksumType  digest_type,
Packit ae235b
                          GBytes        *key,
Packit ae235b
                          GBytes        *data)
Packit ae235b
{
Packit ae235b
  gconstpointer byte_data;
Packit ae235b
  gsize length;
Packit ae235b
  gconstpointer key_data;
Packit ae235b
  gsize key_len;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (data != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (key != NULL, NULL);
Packit ae235b
Packit ae235b
  byte_data = g_bytes_get_data (data, &length);
Packit ae235b
  key_data = g_bytes_get_data (key, &key_len);
Packit ae235b
  return g_compute_hmac_for_data (digest_type, key_data, key_len, byte_data, length);
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_compute_hmac_for_string:
Packit ae235b
 * @digest_type: a #GChecksumType to use for the HMAC
Packit ae235b
 * @key: (array length=key_len): the key to use in the HMAC
Packit ae235b
 * @key_len: the length of the key
Packit ae235b
 * @str: the string to compute the HMAC for
Packit ae235b
 * @length: the length of the string, or -1 if the string is nul-terminated
Packit ae235b
 *
Packit ae235b
 * Computes the HMAC for a string.
Packit ae235b
 *
Packit ae235b
 * The hexadecimal string returned will be in lower case.
Packit ae235b
 *
Packit ae235b
 * Returns: the HMAC as a hexadecimal string.
Packit ae235b
 *     The returned string should be freed with g_free()
Packit ae235b
 *     when done using it.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_compute_hmac_for_string (GChecksumType  digest_type,
Packit ae235b
                           const guchar  *key,
Packit ae235b
                           gsize          key_len,
Packit ae235b
                           const gchar   *str,
Packit ae235b
                           gssize         length)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (length == 0 || str != NULL, NULL);
Packit ae235b
Packit ae235b
  if (length < 0)
Packit ae235b
    length = strlen (str);
Packit ae235b
Packit ae235b
  return g_compute_hmac_for_data (digest_type, key, key_len,
Packit ae235b
                                  (const guchar *) str, length);
Packit ae235b
}