1/* 2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the OpenSSL license (the "License"). You may not use 5 * this file except in compliance with the License. You can obtain a copy 6 * in the file LICENSE in the source distribution or at 7 * https://www.openssl.org/source/license.html 8 */ 9 10#include <openssl/digest.h> 11 12#include <assert.h> 13#include <string.h> 14 15#include <openssl/nid.h> 16 17#include "../../internal.h" 18#include "../bcm_interface.h" 19#include "../delocate.h" 20#include "internal.h" 21 22#if defined(NDEBUG) 23#define CHECK(x) (void)(x) 24#else 25#define CHECK(x) assert(x) 26#endif 27 28 29static void sha1_init(EVP_MD_CTX *ctx) { 30 BCM_sha1_init(reinterpret_cast<SHA_CTX *>(ctx->md_data)); 31} 32 33static void sha1_update(EVP_MD_CTX *ctx, const void *data, size_t count) { 34 BCM_sha1_update(reinterpret_cast<SHA_CTX *>(ctx->md_data), data, count); 35} 36 37static void sha1_final(EVP_MD_CTX *ctx, uint8_t *md) { 38 BCM_sha1_final(md, reinterpret_cast<SHA_CTX *>(ctx->md_data)); 39} 40 41DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha1) { 42 out->type = NID_sha1; 43 out->md_size = BCM_SHA_DIGEST_LENGTH; 44 out->flags = 0; 45 out->init = sha1_init; 46 out->update = sha1_update; 47 out->final = sha1_final; 48 out->block_size = 64; 49 out->ctx_size = sizeof(SHA_CTX); 50} 51 52 53static void sha224_init(EVP_MD_CTX *ctx) { 54 BCM_sha224_init(reinterpret_cast<SHA256_CTX *>(ctx->md_data)); 55} 56 57static void sha224_update(EVP_MD_CTX *ctx, const void *data, size_t count) { 58 BCM_sha224_update(reinterpret_cast<SHA256_CTX *>(ctx->md_data), data, count); 59} 60 61static void sha224_final(EVP_MD_CTX *ctx, uint8_t *md) { 62 BCM_sha224_final(md, reinterpret_cast<SHA256_CTX *>(ctx->md_data)); 63} 64 65DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha224) { 66 out->type = NID_sha224; 67 out->md_size = BCM_SHA224_DIGEST_LENGTH; 68 out->flags = 0; 69 out->init = sha224_init; 70 out->update = sha224_update; 71 out->final = sha224_final; 72 out->block_size = 64; 73 out->ctx_size = sizeof(SHA256_CTX); 74} 75 76 77static void sha256_init(EVP_MD_CTX *ctx) { 78 BCM_sha256_init(reinterpret_cast<SHA256_CTX *>(ctx->md_data)); 79} 80 81static void sha256_update(EVP_MD_CTX *ctx, const void *data, size_t count) { 82 BCM_sha256_update(reinterpret_cast<SHA256_CTX *>(ctx->md_data), data, count); 83} 84 85static void sha256_final(EVP_MD_CTX *ctx, uint8_t *md) { 86 BCM_sha256_final(md, reinterpret_cast<SHA256_CTX *>(ctx->md_data)); 87} 88 89DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha256) { 90 out->type = NID_sha256; 91 out->md_size = BCM_SHA256_DIGEST_LENGTH; 92 out->flags = 0; 93 out->init = sha256_init; 94 out->update = sha256_update; 95 out->final = sha256_final; 96 out->block_size = 64; 97 out->ctx_size = sizeof(SHA256_CTX); 98} 99 100 101static void sha384_init(EVP_MD_CTX *ctx) { 102 BCM_sha384_init(reinterpret_cast<SHA512_CTX *>(ctx->md_data)); 103} 104 105static void sha384_update(EVP_MD_CTX *ctx, const void *data, size_t count) { 106 BCM_sha384_update(reinterpret_cast<SHA512_CTX *>(ctx->md_data), data, count); 107} 108 109static void sha384_final(EVP_MD_CTX *ctx, uint8_t *md) { 110 BCM_sha384_final(md, reinterpret_cast<SHA512_CTX *>(ctx->md_data)); 111} 112 113DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha384) { 114 out->type = NID_sha384; 115 out->md_size = BCM_SHA384_DIGEST_LENGTH; 116 out->flags = 0; 117 out->init = sha384_init; 118 out->update = sha384_update; 119 out->final = sha384_final; 120 out->block_size = 128; 121 out->ctx_size = sizeof(SHA512_CTX); 122} 123 124 125static void sha512_init(EVP_MD_CTX *ctx) { 126 BCM_sha512_init(reinterpret_cast<SHA512_CTX *>(ctx->md_data)); 127} 128 129static void sha512_update(EVP_MD_CTX *ctx, const void *data, size_t count) { 130 BCM_sha512_update(reinterpret_cast<SHA512_CTX *>(ctx->md_data), data, count); 131} 132 133static void sha512_final(EVP_MD_CTX *ctx, uint8_t *md) { 134 BCM_sha512_final(md, reinterpret_cast<SHA512_CTX *>(ctx->md_data)); 135} 136 137DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha512) { 138 out->type = NID_sha512; 139 out->md_size = BCM_SHA512_DIGEST_LENGTH; 140 out->flags = 0; 141 out->init = sha512_init; 142 out->update = sha512_update; 143 out->final = sha512_final; 144 out->block_size = 128; 145 out->ctx_size = sizeof(SHA512_CTX); 146} 147 148 149static void sha512_256_init(EVP_MD_CTX *ctx) { 150 BCM_sha512_256_init(reinterpret_cast<SHA512_CTX *>(ctx->md_data)); 151} 152 153static void sha512_256_update(EVP_MD_CTX *ctx, const void *data, size_t count) { 154 BCM_sha512_256_update(reinterpret_cast<SHA512_CTX *>(ctx->md_data), data, 155 count); 156} 157 158static void sha512_256_final(EVP_MD_CTX *ctx, uint8_t *md) { 159 BCM_sha512_256_final(md, reinterpret_cast<SHA512_CTX *>(ctx->md_data)); 160} 161 162DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha512_256) { 163 out->type = NID_sha512_256; 164 out->md_size = BCM_SHA512_256_DIGEST_LENGTH; 165 out->flags = 0; 166 out->init = sha512_256_init; 167 out->update = sha512_256_update; 168 out->final = sha512_256_final; 169 out->block_size = 128; 170 out->ctx_size = sizeof(SHA512_CTX); 171} 172 173#undef CHECK 174