aboutsummaryrefslogtreecommitdiffstats
path: root/ossl_hmac.c
blob: 8bb5aec0b256a11eaed8726b6c45c83bf2403b23 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * $Id$
 * 'OpenSSL for Ruby' project
 * Copyright (C) 2001-2002  Michal Rokos <m.rokos@sh.cvut.cz>
 * All rights reserved.
 */
/*
 * This program is licenced under the same licence as Ruby.
 * (See the file 'LICENCE'.)
 */
#if !defined(NO_HMAC) && !defined(OPENSSL_NO_HMAC)

#include "ossl.h"

#define MakeHMAC(obj, hmacp) {\
	obj = Data_Make_Struct(cHMAC, ossl_hmac, 0, ossl_hmac_free, hmacp);\
}
#define GetHMAC(obj, hmacp) Data_Get_Struct(obj, ossl_hmac, hmacp)

/*
 * Classes
 */
VALUE cHMAC;
VALUE eHMACError;

/*
 * Struct
 */
typedef struct ossl_hmac_st {
	HMAC_CTX *hmac;
} ossl_hmac;

static void
ossl_hmac_free(ossl_hmac *hmacp)
{
	if (hmacp) {
		if (hmacp->hmac) OPENSSL_free(hmacp->hmac);
		hmacp->hmac = NULL;
		free(hmacp);
	}
}

/*
 * PUBLIC
 */

/*
 * PRIVATE
 */
static VALUE
ossl_hmac_s_new(int argc, VALUE *argv, VALUE klass)
{
	ossl_hmac *hmacp = NULL;
	VALUE obj;

	MakeHMAC(obj, hmacp);
	rb_obj_call_init(obj, argc, argv);

	return obj;
}

static VALUE
ossl_hmac_initialize(int argc, VALUE *argv, VALUE self)
{
	ossl_hmac *hmacp = NULL;
	const EVP_MD *md = NULL;
	VALUE key, digest;

	GetHMAC(self, hmacp);

	rb_scan_args(argc, argv, "20", &key, &digest);

	Check_Type(key, T_STRING);
	md = ossl_digest_get_EVP_MD(digest);

	if (!(hmacp->hmac = OPENSSL_malloc(sizeof(HMAC_CTX)))) {
		OSSL_Raise(eHMACError, "");
	}
	HMAC_Init(hmacp->hmac, RSTRING(key)->ptr, RSTRING(key)->len, md);

	return self;
}

static VALUE
ossl_hmac_update(VALUE self, VALUE data)
{
	ossl_hmac *hmacp = NULL;

	GetHMAC(self, hmacp);

	Check_Type(data, T_STRING);

	HMAC_Update(hmacp->hmac, RSTRING(data)->ptr, RSTRING(data)->len);

	return self;
}

static VALUE
ossl_hmac_hmac(VALUE self)
{
	ossl_hmac *hmacp = NULL;
	char *buf = NULL;
	int buf_len = 0;
	HMAC_CTX final;
	VALUE str;
	
	GetHMAC(self, hmacp);
	
	if (!HMAC_CTX_copy(&final, hmacp->hmac)) {
		OSSL_Raise(eHMACError, "");
	}
	if (!(buf = OPENSSL_malloc(HMAC_size(&final)))) {
		OSSL_Raise(eHMACError, "Cannot allocate memory for hmac");
	}
	HMAC_Final(&final, buf, &buf_len);

	str = rb_str_new(buf, buf_len);
	OPENSSL_free(buf);
	
	return str;
}

static VALUE
ossl_hmac_hexhmac(VALUE self)
{
	ossl_hmac *hmacp = NULL;
	static const char hex[]="0123456789abcdef";
	char *buf = NULL, *hexbuf = NULL;
	int i,buf_len = 0;
	HMAC_CTX final;
	VALUE str;
	
	GetHMAC(self, hmacp);
	
	if (!HMAC_CTX_copy(&final, hmacp->hmac)) {
		OSSL_Raise(eHMACError, "Cannot copy HMAC CTX");
	}
	if (!(buf = OPENSSL_malloc(HMAC_size(&final)))) {
		OSSL_Raise(eHMACError, "Cannot allocate memory for hmac");
	}
	HMAC_Final(&final, buf, &buf_len);
	
	if (!(hexbuf = OPENSSL_malloc(2*buf_len+1))) {
		OPENSSL_free(buf);
		OSSL_Raise(eHMACError, "Memory alloc error");
	}
	for (i = 0; i < buf_len; i++) {
		hexbuf[i + i] = hex[((unsigned char)buf[i]) >> 4];
		hexbuf[i + i + 1] = hex[buf[i] & 0x0f];
	}
	hexbuf[i + i] = '\0';
	
	str = rb_str_new(hexbuf, 2*buf_len);

	OPENSSL_free(buf);
	OPENSSL_free(hexbuf);

	return str;
}

/*
 * INIT
 */
void
Init_hmac(VALUE module)
{
	eHMACError = rb_define_class_under(module, "HMACError", rb_eStandardError);
	
	cHMAC = rb_define_class_under(module, "HMAC", rb_cObject);
	rb_define_singleton_method(cHMAC, "new", ossl_hmac_s_new, -1);
	rb_define_method(cHMAC, "initialize", ossl_hmac_initialize, -1);
	rb_define_method(cHMAC, "update", ossl_hmac_update, 1);
	rb_define_alias(cHMAC, "<<", "update");
	rb_define_method(cHMAC, "hmac", ossl_hmac_hmac, 0);
	rb_define_method(cHMAC, "hexhmac", ossl_hmac_hexhmac, 0);
	rb_define_alias(cHMAC, "inspect", "hexhmac");
	rb_define_alias(cHMAC, "to_str", "hexhmac");
}

#else /* NO_HMAC */

void
Init_hmac(VALUE module)
{
	rb_warning("HMAC will NOT be avaible: OpenSSL is compiled without HMAC.");
}

#endif /* NO_HMAC */