| 1 | /* |
| 2 | * Copyright 2008-2018 Aerospike, Inc. |
| 3 | * |
| 4 | * Portions may be licensed to Aerospike, Inc. under one or more contributor |
| 5 | * license agreements. |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 8 | * use this file except in compliance with the License. You may obtain a copy of |
| 9 | * the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | * License for the specific language governing permissions and limitations under |
| 15 | * the License. |
| 16 | */ |
| 17 | #pragma once |
| 18 | |
| 19 | #include <aerospike/as_std.h> |
| 20 | #include <openssl/ripemd.h> |
| 21 | #include <string.h> |
| 22 | #include <stdio.h> |
| 23 | |
| 24 | #ifdef __APPLE__ |
| 25 | // Openssl is deprecated on mac, but the library is still included. |
| 26 | // Since RIPEMD160 is not supported by the new mac common cryto system library, |
| 27 | // openssl is still used. Save old settings and disable deprecated warnings. |
| 28 | #pragma GCC diagnostic push |
| 29 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 30 | #endif |
| 31 | |
| 32 | #ifdef __cplusplus |
| 33 | extern "C" { |
| 34 | #endif |
| 35 | |
| 36 | /****************************************************************************** |
| 37 | * CONSTANTS & TYPES |
| 38 | *****************************************************************************/ |
| 39 | |
| 40 | #define CF_DIGEST_KEY_SZ RIPEMD160_DIGEST_LENGTH |
| 41 | |
| 42 | typedef struct cf_digest_s { |
| 43 | uint8_t digest[CF_DIGEST_KEY_SZ]; |
| 44 | } cf_digest; |
| 45 | |
| 46 | extern const cf_digest cf_digest_zero; |
| 47 | |
| 48 | /****************************************************************************** |
| 49 | * INLINE FUNCTIONS |
| 50 | *****************************************************************************/ |
| 51 | |
| 52 | static inline void |
| 53 | cf_digest_compute(const void *data, size_t len, cf_digest *d) |
| 54 | { |
| 55 | RIPEMD160((const unsigned char *)data, len, (unsigned char *)d->digest); |
| 56 | } |
| 57 | |
| 58 | static inline void |
| 59 | cf_digest_compute2(const void *data1, size_t len1, const void *data2, size_t len2, cf_digest *d) |
| 60 | { |
| 61 | if (len1 == 0) { |
| 62 | RIPEMD160((const unsigned char *)data2, len2, (unsigned char *)d->digest); |
| 63 | } |
| 64 | else { |
| 65 | RIPEMD160_CTX c; |
| 66 | RIPEMD160_Init(&c); |
| 67 | RIPEMD160_Update(&c, data1, len1); |
| 68 | RIPEMD160_Update(&c, data2, len2); |
| 69 | RIPEMD160_Final((unsigned char *)(d->digest), &c); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | static inline int |
| 74 | cf_digest_compare(const cf_digest *d1, const cf_digest *d2) |
| 75 | { |
| 76 | return memcmp(d1->digest, d2->digest, CF_DIGEST_KEY_SZ); |
| 77 | } |
| 78 | |
| 79 | static inline void |
| 80 | cf_digest_string(const cf_digest *d, char* output) |
| 81 | { |
| 82 | if (d == NULL) { |
| 83 | strcpy(output, "null" ); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | char* p = output; |
| 88 | |
| 89 | *p++ = '0'; |
| 90 | *p++ = 'x'; |
| 91 | |
| 92 | for (int i = 0; i < CF_DIGEST_KEY_SZ; i++) { |
| 93 | sprintf(p, "%02x" , d->digest[i]); |
| 94 | p += 2; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /*****************************************************************************/ |
| 99 | |
| 100 | #ifdef __cplusplus |
| 101 | } // end extern "C" |
| 102 | #endif |
| 103 | |
| 104 | #ifdef __APPLE__ |
| 105 | // Restore old settings. |
| 106 | #pragma GCC diagnostic pop |
| 107 | #endif |
| 108 | |