Exemplo n.º 1
0
Arquivo: sms.c Projeto: AndriusA/ofono
static gboolean compute_incoming_msgid(GSList *sms_list,
						struct ofono_uuid *uuid)
{
	GChecksum *checksum;
	GSList *l;
	const struct sms *s;
	unsigned char buf[176];
	gsize uuid_size = sizeof(uuid->uuid);
	int len;

	checksum = g_checksum_new(G_CHECKSUM_SHA1);
	if (checksum == NULL)
		return FALSE;

	for (l = sms_list; l; l = l->next) {
		s = l->data;

		if (sms_encode(s, &len, NULL, buf) == FALSE) {
			g_checksum_free(checksum);
			return FALSE;
		}

		g_checksum_update(checksum, buf, len);
	}

	g_checksum_get_digest(checksum, uuid->uuid, &uuid_size);
	g_checksum_free(checksum);

	return TRUE;
}
Exemplo n.º 2
0
sc_bool sc_link_calculate_checksum(const sc_stream *stream, sc_check_sum *check_sum)
{
    sc_char buffer[1024];
    sc_uint32 data_read;
    const gchar *result = 0;
    GChecksum *checksum = g_checksum_new(SC_DEFAULT_CHECKSUM);

    g_assert(stream != 0);
    g_assert(check_sum != 0);
    g_checksum_reset(checksum);

    sc_stream_seek(stream, SC_STREAM_SEEK_SET, 0);

    while (sc_stream_eof(stream) == SC_FALSE)
    {
        if (sc_stream_read_data(stream, buffer, 1024, &data_read) == SC_RESULT_ERROR)
        {
            g_checksum_free(checksum);
            return SC_FALSE;
        }

        g_checksum_update(checksum, (guchar*)buffer, data_read);
    }

    // store results
    check_sum->len = g_checksum_type_get_length(SC_DEFAULT_CHECKSUM);
    result = g_checksum_get_string(checksum);
    memcpy(&(check_sum->data[0]), result, check_sum->len);

    g_checksum_free(checksum);

    sc_stream_seek(stream, SC_STREAM_SEEK_SET, 0);

    return SC_TRUE;
}
Exemplo n.º 3
0
/*
 * Get the check sum of password and verify code.
 *
 * First, compute check sum of password for three times.
 * Then, join the result with the capitalizaion of the verify code.
 * Compute the chekc sum of the new string.
 */
GString* get_pwvc_md5(const gchar *pwd, const gchar *vc, GError **err)
{
	guint8 buf[100];
	gsize bsize = 100;
	
	GChecksum *cs = g_checksum_new(G_CHECKSUM_MD5);
	g_checksum_update(cs, (const guchar*)pwd, strlen(pwd));
	g_checksum_get_digest(cs, buf, &bsize);
	g_checksum_free(cs);
	
	cs = g_checksum_new(G_CHECKSUM_MD5);
	g_checksum_update(cs, buf, bsize);
	g_checksum_get_digest(cs, buf, &bsize);
	g_checksum_free(cs);
	
	cs = g_checksum_new(G_CHECKSUM_MD5);
	g_checksum_update(cs, buf, bsize);
	const gchar * md5_3 = g_checksum_get_string(cs);
	md5_3 = g_ascii_strup(md5_3, strlen(md5_3));
	gchar buf2[100];
	g_sprintf(buf2, "%s%s", md5_3, vc);
	g_checksum_free(cs);

	gchar *tmp1;
	tmp1 = g_ascii_strup(buf2, strlen(buf2));
	tmp1 = g_compute_checksum_for_string(G_CHECKSUM_MD5
						, tmp1, -1);
	tmp1 = g_ascii_strup(tmp1, strlen(tmp1));
	GString *re = g_string_new(tmp1);
	g_free(tmp1);

	return re;
}
Exemplo n.º 4
0
static RejillaBurnResult
rejilla_checksum_files_get_file_checksum (RejillaChecksumFiles *self,
					  GChecksumType type,
					  const gchar *path,
					  gchar **checksum_string,
					  GError **error)
{
	RejillaChecksumFilesPrivate *priv;
	guchar buffer [BLOCK_SIZE];
	GChecksum *checksum;
	gint read_bytes;
	FILE *file;

	priv = REJILLA_CHECKSUM_FILES_PRIVATE (self);

	file = fopen (path, "r");
	if (!file) {
                int errsv;
		gchar *name = NULL;

		/* If the file doesn't exist carry on with next */
		if (errno == ENOENT)
			return REJILLA_BURN_RETRY;

		name = g_path_get_basename (path);

                errsv = errno;
		g_set_error (error,
			     REJILLA_BURN_ERROR,
			     REJILLA_BURN_ERROR_GENERAL,
			     _("File \"%s\" could not be opened (%s)"),
			     name,
			     g_strerror (errsv));
		g_free (name);

		return REJILLA_BURN_ERR;
	}

	checksum = g_checksum_new (type);

	read_bytes = fread (buffer, 1, BLOCK_SIZE, file);
	g_checksum_update (checksum, buffer, read_bytes);

	while (read_bytes == BLOCK_SIZE) {
		if (priv->cancel) {
			fclose (file);
			g_checksum_free (checksum);
			return REJILLA_BURN_CANCEL;
		}

		read_bytes = fread (buffer, 1, BLOCK_SIZE, file);
		g_checksum_update (checksum, buffer, read_bytes);
	}

	*checksum_string = g_strdup (g_checksum_get_string (checksum));
	g_checksum_free (checksum);
	fclose (file);

	return REJILLA_BURN_OK;
}
Exemplo n.º 5
0
Arquivo: ghmac.c Projeto: GNOME/glib
/**
 * g_hmac_unref:
 * @hmac: a #GHmac
 *
 * Atomically decrements the reference count of @hmac by one.
 *
 * If the reference count drops to 0, all keys and values will be
 * destroyed, and all memory allocated by the hash table is released.
 * This function is MT-safe and may be called from any thread.
 * Frees the memory allocated for @hmac.
 *
 * Since: 2.30
 */
