chashprintinghexkeccak

My C program prints 0xE instead of 0x0E in Keccak implementation


Im working on an implementation of Keccak(SHA-3) and for example I got this:

Keccak("abc") = 3A985DA74FE225B245C172D6BD390BD855F86E3E9D525B46BFE24511431532

Instead of (according: http://www.di-mgt.com.au/sha_testvectors.html)

3A985DA74FE225B2045C172D6BD390BD855F086E3E9D525B46BFE24511431532

As you can see my program miss a "0x00" and Im not sure what I am doing wrong.

Sorry guys here it is my main method code:

#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include "round.h"
#include "keccak.h"
#define TAM_MAX_ENTRADA 4096 /*Especificacion del numero de caracteres de la entrada*/
int main(int argc, char *argv[])
{

    int32_t sha3Tipo; /*Nos dice el tipo de SHA3*/
    char Mensaje[TAM_MAX_ENTRADA]; /*Array de caracteres donde guardamos el mensaje de  entrada*/
    printf("Elige el tamaño de salida (224,256,384,512): "); 
    scanf("%d",&sha3Tipo);
    if(argc == 1)
    {
        strcat(Mensaje,""); /*Si no nos han pasado argumentos significa que es un string vacio*/
    }
    else
    {
        strcpy(Mensaje,argv[1]); /*Copiamos la primera palabra en el array de caracteres*/
    }

    for(int32_t i = 2; i<argc; i++)
    {
        strcat(Mensaje," "); /*Si tenemos mas de una palabra entonces anadimos */
        strcat(Mensaje,argv[i]);
    }
    int32_t size = strlen(Mensaje);
    int32_t *psize = &size;
    uint8_t *newmessage;

    newmessage=keccak((uint8_t *)Mensaje, *psize, sha3Tipo);
    printf("Keccak(\"%s\") = ",Mensaje);
    for(int32_t i =0; i<sha3Tipo/8; i++)
    {   
        printf("%X", *(newmessage+i));
    }
    printf("\n");

    return 0;
}

Solution

  • Perhaps you're losing the leading zeroes when converting the numbers into hexadecimal strings? For example, consider the following code:

    #include <stdio.h>
    int main(int argc, char *argv[])
    {
        printf("Without formatting: %x and %x\n", 12, 99);
        printf("With formatting: %02x and %02x\n", 12, 99);
        return 0;
    }
    

    When run, the output is:

    Without formatting: c and 63 With formatting: 0c and 63

    I'm thinking that your 4 and 8 should be 04 and 08, but they are not being formatted correctly.