Filling memory with random bytes - C / Objective-C

I use CommonCrypto for encryption in Objective-C (AES256), and I would like to provide an IV (initialization vector) for more secure encryption. I am doing this now:

const void* iv = malloc(kCCBlockSizeAES128); // EDIT: //if (!iv) { // iv = NULL; //} 

and then create a cryptor object:

 CCCryptorRef cryptor; CCCryptorStatus cryptStatus = CCCryptorCreate(operation, kCCAlgorithmAES128, kCCOptionPKCS7Padding, keyPtr, kCCKeySizeAES256, iv, &cryptor); 

The problem is that encryption seems unsuccessful in this way (sad face ...). I mean: it encrypts without visible problems, but it decrypts data other than the original. I, although this will work, because when you malloc() memory, it is not written completely to zeros, it is random. I also tried to write random values ​​myself, but my C-background doesn’t work much. If there is a function (e.g. bzero ) that writes random bytes, tell me.

I also tried to do something like this:

 char* iv = malloc(kCCBlockSizeAES128); int i; srand((unsigned int)time(NULL)); for (i = 0; i < kCCBlockSizeAES128; i++) { iv[i] = (char)rand()%256; } 

Oh, by the way, I understand that this must be a very dangerous question :)

In the end, all I want is something like const void* iv = malloc(kCCBlockSizeAES128) , that after some operations I am sure that the data is completely random. Any ideas on this?

PS: I just provided the crypto / Objective-C background so that you know what I need it for. I think it will not affect something. kCCBlockSizeAES128 = 16 (90% sure :)

EDIT:

Allright! After some deactivation, I am pleased to report that the problem that I encountered with encryption and decryption was due to an error in another part of my program that I just solved. So now I need to understand how to fill iv with random bytes. Some options:

  • Use malloc (), which returns unwanted rather than random bytes -> potentially unsafe (?)
  • Use arc4random_buf (), which is exactly what I want, except that it only works 10.7+ and my mac is 10.6.6 (plus I want to support 10.6).
  • Something else I did not consider ...? <- help is here!

EDIT 2:

Allright! After filling iv with some test data (all zeros, everything, etc.), and some are more deactivated, I am NOT happy to announce that ccrypto does not seem to work in some conditions. I will explain how:

Whenever I feed krypton with a zero value of iv or NULL (the same for cryptography), it works. For example, this works well for encryption and decryption:

 uint8_t iv[kCCBlockSizeAES128]; int i; for (i = 0; i < kCCBlockSizeAES128; i++) { iv[i] = 0x0; // I know this is the same as doing: memset((void *)iv, 0x0, (size_t)sizeof(iv)); } CCCryptorRef cryptor; CCCryptorStatus cryptStatus = CCCryptorCreate(operation, kCCAlgorithmAES128, kCCOptionPKCS7Padding, (const void *)keyPtr, kCCKeySizeAES256, iv, &cryptor); 

BUT , when I give it iv, in which at least one of its bytes is NOT equal to zero, encryption / decryption does not provide errors, but decryption does not give the source data. For example, this is ...

 uint8_t iv[kCCBlockSizeAES128]; int i; for (i = 0; i < kCCBlockSizeAES128; i++) { iv[i] = 0x1; } 

or for completely random data ...

 uint8_t iv[kCCBlockSizeAES128]; int i; for (i = 0; i < kCCBlockSizeAES128; i++) { iv[i] = arc4random() % 256; } 

... will not work.

I don't understand this logic one bit ... Any ideas?

+4
source share
1 answer

You can use arc4random_buf to fill the buffer with random data:

 #include <stdlib.h> #include <stdint.h> uint8_t iv[kCCBlockSizeAES128]; arc4random_buf(&iv, kCCBlockSizeAES128); 

In addition, the memory block returned by malloc (as in any uninitialized memory) is filled with garbage. You should not assume that it will be filled with anything, especially non-cryptographically useful random numbers.

+10
source

All Articles