Class: CryptoPP::Cipher

Inherits:
Object
  • Object
show all
Defined in:
ext/cryptopp.cpp

Instance Method Summary collapse

Instance Method Details

#algorithm_nameString

Returns the name of the cipher algorithm being used.

Returns:

  • (String)


1217
1218
1219
1220
1221
1222
# File 'ext/ciphers.cpp', line 1217

VALUE rb_cipher_algorithm_name(VALUE self)
{
  JBase *cipher = NULL;
  Data_Get_Struct(self, JBase, cipher);
  return rb_tainted_str_new2(cipher->getCipherName().c_str());
}

#block_modeFixnum

Get the block mode. Returns nil if you try to use this on a stream cipher.

Returns:

  • (Fixnum)


493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'ext/ciphers.cpp', line 493

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"
}

#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.

Returns:

  • (Symbol)


467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'ext/ciphers.cpp', line 467

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;
  }
}

#block_mode_nameString

Returns the name of the mode being used.

Returns:

  • (String)


1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
# File 'ext/ciphers.cpp', line 1243

VALUE rb_cipher_block_mode_name(VALUE self)
{
  JBase *cipher = NULL;
  Data_Get_Struct(self, JBase, cipher);
  if (IS_STREAM_CIPHER(cipher->getCipherType())) {
    return Qnil;
  }
  else {
    return rb_tainted_str_new2(JCipher::getModeName(((JCipher*) cipher)->getMode()).c_str());
  }
}

#block_sizeFixnum

Gets the block size.

Returns:

  • (Fixnum)


988
989
990
991
992
993
# File 'ext/ciphers.cpp', line 988

VALUE rb_cipher_block_size(VALUE self)
{
  JBase *cipher = NULL;
  Data_Get_Struct(self, JBase, cipher);
  return rb_fix_new(cipher->getBlockSize());
}

#cipher_typeSymbol

Returns the type of cipher being used as a Symbol.

Returns:

  • (Symbol)


1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
# File 'ext/ciphers.cpp', line 1320

