|
17 | 17 | #pragma clang diagnostic push |
18 | 18 | #pragma clang diagnostic ignored "-Wdocumentation-deprecated-sync" |
19 | 19 | #include "mbedtls/sha1.h" |
| 20 | +#include "mbedtls/sha256.h" |
20 | 21 | #pragma clang diagnostic pop |
21 | 22 |
|
22 | 23 | #ifdef __APPLE__ |
23 | | -#define USE_COMMON_CRYPTO |
| 24 | +# define USE_COMMON_CRYPTO |
24 | 25 | #endif |
25 | 26 |
|
26 | 27 | #ifdef USE_COMMON_CRYPTO |
27 | 28 | #include <CommonCrypto/CommonDigest.h> |
28 | | - #define _CONTEXT ((CC_SHA1_CTX*)_context) |
29 | | -#else |
30 | | - #define _CONTEXT ((mbedtls_sha1_context*)_context) |
31 | 29 | #endif |
32 | 30 |
|
33 | 31 | namespace litecore { |
34 | 32 |
|
35 | | - void SHA1::computeFrom(fleece::slice s) { |
36 | | - (SHA1Builder() << s).finish(&bytes, sizeof(bytes)); |
37 | | - } |
38 | | - |
39 | 33 |
|
40 | | - bool SHA1::setDigest(fleece::slice s) { |
41 | | - if (s.size != sizeof(bytes)) |
| 34 | + template <DigestType TYPE, size_t SIZE> |
| 35 | + bool Digest<TYPE,SIZE>::setDigest(fleece::slice s) { |
| 36 | + if (s.size != _bytes.size()) |
42 | 37 | return false; |
43 | | - memcpy(bytes, s.buf, sizeof(bytes)); |
| 38 | + s.copyTo(_bytes.data()); |
44 | 39 | return true; |
45 | 40 | } |
46 | 41 |
|
47 | 42 |
|
48 | | - std::string SHA1::asBase64() const { |
| 43 | + template <DigestType TYPE, size_t SIZE> |
| 44 | + std::string Digest<TYPE,SIZE>::asBase64() const { |
49 | 45 | return fleece::base64::encode(asSlice()); |
50 | 46 | } |
51 | 47 |
|
52 | 48 |
|
53 | | - SHA1Builder::SHA1Builder() { |
| 49 | +#pragma mark - SHA1: |
| 50 | + |
| 51 | + |
| 52 | + template <> |
| 53 | + Digest<SHA,1>::Builder::Builder() { |
54 | 54 | static_assert(sizeof(_context) >= sizeof(mbedtls_sha1_context)); |
55 | 55 | #ifdef USE_COMMON_CRYPTO |
56 | 56 | static_assert(sizeof(_context) >= sizeof(CC_SHA1_CTX)); |
57 | | - CC_SHA1_Init(_CONTEXT); |
| 57 | + CC_SHA1_Init((CC_SHA1_CTX*)_context); |
58 | 58 | #else |
59 | | - mbedtls_sha1_init(_CONTEXT); |
60 | | - mbedtls_sha1_starts(_CONTEXT); |
| 59 | + mbedtls_sha1_init((mbedtls_sha1_context*)_context); |
| 60 | + mbedtls_sha1_starts((mbedtls_sha1_context*)_context); |
61 | 61 | #endif |
62 | 62 | } |
63 | 63 |
|
64 | 64 |
|
65 | | - SHA1Builder& SHA1Builder::operator<< (fleece::slice s) { |
| 65 | + template <> |
| 66 | + Digest<SHA,1>::Builder& Digest<SHA,1>::Builder::operator<< (fleece::slice s) { |
66 | 67 | #ifdef USE_COMMON_CRYPTO |
67 | | - CC_SHA1_Update(_CONTEXT, s.buf, (CC_LONG)s.size); |
| 68 | + CC_SHA1_Update((CC_SHA1_CTX*)_context, s.buf, (CC_LONG)s.size); |
68 | 69 | #else |
69 | | - mbedtls_sha1_update(_CONTEXT, (unsigned char*)s.buf, s.size); |
| 70 | + mbedtls_sha1_update((mbedtls_sha1_context*)_context, (unsigned char*)s.buf, s.size); |
70 | 71 | #endif |
71 | 72 | return *this; |
72 | 73 | } |
73 | 74 |
|
74 | 75 |
|
75 | | - void SHA1Builder::finish(void *result, size_t resultSize) { |
76 | | - DebugAssert(resultSize == sizeof(SHA1::bytes)); |
| 76 | + template <> |
| 77 | + void Digest<SHA,1>::Builder::finish(void *result, size_t resultSize) { |
| 78 | + Assert(resultSize == kSizeInBytes); |
77 | 79 | #ifdef USE_COMMON_CRYPTO |
78 | | - CC_SHA1_Final((uint8_t*)result, _CONTEXT); |
| 80 | + CC_SHA1_Final((uint8_t*)result, (CC_SHA1_CTX*)_context); |
79 | 81 | #else |
80 | | - mbedtls_sha1_finish(_CONTEXT, (uint8_t*)result); |
81 | | - mbedtls_sha1_free(_CONTEXT); |
| 82 | + mbedtls_sha1_finish((mbedtls_sha1_context*)_context, (uint8_t*)result); |
| 83 | + mbedtls_sha1_free((mbedtls_sha1_context*)_context); |
82 | 84 | #endif |
83 | 85 | } |
84 | 86 |
|
| 87 | + // Force the non-specialized methods to be instantiated: |
| 88 | + template class Digest<SHA,1>; |
| 89 | + |
| 90 | + |
| 91 | +#pragma mark - SHA256: |
| 92 | + |
| 93 | + |
| 94 | + template <> |
| 95 | + Digest<SHA,256>::Builder::Builder() { |
| 96 | + static_assert(sizeof(_context) >= sizeof(mbedtls_sha256_context)); |
| 97 | +#ifdef USE_COMMON_CRYPTO |
| 98 | + static_assert(sizeof(_context) >= sizeof(CC_SHA256_CTX)); |
| 99 | + CC_SHA256_Init((CC_SHA256_CTX*)_context); |
| 100 | +#else |
| 101 | + mbedtls_sha256_init((mbedtls_sha256_context*)_context); |
| 102 | + mbedtls_sha256_starts((mbedtls_sha256_context*)_context, 0); |
| 103 | +#endif |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + template <> |
| 108 | + Digest<SHA,256>::Builder& Digest<SHA,256>::Builder::operator<< (fleece::slice s) { |
| 109 | +#ifdef USE_COMMON_CRYPTO |
| 110 | + CC_SHA256_Update((CC_SHA256_CTX*)_context, s.buf, (CC_LONG)s.size); |
| 111 | +#else |
| 112 | + mbedtls_sha256_update((mbedtls_sha256_context*)_context, (unsigned char*)s.buf, s.size); |
| 113 | +#endif |
| 114 | + return *this; |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + template <> |
| 119 | + void Digest<SHA,256>::Builder::finish(void *result, size_t resultSize) { |
| 120 | + Assert(resultSize == kSizeInBytes); |
| 121 | +#ifdef USE_COMMON_CRYPTO |
| 122 | + CC_SHA256_Final((uint8_t*)result, (CC_SHA256_CTX*)_context); |
| 123 | +#else |
| 124 | + mbedtls_sha256_finish((mbedtls_sha256_context*)_context, (uint8_t*)result); |
| 125 | + mbedtls_sha256_free((mbedtls_sha256_context*)_context); |
| 126 | +#endif |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | + // Force the non-specialized methods to be instantiated: |
| 131 | + template class Digest<SHA,256>; |
| 132 | + |
85 | 133 | } |
0 commit comments