/
ciphers.cpp
1402 lines (1247 loc) · 34.3 KB
/
ciphers.cpp
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2002-2014 J Smith <dark.panda@gmail.com>
* Crypto++ copyright (c) 1995-2013 Wei Dai
* See MIT-LICENSE for the extact license
*/
#include "j3way.h"
#include "jaes.h"
#include "jblowfish.h"
#include "jcamellia.h"
#include "jcast128.h"
#include "jcast256.h"
#include "jdes.h"
#include "jdes_ede2.h"
#include "jdes_ede3.h"
#include "jdes_xex3.h"
#include "jdiamond2.h"
#include "jdiamond2lite.h"
#include "jgost.h"
#include "jidea.h"
#include "jmars.h"
#include "jrc2.h"
#include "jrc5.h"
#include "jrc6.h"
#include "jsafer.h"
#include "jserpent.h"
#include "jshacal2.h"
#include "jshark.h"
#include "jskipjack.h"
#include "jsquare.h"
#include "jtea.h"
#include "jtwofish.h"
// stream cipher algorithms:
#include "jarc4.h"
#include "jmarc4.h"
#include "jpanamacipher.h"
#include "jseal.h"
#include "jbasiccipherinfo.h"
#include "jexception.h"
#include "cryptopp_ruby_api.h"
extern void cipher_mark(JBase *c);
extern void cipher_free(JBase *c);
// forward declarations
static CipherEnum cipher_sym_to_const(VALUE cipher);
static ModeEnum mode_sym_to_const(VALUE m);
static PaddingEnum padding_sym_to_const(VALUE p);
static RNGEnum rng_sym_to_const(VALUE rng);
static bool cipher_enabled(CipherEnum cipher);
static void cipher_options(VALUE self, VALUE options);
static JBase* cipher_factory(long algorithm);
static VALUE wrap_cipher_in_ruby(JBase* cipher);
static void cipher_rand_iv(VALUE self, VALUE l);
static string cipher_iv_eq(VALUE self, VALUE iv, bool hex);
static string cipher_iv(VALUE self, bool hex);
static string cipher_plaintext_eq(VALUE self, VALUE plaintext, bool hex);
static string cipher_plaintext(VALUE self, bool hex);
static string cipher_ciphertext_eq(VALUE self, VALUE ciphertext, bool hex);
static string cipher_ciphertext(VALUE self, bool hex);
static string cipher_key_eq(VALUE self, VALUE key, bool hex);
static string cipher_key(VALUE self, bool hex);
static VALUE cipher_encrypt(VALUE self, bool hex);
static VALUE cipher_decrypt(VALUE self, bool hex);
static CipherEnum cipher_sym_to_const(VALUE c)
{
CipherEnum cipher = UNKNOWN_CIPHER;
ID id = SYM2ID(c);
if (id == rb_intern("rijndael")) {
cipher = AES_CIPHER;
}
else if (id == rb_intern("panama")) {
cipher = PANAMA_CIPHER;
}
else if (id == rb_intern("seal")) {
cipher = SEAL_CIPHER;
}
# define CIPHER_ALGORITHM_X_FORCE 1
# define CIPHER_ALGORITHM_X(klass, r, c, s) \
else if (id == rb_intern(# s)) { \
cipher = r ## _CIPHER; \
}
# include "defs/ciphers.def"
return cipher;
}
static ModeEnum mode_sym_to_const(VALUE m)
{
ModeEnum mode = UNKNOWN_MODE;
ID id = SYM2ID(m);
if (id == rb_intern("counter")) {
mode = CTR_MODE;
}
# define BLOCK_MODE_X(c, s) \
else if (id == rb_intern(# s)) { \
mode = c ## _MODE; \
}
# include "defs/block_modes.def"
return mode;
}
static PaddingEnum padding_sym_to_const(VALUE p)
{
PaddingEnum padding = UNKNOWN_PADDING;
ID id = SYM2ID(p);
if (id == rb_intern("none")) {
padding = NO_PADDING;
}
else if (id == rb_intern("zeroes")) {
padding = ZEROS_PADDING;
}
else if (id == rb_intern("one_and_zeroes")) {
padding = ONE_AND_ZEROS_PADDING;
}
# define PADDING_X(c, s) \
else if (id == rb_intern(# s)) { \
padding = c ## _PADDING; \
}
# include "defs/paddings.def"
return padding;
}
static RNGEnum rng_sym_to_const(VALUE r)
{
RNGEnum rng = UNKNOWN_RNG;
ID id = SYM2ID(r);
if (false) {
// no-op so we can use our x-macro
}
# define RNG_X(c, s) \
else if (id == rb_intern(# s)) { \
rng = c ## _RNG; \
}
# include "defs/rngs.def"
return rng;
}
/* See if a cipher algorithm is enabled. */
static bool cipher_enabled(CipherEnum cipher)
{
switch (cipher) {
# define CIPHER_ALGORITHM_X(klass, r, c, s) \
case r ##_CIPHER:
# include "defs/ciphers.def"
return true;
}
return false;
}
/* Figure out options for a cipher. We only check for Symbols, not Strings. */
static void cipher_options(VALUE self, VALUE options)
{
Check_Type(options, T_HASH);
{
VALUE plaintext = rb_hash_aref(options, ID2SYM(rb_intern("plaintext")));
VALUE plaintext_hex = rb_hash_aref(options, ID2SYM(rb_intern("plaintext_hex")));
if (!NIL_P(plaintext) && !NIL_P(plaintext_hex)) {
rb_raise(rb_eCryptoPP_Error, "can't set both plaintext and plaintext_hex in options");
}
else if (!NIL_P(plaintext)) {
cipher_plaintext_eq(self, plaintext, false);
}
else if (!NIL_P(plaintext_hex)) {
cipher_plaintext_eq(self, plaintext_hex, true);
}
}
{
VALUE ciphertext = rb_hash_aref(options, ID2SYM(rb_intern("ciphertext")));
VALUE ciphertext_hex = rb_hash_aref(options, ID2SYM(rb_intern("ciphertext_hex")));
if (!NIL_P(ciphertext) && !NIL_P(ciphertext_hex)) {
rb_raise(rb_eCryptoPP_Error, "can't set both ciphertext and ciphertext_hex in options");
}
else if (!NIL_P(ciphertext)) {
cipher_ciphertext_eq(self, ciphertext, false);
}
else if (!NIL_P(ciphertext_hex)) {
cipher_ciphertext_eq(self, ciphertext_hex, true);
}
}
{
VALUE key = rb_hash_aref(options, ID2SYM(rb_intern("key")));
VALUE key_hex = rb_hash_aref(options, ID2SYM(rb_intern("key_hex")));
if (!NIL_P(key) && !NIL_P(key_hex)) {
rb_raise(rb_eCryptoPP_Error, "can't set both key and key_hex in options");
}
else if (!NIL_P(key)) {
cipher_key_eq(self, key, false);
}
else if (!NIL_P(key_hex)) {
cipher_key_eq(self, key_hex, true);
}
}
{
VALUE key_length = rb_hash_aref(options, ID2SYM(rb_intern("key_length")));
if (!NIL_P(key_length)) {
rb_cipher_key_length_eq(self, key_length);
}
}
# if ENABLED_RC2_CIPHER
{
VALUE effective_key_length = rb_hash_aref(options, ID2SYM(rb_intern("effective_key_length")));
if (!NIL_P(effective_key_length)) {
rb_cipher_effective_key_length_eq(self, effective_key_length);
}
}
# endif
{
VALUE rounds = rb_hash_aref(options, ID2SYM(rb_intern("rounds")));
if (!NIL_P(rounds)) {
rb_cipher_rounds_eq(self, rounds);
}
}
{
VALUE rng = rb_hash_aref(options, ID2SYM(rb_intern("rng")));
if (!NIL_P(rng)) {
rb_cipher_rng_eq(self, rng);
}
}
{
VALUE rand_iv = rb_hash_aref(options, ID2SYM(rb_intern("rand_iv")));
VALUE iv = rb_hash_aref(options, ID2SYM(rb_intern("iv")));
VALUE iv_hex = rb_hash_aref(options, ID2SYM(rb_intern("iv_hex")));
if (!NIL_P(rand_iv) && (!NIL_P(iv) || !NIL_P(iv_hex))) {
rb_raise(rb_eCryptoPP_Error, "can't set both rand_iv and iv or iv_hex in options");
}
else if (!NIL_P(iv) && !NIL_P(iv_hex)) {
rb_raise(rb_eCryptoPP_Error, "can't set both iv and iv_hex in options");
}
else if (!NIL_P(rand_iv)) {
cipher_rand_iv(self, rand_iv);
}
else if (!NIL_P(iv)) {
cipher_iv_eq(self, iv, false);
}
else if (!NIL_P(iv_hex)) {
cipher_iv_eq(self, iv_hex, true);
}
}
{
VALUE block_mode = rb_hash_aref(options, ID2SYM(rb_intern("block_mode")));
if (!NIL_P(block_mode)) {
rb_cipher_block_mode_eq(self, block_mode);
}
}
{
VALUE padding = rb_hash_aref(options, ID2SYM(rb_intern("padding")));
if (!NIL_P(padding)) {
rb_cipher_padding_eq(self, padding);
}
}
}
/* Creates a new Crypto++ cipher. May throw a JException if no suitable cipher
* is found. May throw a JException if no suitable cipher is found. */
static JBase* cipher_factory(VALUE algorithm)
{
try {
switch (cipher_sym_to_const(algorithm)) {
default:
throw JException("the requested algorithm cannot be found");
break;
# define CIPHER_ALGORITHM_X(klass, r, c, s) \
case r ## _CIPHER: \
return static_cast<c*>(new c);
# include "defs/ciphers.def"
}
}
catch (Exception e) {
throw JException("Crypto++ exception: " + e.GetWhat());
}
}
/* Wraps a Cipher object into a Ruby object. May throw a JException if no
* suitable Cipher is found. */
static VALUE wrap_cipher_in_ruby(JBase* cipher)
{
const type_info& info = typeid(*cipher);
# define CIPHER_ALGORITHM_X(klass, r, c, s) \
if (info == typeid(c)) { \
return Data_Wrap_Struct(rb_cCryptoPP_Cipher_## r, cipher_mark, cipher_free, cipher); \
} \
else
# include "defs/ciphers.def"
{
throw JException("the requested algorithm has been disabled");
}
}
/**
* call-seq:
* cipher_factory(algorithm) => Cipher
* cipher_factory(algorithm, options) => Cipher
*
* Creates a new Cipher object.
*
* See the Cipher class for available options.
*/
VALUE rb_module_cipher_factory(int argc, VALUE *argv, VALUE self)
{
VALUE algorithm, options, retval;
rb_scan_args(argc, argv, "11", &algorithm, &options);
try {
retval = wrap_cipher_in_ruby(cipher_factory(algorithm));
}
catch (Exception& e) {
rb_raise(rb_eCryptoPP_Error, "%s", e.GetWhat().c_str());
}
if (!NIL_P(options)) {
cipher_options(retval, options);
}
return retval;
}
#define CIPHER_ALGORITHM_X(klass, r, n, s) \
VALUE rb_cipher_ ## r ##_new(int argc, VALUE *argv, VALUE self) \
{ \
VALUE options, retval; \
rb_scan_args(argc, argv, "01", &options); \
try { \
retval = wrap_cipher_in_ruby(cipher_factory(ID2SYM(rb_intern(# s)))); \
} \
catch (Exception& e) { \
rb_raise(rb_eCryptoPP_Error, "%s", e.GetWhat().c_str()); \
} \
if (!NIL_P(options)) { \
cipher_options(retval, options); \
} \
return retval; \
}
#include "defs/ciphers.def"
/* Creates a random initialization vector on the cipher. */
static void cipher_rand_iv(VALUE self, VALUE l)
{
JBase *cipher = NULL;
unsigned int length = NUM2UINT(rb_funcall(l, rb_intern("to_i"), 0));
Data_Get_Struct(self, JBase, cipher);
cipher->setRandIV(length);
}
/**
* call-seq:
* rand_iv(length) => nil
*
* Sets a random initialization vector on the Cipher. This method uses the
* random number generator set on the Cipher to produce the IV, so be sure to
* set the RNG before you try creating a random IV.
*
* The random IV created will is returned in binary.
*/
VALUE rb_cipher_rand_iv(VALUE self, VALUE l)
{
JBase *cipher = NULL;
unsigned int length = NUM2UINT(rb_funcall(l, rb_intern("to_i"), 0));
Data_Get_Struct(self, JBase, cipher);
cipher->setRandIV(length);
return l;
}
/* Sets an IV on the cipher. */
static string cipher_iv_eq(VALUE self, VALUE iv, bool hex)
{
JBase *cipher = NULL;
Check_Type(iv, T_STRING);
Data_Get_Struct(self, JBase, cipher);
cipher->setIV(string(StringValuePtr(iv), RSTRING_LEN(iv)), hex);
return cipher->getIV(hex);
}
/**
* call-seq:
* iv=(iv) => String
*
* Set an initialization vector on the Cipher.
*/
VALUE rb_cipher_iv_eq(VALUE self, VALUE iv)
{
cipher_iv_eq(self, iv, false);
return iv;
}
/**
* call-seq:
* iv_hex=(iv) => String
*
* Set an initialization vector on the Cipher. This method uses hex data and
* returns the IV as set.
*/
VALUE rb_cipher_iv_hex_eq(VALUE self, VALUE iv)
{
cipher_iv_eq(self, iv, true);
return iv;
}
/* Gets the IV. */
static string cipher_iv(VALUE self, bool hex)
{
JBase *cipher = NULL;
Data_Get_Struct(self, JBase, cipher);
return cipher->getIV(hex);
}
/**
* call-seq:
* iv => String
*
* Returns the Cipher's IV in binary.
*/
VALUE rb_cipher_iv(VALUE self)
{
string retval = cipher_iv(self, false);
return rb_tainted_str_new(retval.data(), retval.length());
}
/**
* call-seq:
* iv => String
*
* Returns the Cipher's IV in hex.
*/
VALUE rb_cipher_iv_hex(VALUE self)
{
string retval = cipher_iv(self, true);
return rb_tainted_str_new(retval.data(), retval.length());
}
/**
* call-seq:
* block_mode=(mode) => Symbol
*
* Set the block mode on block ciphers. Returns the mode as set and may raise
* a CryptoPPError if the mode is invalid or you are using a stream cipher.
*/
VALUE rb_cipher_block_mode_eq(VALUE self, VALUE m)
{
JBase *cipher = NULL;
ModeEnum mode = mode_sym_to_const(m);
if (!VALID_MODE(mode)) {
rb_raise(rb_eCryptoPP_Error, "invalid cipher mode");
}
Data_Get_Struct(self, JBase, cipher);
if (IS_STREAM_CIPHER(cipher->getCipherType())) {
rb_raise(rb_eCryptoPP_Error, "can't set mode on stream ciphers");
}
else {
((JCipher*) cipher)->setMode(mode);
return m;
}
}
/**
* call-seq:
* block_mode => Fixnum
*
* Get the block mode. Returns <tt>nil</tt> if you try to use this on a stream
* cipher.
*/
VALUE rb_cipher_block_mode(VALUE self)
{
JBase *cipher = NULL;
Data_Get_Struct(self, JBase, cipher);
ModeEnum mode = ((JCipher*) cipher)->getMode();
if (IS_STREAM_CIPHER(cipher->getCipherType())) {
return Qnil;
}
# define BLOCK_MODE_X(c, s) \
else if (mode == c ## _MODE) { \
return ID2SYM(rb_intern(# s)); \
}
# include "defs/block_modes.def"
}
/**
* call-seq:
* padding=(padding) => Symbol
*
* Set the padding on block ciphers.
*
* Note that not all block cipher modes can use all of the padding types.
* A CryptoPPError will be raised if you try to set an invalid padding. Also
* note that the padding should be set <i>AFTER</i> the block mode, as setting
* the mode causes the padding to revert to its default setting.
*
* Returns the padding as set.
*/
VALUE rb_cipher_padding_eq(VALUE self, VALUE p)
{
JBase *cipher = NULL;
PaddingEnum padding = padding_sym_to_const(p);
if (!VALID_PADDING(padding)) {
rb_raise(rb_eCryptoPP_Error, "invalid cipher padding");
}
Data_Get_Struct(self, JBase, cipher);
if (IS_STREAM_CIPHER(cipher->getCipherType())) {
rb_raise(rb_eCryptoPP_Error, "can't set padding on stream ciphers");
}
else {
((JCipher*) cipher)->setPadding(padding);
if (((JCipher*) cipher)->getPadding() != padding) {
rb_raise(rb_eCryptoPP_Error, "Padding '%s' cannot be used with mode '%s'", JCipher::getPaddingName(padding).c_str(), ((JCipher*) cipher)->getModeName().c_str());
}
else {
return p;
}
}
}
/**
* call-seq:
* padding => Fixnum
*
* Get the cipher padding being used. Returns <tt>nil</tt> if you try to use
* this on a stream cipher.
*/
VALUE rb_cipher_padding(VALUE self)
{
JBase *cipher = NULL;
Data_Get_Struct(self, JBase, cipher);
PaddingEnum padding = ((JCipher*) cipher)->getPadding();
if (IS_STREAM_CIPHER(cipher->getCipherType())) {
return Qnil;
}
# define PADDING_X(c, s) \
else if (padding == c ## _PADDING) { \
return ID2SYM(rb_intern(# s)); \
}
# include "defs/paddings.def"
}
/**
* call-seq:
* rng=(rng) => Symbol
*
* Set the random number generator to use for IVs. A CryptoPPError will be
* raised if an RNG is not available on the system.
*
* RNGs are used to create random initialization vectors using
* <tt>rand_iv</tt>. The default is a non-blocking RNG, such as
* <tt>/dev/urandom</tt> on UNIX-alikes or CryptoAPI on Microsoft systems.
*/
VALUE rb_cipher_rng_eq(VALUE self, VALUE r)
{
JBase *cipher = NULL;
RNGEnum rng = rng_sym_to_const(r);
if (!VALID_RNG(rng)) {
rb_raise(rb_eCryptoPP_Error, "invalid cipher RNG");
}
Data_Get_Struct(self, JBase, cipher);
((JCipher*) cipher)->setRNG(rng);
if (((JCipher*) cipher)->getRNG() != rng) {
rb_raise(rb_eCryptoPP_Error, "RNG '%s' is unavailable", JBase::getRNGName(rng).c_str());
}
else {
return r;
}
}
/**
* call-seq:
* rng => Fixnum
*
* Get the RNG being used.
*/
VALUE rb_cipher_rng(VALUE self)
{
JBase *cipher = NULL;
Data_Get_Struct(self, JBase, cipher);
RNGEnum rng = ((JCipher*) cipher)->getRNG();
if (false) {
// no-op so we can use our x-macro
}
# define RNG_X(c, s) \
else if (rng == c ## _RNG) { \
return ID2SYM(rb_intern(# s)); \
}
# include "defs/rngs.def"
}
/* Set the plaintext. */
static string cipher_plaintext_eq(VALUE self, VALUE plaintext, bool hex)
{
JBase *cipher = NULL;
Check_Type(plaintext, T_STRING);
Data_Get_Struct(self, JBase, cipher);
cipher->setPlaintext(string(StringValuePtr(plaintext), RSTRING_LEN(plaintext)), hex);
return cipher->getPlaintext(hex);
}
/**
* call-seq:
* plaintext=(string) => String
*
* Sets the plaintext on the Cipher in binary and returns the same.
*/
VALUE rb_cipher_plaintext_eq(VALUE self, VALUE plaintext)
{
cipher_plaintext_eq(self, plaintext, false);
return plaintext;
}
/**
* call-seq:
* plaintext_hex=(string) => String
*
* Sets the plaintext on the Cipher in hex and returns the same.
*/
VALUE rb_cipher_plaintext_hex_eq(VALUE self, VALUE plaintext)
{
cipher_plaintext_eq(self, plaintext, true);
return plaintext;
}
/* Get the plaintext. */
static string cipher_plaintext(VALUE self, bool hex)
{
JBase *cipher = NULL;
Data_Get_Struct(self, JBase, cipher);
return cipher->getPlaintext(hex);
}
/**
* call-seq:
* plaintext => String
*
* Gets the plaintext from the Cipher in binary.
*/
VALUE rb_cipher_plaintext(VALUE self)
{
string retval = cipher_plaintext(self, false);
return rb_tainted_str_new(retval.data(), retval.length());
}
/**
* call-seq:
* plaintext_hex => String
*
* Gets the plaintext from the Cipher in hex.
*/
VALUE rb_cipher_plaintext_hex(VALUE self)
{
string retval = cipher_plaintext(self, true);
return rb_tainted_str_new(retval.data(), retval.length());
}
/* Set the ciphertext. */
static string cipher_ciphertext_eq(VALUE self, VALUE ciphertext, bool hex)
{
JBase *cipher = NULL;
Check_Type(ciphertext, T_STRING);
Data_Get_Struct(self, JBase, cipher);
cipher->setCiphertext(string(StringValuePtr(ciphertext), RSTRING_LEN(ciphertext)), hex);
return cipher->getCiphertext(hex);
}
/**
* call-seq:
* ciphertext=(string) => String
*
* Sets the ciphertext on the Cipher in binary and returns the same.
*/
VALUE rb_cipher_ciphertext_eq(VALUE self, VALUE ciphertext)
{
cipher_ciphertext_eq(self, ciphertext, false);
return ciphertext;
}
/**
* call-seq:
* ciphertext_hex=(string) => String
*
* Sets the ciphertext on the Cipher in hex and returns the same.
*/
VALUE rb_cipher_ciphertext_hex_eq(VALUE self, VALUE ciphertext)
{
cipher_ciphertext_eq(self, ciphertext, true);
return ciphertext;
}
/* Get the ciphertext. */
static string cipher_ciphertext(VALUE self, bool hex)
{
JBase *cipher = NULL;
Data_Get_Struct(self, JBase, cipher);
return cipher->getCiphertext(hex);
}
/**
* call-seq:
* ciphertext => String
*
* Gets the ciphertext from the Cipher in binary.
*/
VALUE rb_cipher_ciphertext(VALUE self)
{
string retval = cipher_ciphertext(self, false);
return rb_tainted_str_new(retval.data(), retval.length());
}
/**
* call-seq:
* ciphertext_hex => String
*
* Gets the ciphertext from the Cipher in hex.
*/
VALUE rb_cipher_ciphertext_hex(VALUE self)
{
string retval = cipher_ciphertext(self, true);
return rb_tainted_str_new(retval.data(), retval.length());
}
/* Set the key. The true length of the key might not be what you expect,
* as different algorithms behave differently, i.e. 3Way has a fixed keylength
* of 12 bytes, while Blowfish can use keys of 1 to 72 bytes. */
static string cipher_key_eq(VALUE self, VALUE key, bool hex)
{
JBase *cipher = NULL;
Check_Type(key, T_STRING);
Data_Get_Struct(self, JBase, cipher);
cipher->setKey(string(StringValuePtr(key), RSTRING_LEN(key)), hex);
return cipher->getKey(hex);
}
/**
* call-seq:
* key=(string) => String
*
* Sets the key on the Cipher in binary and returns the same. Note that the
* key try to set may be truncated or padded depending on its length. Some
* ciphers have fixed key lengths while others have specific requirements on
* their size. For instance, the Threeway cipher has a fixed key length of 12
* bytes, while Blowfish can use keys of 1 to 72 bytes.
*
* When the key being used is shorter than an allowed key length, the key
* will be padded with \0's before being set. When the key is longer than
* an allowed key length, the key will be truncated before being set.
*
* In both cases, this happens automatically. You should check the actual
* key that is set using <tt>#key</tt>.
*/
VALUE rb_cipher_key_eq(VALUE self, VALUE key)
{
cipher_key_eq(self, key, false);
return key;
}
/**
* call-seq:
* key_hex=(string) => String
*
* Sets the key on the Cipher in hex and returns the same. Note that the
* key try to set may be truncated or padded depending on its length. Some
* ciphers have fixed key lengths while others have specific requirements on
* their size. For instance, the Threeway cipher has a fixed key length of 12
* bytes, while Blowfish can use keys of 1 to 72 bytes.
*
* When the key being used is shorter than an allowed key length, the key
* will be padded with \0's before being set. When the key is longer than
* an allowed key length, the key will be truncated before being set.
*
* In both cases, this happens automatically. You should check the actual
* key that is set using <tt>#key</tt>.
*/
VALUE rb_cipher_key_hex_eq(VALUE self, VALUE key)
{
cipher_key_eq(self, key, true);
return key;
}
/* Get the key. */
static string cipher_key(VALUE self, bool hex)
{
JBase *cipher = NULL;
Data_Get_Struct(self, JBase, cipher);
return cipher->getKey(hex);
}
/**
* call-seq:
* key => String
*
* Returns the key set on the Cipher in binary.
*/
VALUE rb_cipher_key(VALUE self)
{
string retval = cipher_key(self, false);
return rb_tainted_str_new(retval.data(), retval.length());
}
/**
* call-seq:
* key => String
*
* Returns the key set on the Cipher in hex.
*/
VALUE rb_cipher_key_hex(VALUE self)
{
string retval = cipher_key(self, true);
return rb_tainted_str_new(retval.data(), retval.length());
}
/**
* call-seq:
* key_length=(length) => Fixnum
*
* Sets the key length. Some ciphers require rather specific key lengths,
* and if the key length you attempt to set is invalid, an exception will
* be thrown. The key length being set is set in terms of bytes in binary, not
* hex characters.
*/
VALUE rb_cipher_key_length_eq(VALUE self, VALUE l)
{
JBase *cipher = NULL;
unsigned int length = NUM2UINT(rb_funcall(l, rb_intern("to_i"), 0));
Data_Get_Struct(self, JBase, cipher);
cipher->setKeylength(length);
if (cipher->getKeylength() != length) {
rb_raise(rb_eCryptoPP_Error, "tried to set a key length of %d but %d was used", length, cipher->getKeylength());
}
else {
return l;
}
}
/**
* call-seq:
* key_length => Fixnum
*
* Gets the key length. The key length being returned in terms of bytes
* in binary, not hex characters.
*/
VALUE rb_cipher_key_length(VALUE self)
{
JBase *cipher = NULL;
Data_Get_Struct(self, JBase, cipher);
return rb_fix_new(cipher->getKeylength());
}
/**
* Returns the default key length.
*/
VALUE rb_cipher_default_key_length(VALUE self)
{
JBase *cipher = NULL;
Data_Get_Struct(self, JBase, cipher);
return rb_fix_new(cipher->getDefaultKeylength());
}
/**
* Returns the minimum key length.
*/
VALUE rb_cipher_min_key_length(VALUE self)
{
JBase *cipher = NULL;
Data_Get_Struct(self, JBase, cipher);
return rb_fix_new(cipher->getMinKeylength());
}
/**
* Returns the maximum key length.
*/
VALUE rb_cipher_max_key_length(VALUE self)
{
JBase *cipher = NULL;
Data_Get_Struct(self, JBase, cipher);
return rb_fix_new(cipher->getMaxKeylength());
}
/**
* Returns the multiplier used for the key length.
*/
VALUE rb_cipher_mult_key_length(VALUE self)
{
JBase *cipher = NULL;
Data_Get_Struct(self, JBase, cipher);
return rb_fix_new(cipher->getMultKeylength());
}
/**
* Returns the closest valid key length without actually setting it.
*/
VALUE rb_cipher_valid_key_length(VALUE self, VALUE l)
{
JBase *cipher = NULL;
unsigned int length = NUM2UINT(l);
Data_Get_Struct(self, JBase, cipher);
return rb_fix_new(cipher->getValidKeylength(length));
}
#if ENABLED_RC2_CIPHER
/**
* call-seq:
* effect_key_length=(length) => Fixnum
*
* Set the effective keylength on the RC2 algorithm. This function can
* only be used with RC2. Returns the actual effective keylength set. The
* default is 1024, which is also the maximum.
*/
VALUE rb_cipher_effective_key_length_eq(VALUE self, VALUE l)
{
JBase *cipher = NULL;
unsigned int length = NUM2UINT(rb_funcall(l, rb_intern("to_i"), 0));
Data_Get_Struct(self, JBase, cipher);
if (cipher->getCipherType() != RC2_CIPHER) {
rb_raise(rb_eCryptoPP_Error, "effective key lengths can only be used with the RC2 cipher");
}
else {
((JRC2*) cipher)->setEffectiveKeylength(length);
if (((JRC2*) cipher)->getEffectiveKeylength() != length) {
rb_raise(rb_eCryptoPP_Error, "tried to set an effective key length of %d but %d was used", length, ((JRC2*) cipher)->getEffectiveKeylength());
}
else {
return l;
}
}
}
/**
* Gets the key length. The key length being returned in terms of bytes in
* binary, not hex characters.
*/
VALUE rb_cipher_effective_key_length(VALUE self)
{
JBase *cipher = NULL;
Data_Get_Struct(self, JBase, cipher);
return rb_fix_new(((JRC2*) cipher)->getEffectiveKeylength());
}
#endif
/**
* call-seq:
* block_size => Fixnum
*
* Gets the block size.
*/
VALUE rb_cipher_block_size(VALUE self)
{
JBase *cipher = NULL;
Data_Get_Struct(self, JBase, cipher);
return rb_fix_new(cipher->getBlockSize());
}
/**
* call-seq:
* rounds=(rounds) => Fixnum
*
* Sets the number of rounds to perform on block ciphers. Some block ciphers