VALUE rb_cipher_cipher_type(VALUE self)
{
  JBase *cipher = NULL;
  Data_Get_Struct(self, JBase, cipher);

  switch (cipher->getCipherType()) {
#    define CIPHER_ALGORITHM_X(klass, r, c, s) \
      case r ## _CIPHER: \
        return ID2SYM(rb_intern(# s));
#    include "defs/ciphers.def"

    default:
      return Qnil;
  }
}

#ciphertextString

Gets the ciphertext from the Cipher in binary.

Returns:

  • (String)


738
739
740
741
742
# File 'ext/ciphers.cpp', line 738

VALUE rb_cipher_ciphertext(VALUE self)
{
  string retval = cipher_ciphertext(self, false);
  return rb_tainted_str_new(retval.data(), retval.length());
}

#ciphertext=(string) ⇒ String

Sets the ciphertext on the Cipher in binary and returns the same.

Returns:

  • (String)


705
706
707
708
709
# File 'ext/ciphers.cpp', line 705

VALUE rb_cipher_ciphertext_eq(VALUE self, VALUE ciphertext)
{
  cipher_ciphertext_eq(self, ciphertext, false);
  return ciphertext;
}

#ciphertext_hexString

Gets the ciphertext from the Cipher in hex.

Returns:

  • (String)


750
751
752
753
754
# File 'ext/ciphers.cpp', line 750

VALUE rb_cipher_ciphertext_hex(VALUE self)
{
  string retval = cipher_ciphertext(self, true);
  return rb_tainted_str_new(retval.data(), retval.length());
}

#ciphertext_hex=(string) ⇒ String

Sets the ciphertext on the Cipher in hex and returns the same.

Returns:

  • (String)


717
718
719
720
721
# File 'ext/ciphers.cpp', line 717

VALUE rb_cipher_ciphertext_hex_eq(VALUE self, VALUE ciphertext)
{
  cipher_ciphertext_eq(self, ciphertext, true);
  return ciphertext;
}

#decryptString

Decrypt the ciphertext using the options set on the Cipher. This method will return the plaintext in binary. The raw plaintext will always be available through the plaintext and plaintext_hex methods afterwards.

Returns:

  • (String)


1113
1114
1115
1116
# File 'ext/ciphers.cpp', line 1113

VALUE rb_cipher_decrypt(VALUE self)
{
  return cipher_decrypt(self, false);
}

#decrypt_hexString

Decrypt the ciphertext using the options set on the Cipher. This method will return the plaintext in hex. The raw plaintext will always be available through the plaintext and plaintext_hex methods afterwards.

Returns:

  • (String)


1126
1127
1128
1129
# File 'ext/ciphers.cpp', line 1126

VALUE rb_cipher_decrypt_hex(VALUE self)
{
  return cipher_decrypt(self, true);
}

#decrypt_iotrue

Decrypts a Ruby IO object and spits the result into another one. You can use any sort of Ruby object as long as it implements eof?, read, write and flush.

Examples:

cipher.decrypt_io(File.open("http://example.com/"), File.open("test.out", 'w'))

output = StringIO.new
cipher.decrypt_io(File.open('test.enc'), output)

Returns:

  • (true)


1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
# File 'ext/ciphers.cpp', line 1176

VALUE rb_cipher_decrypt_io(VALUE self, VALUE in, VALUE out)
{
  JBase *cipher = NULL;
  Data_Get_Struct(self, JBase, cipher);
  try {
    cipher->decryptRubyIO(&in, &out);
    return Qtrue;
  }
  catch (Exception e) {
    rb_raise(rb_eCryptoPP_Error, "Crypto++ exception: %s", e.GetWhat().c_str());
  }
}

#default_key_lengthObject

Returns the default key length.



891
892
893
894
895
896
# File 'ext/ciphers.cpp', line 891

VALUE rb_cipher_default_key_length(VALUE self)
{
  JBase *cipher = NULL;
  Data_Get_Struct(self, JBase, cipher);
  return rb_fix_new(cipher->getDefaultKeylength());
}

#encryptString

Encrypt the plaintext using the options set on the Cipher. This method will return the ciphertext in binary. The raw ciphertext will always be available through the ciphertext and ciphertext_hex afterwards.

Returns:

  • (String)


1068
1069
1070
1071
# File 'ext/ciphers.cpp', line 1068

VALUE rb_cipher_encrypt(VALUE self)
{
  return cipher_encrypt(self, false);
}

#encrypt_hexString

Encrypt the plaintext using the options set on the Cipher. This method will return the ciphertext in hex. The raw ciphertext will always be available through the ciphertext and ciphertext_hex afterwards.

Returns:

  • (String)


1081
1082
1083
1084
# File 'ext/ciphers.cpp', line 1081

VALUE rb_cipher_encrypt_hex(VALUE self)
{
  return cipher_encrypt(self, true);
}

#encrypt_iotrue

Encrypts a Ruby IO object and spits the result into another one. You can use any sort of Ruby object as long as it implements eof?, read, write and flush.

Examples:

cipher.encrypt_io(File.open("http://example.com/"), File.open("test.out", 'w'))

output = StringIO.new
cipher.encrypt_io(File.open('test.enc'), output)

Returns:

  • (true)


1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
# File 'ext/ciphers.cpp', line 1147

VALUE rb_cipher_encrypt_io(VALUE self, VALUE in, VALUE out)
{
  JBase *cipher = NULL;
  Data_Get_Struct(self, JBase, cipher);
  try {
    cipher->encryptRubyIO(&in, &out);
    return Qtrue;
  }
  catch (Exception e) {
    rb_raise(rb_eCryptoPP_Error, "Crypto++ exception: %s", e.GetWhat().c_str());
  }
}

#ivString

Returns the Cipher’s IV in binary.

Returns:

  • (String)


441
442
443
444
445
# File 'ext/ciphers.cpp', line 441

VALUE rb_cipher_iv(VALUE self)
{
  string retval = cipher_iv(self, false);
  return rb_tainted_str_new(retval.data(), retval.length());
}

#iv=(iv) ⇒ String

Set an initialization vector on the Cipher.

Returns:

  • (String)


407
408
409
410
411
# File 'ext/ciphers.cpp', line 407

VALUE rb_cipher_iv_eq(VALUE self, VALUE iv)
{
  cipher_iv_eq(self, iv, false);
  return iv;
}

#ivString

Returns the Cipher’s IV in hex.

Returns:

  • (String)


453
454
455
456
457
# File 'ext/ciphers.cpp', line 453

VALUE rb_cipher_iv_hex(VALUE self)
{
  string retval = cipher_iv(self, true);
  return rb_tainted_str_new(retval.data(), retval.length());
}

#iv_hex=(iv) ⇒ String

Set an initialization vector on the Cipher. This method uses hex data and returns the IV as set.

Returns:

  • (String)


420
421
422
423
424
# File 'ext/ciphers.cpp', line 420

VALUE rb_cipher_iv_hex_eq(VALUE self, VALUE iv)
{
  cipher_iv_eq(self, iv, true);
  return iv;
}

#keyString

Returns the key set on the Cipher in binary.

Returns:

  • (String)


830
831
832
833
834
# File 'ext/ciphers.cpp', line 830

VALUE rb_cipher_key(VALUE self)
{
  string retval = cipher_key(self, false);
  return rb_tainted_str_new(retval.data(), retval.length());
}

#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 #key.

Returns:

  • (String)


786
787
788
789
790
# File 'ext/ciphers.cpp', line 786

VALUE rb_cipher_key_eq(VALUE self, VALUE key)
{
  cipher_key_eq(self, key, false);
  return key;
}

#keyString

Returns the key set on the Cipher in hex.

Returns:

  • (String)


842
843
844
845
846
# File 'ext/ciphers.cpp', line 842

VALUE rb_cipher_key_hex(VALUE self)
{
  string retval = cipher_key(self, true);
  return rb_tainted_str_new(retval.data(), retval.length());
}

#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 #key.

Returns:

  • (String)


809
810
811
812
813
# File 'ext/ciphers.cpp', line 809

VALUE rb_cipher_key_hex_eq(VALUE self, VALUE key)
{
  cipher_key_eq(self, key, true);
  return key;
}

#key_lengthFixnum

Gets the key length. The key length being returned in terms of bytes in binary, not hex characters.

Returns:

  • (Fixnum)


880
881
882
883
884
885
# File 'ext/ciphers.cpp', line 880

VALUE rb_cipher_key_length(VALUE self)
{
  JBase *cipher = NULL;
  Data_Get_Struct(self, JBase, cipher);
  return rb_fix_new(cipher->getKeylength());
}

#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.

Returns:

  • (Fixnum)


858
859
860
861
862
863
864
865
866
867
868
869
870
# File 'ext/ciphers.cpp', line 858

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;
  }
}

