-2

I am implementing the sha256 algorithm in C. I don't need help with the algorithm. One of the tasks while implementing the algorithm is to pad bits to a message such that the message is either 512 bits long or its length is a multiple of 512 bits.

I initially thought that padding bits to a message would simply mean padding zeroes to it. But since the message is a character message(a string), I don't think my logic is correct.

I don't have a problem in the code. I just don't know how to pad bits to the string(msg in the code)

    #include<stdio.h>
    #include<stdlib.h>
    int main(int argc,char *argv[])
    {
       char *msg=(char*)malloc(sizeof(char)*512);
       printf("\n Enter a message");
       scanf("%s",msg);
       //I don't know how to pad bits to msg
       return 0;
    }

1 Answers1

0

I initially thought that padding bits to a message would simply mean padding zeroes to it.

There are algorithms that expect a constant size of the block, ex. AES or sha256. They are called, literally, "block algorithms", ex. block ciphers. When dealing with messages shorter then the size of the block you have to fill the unused bits with something.

Usually, by convention, for simplicity, they are filled with zero. There are standards that say with what you may pad the unused bits/bytes. Sometimes they are filled with random bytes or as with a single 1 and then zeros 0000...00 bits, etc. See ex. this wiki page

The client has to know the length of the message. In case of C strings, I would just pad it with zero bytes. C strings end with a zero byte anyway, so the user can just strlen(received_message) and he will know the length of the string.

In case of dealing with arbitrary data, you have to pass the length of the message in some way. Maybe some other way or with the message, at the beginning of it, for example. So, for example, you can choose the first 4 bytes to represent the message length. Or you can specify that the message starts with a C string with the message length encoded using a number represented in base 16 using ASCII characters (so you can read the message length with %x) and after the string your message continues.

How do I pad bits to a string in C?

Just initialize the msg memory with zeros, either with calloc or meset:

#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[]) {
   char *msg = calloc(128, sizeof(*msg));
   if (msg == NULL) abort();
   printf("\n Enter a message");
   if (scanf("%127s", msg) != 1) abort()
   # bits are padded with zeros, proceed with your algorithm
   return 0;
}

or you can memset only the unused bytes in the memory:

#include<stdio.h>
#include<stdlib.h>
#include <string.h>
int main(int argc,char *argv[]) {
   char *msg = malloc(128 * sizeof(*msg));
   if (msg == NULL) abort();
   printf("\n Enter a message");
   if (scanf("%127s", msg) != 1) abort();
   memset(&msg[strlen(msg)], 0, 128 - strlen(msg));
   # bits are padded with zeros, proceed with your algorithm
   return 0;
}

Notes to your code:

KamilCuk
  • 120,984
  • 8
  • 59
  • 111