All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
src/include/citrusleaf/cf_digest.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2008-2013 by Aerospike.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  *****************************************************************************/
22 #pragma once
23 
24 /*
25  * Cryptographic message digests
26  * The intent is to provide an algorithm-neutral API for the computation of
27  * cryptographic digests of arbitrary bytes. Consequently, we define the
28  * cf_digest type to be an array of bytes of the appropriate length.
29  * The actual computation is done in one shot by calling cf_digest_compute().
30  */
31 
32 #include <stdint.h>
33 #include <stddef.h>
34 #include <openssl/ripemd.h>
35 #include <openssl/md4.h>
36 #include <stdio.h>
37 #include <string.h>
38 
39 #ifdef __APPLE__
40 // Openssl is deprecated on mac, but the library is still included.
41 // Since RIPEMD160 is not supported by the new mac common cryto system library,
42 // openssl is still used. Save old settings and disable deprecated warnings.
43 #pragma GCC diagnostic push
44 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
45 #endif
46 
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 
51 /******************************************************************************
52  * CONSTANTS
53  *****************************************************************************/
54 
55 // #define DEBUG_VERBOSE 1
56 
57 #define CF_DIGEST_KEY_SZ RIPEMD160_DIGEST_LENGTH
58 
59 #define CF_SIGNATURE_SZ (sizeof(uint64_t))
60 
61 /******************************************************************************
62  * TYPES
63  *****************************************************************************/
64 
65 typedef struct cf_digest_s cf_digest;
66 
67 typedef uint64_t cf_signature;
68 
69 typedef uint16_t cl_partition_id;
70 
71 /**
72  * cf_digest_s
73  * Storage for a message digest
74  */
75 struct cf_digest_s {
77 };
78 
79 /******************************************************************************
80  * FUNCTIONS
81  *****************************************************************************/
82 
83 void cf_digest_string(cf_digest *digest, char* output);
84 
85 /******************************************************************************
86  * INLINE FUNCTIONS
87  *****************************************************************************/
88 
89 /**
90  * cf_digest_compute
91  * Compute the digest of an input
92  */
93 static inline void cf_digest_compute(const void *data, size_t len, cf_digest *d) {
94  RIPEMD160((const unsigned char *) data, len, (unsigned char *) d->digest);
95 }
96 
97 
98 /**
99  * Compute a digest of two parts
100  * (often the set and the key)
101  */
102 static inline void cf_digest_compute2(const void *data1, size_t len1, const void *data2, size_t len2, cf_digest *d) {
103  if (len1 == 0) {
104  RIPEMD160((const unsigned char *) data2, len2, (unsigned char *) d->digest);
105  }
106  else {
107  RIPEMD160_CTX c;
108  RIPEMD160_Init(&c);
109  RIPEMD160_Update(&c, data1, len1);
110  RIPEMD160_Update(&c, data2, len2);
111  RIPEMD160_Final( (unsigned char *)(d->digest), &c);
112  }
113 }
114 
115 static inline uint32_t cf_digest_gethash(const cf_digest *d, uint32_t MASK) {
116  return((*(uint32_t *)d->digest) & MASK);
117 }
118 
119 static inline uint32_t cf_digest_gethash_mod(const cf_digest *d, uint32_t MOD) {
120  return((*(uint32_t *)d->digest) % MOD);
121 }
122 
123 /**
124  * SIGNATURE
125  * A non-crypto-solid signature
126  */
127 static inline void cf_signature_compute(const void *data, size_t len, cf_signature *s) {
128  uint8_t sig[MD4_DIGEST_LENGTH];
129 
130  MD4_CTX c;
131  MD4_Init( &c );
132  MD4_Update(&c, data, len);
133  MD4_Final( (unsigned char *) &sig[0], &c);
134  memcpy(s, sig, sizeof(*s));
135 }
136 
137 /**
138  * as_partition_getid
139  * A brief utility function to derive the partition ID from a digest
140  */
141 static inline cl_partition_id cl_partition_getid(uint32_t n_partitions, const cf_digest *d) {
142  uint16_t *d_int = (uint16_t *)&d->digest[0];
143  cl_partition_id r = *d_int & (n_partitions - 1);
144  return(r);
145 }
146 
147 /*****************************************************************************/
148 
149 #ifdef __cplusplus
150 } // end extern "C"
151 #endif
152 
153 #ifdef __APPLE__
154 // Restore old settings.
155 #pragma GCC diagnostic pop
156 #endif