#max_key_lengthObject

Returns the maximum key length.



911
912
913
914
915
916
# File 'ext/ciphers.cpp', line 911

VALUE rb_cipher_max_key_length(VALUE self)
{
  JBase *cipher = NULL;
  Data_Get_Struct(self, JBase, cipher);
  return rb_fix_new(cipher->getMaxKeylength());
}

#min_key_lengthObject

Returns the minimum key length.



901
902
903
904
905
906
# File 'ext/ciphers.cpp', line 901

VALUE rb_cipher_min_key_length(VALUE self)
{
  JBase *cipher = NULL;
  Data_Get_Struct(self, JBase, cipher);
  return rb_fix_new(cipher->getMinKeylength());
}

#mult_key_lengthObject

Returns the multiplier used for the key length.



921
922
923
924
925
926
# File 'ext/ciphers.cpp', line 921

VALUE rb_cipher_mult_key_length(VALUE self)
{
  JBase *cipher = NULL;
  Data_Get_Struct(self, JBase, cipher);
  return rb_fix_new(cipher->getMultKeylength());
}

#paddingFixnum

Get the cipher padding being used. Returns nil if you try to use this on a stream cipher.

Returns:

  • (Fixnum)


553
554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'ext/ciphers.cpp', line 553

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"
}

#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 AFTER the block mode, as setting the mode causes the padding to revert to its default setting.

Returns the padding as set.

Returns:

  • (Symbol)


522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# File 'ext/ciphers.cpp', line 522

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;
    }
  }
}

#padding_nameString

Returns the name of the padding being used.

Returns:

  • (String)


1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
# File 'ext/ciphers.cpp', line 1274

VALUE rb_cipher_padding_name(VALUE self)
{
  JBase *cipher = NULL;
  Data_Get_Struct(self, JBase, cipher);
  if (IS_STREAM_CIPHER(cipher->getCipherType())) {
    return Qnil;
  }
  else {
    return rb_tainted_str_new2(JCipher::getPaddingName(((JCipher*) cipher)->getPadding()).c_str());
  }
}

#plaintextString

Gets the plaintext from the Cipher in binary.

