Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hashes implementation #3

Merged
merged 7 commits into from
May 22, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@ cmake-build-*/
####
# Sublime Text 3
*.sublime-workspace

####
# Visual Studio (VisualGDB)
Debug*/
gcc*
virgil-crypto-c.*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add file virgil-crypto-c.sublime-project that is not expected behavior.

virgil-crypto-c-*
53 changes: 52 additions & 1 deletion codegen/implementation/implementor_mbedtls.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@

<require library = "mbedTLS" />

<implementation name = "sha256">
This is MbedTLS implementation of SHA224.

<require_include name = "mbedtls/sha224.h" type = "context" />

<interface name = "hash info">
<constant name = "digest size" value = "24" />
</interface>

<interface name = "hash">
</interface>

<interface name = "hash stream">
<context name = "hash ctx" class = "mbedtls_sha224_context" access = "readwrite" is_reference="0"/>
</interface>

</implementation>
<implementation name = "sha256">
This is MbedTLS implementation of SHA256.

Expand All @@ -20,7 +37,41 @@
</interface>

</implementation>
<!--
<implementation name = "sha384">
This is MbedTLS implementation of SHA384.

<require_include name = "mbedtls/sha512.h" type = "context" />

<interface name = "hash info">
<constant name = "digest size" value = "48" />
</interface>

<interface name = "hash">
</interface>

<interface name = "hash stream">
<context name = "hash ctx" class = "mbedtls_sha384_context" access = "readwrite" is_reference="0"/>
</interface>

</implementation>
<implementation name = "sha512">
This is MbedTLS implementation of SHA512.

<require_include name = "mbedtls/sha512.h" type = "context" />

<interface name = "hash info">
<constant name = "digest size" value = "64" />
</interface>

<interface name = "hash">
</interface>

<interface name = "hash stream">
<context name = "hash ctx" class = "mbedtls_sha512_context" access = "readwrite" is_reference="0"/>
</interface>

</implementation>
<!--
<implementation name = "asn1_wr">
This is MbedTLS implementation of ASN.1 writer.

Expand Down
35 changes: 19 additions & 16 deletions library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ project(virgil-crypto-c VERSION 0.1.0 LANGUAGES C)
# MbedTLS
#
if(NOT TARGET mbed::crypto)
message(FATAL_ERROR "Required library MbedTLS is not found - looking for target 'mbed::crypto'.")
message(FATAL_ERROR "Require library MbedTLS defined as a target mbed::crypto")
endif()

