|
Packit Bot |
8ac6de |
/* ghmac.h - data hashing functions
|
|
Packit Bot |
8ac6de |
*
|
|
Packit Bot |
8ac6de |
* Copyright (C) 2011 Collabora Ltd.
|
|
Packit Bot |
8ac6de |
* Copyright (C) 2019 Red Hat, Inc.
|
|
Packit Bot |
8ac6de |
*
|
|
Packit Bot |
8ac6de |
* This library is free software; you can redistribute it and/or
|
|
Packit Bot |
8ac6de |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit Bot |
8ac6de |
* License as published by the Free Software Foundation; either
|
|
Packit Bot |
8ac6de |
* version 2.1 of the License, or (at your option) any later version.
|
|
Packit Bot |
8ac6de |
*
|
|
Packit Bot |
8ac6de |
* This library is distributed in the hope that it will be useful,
|
|
Packit Bot |
8ac6de |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Bot |
8ac6de |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Bot |
8ac6de |
* Lesser General Public License for more details.
|
|
Packit Bot |
8ac6de |
*
|
|
Packit Bot |
8ac6de |
* You should have received a copy of the GNU Lesser General Public License
|
|
Packit Bot |
8ac6de |
* along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
Packit Bot |
8ac6de |
*/
|
|
Packit Bot |
8ac6de |
|
|
Packit Bot |
8ac6de |
#include "config.h"
|
|
Packit Bot |
8ac6de |
|
|
Packit Bot |
8ac6de |
#include <string.h>
|
|
Packit Bot |
8ac6de |
|
|
Packit Bot |
8ac6de |
#include "ghmac.h"
|
|
Packit Bot |
8ac6de |
|
|
Packit Bot |
8ac6de |
#include "glib/galloca.h"
|
|
Packit Bot |
8ac6de |
#include "gatomic.h"
|
|
Packit Bot |
8ac6de |
#include "gslice.h"
|
|
Packit Bot |
8ac6de |
#include "gmem.h"
|
|
Packit Bot |
8ac6de |
#include "gstrfuncs.h"
|
|
Packit Bot |
8ac6de |
#include "gtestutils.h"
|
|
Packit Bot |
8ac6de |
#include "gtypes.h"
|
|
Packit Bot |
8ac6de |
#include "glibintl.h"
|
|
Packit Bot |
8ac6de |
|
|
Packit Bot |
8ac6de |
/**
|
|
Packit Bot |
8ac6de |
* g_compute_hmac_for_data:
|
|
Packit Bot |
8ac6de |
* @digest_type: a #GChecksumType to use for the HMAC
|
|
Packit Bot |
8ac6de |
* @key: (array length=key_len): the key to use in the HMAC
|
|
Packit Bot |
8ac6de |
* @key_len: the length of the key
|
|
Packit Bot |
8ac6de |
* @data: (array length=length): binary blob to compute the HMAC of
|
|
Packit Bot |
8ac6de |
* @length: length of @data
|
|
Packit Bot |
8ac6de |
*
|
|
Packit Bot |
8ac6de |
* Computes the HMAC for a binary @data of @length. This is a
|
|
Packit Bot |
8ac6de |
* convenience wrapper for g_hmac_new(), g_hmac_get_string()
|
|
Packit Bot |
8ac6de |
* and g_hmac_unref().
|
|
Packit Bot |
8ac6de |
*
|
|
Packit Bot |
8ac6de |
* The hexadecimal string returned will be in lower case.
|
|
Packit Bot |
8ac6de |
*
|
|
Packit Bot |
8ac6de |
* Returns: the HMAC of the binary data as a string in hexadecimal.
|
|
Packit Bot |
8ac6de |
* The returned string should be freed with g_free() when done using it.
|
|
Packit Bot |
8ac6de |
*
|
|
Packit Bot |
8ac6de |
* Since: 2.30
|
|
Packit Bot |
8ac6de |
*/
|
|
Packit Bot |
8ac6de |
gchar *
|
|
Packit Bot |
8ac6de |
g_compute_hmac_for_data (GChecksumType digest_type,
|
|
Packit Bot |
8ac6de |
const guchar *key,
|
|
Packit Bot |
8ac6de |
gsize key_len,
|
|
Packit Bot |
8ac6de |
const guchar *data,
|
|
Packit Bot |
8ac6de |
gsize length)
|
|
Packit Bot |
8ac6de |
{
|
|
Packit Bot |
8ac6de |
GHmac *hmac;
|
|
Packit Bot |
8ac6de |
gchar *retval;
|
|
Packit Bot |
8ac6de |
|
|
Packit Bot |
8ac6de |
g_return_val_if_fail (length == 0 || data != NULL, NULL);
|
|
Packit Bot |
8ac6de |
|
|
Packit Bot |
8ac6de |
hmac = g_hmac_new (digest_type, key, key_len);
|
|
Packit Bot |
8ac6de |
if (!hmac)
|
|
Packit Bot |
8ac6de |
return NULL;
|
|
Packit Bot |
8ac6de |
|
|
Packit Bot |
8ac6de |
g_hmac_update (hmac, data, length);
|
|
Packit Bot |
8ac6de |
retval = g_strdup (g_hmac_get_string (hmac));
|
|
Packit Bot |
8ac6de |
g_hmac_unref (hmac);
|
|
Packit Bot |
8ac6de |
|
|
Packit Bot |
8ac6de |
return retval;
|
|
Packit Bot |
8ac6de |
}
|
|
Packit Bot |
8ac6de |
|
|
Packit Bot |
8ac6de |
/**
|
|
Packit Bot |
8ac6de |
* g_compute_hmac_for_bytes:
|
|
Packit Bot |
8ac6de |
* @digest_type: a #GChecksumType to use for the HMAC
|
|
Packit Bot |
8ac6de |
* @key: the key to use in the HMAC
|
|
Packit Bot |
8ac6de |
* @data: binary blob to compute the HMAC of
|
|
Packit Bot |
8ac6de |
*
|
|
Packit Bot |
8ac6de |
* Computes the HMAC for a binary @data. This is a
|
|
Packit Bot |
8ac6de |
* convenience wrapper for g_hmac_new(), g_hmac_get_string()
|
|
Packit Bot |
8ac6de |
* and g_hmac_unref().
|
|
Packit Bot |
8ac6de |
*
|
|
Packit Bot |
8ac6de |
* The hexadecimal string returned will be in lower case.
|
|
Packit Bot |
8ac6de |
*
|
|
Packit Bot |
8ac6de |
* Returns: the HMAC of the binary data as a string in hexadecimal.
|
|
Packit Bot |
8ac6de |
* The returned string should be freed with g_free() when done using it.
|
|
Packit Bot |
8ac6de |
*
|
|
Packit Bot |
8ac6de |
* Since: 2.50
|
|
Packit Bot |
8ac6de |
*/
|
|
Packit Bot |
8ac6de |
gchar *
|
|
Packit Bot |
8ac6de |
g_compute_hmac_for_bytes (GChecksumType digest_type,
|
|
Packit Bot |
8ac6de |
GBytes *key,
|
|
Packit Bot |
8ac6de |
GBytes *data)
|
|
Packit Bot |
8ac6de |
{
|
|
Packit Bot |
8ac6de |
gconstpointer byte_data;
|
|
Packit Bot |
8ac6de |
gsize length;
|
|
Packit Bot |
8ac6de |
gconstpointer key_data;
|
|
Packit Bot |
8ac6de |
gsize key_len;
|
|
Packit Bot |
8ac6de |
|
|
Packit Bot |
8ac6de |
g_return_val_if_fail (data != NULL, NULL);
|
|
Packit Bot |
8ac6de |
g_return_val_if_fail (key != NULL, NULL);
|
|
Packit Bot |
8ac6de |
|
|
Packit Bot |
8ac6de |
byte_data = g_bytes_get_data (data, &length);
|
|
Packit Bot |
8ac6de |
key_data = g_bytes_get_data (key, &key_len);
|
|
Packit Bot |
8ac6de |
return g_compute_hmac_for_data (digest_type, key_data, key_len, byte_data, length);
|
|
Packit Bot |
8ac6de |
}
|
|
Packit Bot |
8ac6de |
|
|
Packit Bot |
8ac6de |
|
|
Packit Bot |
8ac6de |
/**
|
|
Packit Bot |
8ac6de |
* g_compute_hmac_for_string:
|
|
Packit Bot |
8ac6de |
* @digest_type: a #GChecksumType to use for the HMAC
|
|
Packit Bot |
8ac6de |
* @key: (array length=key_len): the key to use in the HMAC
|
|
Packit Bot |
8ac6de |
* @key_len: the length of the key
|
|
Packit Bot |
8ac6de |
* @str: the string to compute the HMAC for
|
|
Packit Bot |
8ac6de |
* @length: the length of the string, or -1 if the string is nul-terminated
|
|
Packit Bot |
8ac6de |
*
|
|
Packit Bot |
8ac6de |
* Computes the HMAC for a string.
|
|
Packit Bot |
8ac6de |
*
|
|
Packit Bot |
8ac6de |
* The hexadecimal string returned will be in lower case.
|
|
Packit Bot |
8ac6de |
*
|
|
Packit Bot |
8ac6de |
* Returns: the HMAC as a hexadecimal string.
|
|
Packit Bot |
8ac6de |
* The returned string should be freed with g_free()
|
|
Packit Bot |
8ac6de |
* when done using it.
|
|
Packit Bot |
8ac6de |
*
|
|
Packit Bot |
8ac6de |
* Since: 2.30
|
|
Packit Bot |
8ac6de |
*/
|
|
Packit Bot |
8ac6de |
gchar *
|
|
Packit Bot |
8ac6de |
g_compute_hmac_for_string (GChecksumType digest_type,
|
|
Packit Bot |
8ac6de |
const guchar *key,
|
|
Packit Bot |
8ac6de |
gsize key_len,
|
|
Packit Bot |
8ac6de |
const gchar *str,
|
|
Packit Bot |
8ac6de |
gssize length)
|
|
Packit Bot |
8ac6de |
{
|
|
Packit Bot |
8ac6de |
g_return_val_if_fail (length == 0 || str != NULL, NULL);
|
|
Packit Bot |
8ac6de |
|
|
Packit Bot |
8ac6de |
if (length < 0)
|
|
Packit Bot |
8ac6de |
length = strlen (str);
|
|
Packit Bot |
8ac6de |
|
|
Packit Bot |
8ac6de |
return g_compute_hmac_for_data (digest_type, key, key_len,
|
|
Packit Bot |
8ac6de |
(const guchar *) str, length);
|
|
Packit Bot |
8ac6de |
}
|