| // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/digest.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <string.h> |
| 19 |
| 20 | #include <openssl/nid.h> |
| 21 |
| 22 | #include "../../internal.h" |
| 23 | #include "../bcm_interface.h" |
| 24 | #include "../delocate.h" |
| 25 | #include "internal.h" |
| 26 |
| 27 | #if defined(NDEBUG) |
| 28 | #define CHECK(x) (void)(x) |
| 29 | #else |
| 30 | #define CHECK(x) assert(x) |
| 31 | #endif |
| 32 |
| 33 |
| 34 | static void sha1_init(EVP_MD_CTX *ctx) { |
| 35 | BCM_sha1_init(reinterpret_cast<SHA_CTX *>(ctx->md_data)); |
| 36 | } |
| 37 |
| 38 | static void sha1_update(EVP_MD_CTX *ctx, const void *data, size_t count) { |
| 39 | BCM_sha1_update(reinterpret_cast<SHA_CTX *>(ctx->md_data), data, count); |
| 40 | } |
| 41 |
| 42 | static void sha1_final(EVP_MD_CTX *ctx, uint8_t *md) { |
| 43 | BCM_sha1_final(md, reinterpret_cast<SHA_CTX *>(ctx->md_data)); |
| 44 | } |
| 45 |
| 46 | DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha1) { |
| 47 | out->type = NID_sha1; |
| 48 | out->md_size = BCM_SHA_DIGEST_LENGTH; |
| 49 | out->flags = 0; |
| 50 | out->init = sha1_init; |
| 51 | out->update = sha1_update; |
| 52 | out->final = sha1_final; |
| 53 | out->block_size = 64; |
| 54 | out->ctx_size = sizeof(SHA_CTX); |
| 55 | } |
| 56 |
| 57 |
| 58 | static void sha224_init(EVP_MD_CTX *ctx) { |
| 59 | BCM_sha224_init(reinterpret_cast<SHA256_CTX *>(ctx->md_data)); |
| 60 | } |
| 61 |
| 62 | static void sha224_update(EVP_MD_CTX *ctx, const void *data, size_t count) { |
| 63 | BCM_sha224_update(reinterpret_cast<SHA256_CTX *>(ctx->md_data), data, count); |
| 64 | } |
| 65 |
| 66 | static void sha224_final(EVP_MD_CTX *ctx, uint8_t *md) { |
| 67 | BCM_sha224_final(md, reinterpret_cast<SHA256_CTX *>(ctx->md_data)); |
| 68 | } |
| 69 |
| 70 | DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha224) { |
| 71 | out->type = NID_sha224; |
| 72 | out->md_size = BCM_SHA224_DIGEST_LENGTH; |
| 73 | out->flags = 0; |
| 74 | out->init = sha224_init; |
| 75 | out->update = sha224_update; |
| 76 | out->final = sha224_final; |
| 77 | out->block_size = 64; |
| 78 | out->ctx_size = sizeof(SHA256_CTX); |
| 79 | } |
| 80 |
| 81 |
| 82 | static void sha256_init(EVP_MD_CTX *ctx) { |
| 83 | BCM_sha256_init(reinterpret_cast<SHA256_CTX *>(ctx->md_data)); |
| 84 | } |
| 85 |
| 86 | static void sha256_update(EVP_MD_CTX *ctx, const void *data, size_t count) { |
| 87 | BCM_sha256_update(reinterpret_cast<SHA256_CTX *>(ctx->md_data), data, count); |
| 88 | } |
| 89 |
| 90 | static void sha256_final(EVP_MD_CTX *ctx, uint8_t *md) { |
| 91 | BCM_sha256_final(md, reinterpret_cast<SHA256_CTX *>(ctx->md_data)); |
| 92 | } |
| 93 |
| 94 | DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha256) { |
| 95 | out->type = NID_sha256; |
| 96 | out->md_size = BCM_SHA256_DIGEST_LENGTH; |
| 97 | out->flags = 0; |
| 98 | out->init = sha256_init; |
| 99 | out->update = sha256_update; |
| 100 | out->final = sha256_final; |
| 101 | out->block_size = 64; |
| 102 | out->ctx_size = sizeof(SHA256_CTX); |
| 103 | } |
| 104 |
| 105 |
| 106 | static void sha384_init(EVP_MD_CTX *ctx) { |
| 107 | BCM_sha384_init(reinterpret_cast<SHA512_CTX *>(ctx->md_data)); |
| 108 | } |
| 109 |
| 110 | static void sha384_update(EVP_MD_CTX *ctx, const void *data, size_t count) { |
| 111 | BCM_sha384_update(reinterpret_cast<SHA512_CTX *>(ctx->md_data), data, count); |
| 112 | } |
| 113 |
| 114 | static void sha384_final(EVP_MD_CTX *ctx, uint8_t *md) { |
| 115 | BCM_sha384_final(md, reinterpret_cast<SHA512_CTX *>(ctx->md_data)); |
| 116 | } |
| 117 |
| 118 | DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha384) { |
| 119 | out->type = NID_sha384; |
| 120 | out->md_size = BCM_SHA384_DIGEST_LENGTH; |
| 121 | out->flags = 0; |
| 122 | out->init = sha384_init; |
| 123 | out->update = sha384_update; |
| 124 | out->final = sha384_final; |
| 125 | out->block_size = 128; |
| 126 | out->ctx_size = sizeof(SHA512_CTX); |
| 127 | } |
| 128 |
| 129 |
| 130 | static void sha512_init(EVP_MD_CTX *ctx) { |
| 131 | BCM_sha512_init(reinterpret_cast<SHA512_CTX *>(ctx->md_data)); |
| 132 | } |
| 133 |
| 134 | static void sha512_update(EVP_MD_CTX *ctx, const void *data, size_t count) { |
| 135 | BCM_sha512_update(reinterpret_cast<SHA512_CTX *>(ctx->md_data), data, count); |
| 136 | } |
| 137 |
| 138 | static void sha512_final(EVP_MD_CTX *ctx, uint8_t *md) { |
| 139 | BCM_sha512_final(md, reinterpret_cast<SHA512_CTX *>(ctx->md_data)); |
| 140 | } |
| 141 |
| 142 | DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha512) { |
| 143 | out->type = NID_sha512; |
| 144 | out->md_size = BCM_SHA512_DIGEST_LENGTH; |
| 145 | out->flags = 0; |
| 146 | out->init = sha512_init; |
| 147 | out->update = sha512_update; |
| 148 | out->final = sha512_final; |
| 149 | out->block_size = 128; |
| 150 | out->ctx_size = sizeof(SHA512_CTX); |
| 151 | } |
| 152 |
| 153 |
| 154 | static void sha512_256_init(EVP_MD_CTX *ctx) { |
| 155 | BCM_sha512_256_init(reinterpret_cast<SHA512_CTX *>(ctx->md_data)); |
| 156 | } |
| 157 |
| 158 | static void sha512_256_update(EVP_MD_CTX *ctx, const void *data, size_t count) { |
| 159 | BCM_sha512_256_update(reinterpret_cast<SHA512_CTX *>(ctx->md_data), data, |
| 160 | count); |
| 161 | } |
| 162 |
| 163 | static void sha512_256_final(EVP_MD_CTX *ctx, uint8_t *md) { |
| 164 | BCM_sha512_256_final(md, reinterpret_cast<SHA512_CTX *>(ctx->md_data)); |
| 165 | } |
| 166 |
| 167 | DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha512_256) { |
| 168 | out->type = NID_sha512_256; |
| 169 | out->md_size = BCM_SHA512_256_DIGEST_LENGTH; |
| 170 | out->flags = 0; |
| 171 | out->init = sha512_256_init; |
| 172 | out->update = sha512_256_update; |
| 173 | out->final = sha512_256_final; |
| 174 | out->block_size = 128; |
| 175 | out->ctx_size = sizeof(SHA512_CTX); |
| 176 | } |
| 177 |
| 178 | #undef CHECK |
| 179 |