PHP Classes

Kravatte PHP Keccak Farfalle: Encrypt and decrypt data based on Farfalle

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 49 All time: 10,716 This week: 129Up
Version License PHP version Categories
kravatte 1.0The PHP License5PHP 5, Cryptography
Description 

Author

This class can encrypt, decrypt and authenticate data based on Keccak Farfalle modes.

It can take data and computes a hash using a given secret key using Farfalle.

The class can also encrypt and decrypt data with a given key also using Farfalle modes.

Innovation Award
PHP Programming Innovation award nominee
January 2021
Number 12
Kravatte is a function that implements a cryptography approach that can be used to encrypt data incrementally as a sequence of blocks of data that can be appended to the encryption output.

This approach is useful when you need to encrypt a stream of data with a length that is unknown initially.

This class implements a pure PHP based solution to encrypt and decrypt data using the Kravatte Farfalle construction.

Manuel Lemos
Picture of Jose Luis Lucas
Name: Jose Luis Lucas <contact>
Classes: 10 packages by
Country: Spain Spain
Age: ???
All time rank: 249163 in Spain Spain
Week rank: 49 Up2 in Spain Spain Up
Innovation award
Innovation award
Nominee: 7x

Example

<?
   
include "kravatte_final.php";
   
    function
xtrae($key,$vector,$x="")
        {
       
$k = explode(",",explode('])',explode("$key = $x"."bytes([",$vector)[1])[0]);
       
$zk = "";foreach ($k as $m) $zk .=@explode("0x",trim($m))[1];
        return
pack("H*",$zk);
        }
       
   
$x=new Kravatte;

   
// Test Vectors

   
$server = "http://raw.githubusercontent.com/inmcm/kravatte/master/tests";
   
   
$fvectors = ["mac"];

    foreach (
$fvectors as $f)
        {
       
$count = 0;
        echo
"Kravatte $f ";
       
$vectors = file_get_contents("$server/test_kravatte_$f.py");
       
$vectors = explode("def test_",$vectors);

        foreach (
array_slice(array_slice($vectors,1),0,-1) as $vector)
            {
           
$count++;
           
$type = explode('(',explode('kravatte_',$vector)[1])[0];
           
$size = explode("_",explode("(",$vector)[0]); $size = $size[sizeof($size)-1];
                   
           
$zk = xtrae("my_key",$vector);
           
$zm = xtrae("my_message",$vector);
           
$out = bin2hex(xtrae("real_output",$vector));
           
           
$res = $x->mac($zk,$zm, $size);
       
            if ((
$res != $out))
                die(
"$count Bad");
            }
        echo
"OK $count tested\n";
        }

   
$fvectors = ["sane"];

    foreach (
$fvectors as $f)
        {
       
$count = 0;
        echo
"Kravatte $f ";
       
$vectors = file_get_contents("$server/test_kravatte_$f.py");
       
$vectors = explode("def test_kravatte",$vectors);

        foreach (
array_slice($vectors,1) as $vector)
            {
           
$count++;
           
$type = explode('(',explode('SANE_',$vector)[1])[0];
                       
           
$zk = xtrae("my_key",$vector);
           
$zm = xtrae("my_message",$vector);
           
$meta = xtrae("my_metadata",$vector);
           
$nonce = xtrae("my_nonce",$vector);
           
$output_ciphertext = bin2hex(xtrae("output_ciphertexts",$vector,"["));
           
$output_tag = bin2hex(xtrae("output_tags",$vector,"["));
           
           
$x->Kravatte_SANE($nonce,$zk);
           
            [
$cipher,$tag] = $x->Kravatte_SANE_enc($zm, $meta);
           
            if ((
$cipher != $output_ciphertext) or ($tag != $output_tag))
                die(
"$count Bad");
            }
        echo
"OK $count tested\n";
        }
               
   
$fvectors = ["sanse"];

    foreach (
$fvectors as $f)
        {
       
$count = 0;
        echo
"Kravatte $f ";
       
$vectors = file_get_contents("$server/test_kravatte_$f.py");
       
$vectors = explode("def test_kravatte",$vectors);

        foreach (
array_slice($vectors,1) as $vector)
            {
           
$count++;
           
$type = explode('(',explode('SANSE_',$vector)[1])[0];
       
           
$zk = xtrae("my_key",$vector);
           
$zm = xtrae("my_message",$vector);
           
$meta = xtrae("my_metadata",$vector);
           
$output_ciphertext = bin2hex(xtrae("output_ciphertexts",$vector,"["));
           
$output_tag = bin2hex(xtrae("output_tags",$vector,"["));
           
           
$x->Kravatte_SANSE($zk);
           
            [
$cipher,$tag] = $x->Kravatte_SANSE_enc($zm, $meta);
           
            if ((
$cipher != $output_ciphertext) or ($tag != $output_tag))
                die(
"$count Bad");
            }
        echo
"OK $count tested\n";
        }
               
   
