Ruby  3.1.0dev(2021-09-10revisionb76ad15ed0da636161de0243c547ee1e6fc95681)
ossl_hmac.c
Go to the documentation of this file.
1 /*
2  * 'OpenSSL for Ruby' project
3  * Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
4  * All rights reserved.
5  */
6 /*
7  * This program is licensed under the same licence as Ruby.
8  * (See the file 'LICENCE'.)
9  */
10 #include "ossl.h"
11 
12 #define NewHMAC(klass) \
13  TypedData_Wrap_Struct((klass), &ossl_hmac_type, 0)
14 #define GetHMAC(obj, ctx) do { \
15  TypedData_Get_Struct((obj), EVP_MD_CTX, &ossl_hmac_type, (ctx)); \
16  if (!(ctx)) { \
17  ossl_raise(rb_eRuntimeError, "HMAC wasn't initialized"); \
18  } \
19 } while (0)
20 
21 /*
22  * Classes
23  */
26 
27 /*
28  * Public
29  */
30 
31 /*
32  * Private
33  */
34 static void
35 ossl_hmac_free(void *ctx)
36 {
37  EVP_MD_CTX_free(ctx);
38 }
39 
40 static const rb_data_type_t ossl_hmac_type = {
41  "OpenSSL/HMAC",
42  {
43  0, ossl_hmac_free,
44  },
46 };
47 
48 static VALUE
49 ossl_hmac_alloc(VALUE klass)
50 {
51  VALUE obj;
52  EVP_MD_CTX *ctx;
53 
54  obj = NewHMAC(klass);
55  ctx = EVP_MD_CTX_new();
56  if (!ctx)
57  ossl_raise(eHMACError, "EVP_MD_CTX");
58  RTYPEDDATA_DATA(obj) = ctx;
59 
60  return obj;
61 }
62 
63 
64 /*
65  * call-seq:
66  * HMAC.new(key, digest) -> hmac
67  *
68  * Returns an instance of OpenSSL::HMAC set with the key and digest
69  * algorithm to be used. The instance represents the initial state of
70  * the message authentication code before any data has been processed.
71  * To process data with it, use the instance method #update with your
72  * data as an argument.
73  *
74  * === Example
75  *
76  * key = 'key'
77  * instance = OpenSSL::HMAC.new(key, 'SHA1')
78  * #=> f42bb0eeb018ebbd4597ae7213711ec60760843f
79  * instance.class
80  * #=> OpenSSL::HMAC
81  *
82  * === A note about comparisons
83  *
84  * Two instances can be securely compared with #== in constant time:
85  *
86  * other_instance = OpenSSL::HMAC.new('key', 'SHA1')
87  * #=> f42bb0eeb018ebbd4597ae7213711ec60760843f
88  * instance == other_instance
89  * #=> true
90  *
91  */
92 static VALUE
93 ossl_hmac_initialize(VALUE self, VALUE key, VALUE digest)
94 {
95  EVP_MD_CTX *ctx;
96  EVP_PKEY *pkey;
97 
98  GetHMAC(self, ctx);
100  pkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
101  (unsigned char *)RSTRING_PTR(key),
102  RSTRING_LENINT(key));
103  if (!pkey)
104  ossl_raise(eHMACError, "EVP_PKEY_new_mac_key");
105  if (EVP_DigestSignInit(ctx, NULL, ossl_evp_get_digestbyname(digest),
106  NULL, pkey) != 1) {
107  EVP_PKEY_free(pkey);
108  ossl_raise(eHMACError, "EVP_DigestSignInit");
109  }
110  /* Decrement reference counter; EVP_MD_CTX still keeps it */
111  EVP_PKEY_free(pkey);
112 
113  return self;
114 }
115 
116 static VALUE
117 ossl_hmac_copy(VALUE self, VALUE other)
118 {
119  EVP_MD_CTX *ctx1, *ctx2;
120 
121  rb_check_frozen(self);
122  if (self == other) return self;
123 
124  GetHMAC(self, ctx1);
125  GetHMAC(other, ctx2);
126  if (EVP_MD_CTX_copy(ctx1, ctx2) != 1)
127  ossl_raise(eHMACError, "EVP_MD_CTX_copy");
128  return self;
129 }
130 
131 /*
132  * call-seq:
133  * hmac.update(string) -> self
134  *
135  * Returns _hmac_ updated with the message to be authenticated.
136  * Can be called repeatedly with chunks of the message.
137  *
138  * === Example
139  *
140  * first_chunk = 'The quick brown fox jumps '
141  * second_chunk = 'over the lazy dog'
142  *
143  * instance.update(first_chunk)
144  * #=> 5b9a8038a65d571076d97fe783989e52278a492a
145  * instance.update(second_chunk)
146  * #=> de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9
147  *
148  */
149 static VALUE
150 ossl_hmac_update(VALUE self, VALUE data)
151 {
152  EVP_MD_CTX *ctx;
153 
154  StringValue(data);
155  GetHMAC(self, ctx);
156  if (EVP_DigestSignUpdate(ctx, RSTRING_PTR(data), RSTRING_LEN(data)) != 1)
157  ossl_raise(eHMACError, "EVP_DigestSignUpdate");
158 
159  return self;
160 }
161 
162 /*
163  * call-seq:
164  * hmac.digest -> string
165  *
166  * Returns the authentication code an instance represents as a binary string.
167  *
168  * === Example
169  * instance = OpenSSL::HMAC.new('key', 'SHA1')
170  * #=> f42bb0eeb018ebbd4597ae7213711ec60760843f
171  * instance.digest
172  * #=> "\xF4+\xB0\xEE\xB0\x18\xEB\xBDE\x97\xAEr\x13q\x1E\xC6\a`\x84?"
173  */
174 static VALUE
175 ossl_hmac_digest(VALUE self)
176 {
177  EVP_MD_CTX *ctx;
178  size_t buf_len;
179  VALUE ret;
180 
181  GetHMAC(self, ctx);
182  ret = rb_str_new(NULL, EVP_MAX_MD_SIZE);
183  if (EVP_DigestSignFinal(ctx, (unsigned char *)RSTRING_PTR(ret),
184  &buf_len) != 1)
185  ossl_raise(eHMACError, "EVP_DigestSignFinal");
186  rb_str_set_len(ret, (long)buf_len);
187 
188  return ret;
189 }
190 
191 /*
192  * call-seq:
193  * hmac.hexdigest -> string
194  *
195  * Returns the authentication code an instance represents as a hex-encoded
196  * string.
197  */
198 static VALUE
199 ossl_hmac_hexdigest(VALUE self)
200 {
201  EVP_MD_CTX *ctx;
202  unsigned char buf[EVP_MAX_MD_SIZE];
203  size_t buf_len;
204  VALUE ret;
205 
206  GetHMAC(self, ctx);
207  if (EVP_DigestSignFinal(ctx, buf, &buf_len) != 1)
208  ossl_raise(eHMACError, "EVP_DigestSignFinal");
209  ret = rb_str_new(NULL, buf_len * 2);
210  ossl_bin2hex(buf, RSTRING_PTR(ret), buf_len);
211 
212  return ret;
213 }
214 
215 /*
216  * call-seq:
217  * hmac.reset -> self
218  *
219  * Returns _hmac_ as it was when it was first initialized, with all processed
220  * data cleared from it.
221  *
222  * === Example
223  *
224  * data = "The quick brown fox jumps over the lazy dog"
225  * instance = OpenSSL::HMAC.new('key', 'SHA1')
226  * #=> f42bb0eeb018ebbd4597ae7213711ec60760843f
227  *
228  * instance.update(data)
229  * #=> de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9
230  * instance.reset
231  * #=> f42bb0eeb018ebbd4597ae7213711ec60760843f
232  *
233  */
234 static VALUE
235 ossl_hmac_reset(VALUE self)
236 {
237  EVP_MD_CTX *ctx;
238  EVP_PKEY *pkey;
239 
240  GetHMAC(self, ctx);
241  pkey = EVP_PKEY_CTX_get0_pkey(EVP_MD_CTX_pkey_ctx(ctx));
242  if (EVP_DigestSignInit(ctx, NULL, EVP_MD_CTX_md(ctx), NULL, pkey) != 1)
243  ossl_raise(eHMACError, "EVP_DigestSignInit");
244 
245  return self;
246 }
247 
248 /*
249  * INIT
250  */
251 void
253 {
254 #if 0
255  mOSSL = rb_define_module("OpenSSL");
257 #endif
258 
259  /*
260  * Document-class: OpenSSL::HMAC
261  *
262  * OpenSSL::HMAC allows computing Hash-based Message Authentication Code
263  * (HMAC). It is a type of message authentication code (MAC) involving a
264  * hash function in combination with a key. HMAC can be used to verify the
265  * integrity of a message as well as the authenticity.
266  *
267  * OpenSSL::HMAC has a similar interface to OpenSSL::Digest.
268  *
269  * === HMAC-SHA256 using one-shot interface
270  *
271  * key = "key"
272  * data = "message-to-be-authenticated"
273  * mac = OpenSSL::HMAC.hexdigest("SHA256", key, data)
274  * #=> "cddb0db23f469c8bf072b21fd837149bd6ace9ab771cceef14c9e517cc93282e"
275  *
276  * === HMAC-SHA256 using incremental interface
277  *
278  * data1 = File.binread("file1")
279  * data2 = File.binread("file2")
280  * key = "key"
281  * hmac = OpenSSL::HMAC.new(key, 'SHA256')
282  * hmac << data1
283  * hmac << data2
284  * mac = hmac.digest
285  */
287 
289 
290  rb_define_alloc_func(cHMAC, ossl_hmac_alloc);
291 
292  rb_define_method(cHMAC, "initialize", ossl_hmac_initialize, 2);
293  rb_define_method(cHMAC, "initialize_copy", ossl_hmac_copy, 1);
294 
295  rb_define_method(cHMAC, "reset", ossl_hmac_reset, 0);
296  rb_define_method(cHMAC, "update", ossl_hmac_update, 1);
297  rb_define_alias(cHMAC, "<<", "update");
298  rb_define_method(cHMAC, "digest", ossl_hmac_digest, 0);
299  rb_define_method(cHMAC, "hexdigest", ossl_hmac_hexdigest, 0);
300  rb_define_alias(cHMAC, "inspect", "hexdigest");
301  rb_define_alias(cHMAC, "to_s", "hexdigest");
302 }
NewHMAC
#define NewHMAC(klass)
Definition: ossl_hmac.c:12
StringValue
#define StringValue(v)
Definition: rstring.h:50
Init_ossl_hmac
void Init_ossl_hmac(void)
Definition: ossl_hmac.c:252
rb_define_module
VALUE rb_define_module(const char *name)
Definition: class.c:887
ossl.h
rb_str_set_len
void rb_str_set_len(VALUE, long)
Definition: string.c:2833
RSTRING_LEN
#define RSTRING_LEN(string)
Definition: fbuffer.h:22
EVP_MD_CTX_pkey_ctx
#define EVP_MD_CTX_pkey_ctx(x)
Definition: openssl_missing.h:25
rb_define_alias
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition: class.c:2050
cHMAC
VALUE cHMAC
Definition: ossl_hmac.c:24
rb_str_new
#define rb_str_new(str, len)
Definition: string.h:213
RUBY_TYPED_FREE_IMMEDIATELY
@ RUBY_TYPED_FREE_IMMEDIATELY
Definition: rtypeddata.h:62
eHMACError
VALUE eHMACError
Definition: ossl_hmac.c:25
mOSSL
VALUE mOSSL
Definition: ossl.c:237
rb_define_alloc_func
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
rb_cObject
VALUE rb_cObject
Object class.
Definition: object.c:50
GetHMAC
#define GetHMAC(obj, ctx)
Definition: ossl_hmac.c:14
ossl_raise
void ossl_raise(VALUE exc, const char *fmt,...)
Definition: ossl.c:299
ossl_bin2hex
void ossl_bin2hex(unsigned char *in, char *out, size_t inlen)
Definition: ossl.c:139
NULL
#define NULL
Definition: regenc.h:69
RTYPEDDATA_DATA
#define RTYPEDDATA_DATA(v)
Definition: rtypeddata.h:47
key
key
Definition: openssl_missing.h:145
VALUE
unsigned long VALUE
Definition: value.h:38
EVP_MD_CTX_free
#define EVP_MD_CTX_free
Definition: openssl_missing.h:21
buf
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4322
RSTRING_PTR
#define RSTRING_PTR(string)
Definition: fbuffer.h:19
ret
return ret
Definition: memory.h:232
rb_check_frozen
#define rb_check_frozen
Definition: error.h:72
rb_data_type_struct
Definition: rtypeddata.h:70
eOSSLError
VALUE eOSSLError
Definition: ossl.c:242
rb_define_class_under
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:809
rb_define_method
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
Definition: cxxanyargs.hpp:655
ossl_evp_get_digestbyname
const EVP_MD * ossl_evp_get_digestbyname(VALUE obj)
Definition: ossl_digest.c:45
rb_eStandardError
VALUE rb_eStandardError
Definition: error.c:1090
EVP_MD_CTX_new
#define EVP_MD_CTX_new
Definition: openssl_missing.h:17