void
g_hmac_unref (GHmac *hmac)
{
  g_return_if_fail (hmac != NULL);

  if (g_atomic_int_dec_and_test (&hmac->ref_count))
    {
      g_checksum_free (hmac->digesti);
      g_checksum_free (hmac->digesto);
      g_slice_free (GHmac, hmac);
    }
}
Exemplo n.º 6
0
static void chap_process_challenge(struct ppp_chap *chap, const guint8 *packet)
{
	const struct chap_header *header = (const struct chap_header *) packet;
	struct chap_header *response;
	GChecksum *checksum;
	const char *secret = g_at_ppp_get_password(chap->ppp);
	const char *username = g_at_ppp_get_username(chap->ppp);
	guint16 response_length;
	struct ppp_header *ppp_packet;
	gsize digest_len;

	/* create a checksum over id, secret, and challenge */
	checksum = g_checksum_new(chap->method);
	if (checksum == NULL)
		return;

	g_checksum_update(checksum, &header->identifier, 1);

	if (secret)
		g_checksum_update(checksum, (guchar *) secret, strlen(secret));

	g_checksum_update(checksum, &header->data[1], header->data[0]);

	/* transmit a response packet */
	/*
	 * allocate space for the header, the checksum, and the ppp header,
	 * and the value size byte
	 */
	digest_len = g_checksum_type_get_length(chap->method);
	response_length = digest_len + sizeof(*header) + 1;

	if (username != NULL)
		response_length += strlen(username);

	ppp_packet = ppp_packet_new(response_length, CHAP_PROTOCOL);
	if (ppp_packet == NULL)
		goto challenge_out;

	response = (struct chap_header *) &ppp_packet->info;
	if (response) {
		response->code = RESPONSE;
		response->identifier = header->identifier;
		response->length = htons(response_length);
		g_checksum_get_digest(checksum, response->data + 1,
							&digest_len);
		response->data[0] = digest_len;
		/* leave the name empty? */
	}

	if (username != NULL)
		memcpy(response->data + digest_len + 1, username,
				strlen(username));

	/* transmit the packet */
	ppp_transmit(chap->ppp, (guint8 *) ppp_packet, response_length);
	g_free(ppp_packet);

challenge_out:
	g_checksum_free(checksum);
}
Exemplo n.º 7
0
/**
 * gum_validate_generate_username:
 * @str: (transfer none): the string to be validated
 * @error: (transfer none): the #GError to be set in case of error
 *
 * Generate username based on the string @str and then validate the generated
 * user name against #GUM_NAME_PATTERN pattern.
 *
 * Returns: (transfer full): string if successful, NULL otherwise.
 */