$fvectors = ["wbc"];

    foreach (
$fvectors as $f)
        {
       
$count = 0;
        echo
"Kravatte $f ";
       
$vectors = file_get_contents("$server/test_kravatte_$f.py");
       
$vectors = explode("def test_kravatte",$vectors);

        foreach (
array_slice($vectors,2) as $vector)
            {
           
$count++;
           
$type=explode('(',explode('WBC_',$vector)[1])[0];
                   
           
$zk = xtrae("my_key",$vector);
           
$zm = xtrae("my_message",$vector);
           
$tweak = xtrae("my_tweak",$vector);
           
$output_ciphertext = bin2hex(xtrae("real_ciphertext",$vector));
           
           
$x->Kravatte_WBC(strlen($zm), $tweak,$zk);
           
           
$cipher = $x->Kravatte_WBC_enc($zm);
           
            if ((
$cipher != $output_ciphertext))
                die(
"$count Bad");
            }
        echo
"OK $count $f tested\n";
        }
       
   
$fvectors = ["wbc_ae"];

    foreach (
$fvectors as $f)
        {
       
$count = 0;
        echo
"Kravatte $f ";
       
$vectors = file_get_contents("$server/test_kravatte_$f.py");
       
$vectors = explode("def test_kravatte",$vectors);

        foreach (
array_slice($vectors,1) as $vector)
            {
           
$count++;
           
$type = explode('(',explode('WBC_AE_',$vector)[1])[0];

           
$zk = xtrae("my_key",$vector);
           
$zm = xtrae("my_message",$vector);
           
$meta = xtrae("my_metadata",$vector);
           
$output_ciphertext = bin2hex(xtrae("real_ciphertext",$vector));
                       
           
$x->Kravatte_WBC_AE(strlen($zm), $zk);
           
           
$cipher = $x->Kravatte_WBC_AE_enc($zm,$meta);
           
            if ((
$cipher != $output_ciphertext))
                die(
"$count Bad");
            }
        echo
"OK $count $f tested\n";
        }
               
    echo
"Kravatte oracle ";
   
$vectors = file_get_contents("$server/test_kravatte_oracle.py");
   
$vectors = explode("def test_kravatte",$vectors);

    foreach (
array_slice($vectors,1) as $vector)
        {
       
$type=explode('(',explode('random_oracle_',$vector)[1])[0];

       
$zk = xtrae("my_key",$vector);
       
$zm = xtrae("my_message",$vector);
       
$real = bin2hex(xtrae("real_output",$vector));

           
$x->KravatteOracle($zm, $zk);
   
           
$index_ref = [[0, 1], [200, 202], [400, 404], [600, 608], [800, 816], [1000, 1032],
                         [
1200, 1264], [1400, 1528], [1600, 1856], [2000, 2512], [2600, 3624],
                         [
3800, 5848]];
   
            for (
$h=0;$h<12;$h++)
            {
               
$k = 2**$h;
               
$res = $x->random($k);
                [
$start, $stop] = $index_ref[$h];
               
$m = substr($real,$start*2,$stop*2-$start*2);
           
            if ((
$m != $res))
                die(
"$k Bad");
            }
        }
    echo
"OK \n\n";


Details

Kravatte

PHP-Kravatte Achouffe Cipher Suite: Encryption, Decryption, and Authentication Tools based on the Farfalle modes

Based on this Python implementation with minor changes https://github.com/inmcm/kravatte

Kravatte is a high-speed instance of Farfalle based on Keccak-p[1600] permutations, claimed to resist against classical and quantum adversaries. Modes for authentication, encryption and authenticated encryption are defined accordingly.

https://keccak.team/2017/updated_farfalle_kravatte.html https://eprint.iacr.org/2016/1188.pdf

It pass all tests from https://github.com/inmcm/kravatte/tree/master/tests

2021 @denobispsis

$x=new Kravatte;

MAC

$x->mac('Supersecreto', 'Et in Arcadia ego', 64)

SANE

$x->Kravatte_SANE($nonce,$key);

$cipher = $x->Kravatte_SANE_enc($message, $metadata);

[$cipher, $val]

$x->Kravatte_SANE($nonce,$key);

$plain = $x->Kravatte_SANE_dec(pack("H",$cipher[0]), $meta, pack("H",$cipher[1]));

[$message, $val]

SANSE

$x->Kravatte_SANSE($key);

$cipher = $x->Kravatte_SANSE_enc($message, $metadata);

[$cipher, $val]

$x->Kravatte_SANSE($key);

$plain = $x->Kravatte_SANSE_dec(pack("H",$cipher[0]), $meta, pack("H",$cipher[1]));

[$message, $val]

WBC

$x->Kravatte_WBC(strlen($message), $tweak,$key);

$cipher = $x->Kravatte_WBC_enc($message);
	
$x->Kravatte_WBC(strlen($message), $tweak,$key);

$message = $x->Kravatte_WBC_dec(pack("H*",$cipher));

WBC_AE

$x->Kravatte_WBC_AE(strlen($message), $key);

$cipher = $x->Kravatte_WBC_AE_enc($message,$metadata);

$x->Kravatte_WBC_AE(strlen($message),$key);

$plain = $x->Kravatte_WBC_AE_dec(pack("H*",$cipher),$metadata);

Oracle

$x->KravatteOracle($message, $key);

$x->random($size);


  Files folder image Files (3)  
File Role Description
Plain text file kravatte_final.php Class Class source
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file test_kravatte.php Example Example script

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:49
This week:0
All time:10,716
This week:129Up