annotate src/lib/hash-method.c @ 23007:36e01285b5b8

lib: buffer - Improve header comment for buffer_insert() and buffer_delete().
author Stephan Bosch <stephan.bosch@dovecot.fi>
date Mon, 18 Mar 2019 00:52:37 +0100
parents cb108f786fb4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
22713
cb108f786fb4 Updated copyright notices to include the year 2018.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents: 21390
diff changeset
1 /* Copyright (c) 2010-2018 Dovecot authors, see the included COPYING file */
12307
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
2
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
3 #include "lib.h"
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
4 #include "md4.h"
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
5 #include "md5.h"
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
6 #include "sha1.h"
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
7 #include "sha2.h"
21070
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
8 #include "sha3.h"
12307
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
9 #include "hash-method.h"
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
10
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
11 const struct hash_method *hash_method_lookup(const char *name)
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
12 {
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
13 unsigned int i;
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
14
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
15 for (i = 0; hash_methods[i] != NULL; i++) {
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
16 if (strcmp(hash_methods[i]->name, name) == 0)
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
17 return hash_methods[i];
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
18 }
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
19 return NULL;
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
20 }
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
21
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
22 static void hash_method_init_size(void *context)
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
23 {
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
24 uint64_t *ctx = context;
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
25
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
26 *ctx = 0;
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
27 }
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
28
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
29 static void
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
30 hash_method_loop_size(void *context, const void *data ATTR_UNUSED, size_t size)
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
31 {
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
32 uint64_t *ctx = context;
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
33
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
34 *ctx += size;
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
35 }
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
36
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
37 static void hash_method_result_size(void *context, unsigned char *result_r)
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
38 {
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
39 uint64_t *ctx = context;
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
40
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
41 result_r[0] = (*ctx & 0xff00000000000000ULL) >> 56;
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
42 result_r[1] = (*ctx & 0x00ff000000000000ULL) >> 48;
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
43 result_r[2] = (*ctx & 0x0000ff0000000000ULL) >> 40;
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
44 result_r[3] = (*ctx & 0x000000ff00000000ULL) >> 32;
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
45 result_r[4] = (*ctx & 0x00000000ff000000ULL) >> 24;
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
46 result_r[5] = (*ctx & 0x0000000000ff0000ULL) >> 16;
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
47 result_r[6] = (*ctx & 0x000000000000ff00ULL) >> 8;
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
48 result_r[7] = (*ctx & 0x00000000000000ffULL);
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
49 }
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
50
17351
7f6c5e27b908 treewide sparse cleanup - make single-unit-only data static
Phil Carmody <phil@dovecot.fi>
parents: 17130
diff changeset
51 static const struct hash_method hash_method_size = {
12307
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
52 "size",
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
53 sizeof(uint64_t),
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
54 sizeof(uint64_t),
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
55
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
56 hash_method_init_size,
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
57 hash_method_loop_size,
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
58 hash_method_result_size
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
59 };
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
60
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
61 const struct hash_method *hash_methods[] = {
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
62 &hash_method_md4,
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
63 &hash_method_md5,
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
64 &hash_method_sha1,
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
65 &hash_method_sha256,
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
66 &hash_method_sha512,
21070
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
67 &hash_method_sha3_256,
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 19552
diff changeset
68 &hash_method_sha3_512,
12307
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
69 &hash_method_size,
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
70 NULL
368fd1cce4d6 liblib: Added a common API for accessing all hash methods.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
71 };