gchar *
gum_validate_generate_username (
        const gchar *str,
        GError **error)
{
    gchar *gen_name = NULL;
    GChecksum *hid = NULL;
    gsize len = 0;
    if (!str || (len = strlen (str)) == 0) {
        GUM_RETURN_WITH_ERROR (GUM_ERROR_INVALID_NAME,
                "Unable to generate name for 0 len str", error, NULL);
    }

    hid = g_checksum_new (G_CHECKSUM_MD5);
    g_checksum_update (hid, (const guchar*)str, len);

    gen_name = g_strdup (g_checksum_get_string (hid));
    g_checksum_free (hid);
    if (!g_ascii_isalpha ((gchar)gen_name[0])) {
        gen_name[0] = 'U';
        DBG ("First character changed to alpha in %s",gen_name);
    }
    if (!gum_validate_name (gen_name, error)) {
        g_free (gen_name);
        gen_name = NULL;
    }

    return gen_name;
}
Exemplo n.º 8
0
const char *
oio_str_autocontainer_name (const char *path, gchar *dst,
		const struct oio_str_autocontainer_config_s *cfg)
{
	guint8 bin[64];

	g_assert (path != NULL);
	g_assert (dst != NULL);
	g_assert (cfg != NULL);

	gsize len = strlen (path);
	gsize src_offset = cfg->src_offset;
	gsize src_size = cfg->src_size;
	if (src_offset + src_size > len)
		return NULL;
	/* TODO check the sum doesn't cause an overflow... */

	if (!src_size)
		src_size = len - src_offset;

	GChecksum *checksum = g_checksum_new (G_CHECKSUM_SHA256);
	g_checksum_update (checksum, (guint8*)(path+src_offset), src_size);
	len = sizeof(bin);
	g_checksum_get_digest (checksum, bin, &len);
	g_checksum_free (checksum);

	return oio_str_autocontainer_hash (bin, 64, dst, cfg);
}
Exemplo n.º 9
0
static char *
compute_accept_key (const char *key)
{
	gsize digest_len = FIXED_DIGEST_LEN;
	guchar digest[FIXED_DIGEST_LEN];
	GChecksum *checksum;

	if (!key)
		return NULL;

	checksum = g_checksum_new (G_CHECKSUM_SHA1);
	g_return_val_if_fail (checksum != NULL, NULL);

	g_checksum_update (checksum, (guchar *)key, -1);

	/* magic from: http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17 */
	g_checksum_update (checksum, (guchar *)"258EAFA5-E914-47DA-95CA-C5AB0DC85B11", -1);

	g_checksum_get_digest (checksum, digest, &digest_len);
	g_checksum_free (checksum);

	g_assert (digest_len == FIXED_DIGEST_LEN);

	return g_base64_encode (digest, digest_len);
}
Exemplo n.º 10
0
char *
rpmhdrs_rpmdbv (struct RpmHeaders *l1,
                GCancellable   *cancellable,
                GError        **error)
{
  GChecksum *checksum = g_checksum_new (G_CHECKSUM_SHA1);
  g_autofree char *checksum_cstr = NULL;
  char *ret = NULL;
  int num = 0;

  while (num < l1->hs->len)
    {
      Header pkg = l1->hs->pdata[num++];
      g_autofree char *envra = pkg_envra_strdup (pkg);

      g_checksum_update (checksum, (guint8*)envra, strlen(envra));
    }

  checksum_cstr = g_strdup (g_checksum_get_string (checksum));

  ret = g_strdup_printf ("%u:%s", num, checksum_cstr);

  g_checksum_free (checksum);

  return ret;
}
static void
append_fingerprint (GcrCertificateDetailsWidget *self, const guchar *data, 
                    gsize n_data, const gchar *name, GChecksumType type)
{
	GChecksum *checksum;
	guint8 *buffer;
	gsize n_buffer;
	gchar *display;
	
	checksum = g_checksum_new (type);
	g_return_if_fail (checksum);
	g_checksum_update (checksum, data, n_data);
	
	n_buffer = g_checksum_type_get_length (type);
	g_return_if_fail (n_buffer);
	buffer = g_malloc0 (n_buffer);
	
	g_checksum_get_digest (checksum, buffer, &n_buffer);
	g_checksum_free (checksum);
	
	display = egg_hex_encode_full (buffer, n_buffer, TRUE, ' ', 1);
	append_field_and_value (self, name, display, TRUE);
	g_free (display);
	
	g_free (buffer);		
}
Exemplo n.º 12
0
static void
test_checksum (GChecksumType  checksum_type,
               const gchar   *type,
               const gchar   *sum,
               int            length)
{
  GChecksum *checksum;
  const char *p;
  int chunk_length;

  for (chunk_length = MIN (length, 1); chunk_length < length; chunk_length++)
    {
      checksum = g_checksum_new (checksum_type);
      for (p = FIXED_STR; p < FIXED_STR + length; p += chunk_length)
	{
	  g_checksum_update (checksum, (const guchar *)p,
			     MIN (chunk_length, length - (p - FIXED_STR)));
	}

      if (strcmp (g_checksum_get_string (checksum), sum) != 0)
	{
	  g_print ("Invalid %s checksum for `%.*s' (length %d) counting by %d:\n"
		   "%s (expecting: %s)\n",
		   type,
		   length, FIXED_STR,
		   length, chunk_length,
		   g_checksum_get_string (checksum),
		   sum);
	  exit (1);
	}

      g_checksum_free (checksum);
    }
}
Exemplo n.º 13
0
void
_gcr_display_view_append_fingerprint (GcrDisplayView *self, GcrRenderer *renderer, const guchar *data,
                                      gsize n_data, const gchar *name, GChecksumType type)
{
	GChecksum *checksum;
	guint8 *buffer;
	gsize n_buffer;
	gchar *display;

	g_return_if_fail (GCR_IS_DISPLAY_VIEW (self));

	checksum = g_checksum_new (type);
	g_return_if_fail (checksum);
	g_checksum_update (checksum, data, n_data);

	n_buffer = g_checksum_type_get_length (type);
	g_return_if_fail (n_buffer);
	buffer = g_malloc0 (n_buffer);

	g_checksum_get_digest (checksum, buffer, &n_buffer);
	g_checksum_free (checksum);

	display = egg_hex_encode_full (buffer, n_buffer, TRUE, ' ', 1);
	_gcr_display_view_append_value (self, renderer, name, display, TRUE);
	g_free (display);

	g_free (buffer);
}
Exemplo n.º 14
0
void SHA1SKey(char *x)
{
        GChecksum *checksum;
        guint8 digest[20];
        gsize digest_len = sizeof (digest);
	guint32 *results;

        checksum = g_checksum_new (G_CHECKSUM_SHA1);
        g_checksum_update (checksum, (const guchar *) x, SKEY_SIZE);
        g_checksum_get_digest (checksum, digest, &digest_len);
        g_assert (digest_len == 20);

        results = (guint32 *) digest;
#ifndef WORDS_BIGENDIAN
	HTONDIGEST(results);
#else
	byteReverse((unsigned char *)digest, 5);
#endif

	results[0] ^= results[2];
	results[1] ^= results[3];
	results[0] ^= results[4];

	memcpy((void *)x, (void *)results, SKEY_SIZE);

        g_checksum_free (checksum);
}
Exemplo n.º 15
0
void rm_digest_free(RmDigest *digest) {
    switch(digest->type) {
    case RM_DIGEST_MD5:
    case RM_DIGEST_SHA512:
    case RM_DIGEST_SHA256:
    case RM_DIGEST_SHA1:
        g_checksum_free(digest->glib_checksum);
        digest->glib_checksum = NULL;
        break;
    case RM_DIGEST_PARANOID:
        if(digest->shadow_hash) {
            rm_digest_free(digest->shadow_hash);
        }
    case RM_DIGEST_CUMULATIVE:
    case RM_DIGEST_MURMUR512:
    case RM_DIGEST_CITY512:
    case RM_DIGEST_MURMUR256:
    case RM_DIGEST_CITY256:
    case RM_DIGEST_BASTARD:
    case RM_DIGEST_SPOOKY:
    case RM_DIGEST_SPOOKY32:
    case RM_DIGEST_SPOOKY64:
    case RM_DIGEST_MURMUR:
    case RM_DIGEST_CITY:
        if(digest->checksum) {
            g_slice_free1(digest->bytes, digest->checksum);
            digest->checksum = NULL;
        }
        break;
    default:
        g_assert_not_reached();
    }
    g_slice_free(RmDigest, digest);
}
Exemplo n.º 16
0
/**
 * CacheUtilHashPassword:
 * @algorithm: Hash algorithm.
 * @salt: Random data, usually stored with the hash.
 * @password: Secret value to hash.
 *
 * Returns: (allow-none): Hash value of concatenated @salt and @password, or
 * #NULL if an argument was invalid.
 */
