Is AES in PHP safe?

Asked

Viewed 218 times

3

I am intending to implement AES in an area of an intranet that is under development for the purpose that the user can store important data such as account passwords and server access passwords, in addition to data transfer via an API to external servers, but I am concerned about security as I am using a ready class: Link.

Could make me wonder if this class is safe enough for this kind of implementation?

1 answer

7


No. Generally speaking, it is only safe to use libraries that have undergone a lot of scrutiny by experts in this area. Not only does the implementation need to be correct, but it needs to take into account things like side-Channel Attacks (i.e. attacks that exploit flaws in implementation of the algorithm, not in its logic). This is something that "ordinary" developers are not qualified to evaluate.

However, in this case it is not necessary to go so far: a glance at the source code revealed that it operates according to the ECB mode of operation:

public function encrypt($text)
{
    $t = ""; // 16-byte bloco
    $y = ""; // Para retorno do bloco cifrado.


    $xsize = strlen($text);
    for ($i = 0; $i < $xsize; $i += 16){
        for ($j = 0; $j < 16; $j++){
            if (($i+$j) < $xsize){
                $t[$j] = $text[$i+$j];
            }else{
                $t[$j] = chr(0);
            }
        }

        $y .= $this->encryptBlock($t);
    }

    return $y;
}

ECB is a "naive" implementation of cryptography, the way a person who has only studied its basics would implement it. And it is totally insecure. The image below was encrypted using ECB, you can guess what it is about?

ECB

As for a safe alternative, unfortunately I know little about PHP to indicate something. The functions mcrypt_generic and mdecrypt_generic must be good enough, but I don’t know how to use them properly. That answer on Soen also gives some suggestions that at first glance look good (but again, I can not evaluate). And if you have access to Openssl, maybe there is some AES encryption option that you can use (although the focus of this library is public key encryption).

Whatever solution you choose, remember to choose a safe operation mode and preferably, authenticated (e.g. CCM, GCM, EAX or OCB)..

  • +1 for the answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.