From dd3680c8b19a7b07820ae5e32919ff7aee8ed0ae Mon Sep 17 00:00:00 2001 From: Amelia Lach Date: Mon, 30 Sep 2024 00:53:16 -0700 Subject: [PATCH] Finished implementation of encode and decode --- Makefile | 8 ++- encrypted.txt | 1 + project1.txt | 1 + user/arcfour.c | 49 +++++++++++++++ user/arcfour.h | 30 ++++++++++ user/base64.c | 137 ++++++++++++++++++++++++++++++++++++++++++ user/base64.h | 27 +++++++++ user/decode.c | 57 ++++++++++++++++++ user/encode.c | 136 +++++++++++++++++++++++++++++++++++++++++ user/printf.c | 3 + user/sha256.c | 160 +++++++++++++++++++++++++++++++++++++++++++++++++ user/sha256.h | 34 +++++++++++ 12 files changed, 640 insertions(+), 3 deletions(-) create mode 100644 encrypted.txt create mode 100644 project1.txt create mode 100644 user/arcfour.c create mode 100644 user/arcfour.h create mode 100644 user/base64.c create mode 100644 user/base64.h create mode 100644 user/decode.c create mode 100644 user/encode.c create mode 100644 user/sha256.c create mode 100644 user/sha256.h diff --git a/Makefile b/Makefile index 2584e4a..29893d4 100644 --- a/Makefile +++ b/Makefile @@ -87,7 +87,7 @@ $U/initcode: $U/initcode.S tags: $(OBJS) _init etags *.S *.c -ULIB = $U/ulib.o $U/usys.o $U/printf.o $U/umalloc.o +ULIB = $U/ulib.o $U/usys.o $U/printf.o $U/umalloc.o $U/base64.o $U/sha256.o $U/arcfour.o _%: %.o $(ULIB) $(LD) $(LDFLAGS) -T $U/user.ld -o $@ $^ @@ -117,7 +117,9 @@ mkfs/mkfs: mkfs/mkfs.c $K/fs.h $K/param.h UPROGS=\ $U/_cat\ + $U/_decode\ $U/_echo\ + $U/_encode\ $U/_forktest\ $U/_grep\ $U/_init\ @@ -133,8 +135,8 @@ UPROGS=\ $U/_wc\ $U/_zombie\ -fs.img: mkfs/mkfs README.md $(UPROGS) - mkfs/mkfs fs.img README.md $(UPROGS) +fs.img: mkfs/mkfs README.md project1.txt encrypted.txt $(UPROGS) + mkfs/mkfs fs.img README.md project1.txt encrypted.txt $(UPROGS) -include kernel/*.d user/*.d diff --git a/encrypted.txt b/encrypted.txt new file mode 100644 index 0000000..45b983b --- /dev/null +++ b/encrypted.txt @@ -0,0 +1 @@ +hi diff --git a/project1.txt b/project1.txt new file mode 100644 index 0000000..68b6950 --- /dev/null +++ b/project1.txt @@ -0,0 +1 @@ +hi this is a secret diff --git a/user/arcfour.c b/user/arcfour.c new file mode 100644 index 0000000..9b4515b --- /dev/null +++ b/user/arcfour.c @@ -0,0 +1,49 @@ +/********************************************************************* +* Filename: arcfour.c +* Author: Brad Conte (brad AT bradconte.com) +* Copyright: +* Disclaimer: This code is presented "as is" without any guarantees. +* Details: Implementation of the ARCFOUR encryption algorithm. + Algorithm specification can be found here: + * http://en.wikipedia.org/wiki/RC4 +*********************************************************************/ + +/*************************** HEADER FILES ***************************/ +#include "arcfour.h" +#include "kernel/types.h" +#include "kernel/stat.h" +#include "user/user.h" + +/*********************** FUNCTION DEFINITIONS ***********************/ +void arcfour_key_setup(BYTE state[], const BYTE key[], int len) +{ + int i, j; + BYTE t; + + for (i = 0; i < 256; ++i) + state[i] = i; + for (i = 0, j = 0; i < 256; ++i) { + j = (j + state[i] + key[i % len]) % 256; + t = state[i]; + state[i] = state[j]; + state[j] = t; + } +} + +// This does not hold state between calls. It always generates the +// stream starting from the first output byte. +void arcfour_generate_stream(BYTE state[], BYTE out[], size_t len) +{ + int i, j; + size_t idx; + BYTE t; + + for (idx = 0, i = 0, j = 0; idx < len; ++idx) { + i = (i + 1) % 256; + j = (j + state[i]) % 256; + t = state[i]; + state[i] = state[j]; + state[j] = t; + out[idx] = state[(state[i] + state[j]) % 256]; + } +} diff --git a/user/arcfour.h b/user/arcfour.h new file mode 100644 index 0000000..f9f1e87 --- /dev/null +++ b/user/arcfour.h @@ -0,0 +1,30 @@ +/********************************************************************* +* Filename: arcfour.h +* Author: Brad Conte (brad AT bradconte.com) +* Copyright: +* Disclaimer: This code is presented "as is" without any guarantees. +* Details: Defines the API for the corresponding ARCFOUR implementation. +*********************************************************************/ + +#ifndef ARCFOUR_H +#define ARCFOUR_H + +/*************************** HEADER FILES ***************************/ +#include + +/**************************** DATA TYPES ****************************/ +typedef unsigned char BYTE; // 8-bit byte + +/*********************** FUNCTION DECLARATIONS **********************/ +// Input: state - the state used to generate the keystream +// key - Key to use to initialize the state +// len - length of key in bytes (valid lenth is 1 to 256) +void arcfour_key_setup(BYTE state[], const BYTE key[], int len); + +// Pseudo-Random Generator Algorithm +// Input: state - the state used to generate the keystream +// out - Must be allocated to be of at least "len" length +// len - number of bytes to generate +void arcfour_generate_stream(BYTE state[], BYTE out[], size_t len); + +#endif // ARCFOUR_H diff --git a/user/base64.c b/user/base64.c new file mode 100644 index 0000000..1b600bf --- /dev/null +++ b/user/base64.c @@ -0,0 +1,137 @@ +/********************************************************************* +* Filename: base64.c +* Author: Brad Conte (brad AT bradconte.com) +* Copyright: +* Disclaimer: This code is presented "as is" without any guarantees. +* Details: Implementation of the Base64 encoding algorithm. +*********************************************************************/ + +/*************************** HEADER FILES ***************************/ +//#include +#include "base64.h" +#include "kernel/types.h" +#include "kernel/stat.h" +#include "user/user.h" +/****************************** MACROS ******************************/ +#define NEWLINE_INVL 76 + +/**************************** VARIABLES *****************************/ +// Note: To change the charset to a URL encoding, replace the '+' and '/' with '*' and '-' +static const BYTE charset[]={"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"}; + +/*********************** FUNCTION DEFINITIONS ***********************/ +BYTE revchar(char ch) +{ + if (ch >= 'A' && ch <= 'Z') + ch -= 'A'; + else if (ch >= 'a' && ch <='z') + ch = ch - 'a' + 26; + else if (ch >= '0' && ch <='9') + ch = ch - '0' + 52; + else if (ch == '+') + ch = 62; + else if (ch == '/') + ch = 63; + + return(ch); +} + +size_t base64_encode(const BYTE in[], BYTE out[], size_t len, int newline_flag) +{ + size_t idx, idx2, blks, blk_ceiling, left_over, newline_count = 0; + + blks = (len / 3); + left_over = len % 3; + + if (out == NULL) { + idx2 = blks * 4 ; + if (left_over) + idx2 += 4; + if (newline_flag) + idx2 += len / 57; // (NEWLINE_INVL / 4) * 3 = 57. One newline per 57 input bytes. + } + else { + // Since 3 input bytes = 4 output bytes, determine out how many even sets of + // 3 bytes the input has. + blk_ceiling = blks * 3; + for (idx = 0, idx2 = 0; idx < blk_ceiling; idx += 3, idx2 += 4) { + out[idx2] = charset[in[idx] >> 2]; + out[idx2 + 1] = charset[((in[idx] & 0x03) << 4) | (in[idx + 1] >> 4)]; + out[idx2 + 2] = charset[((in[idx + 1] & 0x0f) << 2) | (in[idx + 2] >> 6)]; + out[idx2 + 3] = charset[in[idx + 2] & 0x3F]; + // The offical standard requires a newline every 76 characters. + // (Eg, first newline is character 77 of the output.) + if (((idx2 - newline_count + 4) % NEWLINE_INVL == 0) && newline_flag) { + out[idx2 + 4] = '\n'; + idx2++; + newline_count++; + } + } + + if (left_over == 1) { + out[idx2] = charset[in[idx] >> 2]; + out[idx2 + 1] = charset[(in[idx] & 0x03) << 4]; + out[idx2 + 2] = '='; + out[idx2 + 3] = '='; + idx2 += 4; + } + else if (left_over == 2) { + out[idx2] = charset[in[idx] >> 2]; + out[idx2 + 1] = charset[((in[idx] & 0x03) << 4) | (in[idx + 1] >> 4)]; + out[idx2 + 2] = charset[(in[idx + 1] & 0x0F) << 2]; + out[idx2 + 3] = '='; + idx2 += 4; + } + } + + return(idx2); +} + +size_t base64_decode(const BYTE in[], BYTE out[], size_t len) +{ + //BYTE ch; + size_t idx, idx2, blks, blk_ceiling, left_over; + + if (in[len - 1] == '=') + len--; + if (in[len - 1] == '=') + len--; + + blks = len / 4; + left_over = len % 4; + + if (out == NULL) { + if (len >= 77 && in[NEWLINE_INVL] == '\n') // Verify that newlines where used. + len -= len / (NEWLINE_INVL + 1); + blks = len / 4; + left_over = len % 4; + + idx = blks * 3; + if (left_over == 2) + idx ++; + else if (left_over == 3) + idx += 2; + } + else { + blk_ceiling = blks * 4; + for (idx = 0, idx2 = 0; idx2 < blk_ceiling; idx += 3, idx2 += 4) { + if (in[idx2] == '\n') + idx2++; + out[idx] = (revchar(in[idx2]) << 2) | ((revchar(in[idx2 + 1]) & 0x30) >> 4); + out[idx + 1] = (revchar(in[idx2 + 1]) << 4) | (revchar(in[idx2 + 2]) >> 2); + out[idx + 2] = (revchar(in[idx2 + 2]) << 6) | revchar(in[idx2 + 3]); + } + + if (left_over == 2) { + out[idx] = (revchar(in[idx2]) << 2) | ((revchar(in[idx2 + 1]) & 0x30) >> 4); + idx++; + } + else if (left_over == 3) { + out[idx] = (revchar(in[idx2]) << 2) | ((revchar(in[idx2 + 1]) & 0x30) >> 4); + out[idx + 1] = (revchar(in[idx2 + 1]) << 4) | (revchar(in[idx2 + 2]) >> 2); + idx += 2; + } + } + + return(idx); +} diff --git a/user/base64.h b/user/base64.h new file mode 100644 index 0000000..e35c6c7 --- /dev/null +++ b/user/base64.h @@ -0,0 +1,27 @@ +/********************************************************************* +* Filename: base64.h +* Author: Brad Conte (brad AT bradconte.com) +* Copyright: +* Disclaimer: This code is presented "as is" without any guarantees. +* Details: Defines the API for the corresponding Base64 implementation. +*********************************************************************/ + +#ifndef BASE64_H +#define BASE64_H + +/*************************** HEADER FILES ***************************/ +#include + +/**************************** DATA TYPES ****************************/ +typedef unsigned char BYTE; // 8-bit byte + +/*********************** FUNCTION DECLARATIONS **********************/ +// Returns the size of the output. If called with out = NULL, will just return +// the size of what the output would have been (without a terminating NULL). +size_t base64_encode(const BYTE in[], BYTE out[], size_t len, int newline_flag); + +// Returns the size of the output. If called with out = NULL, will just return +// the size of what the output would have been (without a terminating NULL). +size_t base64_decode(const BYTE in[], BYTE out[], size_t len); + +#endif // BASE64_H diff --git a/user/decode.c b/user/decode.c new file mode 100644 index 0000000..95f0435 --- /dev/null +++ b/user/decode.c @@ -0,0 +1,57 @@ +#include "base64.h" +#include "arcfour.h" +#include "kernel/types.h" +#include "kernel/stat.h" +#include "user/user.h" +#include "kernel/fcntl.h" + +/** + * This function reads Base64 encoded data from the specified file descriptor in chunks, decodes it back to its original form, and + * prints the decoded content to the console with its size. + * + * @param fd The file descriptor from which the encoded content is read + * + */ +void +b64_decode(int fd) +{ + unsigned char buffer[1024]; + unsigned char result[1024]; + int n; + + while ((n = read(fd, buffer, 1024)) > 0) { + int size = base64_decode(buffer, result, n); + printf("Your decoded secret is %s of size %d", result, size); + } + printf("\n"); +} + +/** + * This function reads command-line arguments to determine whether the arguments invoke the Base64 decoding function. + * It processes the specified input file accordingly. + * + * @param argc The number of command-line arguments + * @param argv The command-line arguments + * @return This function prints an error message and exits or closes and exits + * + */ +int +main(int argc, char *argv[]) +{ + char *crypto_option = argv[1]; + char *file_name = argv[2]; + + int fd = open(file_name, O_RDONLY); + if (fd < 0) { + printf("ERROR, could not open the file %s\n", file_name); + exit(1); + } + + if (strcmp(crypto_option, "-b") == 0) { + b64_decode(fd); + } + + close(fd); + exit(1); + +} diff --git a/user/encode.c b/user/encode.c new file mode 100644 index 0000000..e3a7747 --- /dev/null +++ b/user/encode.c @@ -0,0 +1,136 @@ +#include "sha256.h" +#include "base64.h" +#include "arcfour.h" +#include "kernel/types.h" +#include "kernel/stat.h" +#include "user/user.h" +#include "kernel/fcntl.h" + +/** + * This function initializes the SHA256 context, reads data from the specified file descriptor in chunks, updates the SHA256 context + * with the read data, and finally outputs the resulting hash to the console. + * + * @param fd The file descriptor from which data is read. + * + */ +void +sha256_encode(int fd) +{ + SHA256_CTX ctx; + sha256_init(&ctx); + + unsigned char buffer[1024]; + unsigned char hash_result[SHA256_BLOCK_SIZE]; + int n; + + while ((n = read(fd, buffer, 1024)) > 0) { + sha256_update(&ctx, buffer, n); + } + + sha256_final(&ctx, hash_result); + + printf("Your encoded secret is... "); + for (int i = 0; i < SHA256_BLOCK_SIZE; i++) { + printf("%h", hash_result[i]); + } + printf("\n"); +} + +/** + * This function reads data from the specified file descriptor in + * chunks, encodes it using Base64, and writes the encoded data to "encrypted.txt". The output file is created or overwriten. + * + * @param fd The file descriptor from which data is read + * CITE https://www.codequoi.com/en/handling-a-file-by-its-descriptor-in-c/#:~:text=For%20example%2C%20we%20can%20open%20a%20file%20in,in%20truncated%20write%20mode%2C%20we%20could%20do%3A%20open%28%22path%2Fto%2Ffile%22%2CO_WRONLY%7CO_TRUNC%29%3B + * + */ +void +b64_encode(int fd) +{ + unsigned char buffer[1024]; + unsigned char result[2048]; + int n; + + int output_fd = open("encrypted.txt", O_WRONLY | O_CREATE | O_TRUNC); + if (output_fd < 0) { + printf("ERROR, could not open the output file encrypted.txt\n"); + return; + } + + while ((n = read(fd, buffer, 1024)) > 0) { + int size = base64_encode(buffer, result, n, 1); + printf("Saving your secret to encrypted.txt of size %d\n", size); + write(output_fd, result, size); + } + + close(output_fd); +} + +/** + * This function sets up the ARCFOUR state with a key, + * it then reads data from the specified file descriptor in chunks, and + * generates a keystream to encrypt the data. + * The encrypted data is output to the console. + * + * @param fd The file descriptor from which data is read + * + */ +void +arcfour_encode(int fd) +{ + unsigned char state[256]; + const unsigned char *key = (const unsigned char *)"beebop"; + int key_length = strlen((const char *)key); + + arcfour_key_setup(state, key, key_length); + + unsigned char buffer[1024]; + unsigned char keystream[1024]; + int n; + + printf("Your encoded secret is... "); + while ((n = read(fd, buffer, 1024)) > 0) { + arcfour_generate_stream(state, keystream, n); + + for (int i = 0; i < n; i++) { + buffer[i] ^= keystream[i]; + printf("%h", buffer[i]); + } + + printf("\n"); + } +} + +/** + * The main function reads the command-line arguments to determine which + * encoding method to use (SHA256, Base64, or ARCFOUR) and processes + * the specified input file accordingly. + * + * @param argc The number of command-line arguments + * @param argv The command-line arguments + * @return If there is an error, it prints an error message, else it closes and exits + * + */ +int +main(int argc, char *argv[]) +{ + char *crypto_option = argv[1]; + char *file_name = argv[2]; + + int fd = open(file_name, O_RDONLY); + if (fd < 0) { + printf("ERROR, could not open the file %s\n", file_name); + exit(1); + } + + if (strcmp(crypto_option, "-s") == 0) { + sha256_encode(fd); + } else if (strcmp(crypto_option, "-b") == 0) { + b64_encode(fd); + } else if (strcmp(crypto_option, "-a") == 0) { + arcfour_encode(fd); + } + close(fd); + exit(1); + +} diff --git a/user/printf.c b/user/printf.c index 5c5c782..7321853 100644 --- a/user/printf.c +++ b/user/printf.c @@ -70,6 +70,9 @@ vprintf(int fd, const char *fmt, va_list ap) printint(fd, va_arg(ap, uint64), 10, 0); } else if(c == 'x') { printint(fd, va_arg(ap, int), 16, 0); + } else if(c == 'h') { + unsigned char byte = (unsigned char)va_arg(ap, int); + printint(fd, (unsigned int)byte, 16, 0); } else if(c == 'p') { printptr(fd, va_arg(ap, uint64)); } else if(c == 's'){ diff --git a/user/sha256.c b/user/sha256.c new file mode 100644 index 0000000..c6e9b7f --- /dev/null +++ b/user/sha256.c @@ -0,0 +1,160 @@ +/********************************************************************* +* Filename: sha256.c +* Author: Brad Conte (brad AT bradconte.com) +* Copyright: +* Disclaimer: This code is presented "as is" without any guarantees. +* Details: Implementation of the SHA-256 hashing algorithm. + SHA-256 is one of the three algorithms in the SHA2 + specification. The others, SHA-384 and SHA-512, are not + offered in this implementation. + Algorithm specification can be found here: + * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf + This implementation uses little endian byte order. +*********************************************************************/ + +/*************************** HEADER FILES ***************************/ +//#include +//#include +#include "sha256.h" +#include "kernel/types.h" +#include "kernel/stat.h" +#include "user/user.h" +/****************************** MACROS ******************************/ +#define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b)))) +#define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b)))) + +#define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z))) +#define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) +#define EP0(x) (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22)) +#define EP1(x) (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25)) +#define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3)) +#define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10)) + +/**************************** VARIABLES *****************************/ +static const WORD k[64] = { + 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5, + 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174, + 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da, + 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967, + 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85, + 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070, + 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3, + 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2 +}; + +/*********************** FUNCTION DEFINITIONS ***********************/ +void sha256_transform(SHA256_CTX *ctx, const BYTE data[]) +{ + WORD a, b, c, d, e, f, g, h, i, j, t1, t2, m[64]; + + for (i = 0, j = 0; i < 16; ++i, j += 4) + m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]); + for ( ; i < 64; ++i) + m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16]; + + a = ctx->state[0]; + b = ctx->state[1]; + c = ctx->state[2]; + d = ctx->state[3]; + e = ctx->state[4]; + f = ctx->state[5]; + g = ctx->state[6]; + h = ctx->state[7]; + + for (i = 0; i < 64; ++i) { + t1 = h + EP1(e) + CH(e,f,g) + k[i] + m[i]; + t2 = EP0(a) + MAJ(a,b,c); + h = g; + g = f; + f = e; + e = d + t1; + d = c; + c = b; + b = a; + a = t1 + t2; + } + + ctx->state[0] += a; + ctx->state[1] += b; + ctx->state[2] += c; + ctx->state[3] += d; + ctx->state[4] += e; + ctx->state[5] += f; + ctx->state[6] += g; + ctx->state[7] += h; +} + +void sha256_init(SHA256_CTX *ctx) +{ + ctx->datalen = 0; + ctx->bitlen = 0; + ctx->state[0] = 0x6a09e667; + ctx->state[1] = 0xbb67ae85; + ctx->state[2] = 0x3c6ef372; + ctx->state[3] = 0xa54ff53a; + ctx->state[4] = 0x510e527f; + ctx->state[5] = 0x9b05688c; + ctx->state[6] = 0x1f83d9ab; + ctx->state[7] = 0x5be0cd19; +} + +void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len) +{ + WORD i; + + for (i = 0; i < len; ++i) { + ctx->data[ctx->datalen] = data[i]; + ctx->datalen++; + if (ctx->datalen == 64) { + sha256_transform(ctx, ctx->data); + ctx->bitlen += 512; + ctx->datalen = 0; + } + } +} + +void sha256_final(SHA256_CTX *ctx, BYTE hash[]) +{ + WORD i; + + i = ctx->datalen; + + // Pad whatever data is left in the buffer. + if (ctx->datalen < 56) { + ctx->data[i++] = 0x80; + while (i < 56) + ctx->data[i++] = 0x00; + } + else { + ctx->data[i++] = 0x80; + while (i < 64) + ctx->data[i++] = 0x00; + sha256_transform(ctx, ctx->data); + memset(ctx->data, 0, 56); + } + + // Append to the padding the total message's length in bits and transform. + ctx->bitlen += ctx->datalen * 8; + ctx->data[63] = ctx->bitlen; + ctx->data[62] = ctx->bitlen >> 8; + ctx->data[61] = ctx->bitlen >> 16; + ctx->data[60] = ctx->bitlen >> 24; + ctx->data[59] = ctx->bitlen >> 32; + ctx->data[58] = ctx->bitlen >> 40; + ctx->data[57] = ctx->bitlen >> 48; + ctx->data[56] = ctx->bitlen >> 56; + sha256_transform(ctx, ctx->data); + + // Since this implementation uses little endian byte ordering and SHA uses big endian, + // reverse all the bytes when copying the final state to the output hash. + for (i = 0; i < 4; ++i) { + hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff; + hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff; + hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff; + hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff; + hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff; + hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff; + hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff; + hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff; + } +} diff --git a/user/sha256.h b/user/sha256.h new file mode 100644 index 0000000..7123a30 --- /dev/null +++ b/user/sha256.h @@ -0,0 +1,34 @@ +/********************************************************************* +* Filename: sha256.h +* Author: Brad Conte (brad AT bradconte.com) +* Copyright: +* Disclaimer: This code is presented "as is" without any guarantees. +* Details: Defines the API for the corresponding SHA1 implementation. +*********************************************************************/ + +#ifndef SHA256_H +#define SHA256_H + +/*************************** HEADER FILES ***************************/ +#include + +/****************************** MACROS ******************************/ +#define SHA256_BLOCK_SIZE 32 // SHA256 outputs a 32 byte digest + +/**************************** DATA TYPES ****************************/ +typedef unsigned char BYTE; // 8-bit byte +typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines + +typedef struct { + BYTE data[64]; + WORD datalen; + unsigned long long bitlen; + WORD state[8]; +} SHA256_CTX; + +/*********************** FUNCTION DECLARATIONS **********************/ +void sha256_init(SHA256_CTX *ctx); +void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len); +void sha256_final(SHA256_CTX *ctx, BYTE hash[]); + +#endif // SHA256_H