GBytes *CacheUtilHashPassword(GChecksumType algorithm, GBytes *salt,
                               const char *password) {
  GChecksum *checksum = NULL;
  gssize checksum_len = -1;
  guint8 *result = NULL;
  gsize result_len = 0;

  if (!salt || !password)
    return NULL;

  checksum_len = g_checksum_type_get_length(algorithm);
  if (checksum_len <= 0)
    return NULL;

  checksum = g_checksum_new(algorithm);
  if (!checksum)
    return NULL;

  g_checksum_update(
      checksum, g_bytes_get_data(salt, NULL), g_bytes_get_size(salt));
  g_checksum_update(checksum, (guint8 *) password, strlen(password));

  result = g_malloc(checksum_len);
  result_len = checksum_len;

  g_checksum_get_digest(checksum, result, &result_len);
  g_assert(checksum_len == result_len);

  g_checksum_free(checksum);
  return g_bytes_new_take(result, result_len);
}
Exemplo n.º 17
0
/**
 * gcr_fingerprint_from_subject_public_key_info:
 * @key_info: (array length=n_key_info): DER encoded subjectPublicKeyInfo structure
 * @n_key_info: length of DER encoded structure
 * @checksum_type: the type of fingerprint to create
 * @n_fingerprint: the length of fingerprint returned
 *
 * Create a key fingerprint for a DER encoded subjectPublicKeyInfo.
 *
 * Returns: (transfer full) (allow-none) (array length=n_fingerprint): the
 *          fingerprint or %NULL if the input was invalid.
 */
