Encryption and Decryption using AES in Linux Kernel - c

I want to encrypt files during their creation and decrypt files during their read operation using AES algorithm. I have also written code in vfs_write() and vfs_read() for encryption and decryption respectively and also it is working nicely, but the only problem now is that k whenever I pass a file to vfs_write() whose length is not multiple of 16 (AES BLOCK SIZE) den AES does padding to it to make it a multiple of 16 and bcz of this the size of the file increases but the write() function does not know about this and so it rejects
e.g.:- suppose i enter data as "123", here length is 4 (3 length of data + 1 '\0' character) and so AES padds 12 bytes to it to make it 16 bytes (as AES works on 16 bytes blocks), but write() only takes original length which was 4, and so i want to know how to change the file size to 16 (in this case) and also where to do it in kernel code.
i tried this
inode->i_size=new_length;
inode->i_op->truncate(inode);
also i tried
if(file->f_flags & O_APPEND)
*pos=i_size_read(inode);
but this is not working bcz kernel hangs and also i am not understanding where to do such things i.e., in which function and how.
also i tried changing the count variable with new length in vfs_write() but then it gives error as "cat write error: No space left on device".
it works fine when i pass file which is a multiple of 16.

Your current mode requires padding; and it can be hard to do padding in some cases (like what you meet). Usually, disk/filesystem encryption is done in other mode, which require no padding (and easy to do random reads/writes).
Overview of block cipher modes: http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation
Modes: ECB, CBC, PCBC, CFB, OFB require padding
and mode CTR (Counter) doesn't require padding.
This mode is easy and it is even easier to implement it in wrong (unsafe, easy-to-break) way.
There is an overview of Disk encryption http://en.wikipedia.org/wiki/Disk_encryption_theory with even more advanced modes (XEX, XTS). Some of them still require padding.
Even with universal cipher mode you will get a lot of problems.. Some of them are covered in "Cryptfs: A Stackable Vnode Level Encryption File System"

why dont you deliberately make it 16 bytes, encrypt it and then after decryption discard the padding. I mean instead of relying on aes alone, do it yourself. By this you wil be sure that the data you are getting is correct

Related

How to encrypt variable size of data by using AES (CBC) 128 Algorithm

I want encrypt 4 byte of data, but AES takes 16 byte of data as input and gives 16 bytes as output,
So How to overcome this problem, If some have source code please share
Thanks
To encrypt 4 byte of data we need to add 12 more bytes and make a block of 16 bytes, its take more data length to transfer via wireless, So How to send Encrypted data with Real data length
On encryption, append 12 bytes of random junk.
On decryption, ignore the last 12 bytes.
If length is not fixed, then
[1-bit junk][7-bit bit-length][n-bits of data][128-8-n junk bits to fill to 128]
Cipher block chaining (CBC mode) requires that the plaintext be a mulitple of the block size. If it is not, you'll need to pad it out to a multiple of the block size.
Cipher-feedback mode (CFB mode) exists primarily to avoid this -- as it uses the block cipher as a way of generating key bits to XOR with the the plaintext to encrypt it, it can be used with any bit length and padding is not needed. With CFB mode, however, it is critical to not reuse IVs, as that will directly leak the first block of the plaintext. In CBC mode, reusing IVs is still bad (will leak info about correlated inputs), but arguably not as bad.

Encryption algorithm for 14 byte data

