summaryrefslogtreecommitdiff
path: root/src/wsgcrypt.cpp
blob: c6b89a5898246767d1eb3d62785a5f8a51547454 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
 * wsgcrypt.c
 *
 * Created on 2020-11-26
 * @author: qyc
 *
 * @explain: 
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "wsgcrypt.h"

gcry_error_t ws_hmac_buffer(int algo, void *digest, const void *buffer, size_t length, const void *key, size_t keylen)
{
	gcry_md_hd_t hmac_handle;
	gcry_error_t result = gcry_md_open(&hmac_handle, algo, GCRY_MD_FLAG_HMAC);
	if (result) {
		return result;
	}
	result = gcry_md_setkey(hmac_handle, key, keylen);
	if (result) {
		gcry_md_close(hmac_handle);
		return result;
	}
	gcry_md_write(hmac_handle, buffer, length);
	memcpy(digest, gcry_md_read(hmac_handle, 0), gcry_md_get_algo_dlen(algo));
	gcry_md_close(hmac_handle);
	return GPG_ERR_NO_ERROR;
}

gcry_error_t hkdf_expand(int hashalgo, const guint8 *prk, guint prk_len, const guint8 *info, guint info_len, guint8 *out, guint out_len)
{
	// Current maximum hash output size: 48 bytes for SHA-384.
	guchar lastoutput[48];
	gcry_md_hd_t h;
	gcry_error_t err;
	const guint hash_len = gcry_md_get_algo_dlen(hashalgo);

	// Some sanity checks
	if (!(out_len > 0 && out_len <= 255 * hash_len) || !(hash_len > 0 && hash_len <= sizeof(lastoutput)))
		return GPG_ERR_INV_ARG;

	err = gcry_md_open(&h, hashalgo, GCRY_MD_FLAG_HMAC);
	if (err)
		return err;
	
	guint offset;
	for (offset = 0; offset < out_len; offset += hash_len) {
		gcry_md_reset(h);
		// Set PRK
		gcry_md_setkey(h, prk, prk_len);
		if (offset > 0)
			// T(1..N)
			gcry_md_write(h, lastoutput, hash_len);
		// info
		gcry_md_write(h, info, info_len);
		// constant 0x01..N
		gcry_md_putc(h, (guint8)(offset / hash_len + 1));

		memcpy(lastoutput, gcry_md_read(h, hashalgo), hash_len);
		memcpy(out + offset, lastoutput, MIN(hash_len, out_len - offset));
	}

	gcry_md_close(h);

	return 0;
}