guchar *
gcr_fingerprint_from_subject_public_key_info (const guchar *key_info,
                                              gsize n_key_info,
                                              GChecksumType checksum_type,
                                              gsize *n_fingerprint)
{
	GChecksum *check;
	guint8 *fingerprint;

	g_return_val_if_fail (key_info, NULL);
	g_return_val_if_fail (n_key_info, NULL);
	g_return_val_if_fail (n_fingerprint, NULL);

	check = g_checksum_new (checksum_type);
	g_return_val_if_fail (check, NULL);

	g_checksum_update (check, key_info, n_key_info);

	*n_fingerprint = g_checksum_type_get_length (checksum_type);
	fingerprint = g_malloc (*n_fingerprint);
	g_checksum_get_digest (check, fingerprint, n_fingerprint);

	g_checksum_free (check);
	return fingerprint;
}
Exemplo n.º 18
0
static void
do_md5sum (SoupMessage *msg, SoupXMLRPCParams *params)
{
	GVariant *args;
	GVariant *child;
	GChecksum *checksum;
	GByteArray *digest;
	gsize digest_len = 16;

	if (!(args = parse_params (msg, params, "(ay)")))
		return;

	child = g_variant_get_child_value (args, 0);

	checksum = g_checksum_new (G_CHECKSUM_MD5);
	g_checksum_update (checksum,
			   g_variant_get_data (child),
			   g_variant_get_size (child));
	digest = g_byte_array_new ();
	g_byte_array_set_size (digest, digest_len);
	g_checksum_get_digest (checksum, digest->data, &digest_len);
	g_checksum_free (checksum);

	soup_xmlrpc_message_set_response (msg,
					  g_variant_new_from_data (G_VARIANT_TYPE_BYTESTRING,
								   digest->data, digest_len,
								   TRUE, NULL, NULL),
					  NULL);
	g_byte_array_free (digest, TRUE);
	g_variant_unref (child);
	g_variant_unref (args);
}
Exemplo n.º 19
0
static void saveconfig(void)
{
    const char *uid = gtk_entry_get_text(GTK_ENTRY(entry1));
    const char *url = gtk_entry_get_text(GTK_ENTRY(entry3));

    unsigned char md5pword[16];
    gsize md5len = 16;

    if (pwd != NULL && pwd[0] != '\0') {
        GChecksum * state = g_checksum_new (G_CHECKSUM_MD5);
        g_checksum_update (state, (unsigned char *) pwd, strlen (pwd));
        g_checksum_get_digest (state, md5pword, & md5len);
        g_checksum_free (state);

        aud_set_string ("audioscrobbler", "password",
         hexify ((gchar *) md5pword, sizeof md5pword));
    }
    if (uid != NULL && uid[0] != '\0') {
        aud_set_string ("audioscrobbler", "username", uid);
    } else {
        aud_set_string ("audioscrobbler", "username", "");
        aud_set_string ("audioscrobbler", "password", "");
    }

    if (url != NULL && url[0] != '\0')
        aud_set_string ("audioscrobbler", "sc_url", url);
    else
        aud_set_string ("audioscrobbler", "sc_url", LASTFM_HS_URL);
}
Exemplo n.º 20
0
static gchar *
calc_websocket_challenge_reply (const gchar * key)
{
    const gchar *guid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
    gchar *ret = NULL;
    gchar *concat = g_strconcat (key, guid, NULL);
    GChecksum *checksum = g_checksum_new (G_CHECKSUM_SHA1);

    guint8 sha1[20];
    gsize len = 20;

    //g_print ("challenge: %s\n", key);

    g_checksum_update (checksum, (guchar *) (concat), -1);
    g_checksum_get_digest (checksum, sha1, &len);

    g_free (concat);

    ret = g_base64_encode (sha1, len);

    g_checksum_free (checksum);
    //g_print ("reply: %s\n", ret);

    return ret;
}
Exemplo n.º 21
0
static void quit(void)
{
  g_checksum_free(checksum);
  sockmux_sender_reset(sender);
  g_object_unref(sender);
  g_main_loop_quit(loop);
}
Exemplo n.º 22
0
static uint8_t *digest_cert(const char *cert, int length)
{
	GChecksum *checksum;
	uint8_t *hash;
	gsize digest_len;

	if (cert == NULL || length < 0)
		return NULL;

	digest_len = g_checksum_type_get_length(G_CHECKSUM_SHA1);
	hash = g_try_malloc(digest_len);
	if (hash == NULL)
		return NULL;

	checksum = g_checksum_new(G_CHECKSUM_SHA1);
	if (checksum == NULL) {
		g_free(hash);
		return NULL;
	}

	g_checksum_update(checksum, cert, length);

	g_checksum_get_digest(checksum, hash, &digest_len);

	DBG("Digest is: ");
	__seel_apdu_dump(hash, digest_len);

	g_checksum_free(checksum);

	return hash;
}
Exemplo n.º 23
0
static void
do_md5sum (SoupMessage *msg, GValueArray *params)
{
	GChecksum *checksum;
	GByteArray *data, *digest;
	gsize digest_len = 16;

	if (params->n_values != 1) {
		args_error (msg, params, 1);
		return;
	}

	if (!soup_value_array_get_nth (params, 0, SOUP_TYPE_BYTE_ARRAY, &data)) {
		type_error (msg, SOUP_TYPE_BYTE_ARRAY, params, 0);
		return;
	}
	checksum = g_checksum_new (G_CHECKSUM_MD5);
	g_checksum_update (checksum, data->data, data->len);
	digest = g_byte_array_new ();
	g_byte_array_set_size (digest, digest_len);
	g_checksum_get_digest (checksum, digest->data, &digest_len);
	g_checksum_free (checksum);

	soup_xmlrpc_set_response (msg, SOUP_TYPE_BYTE_ARRAY, digest);
	g_byte_array_free (digest, TRUE);
}
Exemplo n.º 24
0
static void
wep128_passphrase_hash (const char *input,
                        size_t input_len,
                        guint8 *out_digest,
                        size_t *out_digest_len)
{
	GChecksum *sum;
	guint8 data[64];
	int i;

	g_return_if_fail (out_digest != NULL);
	g_return_if_fail (out_digest_len != NULL);
	g_return_if_fail (*out_digest_len >= 16);

	/* Get at least 64 bytes by repeating the passphrase into the buffer */
	for (i = 0; i < sizeof (data); i++)
		data[i] = input[i % input_len];

	sum = g_checksum_new (G_CHECKSUM_MD5);
	g_assert (sum);
	g_checksum_update (sum, data, sizeof (data));
	g_checksum_get_digest (sum, out_digest, out_digest_len);
	g_checksum_free (sum);

	g_assert (*out_digest_len == 16);
	/* WEP104 keys are 13 bytes in length (26 hex characters) */
	*out_digest_len = 13;
}
Exemplo n.º 25
0
static void
uuid_randomize(gchar *d, gsize dl)
{
    static guint32 seq = 0;

    struct {
        int pid, ppid;
        GTimeVal now;
        guint32 seq;
        gpointer p0, p1, p2;
        gchar h[512];
    } bulk;

    memset(&bulk, 0, sizeof(bulk));
    bulk.pid = getpid();
    bulk.ppid = getppid();
    g_get_current_time(&(bulk.now));
    bulk.seq = ++seq;
    bulk.p0 = &bulk;
    bulk.p1 = d;
    bulk.p2 = &d;
    gethostname(bulk.h, sizeof(bulk.h));

    GChecksum *cs = g_checksum_new(G_CHECKSUM_SHA256);
    g_checksum_update(cs, (guint8*) &bulk, sizeof(bulk));
    g_strlcpy(d, g_checksum_get_string(cs), dl);
    g_checksum_free(cs);

    for (; dl ;--dl)
        d[dl-1] = g_ascii_toupper(d[dl-1]);
}
Exemplo n.º 26
0
char* bg_blur_pict_get_dest_path (const char* src_uri)
{
    g_debug ("[%s] bg_blur_pict_get_dest_path: src_uri=%s", __func__, src_uri);
    g_return_val_if_fail (src_uri != NULL, NULL);

    //1. calculate original picture md5
    GChecksum* checksum;
    checksum = g_checksum_new (G_CHECKSUM_MD5);
    g_checksum_update (checksum, (const guchar *) src_uri, strlen (src_uri));

    guint8 digest[16];
    gsize digest_len = sizeof (digest);
    g_checksum_get_digest (checksum, digest, &digest_len);
    g_assert (digest_len == 16);

    //2. build blurred picture path
    char* file;
    file = g_strconcat (g_checksum_get_string (checksum), ".png", NULL);
    g_checksum_free (checksum);
    char* path;
    path = g_build_filename (g_get_user_cache_dir (),
                    BG_BLUR_PICT_CACHE_DIR,
                    file,
                    NULL);
    g_free (file);

    return path;
}
Exemplo n.º 27
0
Arquivo: sms.c Projeto: AndriusA/ofono
/**
 * Generate a UUID from an SMS PDU List
 *
 * @param pdu Pointer to array of PDUs data to generate the ID from
 * @param pdus Number of entries in the \e pdu array
 * @return 0 in error (no memory or serious code inconsistency in the
 *     input data structures), otherwise the SMS UUID.
 *
 * @internal
 *
 * The current time is added to avoid the UUID being the same when the
 * same message is sent to the same destination repeatedly. Note we
 * need a high resolution time (not just seconds), otherwise resending
 * in the same second (not that rare) could yield the same UUID.
 */