I have an application, where I have to transfer an encrypted packet over BLE advertisement which is fixed 14 byte in length. AES algorithm restricts data to be minimum of 16 bytes long and DES requires it to be in multiples of 8 byte. I have a odd length of 14 byte which I cannot change. Is there any encryption algorithm which can be used to encrypt this 14 byte data. Also it would be good, if someone could point out any C based implementation of the algorithm?
Assuming you mean both plaintext and ciphertext are exactly 14 bytes in length:
Use AES in CTR mode. This yields the same 16 byte chunks of data on each side. You can use 14 of the 16 bytes as an XOR key and discard the last two.
Initialization of the IV is done with 14 bytes of IV and two bytes of zeros.
However, there's a wrinkle here. The underlying protocol is stateless broadcast. The only way to get an IV is to use a unique packet identifier, and there might not be one. Without about 10 additional bytes I would have a very hard time coming up with a unique IV generator.
You can use ECB-based Ciphertext Stealing (CTS) with a block cipher that has a block size smaller than 14 byte. I won't go into detail how CTS works, because the Wikipedia article does a pretty good job.
If you heard that ECB is bad then you are correct. Sadly any other mode requires some sort of IV which will eat away your payload constraint. Since CTS moves a part of the ciphertext to the last plaintext block the bad property of ECB goes away.
Block ciphers with small block sizes such as 64-bit have worse security properties than say block ciphers with 128-bit blocks. Just look at the Sweet32 attack. In your case I would guess that this is not really an issue since an attacker cannot get you to generate many many encrypted broadcasts and if they tried it would take them a really long time.
A popular block cipher with 64-bit block size is DES. You might have heard that DES is easily brute forceable due to the small key size and you would be correct. Triple-DES (EEE or EDE) comes to the rescue which has a key size that is three times larger and has a much better protection against brute force attacks.
Sadly, you cannot use AES because CTS needs at least two blocks to work and a single block of AES already breaks your size constraint.
Both AES and DES are Block Cipher algorithms, so you're right that they have minimum sizes for the blocks they can handle. BUT there are many different ways to use block encryption, some of which can process streams of data of arbitrary length. Check out "s-bit Cipher Feedback Mode" where the s is the size of the actual chunks that are individually being encrypted. Here you could use s=2.
Why not frame your data so that it features:
header
payload
padding
The header would contain the length of the data, plus maybe a "magic" code to check synchronisation. The payload is your data. The padding is then a block of zeroes (or random numbers if you so wish) in order to fit the necessary multiple of blockside for the block-cipher.
Assuming you want to transmit in the same 14 bytes you are a bit stuck. The suggestions above mean that you would need to transmit 16 bytes (or even more), then ignore the last 2 bytes.
How secure do you need the data to be? One answer might be to use DES to encrypt all the full blocks (one block of 8 bytes in your case), then use something weaker to encode the remaining (6 in your case) bytes.
A reasonably secure way would be to simply xor the previous 6-byte decoded byte values over your remaining 6 bytes of tail data. Until the DES protected data has been decoded, the xor value cannot be determined.
Of course, if the DES portion was all-zeros then your tail codes are going to be visible. If this is a rare occurrence this may not be an issue as the hacker does not know that the DES data is zeros. You don't have a lot of data, so it is hard to think of much more that you could do.
In general, you could generate a 64-bit (8-byte) checksum of the previous 1K of original data, and use this as the xor. In general you will have a number less than 8 bytes of tail data, which you would xor, and only transmit the number of bytes. On decode, the remaining bytes are ignored.

Do I need output buffer to be multiple of AES block size for CTR mode?