Returns:

  • (String)


670
671
672
673
674
# File 'ext/ciphers.cpp', line 670

VALUE rb_cipher_plaintext(VALUE self)
{
  string retval = cipher_plaintext(self, false);
  return rb_tainted_str_new(retval.data(), retval.length());
}

#plaintext=(string) ⇒ String

Sets the plaintext on the Cipher in binary and returns the same.

Returns:

  • (String)


637
638
639
640
641
# File 'ext/ciphers.cpp', line 637

VALUE rb_cipher_plaintext_eq(VALUE self, VALUE plaintext)
{
  cipher_plaintext_eq(self, plaintext, false);
  return plaintext;
}

#plaintext_hexString

Gets the plaintext from the Cipher in hex.

Returns:

  • (String)


682
683
684
685
686
# File 'ext/ciphers.cpp', line 682

VALUE rb_cipher_plaintext_hex(VALUE self)
{
  string retval = cipher_plaintext(self, true);
  return rb_tainted_str_new(retval.data(), retval.length());
}

#plaintext_hex=(string) ⇒ String

Sets the plaintext on the Cipher in hex and returns the same.

Returns:

  • (String)


649
650
651
652
653
# File 'ext/ciphers.cpp', line 649

VALUE rb_cipher_plaintext_hex_eq(VALUE self, VALUE plaintext)
{
  cipher_plaintext_eq(self, plaintext, true);
  return plaintext;
}

#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.

Returns:

  • (nil)


381
382
383
384
385
386
387
388
# File 'ext/ciphers.cpp', line 381

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;
}

#rngFixnum

Get the RNG being used.

Returns:

  • (Fixnum)


605
606
607
608
609
610
611
612
613
614
615
616
617
618
# File 'ext/ciphers.cpp', line 605

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"
}

#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 rand_iv. The default is a non-blocking RNG, such as /dev/urandom on UNIX-alikes or CryptoAPI on Microsoft systems.

Returns:

  • (Symbol)


580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# File 'ext/ciphers.cpp', line 580

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;
  }
}

#rng_nameString

Returns the name of the random number generator being used.

Returns:

  • (String)


1306
1307
1308
1309
1310
1311
# File 'ext/ciphers.cpp', line 1306

VALUE rb_cipher_rng_name(VALUE self)
{
  JBase *cipher = NULL;
  Data_Get_Struct(self, JBase, cipher);
  return rb_tainted_str_new2(JCipher::getRNGName(cipher->getRNG()).c_str());
}

#rountsFixnum

Gets the number of rounds to perform on block ciphers. Returns nil if you try to use this on a stream cipher.

Returns:

  • (Fixnum)


1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
# File 'ext/ciphers.cpp', line 1031

VALUE rb_cipher_rounds(VALUE self)
{
  JBase *cipher = NULL;
  Data_Get_Struct(self, JBase, cipher);
  if (IS_STREAM_CIPHER(cipher->getCipherType())) {
    return Qnil;
  }
  else {
    return rb_fix_new(((JCipher*) cipher)->getRounds());
  }
}

#rounds=(rounds) ⇒ Fixnum

Sets the number of rounds to perform on block ciphers. Some block ciphers have different requirements for their rounds than others. An exception will be raised on invalid round settings.

Returns:

  • (Fixnum)


1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
# File 'ext/ciphers.cpp', line 1004

VALUE rb_cipher_rounds_eq(VALUE self, VALUE r)
{
  JBase *cipher = NULL;
  unsigned int rounds = NUM2UINT(rb_funcall(r, rb_intern("to_i"), 0));
  Data_Get_Struct(self, JBase, cipher);
  if (IS_STREAM_CIPHER(cipher->getCipherType())) {
    rb_raise(rb_eCryptoPP_Error, "can't set rounds on stream ciphers");
  }
  else {
    ((JCipher*) cipher)->setRounds(rounds);
    if (((JCipher*) cipher)->getRounds() != rounds) {
      rb_raise(rb_eCryptoPP_Error, "tried set the number of rounds to %d but %d was used instead", rounds, ((JCipher*) cipher)->getRounds());
    }
    else {
      return r;
    }
  }
}

#valid_key_length(l) ⇒ Object

Returns the closest valid key length without actually setting it.



931
932
933
934
935
936
937
# File 'ext/ciphers.cpp', line 931

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));
}