Ruby  1.9.3p551(2014-11-13revision48407)
ossl_cipher.c
Go to the documentation of this file.
1 /*
2  * $Id: ossl_cipher.c 44754 2014-01-30 03:49:07Z usa $
3  * 'OpenSSL for Ruby' project
4  * Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
5  * All rights reserved.
6  */
7 /*
8  * This program is licenced under the same licence as Ruby.
9  * (See the file 'LICENCE'.)
10  */
11 #include "ossl.h"
12 
13 #define WrapCipher(obj, klass, ctx) \
14  (obj) = Data_Wrap_Struct((klass), 0, ossl_cipher_free, (ctx))
15 #define MakeCipher(obj, klass, ctx) \
16  (obj) = Data_Make_Struct((klass), EVP_CIPHER_CTX, 0, ossl_cipher_free, (ctx))
17 #define AllocCipher(obj, ctx) \
18  memset(DATA_PTR(obj) = (ctx) = ALLOC(EVP_CIPHER_CTX), 0, sizeof(EVP_CIPHER_CTX))
19 #define GetCipherInit(obj, ctx) do { \
20  Data_Get_Struct((obj), EVP_CIPHER_CTX, (ctx)); \
21 } while (0)
22 #define GetCipher(obj, ctx) do { \
23  GetCipherInit((obj), (ctx)); \
24  if (!(ctx)) { \
25  ossl_raise(rb_eRuntimeError, "Cipher not inititalized!"); \
26  } \
27 } while (0)
28 #define SafeGetCipher(obj, ctx) do { \
29  OSSL_Check_Kind((obj), cCipher); \
30  GetCipher((obj), (ctx)); \
31 } while (0)
32 
33 /*
34  * Classes
35  */
38 
39 static VALUE ossl_cipher_alloc(VALUE klass);
40 
41 /*
42  * PUBLIC
43  */
44 const EVP_CIPHER *
46 {
47  EVP_CIPHER_CTX *ctx;
48 
49  SafeGetCipher(obj, ctx);
50 
51  return EVP_CIPHER_CTX_cipher(ctx);
52 }
53 
54 VALUE
55 ossl_cipher_new(const EVP_CIPHER *cipher)
56 {
57  VALUE ret;
58  EVP_CIPHER_CTX *ctx;
59 
61  AllocCipher(ret, ctx);
62  EVP_CIPHER_CTX_init(ctx);
63  if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, -1) != 1)
65 
66  return ret;
67 }
68 
69 /*
70  * PRIVATE
71  */
72 static void
73 ossl_cipher_free(EVP_CIPHER_CTX *ctx)
74 {
75  if (ctx) {
76  EVP_CIPHER_CTX_cleanup(ctx);
77  ruby_xfree(ctx);
78  }
79 }
80 
81 static VALUE
83 {
84  VALUE obj;
85 
86  WrapCipher(obj, klass, 0);
87 
88  return obj;
89 }
90 
91 /*
92  * call-seq:
93  * Cipher.new(string) -> cipher
94  *
95  * The string must contain a valid cipher name like "AES-128-CBC" or "3DES".
96  *
97  * A list of cipher names is available by calling OpenSSL::Cipher.ciphers.
98  */
99 static VALUE
101 {
102  EVP_CIPHER_CTX *ctx;
103  const EVP_CIPHER *cipher;
104  char *name;
105  unsigned char key[EVP_MAX_KEY_LENGTH];
106 
107  name = StringValuePtr(str);
108  GetCipherInit(self, ctx);
109  if (ctx) {
110  ossl_raise(rb_eRuntimeError, "Cipher already inititalized!");
111  }
112  AllocCipher(self, ctx);
113  EVP_CIPHER_CTX_init(ctx);
114  if (!(cipher = EVP_get_cipherbyname(name))) {
115  ossl_raise(rb_eRuntimeError, "unsupported cipher algorithm (%s)", name);
116  }
117  /*
118  * The EVP which has EVP_CIPH_RAND_KEY flag (such as DES3) allows
119  * uninitialized key, but other EVPs (such as AES) does not allow it.
120  * Calling EVP_CipherUpdate() without initializing key causes SEGV so we
121  * set the data filled with "\0" as the key by default.
122  */
123  memset(key, 0, EVP_MAX_KEY_LENGTH);
124  if (EVP_CipherInit_ex(ctx, cipher, NULL, key, NULL, -1) != 1)
126 
127  return self;
128 }
129 
130 static VALUE
132 {
133  EVP_CIPHER_CTX *ctx1, *ctx2;
134 
135  rb_check_frozen(self);
136  if (self == other) return self;
137 
138  GetCipherInit(self, ctx1);
139  if (!ctx1) {
140  AllocCipher(self, ctx1);
141  }
142  SafeGetCipher(other, ctx2);
143  if (EVP_CIPHER_CTX_copy(ctx1, ctx2) != 1)
145 
146  return self;
147 }
148 
149 #ifdef HAVE_OBJ_NAME_DO_ALL_SORTED
150 static void*
151 add_cipher_name_to_ary(const OBJ_NAME *name, VALUE ary)
152 {
153  rb_ary_push(ary, rb_str_new2(name->name));
154  return NULL;
155 }
156 #endif
157 
158 #ifdef HAVE_OBJ_NAME_DO_ALL_SORTED
159 /*
160  * call-seq:
161  * Cipher.ciphers -> array[string...]
162  *
163  * Returns the names of all available ciphers in an array.
164  */
165 static VALUE
166 ossl_s_ciphers(VALUE self)
167 {
168  VALUE ary;
169 
170  ary = rb_ary_new();
171  OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH,
172  (void(*)(const OBJ_NAME*,void*))add_cipher_name_to_ary,
173  (void*)ary);
174 
175  return ary;
176 }
177 #else
178 #define ossl_s_ciphers rb_f_notimplement
179 #endif
180 
181 /*
182  * call-seq:
183  * cipher.reset -> self
184  *
185  * Fully resets the internal state of the Cipher. By using this, the same
186  * Cipher instance may be used several times for en- or decryption tasks.
187  *
188  * Internally calls EVP_CipherInit_ex(ctx, NULL, NULL, NULL, NULL, -1).
189  */
190 static VALUE
192 {
193  EVP_CIPHER_CTX *ctx;
194 
195  GetCipher(self, ctx);
196  if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, NULL, -1) != 1)
198 
199  return self;
200 }
201 
202 static VALUE
203 ossl_cipher_init(int argc, VALUE *argv, VALUE self, int mode)
204 {
205  EVP_CIPHER_CTX *ctx;
206  unsigned char key[EVP_MAX_KEY_LENGTH], *p_key = NULL;
207  unsigned char iv[EVP_MAX_IV_LENGTH], *p_iv = NULL;
208  VALUE pass, init_v;
209 
210  if(rb_scan_args(argc, argv, "02", &pass, &init_v) > 0){
211  /*
212  * oops. this code mistakes salt for IV.
213  * We deprecated the arguments for this method, but we decided
214  * keeping this behaviour for backward compatibility.
215  */
216  VALUE cname = rb_class_path(rb_obj_class(self));
217  rb_warn("arguments for %"PRIsVALUE"#encrypt and %"PRIsVALUE"#decrypt were deprecated; "
218  "use %"PRIsVALUE"#pkcs5_keyivgen to derive key and IV",
219  RB_OBJ_STRING(cname), RB_OBJ_STRING(cname), RB_OBJ_STRING(cname));
220  StringValue(pass);
221  GetCipher(self, ctx);
222  if (NIL_P(init_v)) memcpy(iv, "OpenSSL for Ruby rulez!", sizeof(iv));
223  else{
224  StringValue(init_v);
225  if (EVP_MAX_IV_LENGTH > RSTRING_LEN(init_v)) {
226  memset(iv, 0, EVP_MAX_IV_LENGTH);
227  memcpy(iv, RSTRING_PTR(init_v), RSTRING_LEN(init_v));
228  }
229  else memcpy(iv, RSTRING_PTR(init_v), sizeof(iv));
230  }
231  EVP_BytesToKey(EVP_CIPHER_CTX_cipher(ctx), EVP_md5(), iv,
232  (unsigned char *)RSTRING_PTR(pass), RSTRING_LENINT(pass), 1, key, NULL);
233  p_key = key;
234  p_iv = iv;
235  }
236  else {
237  GetCipher(self, ctx);
238  }
239  if (EVP_CipherInit_ex(ctx, NULL, NULL, p_key, p_iv, mode) != 1) {
241  }
242 
243  return self;
244 }
245 
246 /*
247  * call-seq:
248  * cipher.encrypt -> self
249  *
250  * Initializes the Cipher for encryption.
251  *
252  * Make sure to call Cipher#encrypt or Cipher#decrypt before using any of the
253  * following methods:
254  * * [key=, iv=, random_key, random_iv, pkcs5_keyivgen]
255  *
256  * Internally calls EVP_CipherInit_ex(ctx, NULL, NULL, NULL, NULL, 1).
257  */
258 static VALUE
260 {
261  return ossl_cipher_init(argc, argv, self, 1);
262 }
263 
264 /*
265  * call-seq:
266  * cipher.decrypt -> self
267  *
268  * Initializes the Cipher for decryption.
269  *
270  * Make sure to call Cipher#encrypt or Cipher#decrypt before using any of the
271  * following methods:
272  * * [key=, iv=, random_key, random_iv, pkcs5_keyivgen]
273  *
274  * Internally calls EVP_CipherInit_ex(ctx, NULL, NULL, NULL, NULL, 0).
275  */
276 static VALUE
278 {
279  return ossl_cipher_init(argc, argv, self, 0);
280 }
281 
282 /*
283  * call-seq:
284  * cipher.pkcs5_keyivgen(pass [, salt [, iterations [, digest]]] ) -> nil
285  *
286  * Generates and sets the key/IV based on a password.
287  *
288  * WARNING: This method is only PKCS5 v1.5 compliant when using RC2, RC4-40,
289  * or DES with MD5 or SHA1. Using anything else (like AES) will generate the
290  * key/iv using an OpenSSL specific method. This method is deprecated and
291  * should no longer be used. Use a PKCS5 v2 key generation method from
292  * OpenSSL::PKCS5 instead.
293  *
294  * === Parameters
295  * +salt+ must be an 8 byte string if provided.
296  * +iterations+ is a integer with a default of 2048.
297  * +digest+ is a Digest object that defaults to 'MD5'
298  *
299  * A minimum of 1000 iterations is recommended.
300  *
301  */
302 static VALUE
304 {
305  EVP_CIPHER_CTX *ctx;
306  const EVP_MD *digest;
307  VALUE vpass, vsalt, viter, vdigest;
308  unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH], *salt = NULL;
309  int iter;
310 
311  rb_scan_args(argc, argv, "13", &vpass, &vsalt, &viter, &vdigest);
312  StringValue(vpass);
313  if(!NIL_P(vsalt)){
314  StringValue(vsalt);
315  if(RSTRING_LEN(vsalt) != PKCS5_SALT_LEN)
316  ossl_raise(eCipherError, "salt must be an 8-octet string");
317  salt = (unsigned char *)RSTRING_PTR(vsalt);
318  }
319  iter = NIL_P(viter) ? 2048 : NUM2INT(viter);
320  digest = NIL_P(vdigest) ? EVP_md5() : GetDigestPtr(vdigest);
321  GetCipher(self, ctx);
322  EVP_BytesToKey(EVP_CIPHER_CTX_cipher(ctx), digest, salt,
323  (unsigned char *)RSTRING_PTR(vpass), RSTRING_LENINT(vpass), iter, key, iv);
324  if (EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, -1) != 1)
326  OPENSSL_cleanse(key, sizeof key);
327  OPENSSL_cleanse(iv, sizeof iv);
328 
329  return Qnil;
330 }
331 
332 
333 /*
334  * call-seq:
335  * cipher.update(data [, buffer]) -> string or buffer
336  *
337  * Encrypts data in a streaming fashion. Hand consecutive blocks of data
338  * to the +update+ method in order to encrypt it. Returns the encrypted
339  * data chunk. When done, the output of Cipher#final should be additionally
340  * added to the result.
341  *
342  * === Parameters
343  * +data+ is a nonempty string.
344  * +buffer+ is an optional string to store the result.
345  */
346 static VALUE
348 {
349  EVP_CIPHER_CTX *ctx;
350  unsigned char *in;
351  int in_len, out_len;
352  VALUE data, str;
353 
354  rb_scan_args(argc, argv, "11", &data, &str);
355 
356  StringValue(data);
357  in = (unsigned char *)RSTRING_PTR(data);
358  if ((in_len = RSTRING_LENINT(data)) == 0)
359  ossl_raise(rb_eArgError, "data must not be empty");
360  GetCipher(self, ctx);
361  out_len = in_len+EVP_CIPHER_CTX_block_size(ctx);
362 
363  if (NIL_P(str)) {
364  str = rb_str_new(0, out_len);
365  } else {
366  StringValue(str);
367  rb_str_resize(str, out_len);
368  }
369 
370  if (!EVP_CipherUpdate(ctx, (unsigned char *)RSTRING_PTR(str), &out_len, in, in_len))
372  assert(out_len < RSTRING_LEN(str));
373  rb_str_set_len(str, out_len);
374 
375  return str;
376 }
377 
378 /*
379  * call-seq:
380  * cipher.final -> string
381  *
382  * Returns the remaining data held in the cipher object. Further calls to
383  * Cipher#update or Cipher#final will return garbage.
384  *
385  * See EVP_CipherFinal_ex for further information.
386  */
387 static VALUE
389 {
390  EVP_CIPHER_CTX *ctx;
391  int out_len;
392  VALUE str;
393 
394  GetCipher(self, ctx);
395  str = rb_str_new(0, EVP_CIPHER_CTX_block_size(ctx));
396  if (!EVP_CipherFinal_ex(ctx, (unsigned char *)RSTRING_PTR(str), &out_len))
398  assert(out_len <= RSTRING_LEN(str));
399  rb_str_set_len(str, out_len);
400 
401  return str;
402 }
403 
404 /*
405  * call-seq:
406  * cipher.name -> string
407  *
408  * Returns the name of the cipher which may differ slightly from the original
409  * name provided.
410  */
411 static VALUE
413 {
414  EVP_CIPHER_CTX *ctx;
415 
416  GetCipher(self, ctx);
417 
418  return rb_str_new2(EVP_CIPHER_name(EVP_CIPHER_CTX_cipher(ctx)));
419 }
420 
421 /*
422  * call-seq:
423  * cipher.key = string -> string
424  *
425  * Sets the cipher key. To generate a key, you should either use a secure
426  * random byte string or, if the key is to be derived from a password, you
427  * should rely on PBKDF2 functionality provided by OpenSSL::PKCS5. To
428  * generate a secure random-based key, Cipher#random_key may be used.
429  *
430  * Only call this method after calling Cipher#encrypt or Cipher#decrypt.
431  */
432 static VALUE
434 {
435  EVP_CIPHER_CTX *ctx;
436 
437  StringValue(key);
438  GetCipher(self, ctx);
439 
440  if (RSTRING_LEN(key) < EVP_CIPHER_CTX_key_length(ctx))
441  ossl_raise(eCipherError, "key length too short");
442 
443  if (EVP_CipherInit_ex(ctx, NULL, NULL, (unsigned char *)RSTRING_PTR(key), NULL, -1) != 1)
445 
446  return key;
447 }
448 
449 /*
450  * call-seq:
451  * cipher.iv = string -> string
452  *
453  * Sets the cipher IV. Please note that since you should never be using ECB
454  * mode, an IV is always explicitly required and should be set prior to
455  * encryption. The IV itself can be safely transmitted in public, but it
456  * should be unpredictable to prevent certain kinds of attacks. You may use
457  * Cipher#random_iv to create a secure random IV.
458  *
459  * Only call this method after calling Cipher#encrypt or Cipher#decrypt.
460  *
461  * If not explicitly set, the OpenSSL default of an all-zeroes ("\\0") IV is
462  * used.
463  */
464 static VALUE
466 {
467  EVP_CIPHER_CTX *ctx;
468 
469  StringValue(iv);
470  GetCipher(self, ctx);
471 
472  if (RSTRING_LEN(iv) < EVP_CIPHER_CTX_iv_length(ctx))
473  ossl_raise(eCipherError, "iv length too short");
474 
475  if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, (unsigned char *)RSTRING_PTR(iv), -1) != 1)
477 
478  return iv;
479 }
480 
481 
482 /*
483  * call-seq:
484  * cipher.key_len = integer -> integer
485  *
486  * Sets the key length of the cipher. If the cipher is a fixed length cipher
487  * then attempting to set the key length to any value other than the fixed
488  * value is an error.
489  *
490  * Under normal circumstances you do not need to call this method (and probably shouldn't).
491  *
492  * See EVP_CIPHER_CTX_set_key_length for further information.
493  */
494 static VALUE
496 {
497  int len = NUM2INT(key_length);
498  EVP_CIPHER_CTX *ctx;
499 
500  GetCipher(self, ctx);
501  if (EVP_CIPHER_CTX_set_key_length(ctx, len) != 1)
503 
504  return key_length;
505 }
506 
507 #if defined(HAVE_EVP_CIPHER_CTX_SET_PADDING)
508 /*
509  * call-seq:
510  * cipher.padding = integer -> integer
511  *
512  * Enables or disables padding. By default encryption operations are padded using standard block padding and the
513  * padding is checked and removed when decrypting. If the pad parameter is zero then no padding is performed, the
514  * total amount of data encrypted or decrypted must then be a multiple of the block size or an error will occur.
515  *
516  * See EVP_CIPHER_CTX_set_padding for further information.
517  */
518 static VALUE
519 ossl_cipher_set_padding(VALUE self, VALUE padding)
520 {
521  EVP_CIPHER_CTX *ctx;
522  int pad = NUM2INT(padding);
523 
524  GetCipher(self, ctx);
525  if (EVP_CIPHER_CTX_set_padding(ctx, pad) != 1)
526  ossl_raise(eCipherError, NULL);
527  return padding;
528 }
529 #else
530 #define ossl_cipher_set_padding rb_f_notimplement
531 #endif
532 
533 #define CIPHER_0ARG_INT(func) \
534  static VALUE \
535  ossl_cipher_##func(VALUE self) \
536  { \
537  EVP_CIPHER_CTX *ctx; \
538  GetCipher(self, ctx); \
539  return INT2NUM(EVP_CIPHER_##func(EVP_CIPHER_CTX_cipher(ctx))); \
540  }
541 
542 /*
543  * call-seq:
544  * cipher.key_len -> integer
545  *
546  * Returns the key length in bytes of the Cipher.
547  */
548 CIPHER_0ARG_INT(key_length)
549 /*
550  * call-seq:
551  * cipher.iv_len -> integer
552  *
553  * Returns the expected length in bytes for an IV for this Cipher.
554  */
555 CIPHER_0ARG_INT(iv_length)
556 /*
557  * call-seq:
558  * cipher.block_size -> integer
559  *
560  * Returns the size in bytes of the blocks on which this Cipher operates on.
561  */
562 CIPHER_0ARG_INT(block_size)
563 
564 /*
565  * INIT
566  */
567 void
569 {
570 #if 0
571  mOSSL = rb_define_module("OpenSSL"); /* let rdoc know about mOSSL */
572 #endif
573 
574  /* Document-class: OpenSSL::Cipher
575  *
576  * Provides symmetric algorithms for encryption and decryption. The
577  * algorithms that are available depend on the particular version
578  * of OpenSSL that is installed.
579  *
580  * === Listing all supported algorithms
581  *
582  * A list of supported algorithms can be obtained by
583  *
584  * puts OpenSSL::Cipher.ciphers
585  *
586  * === Instantiating a Cipher
587  *
588  * There are several ways to create a Cipher instance. Generally, a
589  * Cipher algorithm is categorized by its name, the key length in bits
590  * and the cipher mode to be used. The most generic way to create a
591  * Cipher is the following
592  *
593  * cipher = OpenSSL::Cipher.new('<name>-<key length>-<mode>')
594  *
595  * That is, a string consisting of the hyphenated concatenation of the
596  * individual components name, key length and mode. Either all uppercase
597  * or all lowercase strings may be used, for example:
598  *
599  * cipher = OpenSSL::Cipher.new('AES-128-CBC')
600  *
601  * For each algorithm supported, there is a class defined under the
602  * Cipher class that goes by the name of the cipher, e.g. to obtain an
603  * instance of AES, you could also use
604  *
605  * # these are equivalent
606  * cipher = OpenSSL::Cipher::AES.new(128, :CBC)
607  * cipher = OpenSSL::Cipher::AES.new(128, 'CBC')
608  * cipher = OpenSSL::Cipher::AES.new('128-CBC')
609  *
610  * Finally, due to its wide-spread use, there are also extra classes
611  * defined for the different key sizes of AES
612  *
613  * cipher = OpenSSL::Cipher::AES128.new(:CBC)
614  * cipher = OpenSSL::Cipher::AES192.new(:CBC)
615  * cipher = OpenSSL::Cipher::AES256.new(:CBC)
616  *
617  * === Choosing either encryption or decryption mode
618  *
619  * Encryption and decryption are often very similar operations for
620  * symmetric algorithms, this is reflected by not having to choose
621  * different classes for either operation, both can be done using the
622  * same class. Still, after obtaining a Cipher instance, we need to
623  * tell the instance what it is that we intend to do with it, so we
624  * need to call either
625  *
626  * cipher.encrypt
627  *
628  * or
629  *
630  * cipher.decrypt
631  *
632  * on the Cipher instance. This should be the first call after creating
633  * the instance, otherwise configuration that has already been set could
634  * get lost in the process.
635  *
636  * === Choosing a key
637  *
638  * Symmetric encryption requires a key that is the same for the encrypting
639  * and for the decrypting party and after initial key establishment should
640  * be kept as private information. There are a lot of ways to create
641  * insecure keys, the most notable is to simply take a password as the key
642  * without processing the password further. A simple and secure way to
643  * create a key for a particular Cipher is
644  *
645  * cipher = OpenSSL::AES256.new(:CFB)
646  * cipher.encrypt
647  * key = cipher.random_key # also sets the generated key on the Cipher
648  *
649  * If you absolutely need to use passwords as encryption keys, you
650  * should use Password-Based Key Derivation Function 2 (PBKDF2) by
651  * generating the key with the help of the functionality provided by
652  * OpenSSL::PKCS5.pbkdf2_hmac_sha1 or OpenSSL::PKCS5.pbkdf2_hmac.
653  *
654  * Although there is Cipher#pkcs5_keyivgen, its use is deprecated and
655  * it should only be used in legacy applications because it does not use
656  * the newer PKCS#5 v2 algorithms.
657  *
658  * === Choosing an IV
659  *
660  * The cipher modes CBC, CFB, OFB and CTR all need an "initialization
661  * vector", or short, IV. ECB mode is the only mode that does not require
662  * an IV, but there is almost no legitimate use case for this mode
663  * because of the fact that it does not sufficiently hide plaintext
664  * patterns. Therefore
665  *
666  * <b>You should never use ECB mode unless you are absolutely sure that
667  * you absolutely need it</b>
668  *
669  * Because of this, you will end up with a mode that explicitly requires
670  * an IV in any case. Note that for backwards compatibility reasons,
671  * setting an IV is not explicitly mandated by the Cipher API. If not
672  * set, OpenSSL itself defaults to an all-zeroes IV ("\\0", not the
673  * character). Although the IV can be seen as public information, i.e.
674  * it may be transmitted in public once generated, it should still stay
675  * unpredictable to prevent certain kinds of attacks. Therefore, ideally
676  *
677  * <b>Always create a secure random IV for every encryption of your
678  * Cipher</b>
679  *
680  * A new, random IV should be created for every encryption of data. Think
681  * of the IV as a nonce (number used once) - it's public but random and
682  * unpredictable. A secure random IV can be created as follows
683  *
684  * cipher = ...
685  * cipher.encrypt
686  * key = cipher.random_key
687  * iv = cipher.random_iv # also sets the generated IV on the Cipher
688  *
689  * Although the key is generally a random value, too, it is a bad choice
690  * as an IV. There are elaborate ways how an attacker can take advantage
691  * of such an IV. As a general rule of thumb, exposing the key directly
692  * or indirectly should be avoided at all cost and exceptions only be
693  * made with good reason.
694  *
695  * === Calling Cipher#final
696  *
697  * ECB (which should not be used) and CBC are both block-based modes.
698  * This means that unlike for the other streaming-based modes, they
699  * operate on fixed-size blocks of data, and therefore they require a
700  * "finalization" step to produce or correctly decrypt the last block of
701  * data by appropriately handling some form of padding. Therefore it is
702  * essential to add the output of OpenSSL::Cipher#final to your
703  * encryption/decryption buffer or you will end up with decryption errors
704  * or truncated data.
705  *
706  * Although this is not really necessary for streaming-mode ciphers, it is
707  * still recommended to apply the same pattern of adding the output of
708  * Cipher#final there as well - it also enables you to switch between
709  * modes more easily in the future.
710  *
711  * === Encrypting and decrypting some data
712  *
713  * data = "Very, very confidential data"
714  *
715  * cipher = OpenSSL::Cipher::AES.new(128, :CBC)
716  * cipher.encrypt
717  * key = cipher.random_key
718  * iv = cipher.random_iv
719  *
720  * encrypted = cipher.update(data) + cipher.final
721  * ...
722  * decipher = OpenSSL::Cipher::AES.new(128, :CBC)
723  * decipher.decrypt
724  * decipher.key = key
725  * decipher.iv = iv
726  *
727  * plain = decipher.update(encrypted) + decipher.final
728  *
729  * puts data == plain #=> true
730  *
731  */
734 
742  rb_define_method(cCipher, "pkcs5_keyivgen", ossl_cipher_pkcs5_keyivgen, -1);
748  rb_define_method(cCipher, "key_len", ossl_cipher_key_length, 0);
750  rb_define_method(cCipher, "iv_len", ossl_cipher_iv_length, 0);
751  rb_define_method(cCipher, "block_size", ossl_cipher_block_size, 0);
753 }
754 
755