static gboolean sms_uuid_from_pdus(const struct pending_pdu *pdu,
					unsigned char pdus,
					struct ofono_uuid *uuid)

{
	GChecksum *checksum;
	gsize uuid_size = sizeof(uuid->uuid);
	unsigned int cnt;
	struct timeval now;

	checksum = g_checksum_new(G_CHECKSUM_SHA1);
	if (checksum == NULL)
		return FALSE;

	for (cnt = 0; cnt < pdus; cnt++)
		g_checksum_update(checksum, pdu[cnt].pdu, pdu[cnt].pdu_len);

	gettimeofday(&now, NULL);
	g_checksum_update(checksum, (void *) &now, sizeof(now));

	g_checksum_get_digest(checksum, uuid->uuid, &uuid_size);
	g_checksum_free(checksum);

	return TRUE;
}
Exemplo n.º 28
0
void
soup_auth_digest_compute_hex_a1 (const char              *hex_urp,
				 SoupAuthDigestAlgorithm  algorithm,
				 const char              *nonce,
				 const char              *cnonce,
				 char                     hex_a1[33])
{
	if (algorithm == SOUP_AUTH_DIGEST_ALGORITHM_MD5) {
		/* In MD5, A1 is just user:realm:password, so hex_A1
		 * is just hex_urp.
		 */
		/* You'd think you could say "sizeof (hex_a1)" here,
		 * but you'd be wrong.
		 */
		memcpy (hex_a1, hex_urp, 33);
	} else {
		GChecksum *checksum;

		/* In MD5-sess, A1 is hex_urp:nonce:cnonce */

		checksum = g_checksum_new (G_CHECKSUM_MD5);
		g_checksum_update (checksum, (guchar *)hex_urp, strlen (hex_urp));
		g_checksum_update (checksum, (guchar *)":", 1);
		g_checksum_update (checksum, (guchar *)nonce, strlen (nonce));
		g_checksum_update (checksum, (guchar *)":", 1);
		g_checksum_update (checksum, (guchar *)cnonce, strlen (cnonce));
		strncpy (hex_a1, g_checksum_get_string (checksum), 33);
		g_checksum_free (checksum);
	}
}
Exemplo n.º 29
0
void
soup_auth_digest_compute_response (const char        *method,
				   const char        *uri,
				   const char        *hex_a1,
				   SoupAuthDigestQop  qop,
				   const char        *nonce,
				   const char        *cnonce,
				   int                nc,
				   char               response[33])
{
	char hex_a2[33];
	GChecksum *checksum;

	/* compute A2 */
	checksum = g_checksum_new (G_CHECKSUM_MD5);
	g_checksum_update (checksum, (guchar *)method, strlen (method));
	g_checksum_update (checksum, (guchar *)":", 1);
	g_checksum_update (checksum, (guchar *)uri, strlen (uri));
	strncpy (hex_a2, g_checksum_get_string (checksum), 33);
	g_checksum_free (checksum);

	/* compute KD */
	checksum = g_checksum_new (G_CHECKSUM_MD5);
	g_checksum_update (checksum, (guchar *)hex_a1, strlen (hex_a1));
	g_checksum_update (checksum, (guchar *)":", 1);
	g_checksum_update (checksum, (guchar *)nonce, strlen (nonce));
	g_checksum_update (checksum, (guchar *)":", 1);

	if (qop) {
		char tmp[9];

		g_snprintf (tmp, 9, "%.8x", nc);
		g_checksum_update (checksum, (guchar *)tmp, strlen (tmp));
		g_checksum_update (checksum, (guchar *)":", 1);
		g_checksum_update (checksum, (guchar *)cnonce, strlen (cnonce));
		g_checksum_update (checksum, (guchar *)":", 1);

		if (!(qop & SOUP_AUTH_DIGEST_QOP_AUTH))
			g_warn_if_reached ();
		g_checksum_update (checksum, (guchar *)"auth", strlen ("auth"));
		g_checksum_update (checksum, (guchar *)":", 1);
	}

	g_checksum_update (checksum, (guchar *)hex_a2, 32);
	strncpy (response, g_checksum_get_string (checksum), 33);
	g_checksum_free (checksum);
}
static GByteArray *
generate_duid_from_machine_id (void)
{
	GByteArray *duid;
	char *contents = NULL;
	GChecksum *sum;
	guint8 buffer[32]; /* SHA256 digest size */
	gsize sumlen = sizeof (buffer);
	const guint16 duid_type = g_htons (4);
	uuid_t uuid;
	GRand *generator;
	guint i;
	gboolean success = FALSE;

	/* Get the machine ID from /etc/machine-id; it's always in /etc no matter
	 * where our configured SYSCONFDIR is.  Alternatively, it might be in
	 * LOCALSTATEDIR /lib/dbus/machine-id.
	 */
	if (   g_file_get_contents ("/etc/machine-id", &contents, NULL, NULL)
	    || g_file_get_contents (LOCALSTATEDIR "/lib/dbus/machine-id", &contents, NULL, NULL)) {
		contents = g_strstrip (contents);
		success = machine_id_parse (contents, uuid);
		if (success) {
			/* Hash the machine ID so it's not leaked to the network */
			sum = g_checksum_new (G_CHECKSUM_SHA256);
			g_checksum_update (sum, (const guchar *) &uuid, sizeof (uuid));
			g_checksum_get_digest (sum, buffer, &sumlen);
			g_checksum_free (sum);
		}
		g_free (contents);
	}

	if (!success) {
		nm_log_warn (LOGD_DHCP6, "Failed to read " SYSCONFDIR "/machine-id "
		             "or " LOCALSTATEDIR "/lib/dbus/machine-id to generate "
		             "DHCPv6 DUID; creating non-persistent random DUID.");

		generator = g_rand_new ();
		for (i = 0; i < sizeof (buffer) / sizeof (guint32); i++)
			((guint32 *) buffer)[i] = g_rand_int (generator);
		g_rand_free (generator);
	}

	/* Generate a DHCP Unique Identifier for DHCPv6 using the
	 * DUID-UUID method (see RFC 6355 section 4).  Format is:
	 *
	 * u16: type (DUID-UUID = 4)
	 * u8[16]: UUID bytes
	 */
	duid = g_byte_array_sized_new (18);
	g_byte_array_append (duid, (guint8 *) &duid_type, sizeof (duid_type));

	/* Since SHA256 is 256 bits, but UUID is 128 bits, we just take the first
	 * 128 bits of the SHA256 as the DUID-UUID.
	 */
	g_byte_array_append (duid, buffer, 16);

	return duid;
}