Root/fw/hash.c

Source at commit 352779a78fc12cd1126f41080771649890927e81 created 10 years 11 months ago.
By Werner Almesberger, tornado/led/: layout for new LED board version
1/*
2 * fw/hash.h - Secure hash
3 *
4 * Written 2012 by Werner Almesberger
5 * Copyright 2012 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdbool.h>
15#include <stdint.h>
16#include <string.h>
17
18#ifdef __AVR__
19#include <avr/pgmspace.h>
20#endif /* __AVR__ */
21
22#include "hash.h"
23
24
25#define HASH_SIZE 128
26
27
28static uint8_t hash[HASH_SIZE];
29
30
31void hash_init(void)
32{
33    memset(hash, 0, HASH_SIZE);
34}
35
36
37void hash_merge(const uint8_t *buf, uint8_t len)
38{
39    uint8_t i;
40
41    for (i = 0; i != len; i++)
42        hash[i & (HASH_SIZE-1)] ^= buf[i];
43}
44
45
46#ifdef __AVR__
47
48void hash_merge_progmem(const uint8_t *buf, uint8_t len)
49{
50    uint8_t i;
51
52    for (i = 0; i != len; i++)
53        hash[i & (HASH_SIZE-1)] ^= pgm_read_byte(buf+i);
54}
55
56#endif /* __AVR__ */
57
58
59void hash_end(void)
60{
61}
62
63
64bool hash_eq(const uint8_t *buf, uint8_t len, uint8_t off)
65{
66    uint8_t i;
67
68    for (i = 0; i != len; i++)
69        if (hash[(off+i) & (HASH_SIZE-1)] != buf[i])
70            return 0;
71    return 1;
72}
73
74
75void hash_cp(uint8_t *buf, uint8_t len, uint8_t off)
76{
77    memcpy(buf, hash+off, len);
78}
79

Archive Download this file

Branches:
master
tornado-v1



interactive