ARMEBS4  revision-26.06.2015
hash_test.c
Go to the documentation of this file.
1 /**
2  * \file heivs/hash_test.c
3  * \see heivs/hash.h
4  *
5  * This file has been split out of hash.c for size optimization reasons, the
6  * linker has no reason to include it if it unused.
7  */
8 
9 #include "heivs/hash_test.h"
10 #include "heivs/hash.h"
11 #include <stdint.h>
12 #include <string.h>
13 
14 static uint8_t to_bin(char c)
15 {
16  if (c >= '0' && c <= '9')
17  {
18  return c-'0';
19  }
20 
21  if (c >= 'A' && c <= 'F')
22  {
23  return c-'A'+10;
24  }
25 
26  if (c >= 'a' && c <= 'f')
27  {
28  return c-'a'+10;
29  }
30 
31  return 0;
32 }
33 
34 status_e hash_compare_ascii(const struct heivs_hash_t *hash, const void *state, const char *str)
35 {
36  uint8_t orig[strlen(str)/2];
37  size_t str_len = strlen(str);
38  size_t i;
39 
40  if (str_len%2)
41  {
42  return ERROR_BAD_PARAM;
43  }
44 
45  if (str_len/2 != hash->hash_size)
46  {
47  return ERROR_BAD_PARAM;
48  }
49 
50  for (i = 0 ; i < ARRAY_SIZE(orig); i++)
51  {
52  orig[i] = to_bin(str[2*i+0]) << 4;
53  orig[i] |= to_bin(str[2*i+1]) << 0;
54  }
55 
56  if (memcmp(state, orig, sizeof(orig)))
57  {
58  return ERROR_FAILED;
59  }
60 
61  return NO_ERROR;
62 }
63 
64 status_e hash_test(struct heivs_hash_t *hash, const void *data, size_t size, const char *reference)
65 {
66  status_e status;
67  hash_declare_state(hash, state);
68 
69  status = hash_init(hash, &state, sizeof(state));
70  if (status != NO_ERROR)
71  {
72  return status;
73  }
74 
75  status = hash_run(hash, &state, data, size);
76  if (status != NO_ERROR)
77  {
78  return status;
79  }
80 
81  status = hash_finish(hash, state);
82  if (status != NO_ERROR)
83  {
84  return status;
85  }
86 
87  status = hash_compare_ascii(hash, state, reference);
88  if (status != NO_ERROR)
89  {
90  return status;
91  }
92 
93  return NO_ERROR;
94 }
95 
96 status_e hash_test_zeroes(struct heivs_hash_t *hash, size_t size, const char *reference)
97 {
98  uint32_t data[512/sizeof(uint32_t)];
99  status_e status;
100  hash_declare_state(hash, state);
101 
102  memset(data, 0x0, sizeof(data));
103 
104  status = hash_init(hash, &state, sizeof(state));
105  if (status != NO_ERROR)
106  {
107  return status;
108  }
109 
110  while (size)
111  {
112  uint32_t block_size = min(size, sizeof(data));
113  status = hash_run(hash, &state, data, block_size);
114  if (status != NO_ERROR)
115  {
116  return status;
117  }
118 
119  size -= block_size;
120  }
121 
122  status = hash_finish(hash, state);
123  if (status != NO_ERROR)
124  {
125  return status;
126  }
127 
128  status = hash_compare_ascii(hash, state, reference);
129  if (status != NO_ERROR)
130  {
131  return status;
132  }
133 
134  return NO_ERROR;
135 }
hash testing
Unspecified error.
Definition: error.h:35
void * memset(void *dest, int n, size_t n)
hash abstraction
Parameter unsupported.
Definition: error.h:56
int memcmp(const void *s1, const void *s2, size_t n)
status_e
Known errors.
Definition: error.h:21
#define ARRAY_SIZE(x)
Number of elements in the array.
Definition: utils.h:19
No error.
Definition: error.h:28
size_t strlen(const char *str)