Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js Crypto, what's the default padding for AES?

I've traversed the Node.js Crypto documentation but still couldn't find the default padding used by the Cipher class, for example the method cipher.setAutoPadding(true) has no specification about it. So is it PKCS#5, PKCS#7...?

Any info on this will be great!

like image 628
domser Avatar asked Jun 05 '18 13:06

domser


People also ask

Does AES have padding?

Padding is used in a block cipher where we fill up the blocks with padding bytes. AES uses 128-bits (16 bytes), and DES uses 64-bit blocks (8 bytes). The main padding methods are: CMS (Cryptographic Message Syntax).

Why is padding used in AES?

Padding is a way to take data that may or may not be a multiple of the block size for a cipher and extend it out so that it is. This is required for many block cipher modes as they require the data to be encrypted to be an exact multiple of the block size.

Is AES 256 CBC secure?

The AES-GCM mode of operation can actually be carried out in parallel both for encryption and decryption. The additional security that this method provides also allows the VPN to use only a 128-bit key, whereas AES-CBC typically requires a 256-bit key to be considered secure. CBC ciphers were removed in May of 2021.

What is crypto package in node JS?

Crypto is a module in Node. js which deals with an algorithm that performs data encryption and decryption. This is used for security purpose like user authentication where storing the password in Database in the encrypted form. Crypto module provides set of classes like hash, HMAC, cipher, decipher, sign, and verify.


1 Answers

In the documentation (https://nodejs.org/api/crypto.html#crypto_cipher_setautopadding_autopadding) it says:

Disabling automatic padding is useful for non-standard padding, for instance using 0x0 instead of PKCS padding.

So it's using "PKCS". More specifically, PKCS7.

PKCS7 defined the same padding algorithm that PKCS5 did, but PKCS5 assumed all ciphers would have 8 byte (64 bit) block sizes. PKCS7's version describes it as a k-byte block. In practice, people ignore that PKCS5 had a fixed block size, and "PKCS5 padding" and "PKCS7 padding" are the same thing.

PKCS5 (https://www.rfc-editor.org/rfc/rfc2898#section-6.1.1):

4. Concatenate M and a padding string PS to form an encoded
     message EM:

             EM = M || PS ,

     where the padding string PS consists of 8-(||M|| mod 8) octets
     each with value 8-(||M|| mod 8). The padding string PS will
     satisfy one of the following statements:

             PS = 01, if ||M|| mod 8 = 7 ;
             PS = 02 02, if ||M|| mod 8 = 6 ;
             ...
             PS = 08 08 08 08 08 08 08 08, if ||M|| mod 8 = 0.

PKCS7 (https://www.rfc-editor.org/rfc/rfc5652#section-6.3):

Some content-encryption algorithms assume the input length is a
multiple of k octets, where k is greater than one.  For such
algorithms, the input shall be padded at the trailing end with
k-(lth mod k) octets all having value k-(lth mod k), where lth is
the length of the input.  In other words, the input is padded at
the trailing end with one of the following strings:

                 01 -- if lth mod k = k-1
              02 02 -- if lth mod k = k-2
                  .
                  .
                  .
        k k ... k k -- if lth mod k = 0
like image 55
bartonjs Avatar answered Sep 29 '22 19:09

bartonjs