Пример #1
0
static int
dst_hmac_md5_generate_key(DST_KEY *key, const int nothing)
{
	u_char *buff;
	int n;
	unsigned size, len;

	if (key == NULL || key->dk_alg != KEY_HMAC_MD5)
		return (0);
	size = (key->dk_key_size + 7) / 8; /* convert to bytes */
	if (size <= 0)
		return(0);
	
	len = size > 64 ? 64 : size;
	buff = malloc(len+8);

	n = dst_random(DST_RAND_SEMI, len, buff);
	n += dst_random(DST_RAND_KEY, len, buff);
	if (n <= len) {	/* failed getting anything */
		SAFE_FREE2(buff, len);
		return (-1);
	}
	n = dst_buffer_to_hmac_md5(key, buff, len);
	SAFE_FREE2(buff, len);
	if (n <= 0)
		return (n);
	return (1);
}
Пример #2
0
static int
dst_hmac_md5_key_from_file_format(DST_KEY *dkey, const char *buff,
			      const int buff_len)
{
	const char *p = buff, *eol;
	u_char key[HMAC_LEN+1];	/* b64_pton needs more than 64 bytes do decode
				 * it should probably be fixed rather than doing
				 * this
				 */
	u_char *tmp;
	int key_len, len;

	if (dkey == NULL)
		return (-2);
	if (buff == NULL || buff_len < 0)
		return (-1);

	memset(key, 0, sizeof(key));

	if (!dst_s_verify_str(&p, "Key: "))
		return (-3);

	eol = strchr(p, '\n');
	if (eol == NULL)
		return (-4);
	len = eol - p;
	tmp = malloc(len + 2);
	if (tmp == NULL)
		return (-5);
	memcpy(tmp, p, len);
	*(tmp + len) = 0x0;
	key_len = b64_pton((char *)tmp, key, HMAC_LEN+1);	/*%< see above */
	SAFE_FREE2(tmp, len + 2);

	if (dst_buffer_to_hmac_md5(dkey, key, key_len) < 0) {
		return (-6);
	}
	return (0);
}