1
0
mirror of git://projects.qi-hardware.com/antorcha.git synced 2024-11-01 13:15:55 +02:00
antorcha/fw/hash.c

79 lines
1.2 KiB
C
Raw Normal View History

/*
* fw/hash.h - Secure hash
*
* Written 2012 by Werner Almesberger
* Copyright 2012 Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#ifdef __AVR__
#include <avr/pgmspace.h>
#endif /* __AVR__ */
#include "hash.h"
#define HASH_SIZE 128
static uint8_t hash[HASH_SIZE];
void hash_init(void)
{
memset(hash, 0, HASH_SIZE);
}
void hash_merge(const uint8_t *buf, uint8_t len)
{
uint8_t i;
for (i = 0; i != len; i++)
hash[i & (HASH_SIZE-1)] ^= buf[i];
}
#ifdef __AVR__
void hash_merge_progmem(const uint8_t *buf, uint8_t len)
{
uint8_t i;
for (i = 0; i != len; i++)
hash[i & (HASH_SIZE-1)] ^= pgm_read_byte(buf+i);
}
#endif /* __AVR__ */
void hash_end(void)
{
}
bool hash_eq(const uint8_t *buf, uint8_t len, uint8_t off)
{
uint8_t i;
for (i = 0; i != len; i++)
if (hash[(off+i) & (HASH_SIZE-1)] != buf[i])
return 0;
return 1;
}
void hash_cp(uint8_t *buf, uint8_t len, uint8_t off)
{
memcpy(buf, hash+off, len);
}