├── sprite.png ├── memzero.c ├── memzero.h ├── README.md ├── sprite.h ├── Makefile ├── sha2.h ├── sprite.c ├── miner.c └── sha2.c /sprite.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghidraninja/game-boy-bitcoin-miner/HEAD/sprite.png -------------------------------------------------------------------------------- /memzero.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void memzero(void *const pnt, const size_t len) { 4 | memset(pnt, 0x0, len); 5 | } -------------------------------------------------------------------------------- /memzero.h: -------------------------------------------------------------------------------- 1 | #ifndef __MEMZERO_H__ 2 | #define __MEMZERO_H__ 3 | 4 | #include 5 | 6 | void memzero(void* const pnt, const size_t len); 7 | 8 | #endif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # game-boy-bitcoin-miner 2 | The Game Boy ROM of the Game Boy bitcoin miner! 3 | 4 | To build this, currently this patch needs to be applied to GBDK: 5 | https://gist.github.com/nezza/1399aa00632b1cc2cefa9bb65d72f04c 6 | -------------------------------------------------------------------------------- /sprite.h: -------------------------------------------------------------------------------- 1 | //AUTOGENERATED FILE FROM png2mtspr 2 | #define sprite_TILE_H 16 3 | #define sprite_WIDTH 64 4 | #define sprite_HEIGHT 48 5 | #define sprite_PIVOT_X 32 6 | #define sprite_PIVOT_Y 24 7 | 8 | extern const UINT8 sprite_data[960]; 9 | extern const metasprite_t* const sprite_metasprites[2]; 10 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CC = ../../../bin/lcc -Wa-l -Wl-m -Wl-j 2 | 3 | BINS = miner.gb 4 | all: $(BINS) 5 | 6 | make.bat: Makefile 7 | @echo "REM Automatically generated from Makefile" > make.bat 8 | @make -sn | sed y/\\//\\\\/ | grep -v make >> make.bat 9 | 10 | # Compile and link single file in one pass 11 | %.gb: %.c 12 | $(CC) -o $@ $< memzero.c sha2.c sprite.c 13 | 14 | clean: 15 | rm -f *.o *.lst *.map *.gb *~ *.rel *.cdb *.ihx *.lnk *.sym *.asm *.noi 16 | 17 | -------------------------------------------------------------------------------- /sha2.h: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | * Filename: sha256.h 3 | * Author: Brad Conte (brad AT bradconte.com) 4 | * Copyright: 5 | * Disclaimer: This code is presented "as is" without any guarantees. 6 | * Details: Defines the API for the corresponding SHA1 implementation. 7 | *********************************************************************/ 8 | 9 | #ifndef SHA256_H 10 | #define SHA256_H 11 | void calc_sha_256(uint8_t hash[32], const void *input, size_t len); 12 | #endif // SHA256_H -------------------------------------------------------------------------------- /sprite.c: -------------------------------------------------------------------------------- 1 | //AUTOGENERATED FILE FROM png2mtspr 2 | 3 | #include 4 | #include 5 | 6 | const UINT8 sprite_data[960] = { 7 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x03,0x03,0x07,0x07,0x0f,0x0f,0x0f,0x0f,0x1f,0x1f,0x1f,0x1f,0x3f,0x3f,0x3f,0x3f, 8 | 0x00,0x00,0x00,0x00,0x03,0x03,0x0f,0x0f,0x3f,0x3f,0x7f,0x7f,0xff,0xff,0xef,0xf0,0xef,0xf0,0xff,0xfc,0xff,0xfe,0xff,0xfe,0xff,0xfe,0xff,0xfe,0xff,0xfe,0xff,0xfe, 9 | 0x03,0x03,0x7f,0x7f,0xf7,0xfb,0xff,0xf3,0xff,0xf3,0xff,0xf3,0xff,0xf3,0xff,0x00,0xff,0x00,0xfe,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f, 10 | 0xe0,0xe0,0xfe,0xfe,0xff,0xe7,0xff,0xe7,0xff,0xe7,0xff,0xe7,0xff,0xe7,0xff,0x07,0xff,0x01,0xff,0x00,0xff,0xe0,0xff,0xf8,0xff,0xf8,0xff,0xfc,0xff,0xfc,0xff,0xfc, 11 | 0x00,0x00,0x00,0x00,0xc0,0xc0,0xf0,0xf0,0xfc,0xfc,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,0xff,0x3f,0xff,0x1f,0xff,0x1f,0xef,0x1f,0xff,0x0f,0xff,0x0f, 12 | 0x3f,0x3f,0x7f,0x7f,0x7f,0x7f,0x7f,0x7f,0x7f,0x7f,0x7f,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,0x7f,0x7f,0x7f,0x7f,0x7f,0x7f,0x7f,0x3f,0x3f, 13 | 0xff,0xfe,0xff,0xfe,0xff,0xfe,0xff,0xfe,0xff,0xfe,0xff,0xfe,0xff,0xfe,0xff,0xfe,0xff,0xfe,0xff,0xfe,0xff,0xfe,0xff,0xfe,0xff,0xfe,0xff,0xfe,0xff,0xfe,0xff,0xfe, 14 | 0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f, 15 | 0xff,0xfc,0xff,0xfc,0xfb,0xfc,0xff,0xf8,0xff,0xe0,0xff,0x01,0xfe,0x01,0xff,0x00,0xff,0xf0,0xfb,0xfc,0xff,0xfc,0xff,0xfe,0xff,0xfe,0xff,0xff,0xff,0xff,0xfe,0xff, 16 | 0xff,0x0f,0xff,0x1f,0xff,0x1f,0xff,0x3f,0x7f,0xff,0xff,0xff,0xff,0xff,0xbf,0x7f,0xff,0x1f,0xff,0x0f,0xff,0x07,0xff,0x07,0xff,0x07,0xff,0x07,0xfb,0x07,0xff,0x07, 17 | 0xfc,0xfc,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfc,0xfc, 18 | 0xff,0xfe,0xff,0xfe,0xff,0xfe,0xff,0xfe,0xff,0xfe,0xfb,0xfc,0xff,0xe0,0xef,0xf0,0xff,0xff,0xff,0xff,0x7f,0x7f,0x3f,0x3f,0x0f,0x0f,0x03,0x03,0x00,0x00,0x00,0x00, 19 | 0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xfd,0x0e,0xff,0x00,0xff,0x00,0xff,0xf3,0xff,0xf3,0xff,0xf3,0xff,0xf3,0xf7,0xfb,0xff,0xff,0x7f,0x7f,0x07,0x07, 20 | 0xff,0xfe,0xff,0xfe,0xff,0xfc,0xff,0xf8,0xff,0xe0,0xff,0x00,0xff,0x01,0xdf,0x27,0xff,0xe7,0xff,0xe7,0xff,0xe7,0xff,0xe7,0xff,0xe7,0xff,0xff,0xfe,0xfe,0xe0,0xe0, 21 | 0xff,0x07,0xff,0x07,0xf7,0x0f,0xef,0x1f,0xdf,0x3f,0xbf,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfe,0xfc,0xfc,0xf0,0xf0,0xc0,0xc0,0x00,0x00,0x00,0x00, 22 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x02,0x02,0x05,0x05,0x0b,0x0b,0x17,0x17,0x17,0x17,0x2f,0x2f,0x2f,0x2f,0x1f,0x1f, 23 | 0x00,0x00,0x00,0x00,0x06,0x06,0x19,0x19,0x27,0x27,0x5f,0x5f,0xbf,0xbf,0x7f,0x7f,0xff,0xff,0xf8,0xff,0xf8,0xff,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 24 | 0x00,0x00,0x00,0x00,0x7f,0x7b,0xff,0xff,0xff,0xff,0xfb,0xff,0xfb,0xff,0xfb,0xff,0xfb,0xff,0x00,0xff,0x00,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff, 25 | 0x00,0x00,0x00,0x00,0xee,0xee,0xff,0xff,0xff,0xff,0xef,0xff,0xef,0xff,0xef,0xff,0xef,0xff,0x4f,0xff,0x03,0xff,0xc0,0xff,0xf0,0xff,0xf0,0xff,0xf8,0xff,0xf8,0xff, 26 | 0x00,0x00,0x00,0x00,0x60,0x60,0x98,0x98,0xe4,0xe4,0xfa,0xfa,0xfd,0xfd,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,0xff,0x7f,0xff,0x3f,0xff,0x3f,0xff, 27 | 0x1f,0x1f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x1f,0x1f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x1f,0x1f, 28 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 29 | 0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x00,0xff,0x00,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff, 30 | 0xf8,0xff,0xf8,0xff,0xf8,0xff,0xf8,0xff,0xf0,0xff,0xe1,0xff,0x0f,0xff,0x01,0xff,0xe0,0xff,0xf8,0xff,0xfc,0xff,0xfc,0xff,0xfc,0xff,0xfe,0xff,0xfe,0xff,0xfe,0xff, 31 | 0x3f,0xff,0x3f,0xff,0x3f,0xff,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,0xff,0x3f,0xff,0x1f,0xff,0x1f,0xff,0x1f,0xff,0x0f,0xff,0x0f,0xff,0x1f,0xff, 32 | 0xf8,0xf8,0xfc,0xfc,0xfc,0xfc,0xfc,0xfc,0xf8,0xf8,0xfc,0xfc,0xfc,0xfc,0xfc,0xfc,0xfc,0xfc,0xfc,0xfc,0xfc,0xfc,0xf8,0xf8,0xfc,0xfc,0xfc,0xfc,0xfc,0xfc,0xf8,0xf8, 33 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xf8,0xff,0xff,0xff,0xff,0xff,0x7f,0x7f,0xbf,0xbf,0x5f,0x5f,0x27,0x27,0x19,0x19,0x06,0x06,0x00,0x00,0x00,0x00, 34 | 0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x06,0xff,0x00,0xff,0xf3,0xff,0xfb,0xff,0xfb,0xff,0xfb,0xff,0xfb,0xff,0xff,0xff,0xff,0xff,0x77,0x77,0x00,0x00,0x00,0x00, 35 | 0xfc,0xff,0xfc,0xff,0xf8,0xff,0xe0,0xff,0x01,0xff,0x07,0xff,0xef,0xff,0xcf,0xff,0xef,0xff,0xcf,0xff,0xef,0xff,0xff,0xff,0xff,0xff,0xee,0xee,0x00,0x00,0x00,0x00, 36 | 0x1f,0xff,0x1f,0xff,0x3f,0xff,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfe,0xfd,0xfd,0xfa,0xfa,0xe4,0xe4,0x98,0x98,0x60,0x60,0x00,0x00,0x00,0x00 37 | }; 38 | 39 | const metasprite_t sprite_metasprite0[] = { 40 | {-24, -32, 0, 0}, {0, 8, 2, 0}, {0, 8, 4, 0}, {0, 8, 6, 0}, {0, 8, 8, 0}, {0, 8, 0, 32}, {16, -40, 10, 0}, {0, 8, 12, 0}, {0, 8, 14, 0}, {0, 8, 16, 0}, {0, 8, 18, 0}, {0, 8, 20, 0}, {16, -40, 0, 64}, {0, 8, 22, 0}, {0, 8, 24, 0}, {0, 8, 26, 0}, {0, 8, 28, 0}, {0, 8, 0, 96}, {metasprite_end} 41 | }; 42 | 43 | const metasprite_t sprite_metasprite1[] = { 44 | {-24, -32, 30, 0}, {0, 8, 32, 0}, {0, 8, 34, 0}, {0, 8, 36, 0}, {0, 8, 38, 0}, {0, 8, 30, 32}, {16, -40, 40, 0}, {0, 8, 42, 0}, {0, 8, 44, 0}, {0, 8, 46, 0}, {0, 8, 48, 0}, {0, 8, 50, 0}, {16, -40, 30, 64}, {0, 8, 52, 0}, {0, 8, 54, 0}, {0, 8, 56, 0}, {0, 8, 58, 0}, {0, 8, 30, 96}, {metasprite_end} 45 | }; 46 | 47 | const metasprite_t* const sprite_metasprites[2] = { 48 | sprite_metasprite0, sprite_metasprite1 49 | }; 50 | -------------------------------------------------------------------------------- /miner.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "keccak256.h" 6 | 7 | #include 8 | #include "sha2.h" 9 | 10 | #include "sprite.h" 11 | 12 | uint32_t logo_offset = 40; 13 | 14 | void print_header() { 15 | gotoxy(0, 7); 16 | cls(); 17 | puts("Game Boy"); 18 | puts("Bitcoin Miner"); 19 | puts("by stacksmashing\n\n"); 20 | } 21 | 22 | void hex_print(unsigned char *data, int len) { 23 | for(int i=0; i < len; i++) { 24 | printf("%x ", data[i] & 0xFF); 25 | } 26 | printf("\n"); 27 | } 28 | 29 | int receive_block_header_fake(char *header) { 30 | memset(header, 0x11, 76); 31 | return 0; 32 | 33 | } 34 | 35 | void sb(unsigned char c) { 36 | _io_out = c; 37 | receive_byte(); 38 | while(_io_status == IO_RECEIVING) { 39 | // sending... 40 | } 41 | // printf("don\n"); 42 | if(_io_status != IO_IDLE) { 43 | printf("failStatus\n#%d\n", _io_status); 44 | return; 45 | } 46 | return; 47 | } 48 | 49 | int receive_bytes(unsigned char *header, size_t len) { 50 | for(int i = 0; i < len; i++) { 51 | receive_byte(); 52 | while(_io_status == IO_RECEIVING) { 53 | // Receiving... 54 | } 55 | // printf("don\n"); 56 | if(_io_status != IO_IDLE) { 57 | printf("Status #%d\n", _io_status); 58 | return 1; 59 | } 60 | header[i] = _io_in; 61 | } 62 | // printf("Done."); 63 | // hex_print(header, 10); 64 | return 0; 65 | } 66 | 67 | const uint8_t CMD_BLOCK_HEADER = 0x41; 68 | const uint8_t CMD_TARGET = 0x42; 69 | const uint8_t CMD_NEW_BLOCK = 0x43; 70 | const uint8_t CMD_STATUS = 0x44; 71 | const uint8_t RESP_WAIT_CMD = 0x61; 72 | const uint8_t RESP_WAIT_DATA = 0x62; 73 | const uint8_t RESP_SUCCESS = 0x63; 74 | 75 | int receive_block_header(unsigned char *header) { 76 | for(int i = 0; i < 76; i++) { 77 | _io_out = RESP_WAIT_DATA; 78 | receive_byte(); 79 | while(_io_status == IO_RECEIVING) { 80 | // Receiving... 81 | } 82 | // printf("don\n"); 83 | if(_io_status != IO_IDLE) { 84 | printf("Status #%d\n", _io_status); 85 | return 1; 86 | } 87 | header[i] = _io_in; 88 | } 89 | _io_out = 0x41; 90 | // printf("Done."); 91 | // hex_print(header, 10); 92 | return 0; 93 | } 94 | 95 | 96 | void wait_byte(const uint8_t b) { 97 | while(1) { 98 | 99 | // _io_out = 0x88; 100 | receive_byte(); 101 | while(_io_status == IO_RECEIVING) { 102 | // Receiving... 103 | } 104 | // _io_out = 0x41; 105 | if(_io_status != IO_IDLE) { 106 | printf("FAILED TO RECEIVE\n#%d\n", _io_status); 107 | return; 108 | } 109 | if(_io_in == b) { 110 | _io_out = 0x0; 111 | return; 112 | } else { 113 | printf("Invalid command.\n"); 114 | } 115 | } 116 | } 117 | 118 | void hexdump(unsigned char *f, size_t len) { 119 | for(int i=0; i < len; i++) { 120 | unsigned char c = f[i]; 121 | unsigned char c1 = c & 0x0F; 122 | unsigned char c2 = (c >> 4) & 0x0F; 123 | if(c2 < 10) { 124 | printf("%c", '0' + c2); 125 | } else { 126 | printf("%c", 'A' + (c2 - 10)); 127 | } 128 | if(c1 < 10) { 129 | printf("%c", '0' + c1); 130 | } else { 131 | printf("%c", 'A' + (c1 - 10)); 132 | } 133 | 134 | } 135 | puts(""); 136 | } 137 | 138 | void receive_block_data(unsigned char *block_header, unsigned char *target) { 139 | _io_out = RESP_WAIT_CMD; 140 | wait_byte(CMD_BLOCK_HEADER); 141 | receive_block_header(block_header); 142 | _io_out = RESP_WAIT_CMD; 143 | wait_byte(CMD_TARGET); 144 | receive_bytes(target, 32); 145 | } 146 | 147 | int mine_nonce(unsigned char *block_header, unsigned char *target, uint32_t *nonce_out) { 148 | uint32_t nonce = 0; 149 | while(1) { 150 | receive_byte(); 151 | 152 | // Append nonce 153 | memcpy(&block_header[76], &nonce, 4); 154 | print_header(); 155 | // gotoxy(0, 7); 156 | // cls(); 157 | // puts("Game Boy"); 158 | // puts("Bitcoin Miner"); 159 | // puts("by stacksmashing\n\n"); 160 | 161 | printf("Nonce: %5d\n", nonce); 162 | 163 | // Increase nonce 164 | nonce++; 165 | 166 | printf("Hashing...\n"); 167 | uint8_t hash_rev[32]; 168 | uint8_t hash2[32]; 169 | uint8_t hash[32]; 170 | calc_sha_256(hash2, block_header, 80); 171 | calc_sha_256(hash_rev, hash2, 32); 172 | for(int i=0; i < 32; i++) { 173 | hash[31-i] = hash_rev[i]; 174 | } 175 | // printf(""); 176 | 177 | move_metasprite(sprite_metasprites[0], 0, 0, logo_offset, 40); 178 | logo_offset += 4; 179 | logo_offset %= 198; 180 | 181 | 182 | receive_byte(); 183 | // Check hash... 184 | for(int i=0; i < 32; i++) { 185 | if(hash[i] < target[i]) { 186 | printf("Success!\n"); 187 | // _io_out = RESP_SUCCESS; 188 | // hexdump(block_header, 80); 189 | *nonce_out = nonce - 1; 190 | return 0; 191 | } else if(hash[i] > target[i]) { 192 | // printf("Fail!\n"); 193 | // return 1; 194 | break; 195 | } 196 | } 197 | 198 | 199 | // Check if we need to receive a new block. 200 | if(_io_in == CMD_NEW_BLOCK) { 201 | printf("New block data.\n"); 202 | return 1; 203 | } 204 | } 205 | } 206 | 207 | void miner(void) { 208 | unsigned char block_header[80]; 209 | unsigned char target[32]; 210 | 211 | while(1) { 212 | print_header(); 213 | printf("Waiting for data...\n"); 214 | receive_block_data(block_header, target); 215 | 216 | _io_out = 0x0; 217 | uint32_t nonce = 0; 218 | int mine_result = mine_nonce(block_header, target, &nonce); 219 | 220 | // Indicate success to host 221 | if(mine_result == 0) { 222 | _io_out = RESP_SUCCESS; 223 | printf("Sending status...\n"); 224 | wait_byte(CMD_STATUS); 225 | // send nonce over. 226 | char data[4]; 227 | memcpy(data, &nonce, 4); 228 | for(int i=0; i < 4; i++) { 229 | sb(data[i]); 230 | } 231 | } 232 | } 233 | } 234 | 235 | void main(void) 236 | { 237 | UBYTE i, n = 0; 238 | unsigned char *s; 239 | 240 | // load tile data into VRAM 241 | set_sprite_data(0, sizeof(sprite_data) >> 4, sprite_data); 242 | 243 | // set sprite tile 244 | set_sprite_tile(0, 0); 245 | SHOW_SPRITES; 246 | #if sprite_TILE_H == 16 247 | SPRITES_8x16; 248 | #else 249 | SPRITES_8x8; 250 | #endif 251 | move_metasprite(sprite_metasprites[0], 0, 0, 40, 40); 252 | 253 | puts("Game Boy"); 254 | puts("Bitcoin Miner"); 255 | puts("by stacksmashing\n\n"); 256 | CRITICAL { 257 | add_SIO(nowait_int_handler); 258 | set_interrupts(SIO_IFLAG | VBL_IFLAG); 259 | } 260 | miner(); 261 | } 262 | -------------------------------------------------------------------------------- /sha2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "sha2.h" 5 | 6 | #define CHUNK_SIZE 64 7 | #define TOTAL_LEN_LEN 8 8 | 9 | /* 10 | * ABOUT bool: this file does not use bool in order to be as pre-C99 compatible as possible. 11 | */ 12 | 13 | /* 14 | * Comments from pseudo-code at https://en.wikipedia.org/wiki/SHA-2 are reproduced here. 15 | * When useful for clarification, portions of the pseudo-code are reproduced here too. 16 | */ 17 | 18 | /* 19 | * Initialize array of round constants: 20 | * (first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311): 21 | */ 22 | static const uint32_t k[] = { 23 | 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 24 | 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 25 | 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 26 | 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 27 | 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 28 | 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 29 | 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 30 | 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 31 | }; 32 | 33 | struct buffer_state { 34 | const uint8_t * p; 35 | size_t len; 36 | size_t total_len; 37 | int single_one_delivered; /* bool */ 38 | int total_len_delivered; /* bool */ 39 | }; 40 | 41 | static inline uint32_t right_rot(uint32_t value, unsigned int count) 42 | { 43 | /* 44 | * Defined behaviour in standard C for all count where 0 < count < 32, 45 | * which is what we need here. 46 | */ 47 | return value >> count | value << (32 - count); 48 | } 49 | 50 | static void init_buf_state(struct buffer_state * state, const void * input, size_t len) 51 | { 52 | state->p = input; 53 | state->len = len; 54 | state->total_len = len; 55 | state->single_one_delivered = 0; 56 | state->total_len_delivered = 0; 57 | } 58 | 59 | /* Return value: bool */ 60 | static int calc_chunk(uint8_t chunk[CHUNK_SIZE], struct buffer_state * state) 61 | { 62 | size_t space_in_chunk; 63 | 64 | if (state->total_len_delivered) { 65 | return 0; 66 | } 67 | 68 | if (state->len >= CHUNK_SIZE) { 69 | memcpy(chunk, state->p, CHUNK_SIZE); 70 | state->p += CHUNK_SIZE; 71 | state->len -= CHUNK_SIZE; 72 | return 1; 73 | } 74 | 75 | memcpy(chunk, state->p, state->len); 76 | chunk += state->len; 77 | space_in_chunk = CHUNK_SIZE - state->len; 78 | state->p += state->len; 79 | state->len = 0; 80 | 81 | /* If we are here, space_in_chunk is one at minimum. */ 82 | if (!state->single_one_delivered) { 83 | *chunk++ = 0x80; 84 | space_in_chunk -= 1; 85 | state->single_one_delivered = 1; 86 | } 87 | 88 | /* 89 | * Now: 90 | * - either there is enough space left for the total length, and we can conclude, 91 | * - or there is too little space left, and we have to pad the rest of this chunk with zeroes. 92 | * In the latter case, we will conclude at the next invokation of this function. 93 | */ 94 | if (space_in_chunk >= TOTAL_LEN_LEN) { 95 | const size_t left = space_in_chunk - TOTAL_LEN_LEN; 96 | size_t len = state->total_len; 97 | int i; 98 | memset(chunk, 0x00, left); 99 | chunk += left; 100 | 101 | /* Storing of len * 8 as a big endian 64-bit without overflow. */ 102 | chunk[7] = (uint8_t) (len << 3); 103 | len >>= 5; 104 | for (i = 6; i >= 0; i--) { 105 | chunk[i] = (uint8_t) len; 106 | len >>= 8; 107 | } 108 | state->total_len_delivered = 1; 109 | } else { 110 | memset(chunk, 0x00, space_in_chunk); 111 | } 112 | 113 | return 1; 114 | } 115 | 116 | /* 117 | * Limitations: 118 | * - Since input is a pointer in RAM, the data to hash should be in RAM, which could be a problem 119 | * for large data sizes. 120 | * - SHA algorithms theoretically operate on bit strings. However, this implementation has no support 121 | * for bit string lengths that are not multiples of eight, and it really operates on arrays of bytes. 122 | * In particular, the len parameter is a number of bytes. 123 | */ 124 | void calc_sha_256(uint8_t hash[32], const void * input, size_t len) 125 | { 126 | /* 127 | * Note 1: All integers (expect indexes) are 32-bit unsigned integers and addition is calculated modulo 2^32. 128 | * Note 2: For each round, there is one round constant k[i] and one entry in the message schedule array w[i], 0 = i = 63 129 | * Note 3: The compression function uses 8 working variables, a through h 130 | * Note 4: Big-endian convention is used when expressing the constants in this pseudocode, 131 | * and when parsing message block data from bytes to words, for example, 132 | * the first word of the input message "abc" after padding is 0x61626380 133 | */ 134 | 135 | /* 136 | * Initialize hash values: 137 | * (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19): 138 | */ 139 | uint32_t h[] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 }; 140 | unsigned i, j; 141 | 142 | /* 512-bit chunks is what we will operate on. */ 143 | uint8_t chunk[64]; 144 | 145 | struct buffer_state state; 146 | 147 | init_buf_state(&state, input, len); 148 | 149 | while (calc_chunk(chunk, &state)) { 150 | uint32_t ah[8]; 151 | 152 | const uint8_t *p = chunk; 153 | 154 | /* Initialize working variables to current hash value: */ 155 | for (i = 0; i < 8; i++) 156 | ah[i] = h[i]; 157 | 158 | /* Compression function main loop: */ 159 | for (i = 0; i < 4; i++) { 160 | /* 161 | * The w-array is really w[64], but since we only need 162 | * 16 of them at a time, we save stack by calculating 163 | * 16 at a time. 164 | * 165 | * This optimization was not there initially and the 166 | * rest of the comments about w[64] are kept in their 167 | * initial state. 168 | */ 169 | 170 | /* 171 | * create a 64-entry message schedule array w[0..63] of 32-bit words 172 | * (The initial values in w[0..63] don't matter, so many implementations zero them here) 173 | * copy chunk into first 16 words w[0..15] of the message schedule array 174 | */ 175 | uint32_t w[16]; 176 | 177 | for (j = 0; j < 16; j++) { 178 | if (i == 0) { 179 | w[j] = (uint32_t) p[0] << 24 | (uint32_t) p[1] << 16 | 180 | (uint32_t) p[2] << 8 | (uint32_t) p[3]; 181 | p += 4; 182 | } else { 183 | /* Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array: */ 184 | const uint32_t s0 = right_rot(w[(j + 1) & 0xf], 7) ^ right_rot(w[(j + 1) & 0xf], 18) ^ (w[(j + 1) & 0xf] >> 3); 185 | const uint32_t s1 = right_rot(w[(j + 14) & 0xf], 17) ^ right_rot(w[(j + 14) & 0xf], 19) ^ (w[(j + 14) & 0xf] >> 10); 186 | w[j] = w[j] + s0 + w[(j + 9) & 0xf] + s1; 187 | } 188 | const uint32_t s1 = right_rot(ah[4], 6) ^ right_rot(ah[4], 11) ^ right_rot(ah[4], 25); 189 | const uint32_t ch = (ah[4] & ah[5]) ^ (~ah[4] & ah[6]); 190 | const uint32_t temp1 = ah[7] + s1 + ch + k[i << 4 | j] + w[j]; 191 | const uint32_t s0 = right_rot(ah[0], 2) ^ right_rot(ah[0], 13) ^ right_rot(ah[0], 22); 192 | const uint32_t maj = (ah[0] & ah[1]) ^ (ah[0] & ah[2]) ^ (ah[1] & ah[2]); 193 | const uint32_t temp2 = s0 + maj; 194 | 195 | ah[7] = ah[6]; 196 | ah[6] = ah[5]; 197 | ah[5] = ah[4]; 198 | ah[4] = ah[3] + temp1; 199 | ah[3] = ah[2]; 200 | ah[2] = ah[1]; 201 | ah[1] = ah[0]; 202 | ah[0] = temp1 + temp2; 203 | } 204 | } 205 | 206 | /* Add the compressed chunk to the current hash value: */ 207 | for (i = 0; i < 8; i++) 208 | h[i] += ah[i]; 209 | } 210 | 211 | /* Produce the final hash value (big-endian): */ 212 | for (i = 0, j = 0; i < 8; i++) 213 | { 214 | hash[j++] = (uint8_t) (h[i] >> 24); 215 | hash[j++] = (uint8_t) (h[i] >> 16); 216 | hash[j++] = (uint8_t) (h[i] >> 8); 217 | hash[j++] = (uint8_t) h[i]; 218 | } 219 | } --------------------------------------------------------------------------------