hash.c 2.06 KB
Newer Older
lwc-tester committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

#include <stdio.h>
#include<string.h>
#include "photon.h"
#include "api.h"
#include "crypto_hash.h"
int crypto_hash(
                u8 * out,
                const unsigned char *in,
                unsigned long long inlen
                )
{
    u8 upper[CRYPTO_BYTES/2]={0},lower[CRYPTO_BYTES/2]={0},block[CRYPTO_BYTES]={0};
    int flag=0;
    if (inlen==0) {
        PHOTON_256_Permutation(block);
        for (int i=0; i<(CRYPTO_BYTES/2); i++) {
            out[i]= block[i];
        }
        PHOTON_256_Permutation(block);
        for (int i=0; i<(CRYPTO_BYTES/2); i++) {
            out[i+(CRYPTO_BYTES/2)]= block[i];
        }
        return 0;
    }
    
    ul count=0;
    int i =0;

    
    while (count<inlen) {
        for(i=0;i<(CRYPTO_BYTES/2);i++)
        {
            
            if(count<inlen)
                upper[i] = in[count];
           else if(count==inlen)
            {
                upper[i] =0x01;
                flag=1;
            }
            if(count>inlen)
            {
                upper[i]= 0x00;
            }
            if(count>CRYPTO_BYTES/2)
                lower[i] = in[count - (CRYPTO_BYTES/2)];
           
            count = count+1;
        }
        
    
            for (i=0; i<CRYPTO_BYTES/2; i++)
            {
                block[i] = block[i]^upper[i];
                block[i+(CRYPTO_BYTES/2)]= block[i+(CRYPTO_BYTES/2)]^lower[i];
            }
        
            PHOTON_256_Permutation(block);
    }
    
    for (i=0; i<(CRYPTO_BYTES/2); i++)
    {
        lower[i] = upper[i];
        
        upper[i] = 0x00;
    }
    
    if(flag==1)
        upper[0] = 0x02;
    else
        upper[0] = 0x01;
    
    for (i=0; i<(CRYPTO_BYTES/2); i++)
    {
        
        block[i] = block[i]^upper[i];
        block[i+(CRYPTO_BYTES/2)]= block[i+(CRYPTO_BYTES/2)]^lower[i];
        
    }

    
    PHOTON_256_Permutation(block);
    
    for(i=0;i<(CRYPTO_BYTES/2);i++)
        out[i] = block[i];
    PHOTON_256_Permutation(block);
    for(i=0;i<(CRYPTO_BYTES/2);i++)
        out[i+(CRYPTO_BYTES/2)] = block[i];
    
    return 0;
}