void sha256_update(sha256_ctx *ctx, const void *_input, int ilenlft) { int left, fill; const unsigned char *input; if(ilenlft <= 0) return; input = (const unsigned char*)_input; left = ctx->total & 0x3F; fill = 0x40 - left; ctx->total += ilenlft; if(left && ilenlft >= fill) { memcpy(ctx->buffer + left, input, fill); sha256_hash_block(ctx, ctx->buffer, 1); input += fill; ilenlft -= fill; left = 0; } while(ilenlft >= 0x40) { sha256_hash_block(ctx, input, 1); input += 0x40; ilenlft -= 0x40; } if(ilenlft > 0) memcpy(ctx->buffer + left, input, ilenlft); }
int main(void) { // Configure GPIOs: gpio_set_direction(IO_ADDRESS(GPIO1_BASE), 0x0000); // Switches gpio_set_direction(IO_ADDRESS(GPIO2_BASE), 0xffff); // LEDs // Configure the 7-segment displays: seg7_set_enabled_displays(IO_ADDRESS(SEG7_BASE), 0xff); seg7_set_value(IO_ADDRESS(SEG7_BASE), 0); // Set up the timer: timer_set(IO_ADDRESS(TIMER_BASE), SYSTEM_CLK_FREQ); // Print a startup message: uart_puts(IO_ADDRESS(UART_BASE), "The Potato Processor SHA256 Benchmark\n\r\n\r"); // Enable interrupts: potato_enable_irq(IRQ_TIMER); potato_enable_interrupts(); struct sha256_context context; // Prepare a block for hashing: uint32_t block[16]; uint8_t * block_ptr = (uint8_t *) block; block_ptr[0] = 'a'; block_ptr[1] = 'b'; block_ptr[2] = 'c'; sha256_pad_le_block(block_ptr, 3, 3); // Repeatedly hash the same data over and over again: while(true) { uint8_t hash[32]; sha256_reset(&context); sha256_hash_block(&context, block); sha256_get_hash(&context, hash); ++hashes_per_second; } return 0; }