0

i did look around - and everywhere on google - for the direct and clear answer to this question, but got nowhere. so here plain and simple yes or no would be awesome...

i have been asked to create an "AES-256 symmetric (also called "session") key" - not encryption, just the key - to use to sign data within a soap message headers. the requirement is that the key is 256 "in size"... yea, i know...

i know that the MCRYPT_RIJNDAEL_128 size 32 is really the AES-256 cipher in php. i read all of that.

i know that the people asking me that have an implementation in java that works for them. i have to work with php. i have the following code: `

$keysize = mcrypt_module_get_algo_key_size(MCRYPT_RIJNDAEL_128);
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '',MCRYPT_MODE_CBC, '');
$keysize = 32;
while (strlen($key) < $keysize) $key =  mcrypt_create_iv(mcrypt_enc_get_iv_size ($td),MCRYPT_RAND);  

this does give me a raw "key".

the question: is the raw key above an "AES-256 session/symmetric key" that can be used to sign soap security headers?

after the raw key is used for the signature, it gets encrypted using a key from a public certificate (using rsa-oaep-mgf1p) and embedded within the headers (EncryptedKey tag). it has to be decrypted on the other end and used to verify the digests of the signed headers... and then gandalf shows up...

i'm asking because i have been told from the other side (using java) that "there is something wrong how the symmetric key is being generated" but without a clear explanation of what is wrong about it and how to right that wrong.

can anybody help? i'd appreciate it...

thanks.

Diogo Rocha
  • 9,759
  • 4
  • 48
  • 52
  • It probably is, but this code is needlessly complicated. – Artjom B. Apr 26 '16 at 07:24
  • ok. so random bytes() or openssl_random_pseudo_bytes() is better?! also - there is no indication about this anywhere but since i have to encrypt the key - do i have to base64_encode it before encryption? the code i found on soap security encrypts the raw key. just making sure so i can have enough information. thanks. – darwingoat Apr 26 '16 at 14:13
  • Yes, the two alternatives are better in terms of code, because you don't have to use a loop. I have no experience with SOAP encryption, so no I have no idea how it can be used after generating the key. – Artjom B. Apr 26 '16 at 14:42

1 Answers1

0

Your way is ok. But a simplier way is

random_bytes(32)

or

openssl_random_pseudo_bytes(32)

if the version of your php is below 7.0.

You may also want to generate a 16-byte IV

random_bytes(16)
v7d8dpo4
  • 1,399
  • 8
  • 9