1// Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 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 34static void sha1_init(EVP_MD_CTX *ctx) { 35 BCM_sha1_init(reinterpret_cast<SHA_CTX *>(ctx->md_data)); 36} 37 38static 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 42static void sha1_final(EVP_MD_CTX *ctx, uint8_t *md) { 43 BCM_sha1_final(md, reinterpret_cast<SHA_CTX *>(ctx->md_data)); 44} 45 46DEFINE_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 57static_assert(sizeof(SHA_CTX) <= EVP_MAX_MD_DATA_SIZE); 58 59 60static void sha224_init(EVP_MD_CTX *ctx) { 61 BCM_sha224_init(reinterpret_cast<SHA256_CTX *>(ctx->md_data)); 62} 63 64static void sha224_update(EVP_MD_CTX *ctx, const void *data, size_t count) { 65 BCM_sha224_update(reinterpret_cast<SHA256_CTX *>(ctx->md_data), data, count); 66} 67 68static void sha224_final(EVP_MD_CTX *ctx, uint8_t *md) { 69 BCM_sha224_final(md, reinterpret_cast<SHA256_CTX *>(ctx->md_data)); 70} 71 72DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha224) { 73 out->type = NID_sha224; 74 out->md_size = BCM_SHA224_DIGEST_LENGTH; 75 out->flags = 0; 76 out->init = sha224_init; 77 out->update = sha224_update; 78 out->final = sha224_final; 79 out->block_size = 64; 80 out->ctx_size = sizeof(SHA256_CTX); 81} 82 83static_assert(sizeof(SHA256_CTX) <= EVP_MAX_MD_DATA_SIZE); 84 85static void sha256_init(EVP_MD_CTX *ctx) { 86 BCM_sha256_init(reinterpret_cast<SHA256_CTX *>(ctx->md_data)); 87} 88 89static void sha256_update(EVP_MD_CTX *ctx, const void *data, size_t count) { 90 BCM_sha256_update(reinterpret_cast<SHA256_CTX *>(ctx->md_data), data, count); 91} 92 93static void sha256_final(EVP_MD_CTX *ctx, uint8_t *md) { 94 BCM_sha256_final(md, reinterpret_cast<SHA256_CTX *>(ctx->md_data)); 95} 96 97DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha256) { 98 out->type = NID_sha256; 99 out->md_size = BCM_SHA256_DIGEST_LENGTH; 100 out->flags = 0; 101 out->init = sha256_init; 102 out->update = sha256_update; 103 out->final = sha256_final; 104 out->block_size = 64; 105 out->ctx_size = sizeof(SHA256_CTX); 106} 107 108 109static void sha384_init(EVP_MD_CTX *ctx) { 110 BCM_sha384_init(reinterpret_cast<SHA512_CTX *>(ctx->md_data)); 111} 112 113static void sha384_update(EVP_MD_CTX *ctx, const void *data, size_t count) { 114 BCM_sha384_update(reinterpret_cast<SHA512_CTX *>(ctx->md_data), data, count); 115} 116 117static void sha384_final(EVP_MD_CTX *ctx, uint8_t *md) { 118 BCM_sha384_final(md, reinterpret_cast<SHA512_CTX *>(ctx->md_data)); 119} 120 121DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha384) { 122 out->type = NID_sha384; 123 out->md_size = BCM_SHA384_DIGEST_LENGTH; 124 out->flags = 0; 125 out->init = sha384_init; 126 out->update = sha384_update; 127 out->final = sha384_final; 128 out->block_size = 128; 129 out->ctx_size = sizeof(SHA512_CTX); 130} 131 132static_assert(sizeof(SHA512_CTX) <= EVP_MAX_MD_DATA_SIZE); 133 134static void sha512_init(EVP_MD_CTX *ctx) { 135 BCM_sha512_init(reinterpret_cast<SHA512_CTX *>(ctx->md_data)); 136} 137 138static void sha512_update(EVP_MD_CTX *ctx, const void *data, size_t count) { 139 BCM_sha512_update(reinterpret_cast<SHA512_CTX *>(ctx->md_data), data, count); 140} 141 142static void sha512_final(EVP_MD_CTX *ctx, uint8_t *md) { 143 BCM_sha512_final(md, reinterpret_cast<SHA512_CTX *>(ctx->md_data)); 144} 145 146DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha512) { 147 out->type = NID_sha512; 148 out->md_size = BCM_SHA512_DIGEST_LENGTH; 149 out->flags = 0; 150 out->init = sha512_init; 151 out->update = sha512_update; 152 out->final = sha512_final; 153 out->block_size = 128; 154 out->ctx_size = sizeof(SHA512_CTX); 155} 156 157 158static void sha512_256_init(EVP_MD_CTX *ctx) { 159 BCM_sha512_256_init(reinterpret_cast<SHA512_CTX *>(ctx->md_data)); 160} 161 162static void sha512_256_update(EVP_MD_CTX *ctx, const void *data, size_t count) { 163 BCM_sha512_256_update(reinterpret_cast<SHA512_CTX *>(ctx->md_data), data, 164 count); 165} 166 167static void sha512_256_final(EVP_MD_CTX *ctx, uint8_t *md) { 168 BCM_sha512_256_final(md, reinterpret_cast<SHA512_CTX *>(ctx->md_data)); 169} 170 171DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha512_256) { 172 out->type = NID_sha512_256; 173 out->md_size = BCM_SHA512_256_DIGEST_LENGTH; 174 out->flags = 0; 175 out->init = sha512_256_init; 176 out->update = sha512_256_update; 177 out->final = sha512_256_final; 178 out->block_size = 128; 179 out->ctx_size = sizeof(SHA512_CTX); 180} 181 182#undef CHECK 183