In my C code, I use OpenSSL's AES in CTR mode for encryption via EVP interface, that is I have something like this:
ret = EVP_EncryptInit_ex(ctx, EVP_aes_256_ctr(), NULL, key, iv))
...
ret = EVP_EncryptUpdate(ctx, out, &outlen, in, inlen);
Length of input data isn't multiple of AES block size, so if used, say, CBC mode, I would have to allocate additional bytes for padding in output buffer.
Do I need to do the same thing in CTR mode? And also I'm curious about CFB and OFB modes.
OpenSSL man on EVP_EncryptUpdate doesn't say anything specific about "stream-like" modes, they just warn additional bytes for padding are needed.
TLDR: EVP does not pad CTR (and other stream modes/ciphers)
Actually EVP_EncryptUpdate itself never pads, although it can 'carry' a partial block from one call to the next; thus if the ... in your code does not include any previous EncryptUpdate then this (first) EncryptUpdate will always be block-aligned and its output will never be longer:
EVP_EncryptUpdate() encrypts inl bytes from the buffer in and writes the encrypted version to out. This function can be called multiple times to encrypt successive blocks of data. The amount of data written depends on the block alignment of the encrypted data: as a result the amount of data written may be anything from zero bytes to (inl + cipher_block_size - 1) so out should contain sufficient room. The actual number of bytes written is placed in outl. It also checks if in and out are partially overlapping, and if they are 0 is returned to indicate failure.
It is EVP_EncryptFinal[_ex] that adds the padding and thus 'extra' output when needed as described in the next paragraph of the man page:
If padding is enabled (the default) then EVP_EncryptFinal_ex() encrypts the "final" data, that is any data that remains in a partial block. It uses standard block padding (aka PKCS padding) as described in the NOTES section, below. The encrypted final data is written to out which should have sufficient space for one cipher block. The number of bytes written is placed in outl. After this function is called the encryption operation is finished and no further calls to EVP_EncryptUpdate() should be made.
If padding is disabled then EVP_EncryptFinal_ex() will not encrypt any more data and it will return an error if any data remains in a partial block: that is if the total data length is not a multiple of the block size.
However, since the ciphertext actually consists of the output of all the Update calls (or the call if only one) plus the output of the Final call, the total buffer does need to be (up to) one block larger than input in cases where padding applies.
What the man page doesn't say (and should) is that padding doesn't apply to stream ciphers (like RC4) or stream modes (including CTR OFB* CFB*) at all. Each cipher/mode combination is described by an object of type EVP_CIPHER which is a typedef for struct evp_cipher_st; this is what EVP_aes_256_ctr() and similar routines return a pointer to. Among other things this structure contains a field block_size which contains, you guessed it, the block size (in bytes). For stream modes and ciphers it contains a dummy value 1 which is treated specially: EncryptFinal_ex does not add padding even if enabled, and DecryptFinal_ex does not remove and check padding even if enabled.
Do I need output buffer to be multiple of AES block size for CTR mode?
No, CTR mode does not have that requirement. In this respect, CTR mode is like OFB anb CFB mode. In contrast ECB and CBC mode requires a multiple of the block size.
OpenSSL uses the Init/Update/Final pattern, and both input and output bytes are buffered until needed. You can insert one byte at a time, if you'd like.
Internally, the EVP object will buffer input until there's enough to perform an encryption. Since CTR mode can be streamed, one output byte will be available after processing one input byte (its a simple XOR of the encrypted counter with the plain text). The EVP object will also buffer output bytes until you call Final.

Performance of open SSL on large text

I am currently using openSSL on linux c. I would like to ask if the cipher block size is always fixed to 16 unsigned char? The reason for this is because I am encrypting a massively large data. The problem is based on the description of SSL, the ecrypted cipher block of the previous block is XOR to the next plain text block when encrypting. Is there a way to icrease the size of the cipher block?
For example if I had a Gigabyte of data to encrypt, it would take Gigabyte/16byte times of encryption. Is there a standard way to force the AES_cbc_encrypt method call to use a vi that is not 16 byte? The still follows the standard? The reason for this is the encrypted text would be read by another program on another system that uses another CGC standard library.
No, the AES standard mandates a 16 byte block size. The original algorithm which it was based on, Rijndael, allowed more flexibility, but you can't rely on another AES implementation to support that.
For block ciphers, the size of the block is the property of the algorithm in question. DES used 8 bytes for instance. Stream ciphers (RC4 for example) on the other hand do not use fixed block sizes, they are effectively pseudo random generators seeded with the key.
But anyway, the performance of ciphers are published with the block size in mind, and AES does about 160mbyte/sec on a recent CPU. You can measure this using "openssl speed aes".
So, if you want to encrypt 1Gb of data, I'd be more concerned with moving all that data from disk, to memory and back again, rather than the encryption speed of AES itself.

DES encryption and cipher modes

