php block cipher mode operation and initial vector length requirement

by Anish

Posted on Thursday December 13 , 2018


openssl_cipher_iv_length

An IV or initialization vector is, in its broadest sense, just the initial value used to start some iterated process. The term is used in a couple of different contexts, and implies different security requirements in each of them. For example, cryptographic hash functions typically have a fixed IV.

Conversely, most block cipher modes of operation require an IV which is random and unpredictable, or at least unique for each message encrypted with a given key. Different block cipher modes have different IV length requirement.

openssl_cipher_iv_length: Gets the Cipher initialization vector (iv) Length

  • Supported PHP Versions (PHP 5 >= 5.3.0, PHP 7)

The Syntax

int  openssl_cipher_iv_length ( string  $method )
  • method: The cipher method, see openssl_get_cipher_methods() for a list of potential values.
  • Returns: Returns the cipher length on success, or FALSE on failure

Examples

openssl_cipher_iv_length() example

<?php  
/**  
 * Created by https://8gwifi.org  
 * User: Anish Nath  
 * Date: 2018-12-13 * Time: 09:25 
 * */  
$ciphers = openssl_get_cipher_methods();  
  
//ECB mode should be avoided  
$ciphers = array_filter($ciphers, function ($n) {  
  return stripos($n, "ecb") === FALSE;  
});  
  
//  Openssl declared the following weak: RC2, RC4, DES, 3DES, MD5 based  
$ciphers = array_filter($ciphers, function ($c) {  
  return stripos($c, "des") === FALSE;  
});  
$ciphers = array_filter($ciphers, function ($c) {  
  return stripos($c, "rc2") === FALSE;  
});  
$ciphers = array_filter($ciphers, function ($c) {  
  return stripos($c, "rc4") === FALSE;  
});  
$ciphers = array_filter($ciphers, function ($c) {  
  return stripos($c, "md5") === FALSE;  
});  
  
if (is_array($ciphers)) {  
  foreach ($ciphers as $cipher) {  
  echo $cipher.': ';  
  echo openssl_cipher_iv_length($cipher);  
  echo "\n";  
  }  
}

The above example will output something similar to:

$ /usr/bin/php get_iv_length.php
AES-128-CBC: 16
AES-128-CFB: 16
AES-128-CFB1: 16
AES-128-CFB8: 16
AES-128-CTR: 16
AES-128-OFB: 16
AES-128-XTS: 16
AES-192-CBC: 16
AES-192-CFB: 16
AES-192-CFB1: 16
AES-192-CFB8: 16
AES-192-CTR: 16
AES-192-OFB: 16
AES-256-CBC: 16
AES-256-CFB: 16
AES-256-CFB1: 16
AES-256-CFB8: 16
AES-256-CTR: 16
AES-256-OFB: 16
AES-256-XTS: 16
BF-CBC: 8
BF-CFB: 8
BF-OFB: 8
CAMELLIA-128-CBC: 16
CAMELLIA-128-CFB: 16
CAMELLIA-128-CFB1: 16
CAMELLIA-128-CFB8: 16
CAMELLIA-128-OFB: 16
CAMELLIA-192-CBC: 16
CAMELLIA-192-CFB: 16
CAMELLIA-192-CFB1: 16
CAMELLIA-192-CFB8: 16
CAMELLIA-192-OFB: 16
CAMELLIA-256-CBC: 16
CAMELLIA-256-CFB: 16
CAMELLIA-256-CFB1: 16
CAMELLIA-256-CFB8: 16
CAMELLIA-256-OFB: 16
CAST5-CBC: 8
CAST5-CFB: 8
CAST5-OFB: 8
ChaCha: 8
GOST 28147-89: 8
aes-128-cbc: 16
aes-128-cfb: 16
aes-128-cfb1: 16
aes-128-cfb8: 16
aes-128-ctr: 16
aes-128-gcm: 12
aes-128-ofb: 16
aes-128-xts: 16
aes-192-cbc: 16
aes-192-cfb: 16
aes-192-cfb1: 16
aes-192-cfb8: 16
aes-192-ctr: 16
aes-192-gcm: 12
aes-192-ofb: 16
aes-256-cbc: 16
aes-256-cfb: 16
aes-256-cfb1: 16
aes-256-cfb8: 16
aes-256-ctr: 16
aes-256-gcm: 12
aes-256-ofb: 16
aes-256-xts: 16
bf-cbc: 8
bf-cfb: 8
bf-ofb: 8
camellia-128-cbc: 16
camellia-128-cfb: 16
camellia-128-cfb1: 16
camellia-128-cfb8: 16
camellia-128-ofb: 16
camellia-192-cbc: 16
camellia-192-cfb: 16
camellia-192-cfb1: 16
camellia-192-cfb8: 16
camellia-192-ofb: 16
camellia-256-cbc: 16
camellia-256-cfb: 16
camellia-256-cfb1: 16
camellia-256-cfb8: 16
camellia-256-ofb: 16
cast5-cbc: 8
cast5-cfb: 8
cast5-ofb: 8
chacha: 8
gost89: 8
gost89-cnt: 8
id-aes128-GCM: 12
id-aes192-GCM: 12
id-aes256-GCM: 12


Thanku for reading !!! Give a Share for Support

Asking for donation sound bad to me, so i'm raising fund from The Modern Cryptography CookBook for Just $9. Leanpub books Discount coupon first 100 reader. No hurry read the sample chapters here then decide.

Alternatively to support you can buy My all four Cryptography book Just $10.99


Referefce
  • The Modern Cryptography Book.
  • Go lang Cryptography for developers
  • Python Cryptography
  • Cryptography for JavaScript Developer


python Cryptography Topics
Topics
For Coffee/ Beer/ Amazon Bill and further development of the project Support by Purchasing, The Modern Cryptography CookBook for Just $9 Coupon Price

Cryptography for Python Developers

Cryptography for JavaScript Developers

Go lang ryptography for Developers