Root/
| Source at commit a11670bb7bb6e506db72463fea6738100b22c9a7 created 11 years 17 days ago. By Werner Almesberger, tornado/fw/sim/alg.c: accept data from standard input; fix "process" | |
|---|---|
| 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 | |
| 28 | static uint8_t hash[HASH_SIZE]; |
| 29 | |
| 30 | |
| 31 | void hash_init(void) |
| 32 | { |
| 33 | memset(hash, 0, HASH_SIZE); |
| 34 | } |
| 35 | |
| 36 | |
| 37 | void 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 | |
| 48 | void 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 | |
| 59 | void hash_end(void) |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | |
| 64 | bool 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 | |
| 75 | void hash_cp(uint8_t *buf, uint8_t len, uint8_t off) |
| 76 | { |
| 77 | memcpy(buf, hash+off, len); |
| 78 | } |
| 79 | |
Branches:
master
tornado-v1