# ---------------------------------------------------------------------------
Expand All @@ -59,10 +59,6 @@ target_sources(vsf
PUBLIC
"${CMAKE_CURRENT_LIST_DIR}/include/virgil/vsf_api.h"
"${CMAKE_CURRENT_LIST_DIR}/include/virgil/vsf_assert.h"
"${CMAKE_CURRENT_LIST_DIR}/include/virgil/vsf_cipher.h"
"${CMAKE_CURRENT_LIST_DIR}/include/virgil/vsf_cipher_padding.h"
"${CMAKE_CURRENT_LIST_DIR}/include/virgil/vsf_decrypt.h"
"${CMAKE_CURRENT_LIST_DIR}/include/virgil/vsf_encrypt.h"
"${CMAKE_CURRENT_LIST_DIR}/include/virgil/vsf_hash.h"
"${CMAKE_CURRENT_LIST_DIR}/include/virgil/vsf_hash_info.h"
"${CMAKE_CURRENT_LIST_DIR}/include/virgil/vsf_hash_stream.h"
Expand All @@ -71,32 +67,27 @@ target_sources(vsf
"${CMAKE_CURRENT_LIST_DIR}/include/virgil/vsf_kdf1.h"
"${CMAKE_CURRENT_LIST_DIR}/include/virgil/vsf_library.h"
"${CMAKE_CURRENT_LIST_DIR}/include/virgil/vsf_memory.h"
"${CMAKE_CURRENT_LIST_DIR}/include/virgil/vsf_sha224.h"
"${CMAKE_CURRENT_LIST_DIR}/include/virgil/vsf_sha256.h"
"${CMAKE_CURRENT_LIST_DIR}/include/virgil/vsf_sha384.h"
"${CMAKE_CURRENT_LIST_DIR}/include/virgil/vsf_sha512.h"

"${CMAKE_CURRENT_LIST_DIR}/include/virgil/private/vsf_api_private.h"
"${CMAKE_CURRENT_LIST_DIR}/include/virgil/private/vsf_cipher_api.h"
"${CMAKE_CURRENT_LIST_DIR}/include/virgil/private/vsf_decrypt_api.h"
"${CMAKE_CURRENT_LIST_DIR}/include/virgil/private/vsf_encrypt_api.h"
"${CMAKE_CURRENT_LIST_DIR}/include/virgil/private/vsf_hash_api.h"
"${CMAKE_CURRENT_LIST_DIR}/include/virgil/private/vsf_hash_info_api.h"
"${CMAKE_CURRENT_LIST_DIR}/include/virgil/private/vsf_hash_stream_api.h"
"${CMAKE_CURRENT_LIST_DIR}/include/virgil/private/vsf_impl_private.h"
"${CMAKE_CURRENT_LIST_DIR}/include/virgil/private/vsf_kdf1_impl.h"
"${CMAKE_CURRENT_LIST_DIR}/include/virgil/private/vsf_kdf_api.h"
"${CMAKE_CURRENT_LIST_DIR}/include/virgil/private/vsf_sha224_impl.h"
"${CMAKE_CURRENT_LIST_DIR}/include/virgil/private/vsf_sha256_impl.h"

"${CMAKE_CURRENT_LIST_DIR}/include/virgil/private/vsf_sha384_impl.h"
"${CMAKE_CURRENT_LIST_DIR}/include/virgil/private/vsf_sha512_impl.h"

PRIVATE
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_api.c"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_api_private.c"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_assert.c"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_cipher.c"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_cipher_api.c"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_cipher_padding.c"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_decrypt.c"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_decrypt_api.c"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_encrypt.c"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_encrypt_api.c"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_hash.c"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_hash_api.c"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_hash_info.c"
Expand All @@ -113,10 +104,22 @@ target_sources(vsf
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_kdf_api.c"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_library.c"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_memory.c"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_sha224.c"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_sha224_impl.c"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_sha224_internal.c"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_sha224_internal.h"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_sha256.c"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_sha256_impl.c"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_sha256_internal.c"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_sha256_internal.h"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_sha384.c"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_sha384_impl.c"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_sha384_internal.c"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_sha384_internal.h"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_sha512.c"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_sha512_impl.c"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_sha512_internal.c"
"${CMAKE_CURRENT_LIST_DIR}/src/vsf_sha512_internal.h"
)

#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,19 @@

// @description
// --------------------------------------------------------------------------
// Interface 'decrypt' API.
// Types of the 'sha224' implementation.
// This types SHOULD NOT be used directly.
// The only purpose of including this module is to place implementation
// object in the stack memory.
// --------------------------------------------------------------------------

#ifndef VSF_DECRYPT_API_H_INCLUDED
#define VSF_DECRYPT_API_H_INCLUDED
#ifndef VSF_SHA224_IMPL_H_INCLUDED
#define VSF_SHA224_IMPL_H_INCLUDED

#include "vsf_library.h"
#include "vsf_api.h"
#include "vsf_impl.h"
#include "vsf_impl_private.h"

#include <mbedtls/sha256.h>
// @end


Expand All @@ -69,24 +73,17 @@ extern "C" {
// --------------------------------------------------------------------------

//
// Callback. Decrypt given data.
//
typedef int (*vsf_decrypt_api_decrypt_fn) (vsf_impl_t* impl, const byte* enc, size_t enc_len,
byte* data, size_t data_len, size_t* out_len);

//
// Contains API requirements of the interface 'decrypt'.
// Handles implementation details.
//
struct vsf_decrypt_api_t {
struct vsf_sha224_impl_t {
//
// API's unique identifier, MUST be first in the structure.
// For interface 'decrypt' MUST be equal to the 'vsf_api_tag_DECRYPT'.
// Compile-time known information about this implementation.
//
vsf_api_tag_t api_tag;
const vsf_impl_info_t* info;
//
// Decrypt given data.
// Interface implementation specific context.
//
vsf_decrypt_api_decrypt_fn decrypt_cb;
mbedtls_sha256_context hash_ctx;
};


Expand All @@ -102,5 +99,5 @@ struct vsf_decrypt_api_t {


// @footer
#endif // VSF_DECRYPT_API_H_INCLUDED
#endif // VSF_SHA224_IMPL_H_INCLUDED
// @end
103 changes: 103 additions & 0 deletions library/include/virgil/private/vsf_sha384_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// @license
// --------------------------------------------------------------------------
// Copyright (C) 2015-2018 Virgil Security Inc.
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// (1) Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// (2) Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in
// the documentation and/or other materials provided with the
// distribution.
//
// (3) Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
// IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// Lead Maintainer: Virgil Security Inc. <[email protected]>
// --------------------------------------------------------------------------


// @warning
// --------------------------------------------------------------------------
// This file is partially generated.
// Generated blocks are enclosed between tags [@<tag>, @end].
// User's code can be added between tags [@end, @<tag>].
// --------------------------------------------------------------------------


// @description
// --------------------------------------------------------------------------
// Types of the 'sha384' implementation.
// This types SHOULD NOT be used directly.
// The only purpose of including this module is to place implementation
// object in the stack memory.
// --------------------------------------------------------------------------

#ifndef VSF_SHA384_IMPL_H_INCLUDED
#define VSF_SHA384_IMPL_H_INCLUDED

#include "vsf_library.h"
#include "vsf_impl_private.h"

#include <mbedtls/sha512.h>
// @end


#ifdef __cplusplus
extern "C" {
#endif


// @generated
// --------------------------------------------------------------------------
// Generated section start.
// --------------------------------------------------------------------------

//
// Handles implementation details.
//
struct vsf_sha384_impl_t {
//
// Compile-time known information about this implementation.
//
const vsf_impl_info_t* info;
//
// Interface implementation specific context.
//
mbedtls_sha512_context hash_ctx;
};


// --------------------------------------------------------------------------
// Generated section end.
// --------------------------------------------------------------------------
// @end


#ifdef __cplusplus
}
#endif


// @footer
#endif // VSF_SHA384_IMPL_H_INCLUDED
// @end
Loading