I need to encrypt an ISO 8583 message... the problem here is that the message is longer than the key. I need some one help me how to encrypt this string.
For example: I have 300 chars in my string; should I encrypt each 16 chars alone then concat them, since my master key length is 16 bytes?
I appreciate your help...
ISO 8583-1:2003 Financial transaction card originated messages -- Interchange message specifications -- Part 1: Messages, data elements and code values.
DES is a block cipher, and block ciphers have different modes of operation.
The mode you mentioned is known as ECB (Electronic Codebook), and is not very secure (actually, neither is DES, but more on that later).
I'd suggest you use CBC or some other mode.
You can read about block cipher modes of operation here: Block cipher modes of operation
As for the cipher itself, I'd suggest you avoid using DES if this is at all possible. DES is extremely easy to crack nowadays. Please use AES, or at least 3DES if AES is not available.
EDIT: In response to the updated question, yes, you would need to pad the last block if the plaintext size is not a multiple of the block size.
There are many different modes of operation for a block cipher.
If you just need to applay ECB to your plain text, just split the plain text into equally sized blocks of size 8 bytes (DES block size) and encrypt each separately.
Depending on what you want to achieve, you could also use
ECB which encrypts block wise with each block being independent from all other (previous) blocks. Drawback: known plain text attacks can reveal patterns in your cipher text because the same plain text will always be encrypted to the same cipher text
CBC here you have an initialization vector and each blocks depends on all previously encrypted blocks. This is why you need an IV for the first block.
CFB this is an interesting one because it allows you to turn your block cipher into a stream cipher, which might be useful if you want to encrypt a video stream or whatever (similarly for OFB which basically generates a key stream)
CTS cipher text stealing might be of use if you want to encrypt data but want to avoid padding. A usage example might be to encrypt a blob in your database, which you cannot resize after it has been written to the DB.
There are still many more modes, but these are the most commonly used ones (imho).
As others have pointed out visit Wikipedia for all the details.
Update:
As for the padding, you have different possibilities. I'd recommend to use the ANSI X.923 standard which basically requires you to pad the last buffer with zeroes and append a counter in the last byte which gives you the number of valid bytes in the last block. The same idea is used in ISO10126 but this time padding is done with random bytes.
Note that you can avoid padding at all when using CTS.
Maybe ask yourself if it's actually easier to use a crypto library to do the job for you.
If you're using C++ go for Crypto++ (not so straightforward, but consistent c++ style), Java and .NET have built in crypto providers. If you want to use plain C i can recommend libTomCrypt (very easy to use).
The key length does not impose a limit on the message size. The message can be as long as you want, and your 128-bit key (nonstandard for DES?) will still be good. The DES cipher operates on blocks of bytes, one block at a time. Standard DES uses a 56-bit key (plus 8 parity bits) and 64-bit blocks.
should I encrypt each 16 chars alone
then concat them, since my master key
length is 16 bytes?
Ciphers in general do not require the key and block sizes to be the same; they can define complicated operations taking a given block of cleartext and transforming it with the key to a block of ciphertext (usually of the same size). When multiple blocks need to be encrypted, a mode of operation is specified to describe how one block relates to the next block in the process.
When operating in the electronic codebook (ECB) mode, the message is divided into blocks, and each block of cleartext is encrypted separately with the same key (the resulting blocks of ciphertext are then concatenated). Like other modes of operation for DES (i.e. CBC, CFB, OFB), this approach has its pros and cons. You will need to pick the mode most suitable for your application.
Btw, you should also be aware that DES is now considered insecure.
You need to look up encryption modes - which have names such as Cipher Block Chaining (CBC) and the 'do not use' mode Electronic Code Book (ECB), and even some exotic names like the Infinite Garble Extension (IGE). That page has a beautiful illustration of why the ECB mode should not be used.
CBC is a standard, solid mode of operation. OFB and CFB are also widely used.
You realize that the US Federal Government no longer uses plain DES because it is not secure enough (because it uses a 56-bit key and can be broken by brute force)? Triple-DES is just about tolerated - it has a 112-bit or 168-bit key, depending on which way you use it. The standard, though, is Advanced Encryption System, AES. Unless you have backwards compatibility reasons, you should use AES and not DES in new production code.
Also, you should know the answers to these questions before trying to write production code. I trust this is in the nature of homework or personal interest.
You might want to encrypt for the following reason:
Transmit secured data that is the PIN Block (Data Element 52), this one is taken care of by the HSM, by the time you translate from the acquirer key to the issuer key.
or to MAC the message, then you would select some fields to hash and append to the end of the message (usually Data Element 124 or 128)
or you want to store the ISO 8583 message and want to comply with PCI DSS regulations, in this case you would encrypt the following data elements if present
DE 2 - Card Number
DE 14 - Expiry Date
DE 35 - Track II
DE 45 - Track I
DE 48 - EMV (Chip) Data (MasterCard TLV)
DE 52 - PIN Block
DE 55 - EMV (Chip) Data (Visa)
one more thing, your Master Key should be 128 bits to comply with Visa mandates (Triple DES Mandates to have LMK at least double length key that is 32 digits - 128 bits key)

Resources