├── README.md ├── fh_loader ├── stdafx.cpp ├── targetver.h ├── stdafx.h ├── platform.h ├── fh_comdef.h ├── fh_loader.vcxproj.filters ├── fh_loader_sha.h ├── ReadMe.txt ├── fh_loader.vcxproj └── fh_loader_sha.cpp ├── fh_loader.sln ├── .gitattributes └── .gitignore /README.md: -------------------------------------------------------------------------------- 1 | # Compiling using gcc 2 | Hacky as all getout, but still better than using Windows 3 | `gcc -w -D_FILE_OFFSET_BITS=64 fh_loader.cpp fh_loader_sha.cpp -o fh_loader` 4 | -------------------------------------------------------------------------------- /fh_loader/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // stdafx.cpp : source file that includes just the standard includes 2 | // fh_loader.pch will be the pre-compiled header 3 | // stdafx.obj will contain the pre-compiled type information 4 | 5 | #include "stdafx.h" 6 | 7 | // TODO: reference any additional headers you need in STDAFX.H 8 | // and not in this file 9 | -------------------------------------------------------------------------------- /fh_loader/targetver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Including SDKDDKVer.h defines the highest available Windows platform. 4 | 5 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 6 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 7 | 8 | #include 9 | -------------------------------------------------------------------------------- /fh_loader/stdafx.h: -------------------------------------------------------------------------------- 1 | // stdafx.h : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #pragma once 7 | 8 | #include "targetver.h" 9 | 10 | #include 11 | #include 12 | 13 | 14 | 15 | // TODO: reference additional headers your program requires here 16 | -------------------------------------------------------------------------------- /fh_loader/platform.h: -------------------------------------------------------------------------------- 1 | #ifndef __PLATFORM_H__ 2 | #define __PLATFORM_H__ 3 | 4 | #ifdef WINDOWS 5 | #include "stdafx.h" 6 | #include 7 | #include 8 | #include 9 | 10 | #define GETCWD _getcwd 11 | #define O_RDWR _O_RDWR 12 | #define O_SYNC _O_SEQUENTIAL 13 | #define sleep(x) Sleep(x*1000) 14 | 15 | #define fseek _fseeki64 16 | #define ftell _ftelli64 17 | #else 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #define GETCWD getcwd 26 | #define mkdir(x) mkdir(x, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) 27 | #define _stat64(a, b) stat 28 | #endif 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /fh_loader.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.21005.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "fh_loader", "fh_loader\fh_loader.vcxproj", "{049DA516-2BB4-4F13-BC46-51AE7008F6CE}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Win32 = Debug|Win32 11 | Release|Win32 = Release|Win32 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {049DA516-2BB4-4F13-BC46-51AE7008F6CE}.Debug|Win32.ActiveCfg = Debug|Win32 15 | {049DA516-2BB4-4F13-BC46-51AE7008F6CE}.Debug|Win32.Build.0 = Debug|Win32 16 | {049DA516-2BB4-4F13-BC46-51AE7008F6CE}.Release|Win32.ActiveCfg = Release|Win32 17 | {049DA516-2BB4-4F13-BC46-51AE7008F6CE}.Release|Win32.Build.0 = Release|Win32 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /fh_loader/fh_comdef.h: -------------------------------------------------------------------------------- 1 | /************************************************************************** 2 | * Common type definitions. 3 | * 4 | * Copyright (c) 2016 Qualcomm Technologies, Inc. 5 | * All Rights Reserved. 6 | * Qualcomm Proprietary 7 | * 8 | *************************************************************************/ 9 | 10 | /*=========================================================================== 11 | 12 | EDIT HISTORY FOR MODULE 13 | 14 | This section contains comments describing changes made to the module. 15 | Notice that changes are listed in reverse chronological order. 16 | 17 | $Header: //components/rel/boot.xf/1.0/QcomPkg/Tools/storage/fh_loader/fh_loader/fh_comdef.h#1 $ 18 | $DateTime: 2016/05/11 00:15:02 $ 19 | $Author: pwbldsvc $ 20 | 21 | when who what, where, why 22 | ---------- --- --------------------------------------------------------- 23 | 2016-01-15 wek Create. 24 | 25 | ===========================================================================*/ 26 | 27 | #ifndef __COMDEF_H__ 28 | #define __COMDEF_H__ 29 | 30 | #include 31 | 32 | typedef int8_t int8; 33 | typedef int16_t int16; 34 | typedef int32_t int32; 35 | typedef int64_t int64; 36 | typedef uint8_t uint8; 37 | typedef uint16_t uint16; 38 | typedef uint32_t uint32; 39 | typedef uint64_t uint64; 40 | typedef int8_t INT8; 41 | typedef int16_t INT16; 42 | typedef int32_t INT32; 43 | typedef int64_t INT64; 44 | typedef uint8_t UINT8; 45 | typedef uint16_t UINT16; 46 | typedef uint32_t UINT32; 47 | typedef uint64_t UINT64; 48 | 49 | #endif /* __COMDEF_H__ */ 50 | -------------------------------------------------------------------------------- /fh_loader/fh_loader.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | Header Files 23 | 24 | 25 | Header Files 26 | 27 | 28 | Header Files 29 | 30 | 31 | Header Files 32 | 33 | 34 | 35 | 36 | Source Files 37 | 38 | 39 | Source Files 40 | 41 | 42 | Source Files 43 | 44 | 45 | -------------------------------------------------------------------------------- /fh_loader/fh_loader_sha.h: -------------------------------------------------------------------------------- 1 | /************************************************************************** 2 | * 3 | * This module contains the software implementation for sha256. 4 | * 5 | * Copyright (c) 2016 Qualcomm Technologies, Inc. 6 | * All Rights Reserved. 7 | * 8 | *************************************************************************/ 9 | 10 | /*=========================================================================== 11 | 12 | EDIT HISTORY FOR MODULE 13 | 14 | This section contains comments describing changes made to the module. 15 | Notice that changes are listed in reverse chronological order. 16 | 17 | $Header: //components/rel/boot.xf/1.0/QcomPkg/Tools/storage/fh_loader/fh_loader/fh_loader_sha.h#1 $ 18 | $DateTime: 2016/05/11 00:15:02 $ 19 | $Author: pwbldsvc $ 20 | 21 | when who what, where, why 22 | ---------- --- --------------------------------------------------------- 23 | 2016-01-14 wek Create. Move SHA functions from security to a new file. 24 | 25 | ===========================================================================*/ 26 | 27 | 28 | #ifndef __DEVICEPROGRAMMER_SHA_H__ 29 | #define __DEVICEPROGRAMMER_SHA_H__ 30 | 31 | #include "fh_comdef.h" 32 | 33 | #define CONTEXT_LEFTOVER_FIELD_SIZE 64 34 | 35 | struct __sechsh_ctx_s 36 | { 37 | uint32 counter[2]; 38 | uint32 iv[16]; // is 64 byte for SHA2-512 39 | uint8 leftover[CONTEXT_LEFTOVER_FIELD_SIZE]; 40 | uint32 leftover_size; 41 | }; 42 | 43 | void sechsharm_sha256_init( struct __sechsh_ctx_s* ); 44 | void sechsharm_sha256_update( struct __sechsh_ctx_s*, uint8*, uint32*, uint8*, uint32 ); 45 | void sechsharm_sha256_final( struct __sechsh_ctx_s*, uint8*, uint32*, uint8* ); 46 | 47 | #endif /* __DEVICEPROGRAMMER_SHA_H__ */ 48 | -------------------------------------------------------------------------------- /fh_loader/ReadMe.txt: -------------------------------------------------------------------------------- 1 | ======================================================================== 2 | CONSOLE APPLICATION : fh_loader Project Overview 3 | ======================================================================== 4 | 5 | AppWizard has created this fh_loader application for you. 6 | 7 | This file contains a summary of what you will find in each of the files that 8 | make up your fh_loader application. 9 | 10 | 11 | fh_loader.vcxproj 12 | This is the main project file for VC++ projects generated using an Application Wizard. 13 | It contains information about the version of Visual C++ that generated the file, and 14 | information about the platforms, configurations, and project features selected with the 15 | Application Wizard. 16 | 17 | fh_loader.vcxproj.filters 18 | This is the filters file for VC++ projects generated using an Application Wizard. 19 | It contains information about the association between the files in your project 20 | and the filters. This association is used in the IDE to show grouping of files with 21 | similar extensions under a specific node (for e.g. ".cpp" files are associated with the 22 | "Source Files" filter). 23 | 24 | fh_loader.cpp 25 | This is the main application source file. 26 | 27 | ///////////////////////////////////////////////////////////////////////////// 28 | Other standard files: 29 | 30 | StdAfx.h, StdAfx.cpp 31 | These files are used to build a precompiled header (PCH) file 32 | named fh_loader.pch and a precompiled types file named StdAfx.obj. 33 | 34 | ///////////////////////////////////////////////////////////////////////////// 35 | Other notes: 36 | 37 | AppWizard uses "TODO:" comments to indicate parts of the source code you 38 | should add to or customize. 39 | 40 | ///////////////////////////////////////////////////////////////////////////// 41 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.sln.docstates 8 | 9 | # Build results 10 | 11 | [Dd]ebug/ 12 | [Rr]elease/ 13 | x64/ 14 | build/ 15 | [Bb]in/ 16 | [Oo]bj/ 17 | 18 | # Enable "build/" folder in the NuGet Packages folder since NuGet packages use it for MSBuild targets 19 | !packages/*/build/ 20 | 21 | # MSTest test Results 22 | [Tt]est[Rr]esult*/ 23 | [Bb]uild[Ll]og.* 24 | 25 | *_i.c 26 | *_p.c 27 | *.ilk 28 | *.meta 29 | *.obj 30 | *.pch 31 | *.pdb 32 | *.pgc 33 | *.pgd 34 | *.rsp 35 | *.sbr 36 | *.tlb 37 | *.tli 38 | *.tlh 39 | *.tmp 40 | *.tmp_proj 41 | *.log 42 | *.vspscc 43 | *.vssscc 44 | .builds 45 | *.pidb 46 | *.log 47 | *.scc 48 | 49 | # Visual C++ cache files 50 | ipch/ 51 | *.aps 52 | *.ncb 53 | *.opensdf 54 | *.sdf 55 | *.cachefile 56 | 57 | # Visual Studio profiler 58 | *.psess 59 | *.vsp 60 | *.vspx 61 | 62 | # Guidance Automation Toolkit 63 | *.gpState 64 | 65 | # ReSharper is a .NET coding add-in 66 | _ReSharper*/ 67 | *.[Rr]e[Ss]harper 68 | 69 | # TeamCity is a build add-in 70 | _TeamCity* 71 | 72 | # DotCover is a Code Coverage Tool 73 | *.dotCover 74 | 75 | # NCrunch 76 | *.ncrunch* 77 | .*crunch*.local.xml 78 | 79 | # Installshield output folder 80 | [Ee]xpress/ 81 | 82 | # DocProject is a documentation generator add-in 83 | DocProject/buildhelp/ 84 | DocProject/Help/*.HxT 85 | DocProject/Help/*.HxC 86 | DocProject/Help/*.hhc 87 | DocProject/Help/*.hhk 88 | DocProject/Help/*.hhp 89 | DocProject/Help/Html2 90 | DocProject/Help/html 91 | 92 | # Click-Once directory 93 | publish/ 94 | 95 | # Publish Web Output 96 | *.Publish.xml 97 | 98 | # NuGet Packages Directory 99 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 100 | #packages/ 101 | 102 | # Windows Azure Build Output 103 | csx 104 | *.build.csdef 105 | 106 | # Windows Store app package directory 107 | AppPackages/ 108 | 109 | # Others 110 | sql/ 111 | *.Cache 112 | ClientBin/ 113 | [Ss]tyle[Cc]op.* 114 | ~$* 115 | *~ 116 | *.dbmdl 117 | *.[Pp]ublish.xml 118 | *.pfx 119 | *.publishsettings 120 | 121 | # RIA/Silverlight projects 122 | Generated_Code/ 123 | 124 | # Backup & report files from converting an old project file to a newer 125 | # Visual Studio version. Backup files are not needed, because we have git ;-) 126 | _UpgradeReport_Files/ 127 | Backup*/ 128 | UpgradeLog*.XML 129 | UpgradeLog*.htm 130 | 131 | # SQL Server files 132 | App_Data/*.mdf 133 | App_Data/*.ldf 134 | 135 | 136 | #LightSwitch generated files 137 | GeneratedArtifacts/ 138 | _Pvt_Extensions/ 139 | ModelManifest.xml 140 | 141 | # ========================= 142 | # Windows detritus 143 | # ========================= 144 | 145 | # Windows image file caches 146 | Thumbs.db 147 | ehthumbs.db 148 | 149 | # Folder config file 150 | Desktop.ini 151 | 152 | # Recycle Bin used on file shares 153 | $RECYCLE.BIN/ 154 | 155 | # Mac desktop service store files 156 | .DS_Store 157 | -------------------------------------------------------------------------------- /fh_loader/fh_loader.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {049DA516-2BB4-4F13-BC46-51AE7008F6CE} 15 | Win32Proj 16 | fh_loader 17 | 18 | 19 | 20 | Application 21 | true 22 | v120 23 | Unicode 24 | 25 | 26 | Application 27 | false 28 | v120 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | true 44 | 45 | 46 | false 47 | 48 | 49 | 50 | Use 51 | TurnOffAllWarnings 52 | Disabled 53 | WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) 54 | 55 | 56 | Console 57 | true 58 | 59 | 60 | 61 | 62 | Level3 63 | Use 64 | MaxSpeed 65 | true 66 | true 67 | WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) 68 | 69 | 70 | Console 71 | true 72 | true 73 | true 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | true 82 | 83 | 84 | true 85 | 86 | 87 | 88 | 89 | 90 | 91 | true 92 | 93 | 94 | true 95 | 96 | 97 | Create 98 | Create 99 | 100 | 101 | 102 | 103 | 104 | -------------------------------------------------------------------------------- /fh_loader/fh_loader_sha.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************** 2 | * 3 | * This module contains the software implementation for sha256. 4 | * 5 | * Copyright (c) 2016 Qualcomm Technologies, Inc. 6 | * All Rights Reserved. 7 | * 8 | *************************************************************************/ 9 | 10 | /* from OpenBSD: sha2.c,v 1.11 2005/08/08 08:05:35 espie Exp */ 11 | /* 12 | * FILE: sha2.c 13 | * AUTHOR: Aaron D. Gifford 14 | * 15 | * Copyright (c) 2000-2001, Aaron D. Gifford 16 | * All rights reserved. 17 | * 18 | * Redistribution and use in source and binary forms, with or without 19 | * modification, are permitted provided that the following conditions 20 | * are met: 21 | * 1. Redistributions of source code must retain the above copyright 22 | * notice, this list of conditions and the following disclaimer. 23 | * 2. Redistributions in binary form must reproduce the above copyright 24 | * notice, this list of conditions and the following disclaimer in the 25 | * documentation and/or other materials provided with the distribution. 26 | * 3. Neither the name of the copyright holder nor the names of contributors 27 | * may be used to endorse or promote products derived from this software 28 | * without specific prior written permission. 29 | * 30 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) "AS IS" AND 31 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE 34 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 36 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 39 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 40 | * SUCH DAMAGE. 41 | * 42 | * $From: sha2.c,v 1.1 2001/11/08 00:01:51 adg Exp adg $ 43 | */ 44 | 45 | 46 | /*=========================================================================== 47 | 48 | EDIT HISTORY FOR MODULE 49 | 50 | This section contains comments describing changes made to the module. 51 | Notice that changes are listed in reverse chronological order. 52 | 53 | $Header: //source/qcom/qct/core/storage/tools/fh_loader/vs2010/fh_loader/fh_loader_sha.c#1 $ 54 | $DateTime: 2016/01/14 18:37:20 $ 55 | $Author: wkimberl $ 56 | 57 | when who what, where, why 58 | ---------- --- --------------------------------------------------------- 59 | 2016-01-14 wek Create. Move SHA functions from security to a new file. 60 | 61 | ===========================================================================*/ 62 | 63 | #include "platform.h" 64 | #include "fh_loader_sha.h" 65 | #include 66 | 67 | #define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8) 68 | #define HSHSHA_COUNTER_SIZE 8 69 | #define SHA256_BLOCK_LENGTH 64 70 | #define Sigma0_256(x) (SHA_S32(2, (x)) ^ SHA_S32(13, (x)) ^ SHA_S32(22, (x))) 71 | #define Sigma1_256(x) (SHA_S32(6, (x)) ^ SHA_S32(11, (x)) ^ SHA_S32(25, (x))) 72 | #define sigma0_256(x) (SHA_S32(7, (x)) ^ SHA_S32(18, (x)) ^ R(3 , (x))) 73 | #define sigma1_256(x) (SHA_S32(17, (x)) ^ SHA_S32(19, (x)) ^ R(10, (x))) 74 | 75 | // Initial hash value H for SHA-256: 76 | static const uint32 sha256_initial_hash_value[8] = 77 | { 78 | 0x6a09e667UL, 79 | 0xbb67ae85UL, 80 | 0x3c6ef372UL, 81 | 0xa54ff53aUL, 82 | 0x510e527fUL, 83 | 0x9b05688cUL, 84 | 0x1f83d9abUL, 85 | 0x5be0cd19UL 86 | }; 87 | 88 | void sechsharm_sha256_init (struct __sechsh_ctx_s* context) 89 | { 90 | if (NULL == context) 91 | return; 92 | 93 | memset ( context, 0, sizeof ( struct __sechsh_ctx_s) ); 94 | 95 | /* SHA256 initialization constants */ 96 | //memcpy((uint8*)context->iv, sizeof(sha256_initial_hash_value), (uint8*)sha256_initial_hash_value, sizeof(sha256_initial_hash_value)); 97 | memcpy ( (uint8*) context->iv, (uint8*) sha256_initial_hash_value, sizeof (sha256_initial_hash_value) ); 98 | 99 | context->counter[0] = 0; 100 | context->counter[1] = 0; 101 | 102 | } 103 | 104 | static const uint32 K256[64] = 105 | { 106 | 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 107 | 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 108 | 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL, 109 | 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL, 110 | 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL, 111 | 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 112 | 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 113 | 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL, 114 | 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL, 115 | 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL, 116 | 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 117 | 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 118 | 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL, 119 | 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL, 120 | 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL, 121 | 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL 122 | }; 123 | 124 | #define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) 125 | #define R(b,x) ((x) >> (b)) 126 | 127 | #define BE_8_TO_32(dst, cp) do { \ 128 | (dst) = (uint32)(cp)[3] | ((uint32)(cp)[2] << 8) | \ 129 | ((uint32)(cp)[1] << 16) | ((uint32)(cp)[0] << 24); \ 130 | } while(0) 131 | 132 | 133 | #define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z))) 134 | #define SHA_S32(b,x) (((x) >> (b)) | ((x) << (32 - (b)))) 135 | 136 | #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) do { \ 137 | BE_8_TO_32(W256[j], data); \ 138 | data += 4; \ 139 | T1 = (h) + Sigma1_256((e)) + Ch((e), (f), (g)) + K256[j] + W256[j]; \ 140 | (d) += T1; \ 141 | (h) = T1 + Sigma0_256((a)) + Maj((a), (b), (c)); \ 142 | j++; \ 143 | } while(0) 144 | 145 | #define ROUND256(a,b,c,d,e,f,g,h) do { \ 146 | s0 = W256[(j+1)&0x0f]; \ 147 | s0 = sigma0_256(s0); \ 148 | s1 = W256[(j+14)&0x0f]; \ 149 | s1 = sigma1_256(s1); \ 150 | T1 = (h) + Sigma1_256((e)) + Ch((e), (f), (g)) + K256[j] + \ 151 | (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \ 152 | (d) += T1; \ 153 | (h) = T1 + Sigma0_256((a)) + Maj((a), (b), (c)); \ 154 | j++; \ 155 | } while(0) 156 | 157 | static void sechsharm_sha256_transform 158 | ( 159 | uint32 state[8], 160 | const uint8 data[SHA256_BLOCK_LENGTH] 161 | ) 162 | { 163 | uint32 a, b, c, d, e, f, g, h, s0, s1; 164 | uint32 T1, W256[16]; 165 | int j; 166 | 167 | /* Initialize registers with the prev. intermediate value */ 168 | a = state[0]; 169 | b = state[1]; 170 | c = state[2]; 171 | d = state[3]; 172 | e = state[4]; 173 | f = state[5]; 174 | g = state[6]; 175 | h = state[7]; 176 | 177 | j = 0; 178 | 179 | /* Klocwork: Filter. For the second iteration of the loop j 180 | * is 15 for ROUND256_0_TO_15(b,c,d,e,f,g,h,a) and is then 181 | * incremented to 16 at which time it breaks out of the loop. 182 | */ 183 | do 184 | { 185 | /* Rounds 0 to 15 (unrolled): */ 186 | ROUND256_0_TO_15 (a, b, c, d, e, f, g, h); 187 | ROUND256_0_TO_15 (h, a, b, c, d, e, f, g); 188 | ROUND256_0_TO_15 (g, h, a, b, c, d, e, f); 189 | ROUND256_0_TO_15 (f, g, h, a, b, c, d, e); 190 | ROUND256_0_TO_15 (e, f, g, h, a, b, c, d); 191 | ROUND256_0_TO_15 (d, e, f, g, h, a, b, c); 192 | ROUND256_0_TO_15 (c, d, e, f, g, h, a, b); 193 | ROUND256_0_TO_15 (b, c, d, e, f, g, h, a); 194 | } 195 | while (j < 16); 196 | 197 | /* Now for the remaining rounds up to 63: */ 198 | /* Klocwork: Filter. The first time through the loop 199 | * j is 16 for ROUND256(a,b,c,d,e,f,g,h) and 23 200 | * for ROUND256(b,c,d,e,f,g,h,a) and the last time 201 | * through the loop j is 63 for ROUND256(b,c,d,e,f,g,h,a) 202 | * which will not overflow the array. 203 | */ 204 | do 205 | { 206 | ROUND256 (a, b, c, d, e, f, g, h); 207 | ROUND256 (h, a, b, c, d, e, f, g); 208 | ROUND256 (g, h, a, b, c, d, e, f); 209 | ROUND256 (f, g, h, a, b, c, d, e); 210 | 211 | ROUND256 (e, f, g, h, a, b, c, d); 212 | ROUND256 (d, e, f, g, h, a, b, c); 213 | ROUND256 (c, d, e, f, g, h, a, b); 214 | ROUND256 (b, c, d, e, f, g, h, a); 215 | } 216 | while (j < 64); 217 | 218 | /* Compute the current intermediate hash value */ 219 | state[0] += a; 220 | state[1] += b; 221 | state[2] += c; 222 | state[3] += d; 223 | state[4] += e; 224 | state[5] += f; 225 | state[6] += g; 226 | state[7] += h; 227 | 228 | /* Clean up */ 229 | a = b = c = d = e = f = g = h = T1 = 0; 230 | } 231 | 232 | #define BE_32_TO_8(cp, src) do { \ 233 | (cp)[0] = (uint8)((src) >> 24); \ 234 | (cp)[1] = (uint8)((src) >> 16); \ 235 | (cp)[2] = (uint8)((src) >> 8); \ 236 | (cp)[3] = (uint8)((src)); \ 237 | } while (0) 238 | 239 | 240 | #define BE_64_TO_8(cp, src) do { \ 241 | (cp)[0] = (uint8)((src) >> 56); \ 242 | (cp)[1] = (uint8)((src) >> 48); \ 243 | (cp)[2] = (uint8)((src) >> 40); \ 244 | (cp)[3] = (uint8)((src) >> 32); \ 245 | (cp)[4] = (uint8)((src) >> 24); \ 246 | (cp)[5] = (uint8)((src) >> 16); \ 247 | (cp)[6] = (uint8)((src) >> 8); \ 248 | (cp)[7] = (uint8)((src)); \ 249 | } while (0) 250 | 251 | void sechsharm_sha256_update 252 | ( 253 | struct __sechsh_ctx_s* context, /* context */ 254 | uint8* leftover, /* leftover input in context */ 255 | uint32* leftover_size, 256 | uint8* data, 257 | uint32 len 258 | ) 259 | { 260 | uint32 freespace, usedspace; 261 | uint64 bitcounter; 262 | 263 | /* Calling with no data is valid (we do nothing) */ 264 | if (len == 0) 265 | return; 266 | 267 | memcpy (&bitcounter, context->counter, HSHSHA_COUNTER_SIZE); 268 | usedspace = ( (uint32) bitcounter >> 3) % SHA256_BLOCK_LENGTH; 269 | 270 | if (usedspace > 0) 271 | { 272 | /* Calculate how much free space is available in the buffer */ 273 | freespace = SHA256_BLOCK_LENGTH - usedspace; 274 | 275 | if (len >= freespace) 276 | { 277 | /* Fill the buffer completely and process it */ 278 | memcpy ( (uint8*) &leftover[usedspace], data, freespace); 279 | bitcounter += (uint32) (freespace << 3); 280 | len -= freespace; 281 | data += freespace; 282 | sechsharm_sha256_transform (context->iv, leftover); 283 | } 284 | else 285 | { 286 | /* The buffer is not yet full */ 287 | memcpy ( (uint8*) &leftover[usedspace], data, len); 288 | bitcounter += (uint32) (len << 3); 289 | /* Clean up: */ 290 | usedspace = freespace = 0; 291 | memcpy (context->counter, &bitcounter, HSHSHA_COUNTER_SIZE); 292 | return; 293 | } 294 | } 295 | 296 | while (len >= SHA256_BLOCK_LENGTH) 297 | { 298 | /* Process as many complete blocks as we can */ 299 | sechsharm_sha256_transform (context->iv, data); 300 | bitcounter += SHA256_BLOCK_LENGTH << 3; 301 | len -= SHA256_BLOCK_LENGTH; 302 | data += SHA256_BLOCK_LENGTH; 303 | } 304 | 305 | if (len > 0) 306 | { 307 | /* There's left-overs, so save 'em */ 308 | memcpy ( (uint8*) leftover, data, len); 309 | bitcounter += (uint32) (len << 3); 310 | } 311 | 312 | /* Clean up: */ 313 | usedspace = freespace = 0; 314 | memcpy (context->counter, &bitcounter, HSHSHA_COUNTER_SIZE); 315 | } 316 | 317 | static void sechsharm_sha256_pad 318 | ( 319 | struct __sechsh_ctx_s* context, 320 | uint8* leftover /* leftover input in context */ 321 | ) 322 | { 323 | uint32 usedspace; 324 | uint64 bitcounter; 325 | 326 | memcpy (&bitcounter, context->counter, 8); 327 | usedspace = (context->counter[0] >> 3) % SHA256_BLOCK_LENGTH; 328 | 329 | if (usedspace > 0) 330 | { 331 | /* Begin padding with a 1 bit: */ 332 | leftover[usedspace++] = 0x80; 333 | 334 | if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) 335 | { 336 | /* Set-up for the last transform: */ 337 | memset (&leftover[usedspace], 0, 338 | SHA256_SHORT_BLOCK_LENGTH - usedspace); 339 | } 340 | else 341 | { 342 | if (usedspace < SHA256_BLOCK_LENGTH) 343 | { 344 | memset (&leftover[usedspace], 0, 345 | SHA256_BLOCK_LENGTH - usedspace); 346 | } 347 | 348 | /* Do second-to-last transform: */ 349 | sechsharm_sha256_transform (context->iv, leftover); 350 | 351 | /* Prepare for last transform: */ 352 | memset (leftover, 0, SHA256_SHORT_BLOCK_LENGTH); 353 | } 354 | } 355 | else 356 | { 357 | /* Set-up for the last transform: */ 358 | memset (leftover, 0, SHA256_SHORT_BLOCK_LENGTH); 359 | 360 | /* Begin padding with a 1 bit: */ 361 | *leftover = 0x80; 362 | } 363 | 364 | /* Store the length of input data (in bits) in big endian format: */ 365 | BE_64_TO_8 (&leftover[SHA256_SHORT_BLOCK_LENGTH], bitcounter); 366 | 367 | /* Final transform: */ 368 | sechsharm_sha256_transform (context->iv, leftover); 369 | 370 | memcpy (context->counter, &bitcounter, HSHSHA_COUNTER_SIZE); 371 | /* Clean up: */ 372 | usedspace = 0; 373 | } 374 | 375 | void sechsharm_sha256_final 376 | ( 377 | struct __sechsh_ctx_s* context, /* context */ 378 | uint8* leftover, /* leftover input in context */ 379 | uint32* leftover_size, 380 | uint8* digest 381 | ) 382 | { 383 | sechsharm_sha256_pad (context, leftover); 384 | 385 | /* If no digest buffer is passed, we don't bother doing this: */ 386 | if (digest != NULL) 387 | { 388 | int i; 389 | 390 | /* Convert TO host byte order */ 391 | for (i = 0; i < 8; i++) 392 | BE_32_TO_8 (digest + i * 4, context->iv[i]); 393 | 394 | /* zeroize context structure */ 395 | //BM: ??? 396 | //our_memset((uint8*)context, 0, sizeof(*context)); 397 | 398 | sechsharm_sha256_transform ( context->iv, /*context->*/leftover ); 399 | } 400 | } 401 | 402 | 403 | 404 | --------------------------------------------------------------------------------