sha3 2.2.2 → 2.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/ext/sha3/digest.c CHANGED
@@ -79,15 +79,18 @@ static ID _shake_128_id;
79
79
  static ID _shake_256_id;
80
80
 
81
81
  /* TypedData structure for sha3_digest_context_t */
82
- const rb_data_type_t sha3_digest_data_type_t = {"SHA3::Digest",
83
- {
84
- NULL,
85
- sha3_digest_free_context,
86
- sha3_digest_context_size,
87
- },
88
- NULL,
89
- NULL,
90
- RUBY_TYPED_FREE_IMMEDIATELY};
82
+ const rb_data_type_t sha3_digest_data_type = {
83
+ "SHA3::Digest",
84
+ {
85
+ NULL,
86
+ sha3_digest_free_context,
87
+ sha3_digest_context_size,
88
+ NULL,
89
+ },
90
+ NULL,
91
+ NULL,
92
+ RUBY_TYPED_FREE_IMMEDIATELY,
93
+ };
91
94
 
92
95
  void Init_sha3_digest(void) {
93
96
  rb_require("digest");
@@ -154,10 +157,14 @@ void Init_sha3_digest(void) {
154
157
 
155
158
  // Static inline functions replacing macros
156
159
  static inline void get_sha3_digest_context(VALUE obj, sha3_digest_context_t **context) {
157
- TypedData_Get_Struct((obj), sha3_digest_context_t, &sha3_digest_data_type_t, (*context));
160
+ TypedData_Get_Struct((obj), sha3_digest_context_t, &sha3_digest_data_type, (*context));
158
161
  if (!(*context)) {
159
162
  rb_raise(rb_eRuntimeError, "Digest data not initialized!");
160
163
  }
164
+
165
+ if (!(*context)->state) {
166
+ rb_raise(rb_eRuntimeError, "Digest state not initialized!");
167
+ }
161
168
  }
162
169
 
163
170
  static inline void safe_get_sha3_digest_context(VALUE obj, sha3_digest_context_t **context) {
@@ -170,36 +177,36 @@ static inline void safe_get_sha3_digest_context(VALUE obj, sha3_digest_context_t
170
177
 
171
178
  static inline int is_shake_algorithm(sha3_digest_algorithms alg) { return alg == SHAKE_128 || alg == SHAKE_256; }
172
179
 
173
- int get_hashbit_length(VALUE obj, sha3_digest_algorithms *algorithm) {
174
- if (TYPE(obj) == T_SYMBOL) {
175
- ID symid = SYM2ID(obj);
176
-
177
- if (symid == _sha3_224_id) {
178
- *algorithm = SHA3_224;
179
- return 224;
180
- } else if (symid == _sha3_256_id) {
181
- *algorithm = SHA3_256;
182
- return 256;
183
- } else if (symid == _sha3_384_id) {
184
- *algorithm = SHA3_384;
185
- return 384;
186
- } else if (symid == _sha3_512_id) {
187
- *algorithm = SHA3_512;
188
- return 512;
189
- } else if (symid == _shake_128_id) {
190
- *algorithm = SHAKE_128;
191
- return 128;
192
- } else if (symid == _shake_256_id) {
193
- *algorithm = SHAKE_256;
194
- return 256;
195
- }
196
-
197
- rb_raise(rb_eArgError,
198
- "invalid hash algorithm symbol (should be: :sha3_224, "
199
- ":sha3_256, :sha3_384, :sha3_512, :shake_128, or :shake_256)");
200
- }
201
-
202
- rb_raise(_sha3_digest_error_class, "unknown type value");
180
+ static int get_hashbit_length(VALUE obj, sha3_digest_algorithms *algorithm) {
181
+ if (TYPE(obj) != T_SYMBOL) {
182
+ rb_raise(_sha3_digest_error_class, "hash algorithm must be a symbol");
183
+ }
184
+
185
+ ID symid = SYM2ID(obj);
186
+
187
+ if (symid == _sha3_224_id) {
188
+ *algorithm = SHA3_224;
189
+ return 224;
190
+ } else if (symid == _sha3_256_id) {
191
+ *algorithm = SHA3_256;
192
+ return 256;
193
+ } else if (symid == _sha3_384_id) {
194
+ *algorithm = SHA3_384;
195
+ return 384;
196
+ } else if (symid == _sha3_512_id) {
197
+ *algorithm = SHA3_512;
198
+ return 512;
199
+ } else if (symid == _shake_128_id) {
200
+ *algorithm = SHAKE_128;
201
+ return 128;
202
+ } else if (symid == _shake_256_id) {
203
+ *algorithm = SHAKE_256;
204
+ return 256;
205
+ }
206
+
207
+ rb_raise(rb_eArgError,
208
+ "invalid hash algorithm symbol (should be: :sha3_224, "
209
+ ":sha3_256, :sha3_384, :sha3_512, :shake_128, or :shake_256)");
203
210
 
204
211
  return 0; // Never reached, but silences compiler warnings
205
212
  }
@@ -208,9 +215,10 @@ static void sha3_digest_free_context(void *ptr) {
208
215
  sha3_digest_context_t *context = (sha3_digest_context_t *)ptr;
209
216
  if (context) {
210
217
  if (context->state) {
211
- free(context->state);
218
+ ruby_xfree(context->state);
219
+ context->state = NULL;
212
220
  }
213
- free(context);
221
+ ruby_xfree(context);
214
222
  }
215
223
  }
216
224
 
@@ -245,21 +253,22 @@ static HashReturn keccak_hash_initialize(sha3_digest_context_t *context) {
245
253
  }
246
254
 
247
255
  static VALUE rb_sha3_digest_alloc(VALUE klass) {
248
- sha3_digest_context_t *context = (sha3_digest_context_t *)malloc(sizeof(sha3_digest_context_t));
256
+ sha3_digest_context_t *context = RB_ALLOC(sha3_digest_context_t);
249
257
  if (!context) {
250
258
  rb_raise(_sha3_digest_error_class, "failed to allocate object memory");
251
259
  }
252
260
 
253
- context->state = (Keccak_HashInstance *)calloc(1, sizeof(Keccak_HashInstance));
261
+ context->state = RB_ALLOC(Keccak_HashInstance);
254
262
  if (!context->state) {
255
- sha3_digest_free_context(context);
263
+ ruby_xfree(context);
256
264
  rb_raise(_sha3_digest_error_class, "failed to allocate state memory");
257
265
  }
266
+ memset(context->state, 0, sizeof(*context->state));
258
267
 
259
- VALUE obj = TypedData_Wrap_Struct(klass, &sha3_digest_data_type_t, context);
260
268
  context->hashbitlen = 0;
261
- context->algorithm = SHA3_256; // Default algorithm
269
+ context->algorithm = SHA3_256;
262
270
 
271
+ VALUE obj = TypedData_Wrap_Struct(klass, &sha3_digest_data_type, context);
263
272
  return obj;
264
273
  }
265
274
 
@@ -342,7 +351,13 @@ static VALUE rb_sha3_digest_update(VALUE self, VALUE data) {
342
351
  rb_raise(_sha3_digest_error_class, "cannot update with NULL data");
343
352
  }
344
353
 
345
- dlen = (RSTRING_LEN(data) * 8);
354
+ // Prevent integer overflow and validate size
355
+ size_t data_len = RSTRING_LEN(data);
356
+ if (data_len > SIZE_MAX / 8) {
357
+ rb_raise(_sha3_digest_error_class, "data too large (exceeds maximum size)");
358
+ }
359
+
360
+ dlen = (data_len * 8);
346
361
 
347
362
  if (Keccak_HashUpdate(context->state, (BitSequence *)RSTRING_PTR(data), dlen) != KECCAK_SUCCESS) {
348
363
  rb_raise(_sha3_digest_error_class, "failed to update hash data");
@@ -374,6 +389,10 @@ static VALUE rb_sha3_digest_reset(VALUE self) {
374
389
  }
375
390
 
376
391
  static int compare_contexts(const sha3_digest_context_t *context1, const sha3_digest_context_t *context2) {
392
+ if (!context1 || !context2 || !context1->state || !context2->state) {
393
+ return 0;
394
+ }
395
+
377
396
  // First check the hashbitlen and algorithm
378
397
  if (context1->hashbitlen != context2->hashbitlen || context1->algorithm != context2->algorithm) {
379
398
  return 0;
@@ -429,7 +448,11 @@ static VALUE rb_sha3_digest_copy(VALUE self, VALUE other) {
429
448
  }
430
449
 
431
450
  safe_get_sha3_digest_context(other, &other_context);
432
- get_sha3_digest_context(self, &context);
451
+ safe_get_sha3_digest_context(self, &context);
452
+
453
+ if (!context || !other_context) {
454
+ rb_raise(_sha3_digest_error_class, "invalid context for copy");
455
+ }
433
456
 
434
457
  context->hashbitlen = other_context->hashbitlen;
435
458
  context->algorithm = other_context->algorithm;
@@ -489,17 +512,17 @@ static VALUE rb_sha3_digest_name(VALUE self) {
489
512
 
490
513
  switch (context->algorithm) {
491
514
  case SHA3_224:
492
- return rb_str_new2("SHA3-224");
515
+ return rb_str_new_cstr("SHA3-224");
493
516
  case SHA3_256:
494
- return rb_str_new2("SHA3-256");
517
+ return rb_str_new_cstr("SHA3-256");
495
518
  case SHA3_384:
496
- return rb_str_new2("SHA3-384");
519
+ return rb_str_new_cstr("SHA3-384");
497
520
  case SHA3_512:
498
- return rb_str_new2("SHA3-512");
521
+ return rb_str_new_cstr("SHA3-512");
499
522
  case SHAKE_128:
500
- return rb_str_new2("SHAKE128");
523
+ return rb_str_new_cstr("SHAKE128");
501
524
  case SHAKE_256:
502
- return rb_str_new2("SHAKE256");
525
+ return rb_str_new_cstr("SHAKE256");
503
526
  default:
504
527
  rb_raise(_sha3_digest_error_class, "unknown algorithm");
505
528
  }
@@ -560,15 +583,19 @@ static VALUE rb_sha3_digest_finish(int argc, VALUE *argv, VALUE self) {
560
583
  static VALUE rb_sha3_digest_squeeze(VALUE self, VALUE length) {
561
584
  sha3_digest_context_t *context;
562
585
  VALUE str, copy;
563
- int output_bytes;
586
+ long output_bytes;
564
587
 
565
588
  Check_Type(length, T_FIXNUM);
566
- output_bytes = NUM2INT(length);
589
+ output_bytes = NUM2LONG(length);
567
590
 
568
591
  if (output_bytes <= 0) {
569
592
  rb_raise(_sha3_digest_error_class, "output length must be positive");
570
593
  }
571
594
 
595
+ if (output_bytes > (1L << 20)) { // Limit to 1MB output
596
+ rb_raise(_sha3_digest_error_class, "output length too large (max 1MB)");
597
+ }
598
+
572
599
  get_sha3_digest_context(self, &context);
573
600
 
574
601
  // Only SHAKE algorithms support arbitrary-length output
@@ -578,6 +605,9 @@ static VALUE rb_sha3_digest_squeeze(VALUE self, VALUE length) {
578
605
 
579
606
  // Create a copy of the digest object to avoid modifying the original
580
607
  copy = rb_obj_clone(self);
608
+ if (NIL_P(copy)) {
609
+ rb_raise(_sha3_digest_error_class, "failed to clone digest object");
610
+ }
581
611
 
582
612
  // Get the sha3_digest_context_t struct from the copy
583
613
  sha3_digest_context_t *context_copy;
@@ -595,6 +625,9 @@ static VALUE rb_sha3_digest_squeeze(VALUE self, VALUE length) {
595
625
  rb_raise(_sha3_digest_error_class, "failed to squeeze output");
596
626
  }
597
627
 
628
+ // Keep `copy` reachable so the GC can't free context_copy->state via rb_str_new above.
629
+ RB_GC_GUARD(copy);
630
+
598
631
  return str;
599
632
  }
600
633
 
@@ -615,7 +648,7 @@ static VALUE rb_sha3_digest_hex_squeeze(VALUE self, VALUE length) {
615
648
  // Get the binary output using the existing squeeze function
616
649
  VALUE bin_str = rb_sha3_digest_squeeze(self, length);
617
650
  // Use Ruby's built-in unpack method to convert to hex
618
- return rb_funcall(bin_str, rb_intern("unpack1"), 1, rb_str_new2("H*"));
651
+ return rb_funcall(bin_str, rb_intern("unpack1"), 1, rb_str_new_cstr("H*"));
619
652
  }
620
653
 
621
654
  static VALUE prepare_shake_output(VALUE self, int argc, VALUE *argv, int hex_output) {
@@ -800,32 +833,26 @@ static VALUE rb_sha3_digest_self_digest(VALUE klass, VALUE name, VALUE data) {
800
833
  * To squeeze a different length, use #hex_squeeze instance method.
801
834
  */
802
835
  static VALUE rb_sha3_digest_self_hexdigest(VALUE klass, VALUE name, VALUE data) {
803
- VALUE args[2];
836
+ VALUE digest;
804
837
 
805
- // Need to add type checking for the data parameter
806
- StringValue(data);
838
+ if (NIL_P(name) || NIL_P(data)) {
839
+ rb_raise(_sha3_digest_error_class, "algorithm name and data cannot be nil");
840
+ }
807
841
 
808
- /* For SHAKE algorithms, we need to handle them differently */
809
- if (TYPE(name) == T_SYMBOL) {
810
- ID symid = SYM2ID(name);
811
- if (symid == _shake_128_id || symid == _shake_256_id) {
812
- /* Create a new digest instance with the specified algorithm */
813
- VALUE digest = rb_class_new_instance(1, &name, klass);
842
+ if (TYPE(name) != T_SYMBOL) {
843
+ rb_raise(_sha3_digest_error_class, "algorithm name must be a symbol");
844
+ }
814
845
 
815
- /* Update it with the data */
816
- rb_sha3_digest_update(digest, data);
846
+ StringValue(data);
817
847
 
818
- /* For SHAKE algorithms, use a default output length based on the security strength */
819
- int output_length = (symid == _shake_128_id) ? 16 : 32; /* 128/8 or 256/8 */
848
+ ID symid = SYM2ID(name);
849
+ digest = rb_class_new_instance(1, &name, klass);
850
+ rb_sha3_digest_update(digest, data);
820
851
 
821
- /* Return the hexadecimal representation of the squeezed output */
822
- return rb_sha3_digest_hex_squeeze(digest, INT2NUM(output_length));
823
- }
852
+ if (symid == _shake_128_id || symid == _shake_256_id) {
853
+ int output_length = (symid == _shake_128_id) ? 16 : 32;
854
+ return rb_sha3_digest_hex_squeeze(digest, INT2NUM(output_length));
824
855
  }
825
856
 
826
- /* Call the superclass method with arguments in reverse order */
827
- args[0] = data;
828
- args[1] = name;
829
-
830
- return rb_call_super(2, args);
857
+ return rb_funcall(digest, rb_intern("hexdigest"), 0);
831
858
  }
data/ext/sha3/kmac.c CHANGED
@@ -1,5 +1,6 @@
1
1
  #include "kmac.h"
2
2
 
3
+ #include "common.h"
3
4
  #include "sha3.h"
4
5
  #include "sp800_185.h"
5
6
 
@@ -42,7 +43,7 @@ static ID _kmac_128_id;
42
43
  static ID _kmac_256_id;
43
44
 
44
45
  /* TypedData structure for sha3_kmac_context_t */
45
- const rb_data_type_t sha3_kmac_data_type_t = {
46
+ const rb_data_type_t sha3_kmac_data_type = {
46
47
  "SHA3::KMAC",
47
48
  {
48
49
  NULL, sha3_kmac_free_context, sha3_kmac_context_size, NULL, /* dcompact field */
@@ -55,7 +56,7 @@ const rb_data_type_t sha3_kmac_data_type_t = {
55
56
  // Helper function to extract context from a Ruby object
56
57
  void get_kmac_context(VALUE obj, sp800_185_context_t **context) {
57
58
  sha3_kmac_context_t *kmac_ctx;
58
- TypedData_Get_Struct(obj, sha3_kmac_context_t, &sha3_kmac_data_type_t, kmac_ctx);
59
+ TypedData_Get_Struct(obj, sha3_kmac_context_t, &sha3_kmac_data_type, kmac_ctx);
59
60
  *context = &kmac_ctx->base;
60
61
  }
61
62
 
@@ -104,25 +105,11 @@ void Init_sha3_kmac(void) {
104
105
  return;
105
106
  }
106
107
 
107
- static void sha3_kmac_free_context(void *ptr) { sp800_185_free_context((sp800_185_context_t *)ptr); }
108
+ /* Use common memory management functions */
109
+ DEFINE_SP800_185_MEMORY_FUNCS(sha3_kmac, sha3_kmac_context_t)
108
110
 
109
- static size_t sha3_kmac_context_size(const void *ptr) {
110
- return sp800_185_context_size((const sp800_185_context_t *)ptr, sizeof(sha3_kmac_context_t));
111
- }
112
-
113
- static VALUE rb_sha3_kmac_alloc(VALUE klass) {
114
- sha3_kmac_context_t *context =
115
- (sha3_kmac_context_t *)sp800_185_alloc_context(sizeof(sha3_kmac_context_t), sizeof(KMAC_Instance));
116
-
117
- if (!context) {
118
- rb_raise(_sha3_kmac_error_class, "failed to allocate memory");
119
- }
120
-
121
- // Create the Ruby object with TypedData - this will automatically handle freeing
122
- VALUE obj = TypedData_Wrap_Struct(klass, &sha3_kmac_data_type_t, context);
123
-
124
- return obj;
125
- }
111
+ /* Use common allocation function */
112
+ DEFINE_SP800_185_ALLOC(sha3_kmac, sha3_kmac_context_t, KMAC_Instance, _sha3_kmac_error_class)
126
113
 
127
114
  /*
128
115
  * :call-seq:
@@ -179,7 +166,7 @@ static VALUE rb_sha3_kmac_init(int argc, VALUE *argv, VALUE self) {
179
166
  }
180
167
 
181
168
  sha3_kmac_context_t *context;
182
- TypedData_Get_Struct(self, sha3_kmac_context_t, &sha3_kmac_data_type_t, context);
169
+ TypedData_Get_Struct(self, sha3_kmac_context_t, &sha3_kmac_data_type, context);
183
170
 
184
171
  // Store the output length in bits
185
172
  context->base.output_length = NUM2ULONG(output_length) * 8;
@@ -229,31 +216,7 @@ static VALUE rb_sha3_kmac_init(int argc, VALUE *argv, VALUE self) {
229
216
  * = example
230
217
  * new_kmac = kmac.dup
231
218
  */
232
- static VALUE rb_sha3_kmac_copy(VALUE self, VALUE other) {
233
- sha3_kmac_context_t *context, *other_context;
234
-
235
- rb_check_frozen(self);
236
- if (self == other) {
237
- return self;
238
- }
239
-
240
- if (!rb_obj_is_kind_of(other, _sha3_kmac_class)) {
241
- rb_raise(rb_eTypeError, "wrong argument (%s)! (expected %s)", rb_obj_classname(other),
242
- rb_class2name(_sha3_kmac_class));
243
- }
244
-
245
- TypedData_Get_Struct(other, sha3_kmac_context_t, &sha3_kmac_data_type_t, other_context);
246
- TypedData_Get_Struct(self, sha3_kmac_context_t, &sha3_kmac_data_type_t, context);
247
-
248
- // Copy the base context attributes
249
- context->base.functions = other_context->base.functions;
250
- context->base.output_length = other_context->base.output_length;
251
-
252
- // Copy the algorithm-specific state
253
- memcpy(context->base.state, other_context->base.state, context->base.functions->state_size);
254
-
255
- return self;
256
- }
219
+ DEFINE_SP800_185_COPY_METHOD(rb_sha3_kmac_copy, sha3_kmac_context_t, sha3_kmac_data_type, _sha3_kmac_class)
257
220
 
258
221
  /*
259
222
  * :call-seq:
@@ -268,13 +231,7 @@ static VALUE rb_sha3_kmac_copy(VALUE self, VALUE other) {
268
231
  * kmac.update("more data")
269
232
  * kmac << "more data" # alias for update
270
233
  */
271
- static VALUE rb_sha3_kmac_update(VALUE self, VALUE data) {
272
- sp800_185_context_t *context;
273
- get_kmac_context(self, &context);
274
- sp800_185_update(context, data);
275
-
276
- return self;
277
- }
234
+ DEFINE_SP800_185_SIMPLE_METHOD(rb_sha3_kmac_update, sp800_185_rb_update, get_kmac_context)
278
235
 
279
236
  /*
280
237
  * :call-seq:
@@ -285,12 +242,7 @@ static VALUE rb_sha3_kmac_update(VALUE self, VALUE data) {
285
242
  * = example
286
243
  * kmac.name #=> "KMAC128" or "KMAC256"
287
244
  */
288
- static VALUE rb_sha3_kmac_name(VALUE self) {
289
- sp800_185_context_t *context;
290
- get_kmac_context(self, &context);
291
-
292
- return rb_str_new2(sp800_185_name(context));
293
- }
245
+ DEFINE_SP800_185_RETURN_METHOD(rb_sha3_kmac_name, sp800_185_rb_name, get_kmac_context)
294
246
 
295
247
  /*
296
248
  * :call-seq:
@@ -304,14 +256,7 @@ static VALUE rb_sha3_kmac_name(VALUE self) {
304
256
  * = example
305
257
  * kmac.finish
306
258
  */
307
- static VALUE rb_sha3_kmac_finish(int argc, VALUE *argv, VALUE self) {
308
- sp800_185_context_t *context;
309
- get_kmac_context(self, &context);
310
-
311
- VALUE output = argc > 0 ? argv[0] : Qnil;
312
-
313
- return sp800_185_finish(context, output);
314
- }
259
+ DEFINE_SP800_185_VARARGS_METHOD(rb_sha3_kmac_finish, sp800_185_rb_finish, get_kmac_context)
315
260
 
316
261
  /*
317
262
  * :call-seq:
@@ -329,14 +274,7 @@ static VALUE rb_sha3_kmac_finish(int argc, VALUE *argv, VALUE self) {
329
274
  * kmac.digest
330
275
  * kmac.digest('final chunk')
331
276
  */
332
- static VALUE rb_sha3_kmac_digest(int argc, VALUE *argv, VALUE self) {
333
- sp800_185_context_t *context;
334
- get_kmac_context(self, &context);
335
-
336
- VALUE data = argc > 0 ? argv[0] : Qnil;
337
-
338
- return sp800_185_digest(context, data);
339
- }
277
+ DEFINE_SP800_185_VARARGS_METHOD(rb_sha3_kmac_digest, sp800_185_rb_digest, get_kmac_context)
340
278
 
341
279
  /*
342
280
  * :call-seq:
@@ -354,14 +292,7 @@ static VALUE rb_sha3_kmac_digest(int argc, VALUE *argv, VALUE self) {
354
292
  * kmac.hexdigest
355
293
  * kmac.hexdigest('final chunk')
356
294
  */
357
- static VALUE rb_sha3_kmac_hexdigest(int argc, VALUE *argv, VALUE self) {
358
- sp800_185_context_t *context;
359
- get_kmac_context(self, &context);
360
-
361
- VALUE data = argc > 0 ? argv[0] : Qnil;
362
-
363
- return sp800_185_hexdigest(context, data);
364
- }
295
+ DEFINE_SP800_185_VARARGS_METHOD(rb_sha3_kmac_hexdigest, sp800_185_rb_hexdigest, get_kmac_context)
365
296
 
366
297
  /*
367
298
  * :call-seq:
@@ -377,12 +308,7 @@ static VALUE rb_sha3_kmac_hexdigest(int argc, VALUE *argv, VALUE self) {
377
308
  * = example
378
309
  * kmac.squeeze(128)
379
310
  */
380
- static VALUE rb_sha3_kmac_squeeze(VALUE self, VALUE length) {
381
- sp800_185_context_t *context;
382
- get_kmac_context(self, &context);
383
-
384
- return sp800_185_squeeze(context, length);
385
- }
311
+ DEFINE_SP800_185_VALUE_METHOD(rb_sha3_kmac_squeeze, sp800_185_rb_squeeze, get_kmac_context)
386
312
 
387
313
  /*
388
314
  * :call-seq:
@@ -398,12 +324,7 @@ static VALUE rb_sha3_kmac_squeeze(VALUE self, VALUE length) {
398
324
  * = example
399
325
  * kmac.hex_squeeze(128)
400
326
  */
401
- static VALUE rb_sha3_kmac_hex_squeeze(VALUE self, VALUE length) {
402
- sp800_185_context_t *context;
403
- get_kmac_context(self, &context);
404
-
405
- return sp800_185_hex_squeeze(context, length);
406
- }
327
+ DEFINE_SP800_185_VALUE_METHOD(rb_sha3_kmac_hex_squeeze, sp800_185_rb_hex_squeeze, get_kmac_context)
407
328
 
408
329
  /*
409
330
  * :call-seq:
@@ -432,7 +353,7 @@ static VALUE rb_sha3_kmac_self_digest(int argc, VALUE *argv, VALUE klass) {
432
353
  rb_scan_args(argc, argv, "41", &algorithm, &data, &output_length, &key, &customization);
433
354
 
434
355
  Check_Type(output_length, T_FIXNUM);
435
- if (!NIL_P(output_length) && output_length <= INT2FIX(0)) {
356
+ if (!NIL_P(output_length) && NUM2INT(output_length) <= 0) {
436
357
  rb_raise(rb_eArgError, "class method digest does not support XOF mode");
437
358
  }
438
359
 
@@ -468,7 +389,7 @@ static VALUE rb_sha3_kmac_self_hexdigest(int argc, VALUE *argv, VALUE klass) {
468
389
  rb_scan_args(argc, argv, "41", &algorithm, &data, &output_length, &key, &customization);
469
390
 
470
391
  Check_Type(output_length, T_FIXNUM);
471
- if (!NIL_P(output_length) && output_length <= INT2FIX(0)) {
392
+ if (!NIL_P(output_length) && NUM2INT(output_length) <= 0) {
472
393
  rb_raise(rb_eArgError, "class method hexdigest does not support XOF mode");
473
394
  }
474
395