<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> <title>ldns documentation</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> <link href="tabs.css" rel="stylesheet" type="text/css"> </head><body> <div class="logo"> <img src="LogoInGradientBar2-y100.png"/> </div> <!-- Generated by Doxygen 1.7.4 --> <div id="navrow1" class="tabs"> <ul class="tablist"> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="pages.html"><span>Related Pages</span></a></li> <li><a href="annotated.html"><span>Data Structures</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> <li><a href="dirs.html"><span>Directories</span></a></li> </ul> </div> <div id="navrow2" class="tabs2"> <ul class="tablist"> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>Globals</span></a></li> </ul> </div> <div class="header"> <div class="headertitle"> <div class="title">sha2.c</div> </div> </div> <div class="contents"> <a href="sha2_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span> <a name="l00002"></a>00002 <span class="comment"> * FILE: sha2.c</span> <a name="l00003"></a>00003 <span class="comment"> * AUTHOR: Aaron D. Gifford - http://www.aarongifford.com/</span> <a name="l00004"></a>00004 <span class="comment"> * </span> <a name="l00005"></a>00005 <span class="comment"> * Copyright (c) 2000-2001, Aaron D. Gifford</span> <a name="l00006"></a>00006 <span class="comment"> * All rights reserved.</span> <a name="l00007"></a>00007 <span class="comment"> *</span> <a name="l00008"></a>00008 <span class="comment"> * Modified by Jelte Jansen to fit in ldns, and not clash with any</span> <a name="l00009"></a>00009 <span class="comment"> * system-defined SHA code.</span> <a name="l00010"></a>00010 <span class="comment"> * Changes:</span> <a name="l00011"></a>00011 <span class="comment"> * - Renamed (external) functions and constants to fit ldns style</span> <a name="l00012"></a>00012 <span class="comment"> * - Removed _End and _Data functions</span> <a name="l00013"></a>00013 <span class="comment"> * - Added ldns_shaX(data, len, digest) convenience functions</span> <a name="l00014"></a>00014 <span class="comment"> * - Removed prototypes of _Transform functions and made those static</span> <a name="l00015"></a>00015 <span class="comment"> * </span> <a name="l00016"></a>00016 <span class="comment"> * Redistribution and use in source and binary forms, with or without</span> <a name="l00017"></a>00017 <span class="comment"> * modification, are permitted provided that the following conditions</span> <a name="l00018"></a>00018 <span class="comment"> * are met:</span> <a name="l00019"></a>00019 <span class="comment"> * 1. Redistributions of source code must retain the above copyright</span> <a name="l00020"></a>00020 <span class="comment"> * notice, this list of conditions and the following disclaimer.</span> <a name="l00021"></a>00021 <span class="comment"> * 2. Redistributions in binary form must reproduce the above copyright</span> <a name="l00022"></a>00022 <span class="comment"> * notice, this list of conditions and the following disclaimer in the</span> <a name="l00023"></a>00023 <span class="comment"> * documentation and/or other materials provided with the distribution.</span> <a name="l00024"></a>00024 <span class="comment"> * 3. Neither the name of the copyright holder nor the names of contributors</span> <a name="l00025"></a>00025 <span class="comment"> * may be used to endorse or promote products derived from this software</span> <a name="l00026"></a>00026 <span class="comment"> * without specific prior written permission.</span> <a name="l00027"></a>00027 <span class="comment"> * </span> <a name="l00028"></a>00028 <span class="comment"> * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND</span> <a name="l00029"></a>00029 <span class="comment"> * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE</span> <a name="l00030"></a>00030 <span class="comment"> * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE</span> <a name="l00031"></a>00031 <span class="comment"> * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE</span> <a name="l00032"></a>00032 <span class="comment"> * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL</span> <a name="l00033"></a>00033 <span class="comment"> * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS</span> <a name="l00034"></a>00034 <span class="comment"> * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)</span> <a name="l00035"></a>00035 <span class="comment"> * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT</span> <a name="l00036"></a>00036 <span class="comment"> * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY</span> <a name="l00037"></a>00037 <span class="comment"> * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF</span> <a name="l00038"></a>00038 <span class="comment"> * SUCH DAMAGE.</span> <a name="l00039"></a>00039 <span class="comment"> *</span> <a name="l00040"></a>00040 <span class="comment"> * $Id: sha2.c,v 1.1 2001/11/08 00:01:51 adg Exp adg $</span> <a name="l00041"></a>00041 <span class="comment"> */</span> <a name="l00042"></a>00042 <a name="l00043"></a>00043 <span class="preprocessor">#include <<a class="code" href="ldns_2config_8h.html">ldns/config.h</a>></span> <a name="l00044"></a>00044 <span class="preprocessor">#include <string.h></span> <span class="comment">/* memcpy()/memset() or bcopy()/bzero() */</span> <a name="l00045"></a>00045 <span class="preprocessor">#include <assert.h></span> <span class="comment">/* assert() */</span> <a name="l00046"></a>00046 <span class="preprocessor">#include <<a class="code" href="sha2_8h.html">ldns/sha2.h</a>></span> <a name="l00047"></a>00047 <a name="l00048"></a>00048 <span class="comment">/*</span> <a name="l00049"></a>00049 <span class="comment"> * ASSERT NOTE:</span> <a name="l00050"></a>00050 <span class="comment"> * Some sanity checking code is included using assert(). On my FreeBSD</span> <a name="l00051"></a>00051 <span class="comment"> * system, this additional code can be removed by compiling with NDEBUG</span> <a name="l00052"></a>00052 <span class="comment"> * defined. Check your own systems manpage on assert() to see how to</span> <a name="l00053"></a>00053 <span class="comment"> * compile WITHOUT the sanity checking code on your system.</span> <a name="l00054"></a>00054 <span class="comment"> *</span> <a name="l00055"></a>00055 <span class="comment"> * UNROLLED TRANSFORM LOOP NOTE:</span> <a name="l00056"></a>00056 <span class="comment"> * You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform</span> <a name="l00057"></a>00057 <span class="comment"> * loop version for the hash transform rounds (defined using macros</span> <a name="l00058"></a>00058 <span class="comment"> * later in this file). Either define on the command line, for example:</span> <a name="l00059"></a>00059 <span class="comment"> *</span> <a name="l00060"></a>00060 <span class="comment"> * cc -DSHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c</span> <a name="l00061"></a>00061 <span class="comment"> *</span> <a name="l00062"></a>00062 <span class="comment"> * or define below:</span> <a name="l00063"></a>00063 <span class="comment"> *</span> <a name="l00064"></a>00064 <span class="comment"> * #define SHA2_UNROLL_TRANSFORM</span> <a name="l00065"></a>00065 <span class="comment"> *</span> <a name="l00066"></a>00066 <span class="comment"> */</span> <a name="l00067"></a>00067 <a name="l00068"></a>00068 <a name="l00069"></a>00069 <span class="comment">/*** SHA-256/384/512 Machine Architecture Definitions *****************/</span> <a name="l00070"></a>00070 <span class="comment">/*</span> <a name="l00071"></a>00071 <span class="comment"> * BYTE_ORDER NOTE:</span> <a name="l00072"></a>00072 <span class="comment"> *</span> <a name="l00073"></a>00073 <span class="comment"> * Please make sure that your system defines BYTE_ORDER. If your</span> <a name="l00074"></a>00074 <span class="comment"> * architecture is little-endian, make sure it also defines</span> <a name="l00075"></a>00075 <span class="comment"> * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are</span> <a name="l00076"></a>00076 <span class="comment"> * equivilent.</span> <a name="l00077"></a>00077 <span class="comment"> *</span> <a name="l00078"></a>00078 <span class="comment"> * If your system does not define the above, then you can do so by</span> <a name="l00079"></a>00079 <span class="comment"> * hand like this:</span> <a name="l00080"></a>00080 <span class="comment"> *</span> <a name="l00081"></a>00081 <span class="comment"> * #define LITTLE_ENDIAN 1234</span> <a name="l00082"></a>00082 <span class="comment"> * #define BIG_ENDIAN 4321</span> <a name="l00083"></a>00083 <span class="comment"> *</span> <a name="l00084"></a>00084 <span class="comment"> * And for little-endian machines, add:</span> <a name="l00085"></a>00085 <span class="comment"> *</span> <a name="l00086"></a>00086 <span class="comment"> * #define BYTE_ORDER LITTLE_ENDIAN </span> <a name="l00087"></a>00087 <span class="comment"> *</span> <a name="l00088"></a>00088 <span class="comment"> * Or for big-endian machines:</span> <a name="l00089"></a>00089 <span class="comment"> *</span> <a name="l00090"></a>00090 <span class="comment"> * #define BYTE_ORDER BIG_ENDIAN</span> <a name="l00091"></a>00091 <span class="comment"> *</span> <a name="l00092"></a>00092 <span class="comment"> * The FreeBSD machine this was written on defines BYTE_ORDER</span> <a name="l00093"></a>00093 <span class="comment"> * appropriately by including <sys/types.h> (which in turn includes</span> <a name="l00094"></a>00094 <span class="comment"> * <machine/endian.h> where the appropriate definitions are actually</span> <a name="l00095"></a>00095 <span class="comment"> * made).</span> <a name="l00096"></a>00096 <span class="comment"> */</span> <a name="l00097"></a>00097 <span class="preprocessor">#if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)</span> <a name="l00098"></a>00098 <span class="preprocessor"></span><span class="preprocessor">#error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN</span> <a name="l00099"></a>00099 <span class="preprocessor"></span><span class="preprocessor">#endif</span> <a name="l00100"></a>00100 <span class="preprocessor"></span> <a name="l00101"></a><a class="code" href="sha2_8c.html#a078da424bb2de5634b5e52fff5a73815">00101</a> <span class="keyword">typedef</span> uint8_t <a class="code" href="sha2_8c.html#a078da424bb2de5634b5e52fff5a73815">sha2_byte</a>; <span class="comment">/* Exactly 1 byte */</span> <a name="l00102"></a><a class="code" href="sha2_8c.html#a0f9949252a32367cfad50583447e3f5f">00102</a> <span class="keyword">typedef</span> uint32_t <a class="code" href="sha2_8c.html#a0f9949252a32367cfad50583447e3f5f">sha2_word32</a>; <span class="comment">/* Exactly 4 bytes */</span> <a name="l00103"></a>00103 <span class="preprocessor">#ifdef S_SPLINT_S</span> <a name="l00104"></a>00104 <span class="preprocessor"></span><span class="keyword">typedef</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> <span class="keywordtype">long</span> <a class="code" href="sha2_8c.html#aa546cbc3e1ac5d6426230fbf71e7caa9">sha2_word64</a>; <span class="comment">/* lint 8 bytes */</span> <a name="l00105"></a>00105 <span class="preprocessor">#else</span> <a name="l00106"></a><a class="code" href="sha2_8c.html#aa546cbc3e1ac5d6426230fbf71e7caa9">00106</a> <span class="preprocessor"></span><span class="keyword">typedef</span> uint64_t <a class="code" href="sha2_8c.html#aa546cbc3e1ac5d6426230fbf71e7caa9">sha2_word64</a>; <span class="comment">/* Exactly 8 bytes */</span> <a name="l00107"></a>00107 <span class="preprocessor">#endif</span> <a name="l00108"></a>00108 <span class="preprocessor"></span> <a name="l00109"></a>00109 <span class="comment">/*** SHA-256/384/512 Various Length Definitions ***********************/</span> <a name="l00110"></a>00110 <span class="comment">/* NOTE: Most of these are in sha2.h */</span> <a name="l00111"></a><a class="code" href="sha2_8c.html#a30f0f7734f48f233ee4ea8b328342067">00111</a> <span class="preprocessor">#define ldns_sha256_SHORT_BLOCK_LENGTH (LDNS_SHA256_BLOCK_LENGTH - 8)</span> <a name="l00112"></a><a class="code" href="sha2_8c.html#a0af4aa084f4f7b4418f967568b4e1249">00112</a> <span class="preprocessor"></span><span class="preprocessor">#define ldns_sha384_SHORT_BLOCK_LENGTH (LDNS_SHA384_BLOCK_LENGTH - 16)</span> <a name="l00113"></a><a class="code" href="sha2_8c.html#a53ebe62ce8c8959c921160f16ecf83b0">00113</a> <span class="preprocessor"></span><span class="preprocessor">#define ldns_sha512_SHORT_BLOCK_LENGTH (LDNS_SHA512_BLOCK_LENGTH - 16)</span> <a name="l00114"></a>00114 <span class="preprocessor"></span> <a name="l00115"></a>00115 <a name="l00116"></a>00116 <span class="comment">/*** ENDIAN REVERSAL MACROS *******************************************/</span> <a name="l00117"></a>00117 <span class="preprocessor">#if BYTE_ORDER == LITTLE_ENDIAN</span> <a name="l00118"></a><a class="code" href="sha2_8c.html#a7164c79bb6bc80abcb61cb96eef4194e">00118</a> <span class="preprocessor"></span><span class="preprocessor">#define REVERSE32(w,x) { \</span> <a name="l00119"></a>00119 <span class="preprocessor"> sha2_word32 tmp = (w); \</span> <a name="l00120"></a>00120 <span class="preprocessor"> tmp = (tmp >> 16) | (tmp << 16); \</span> <a name="l00121"></a>00121 <span class="preprocessor"> (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \</span> <a name="l00122"></a>00122 <span class="preprocessor">}</span> <a name="l00123"></a>00123 <span class="preprocessor"></span><span class="preprocessor">#ifndef S_SPLINT_S</span> <a name="l00124"></a><a class="code" href="sha2_8c.html#a5399a7e8dab676a6c36fe85c91ae1366">00124</a> <span class="preprocessor"></span><span class="preprocessor">#define REVERSE64(w,x) { \</span> <a name="l00125"></a>00125 <span class="preprocessor"> sha2_word64 tmp = (w); \</span> <a name="l00126"></a>00126 <span class="preprocessor"> tmp = (tmp >> 32) | (tmp << 32); \</span> <a name="l00127"></a>00127 <span class="preprocessor"> tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | \</span> <a name="l00128"></a>00128 <span class="preprocessor"> ((tmp & 0x00ff00ff00ff00ffULL) << 8); \</span> <a name="l00129"></a>00129 <span class="preprocessor"> (x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | \</span> <a name="l00130"></a>00130 <span class="preprocessor"> ((tmp & 0x0000ffff0000ffffULL) << 16); \</span> <a name="l00131"></a>00131 <span class="preprocessor">}</span> <a name="l00132"></a>00132 <span class="preprocessor"></span><span class="preprocessor">#else </span><span class="comment">/* splint */</span> <a name="l00133"></a>00133 <span class="preprocessor">#define REVERSE64(w,x) </span><span class="comment">/* splint */</span> <a name="l00134"></a>00134 <span class="preprocessor">#endif </span><span class="comment">/* splint */</span> <a name="l00135"></a>00135 <span class="preprocessor">#endif </span><span class="comment">/* BYTE_ORDER == LITTLE_ENDIAN */</span> <a name="l00136"></a>00136 <a name="l00137"></a>00137 <span class="comment">/*</span> <a name="l00138"></a>00138 <span class="comment"> * Macro for incrementally adding the unsigned 64-bit integer n to the</span> <a name="l00139"></a>00139 <span class="comment"> * unsigned 128-bit integer (represented using a two-element array of</span> <a name="l00140"></a>00140 <span class="comment"> * 64-bit words):</span> <a name="l00141"></a>00141 <span class="comment"> */</span> <a name="l00142"></a><a class="code" href="sha2_8c.html#a32ce6a800376f90c7cee70ac2e7255d6">00142</a> <span class="preprocessor">#define ADDINC128(w,n) { \</span> <a name="l00143"></a>00143 <span class="preprocessor"> (w)[0] += (sha2_word64)(n); \</span> <a name="l00144"></a>00144 <span class="preprocessor"> if ((w)[0] < (n)) { \</span> <a name="l00145"></a>00145 <span class="preprocessor"> (w)[1]++; \</span> <a name="l00146"></a>00146 <span class="preprocessor"> } \</span> <a name="l00147"></a>00147 <span class="preprocessor">}</span> <a name="l00148"></a>00148 <span class="preprocessor"></span><span class="preprocessor">#ifdef S_SPLINT_S</span> <a name="l00149"></a>00149 <span class="preprocessor"></span><span class="preprocessor">#undef ADDINC128</span> <a name="l00150"></a>00150 <span class="preprocessor"></span><span class="preprocessor">#define ADDINC128(w,n) </span><span class="comment">/* splint */</span> <a name="l00151"></a>00151 <span class="preprocessor">#endif</span> <a name="l00152"></a>00152 <span class="preprocessor"></span> <a name="l00153"></a>00153 <span class="comment">/*</span> <a name="l00154"></a>00154 <span class="comment"> * Macros for copying blocks of memory and for zeroing out ranges</span> <a name="l00155"></a>00155 <span class="comment"> * of memory. Using these macros makes it easy to switch from</span> <a name="l00156"></a>00156 <span class="comment"> * using memset()/memcpy() and using bzero()/bcopy().</span> <a name="l00157"></a>00157 <span class="comment"> *</span> <a name="l00158"></a>00158 <span class="comment"> * Please define either SHA2_USE_MEMSET_MEMCPY or define</span> <a name="l00159"></a>00159 <span class="comment"> * SHA2_USE_BZERO_BCOPY depending on which function set you</span> <a name="l00160"></a>00160 <span class="comment"> * choose to use:</span> <a name="l00161"></a>00161 <span class="comment"> */</span> <a name="l00162"></a>00162 <span class="preprocessor">#if !defined(SHA2_USE_MEMSET_MEMCPY) && !defined(SHA2_USE_BZERO_BCOPY)</span> <a name="l00163"></a>00163 <span class="preprocessor"></span><span class="comment">/* Default to memset()/memcpy() if no option is specified */</span> <a name="l00164"></a><a class="code" href="sha2_8c.html#a324569b417209f3e941991d642302d51">00164</a> <span class="preprocessor">#define SHA2_USE_MEMSET_MEMCPY 1</span> <a name="l00165"></a>00165 <span class="preprocessor"></span><span class="preprocessor">#endif</span> <a name="l00166"></a>00166 <span class="preprocessor"></span><span class="preprocessor">#if defined(SHA2_USE_MEMSET_MEMCPY) && defined(SHA2_USE_BZERO_BCOPY)</span> <a name="l00167"></a>00167 <span class="preprocessor"></span><span class="comment">/* Abort with an error if BOTH options are defined */</span> <a name="l00168"></a>00168 <span class="preprocessor">#error Define either SHA2_USE_MEMSET_MEMCPY or SHA2_USE_BZERO_BCOPY, not both!</span> <a name="l00169"></a>00169 <span class="preprocessor"></span><span class="preprocessor">#endif</span> <a name="l00170"></a>00170 <span class="preprocessor"></span> <a name="l00171"></a>00171 <span class="preprocessor">#ifdef SHA2_USE_MEMSET_MEMCPY</span> <a name="l00172"></a><a class="code" href="sha2_8c.html#a8e212a8c6f84340a49f10895833e2f2d">00172</a> <span class="preprocessor"></span><span class="preprocessor">#define MEMSET_BZERO(p,l) memset((p), 0, (l))</span> <a name="l00173"></a><a class="code" href="sha2_8c.html#aa357cebb0a4ac20de95565894de5f793">00173</a> <span class="preprocessor"></span><span class="preprocessor">#define MEMCPY_BCOPY(d,s,l) memcpy((d), (s), (l))</span> <a name="l00174"></a>00174 <span class="preprocessor"></span><span class="preprocessor">#endif</span> <a name="l00175"></a>00175 <span class="preprocessor"></span><span class="preprocessor">#ifdef SHA2_USE_BZERO_BCOPY</span> <a name="l00176"></a>00176 <span class="preprocessor"></span><span class="preprocessor">#define MEMSET_BZERO(p,l) bzero((p), (l))</span> <a name="l00177"></a>00177 <span class="preprocessor"></span><span class="preprocessor">#define MEMCPY_BCOPY(d,s,l) bcopy((s), (d), (l))</span> <a name="l00178"></a>00178 <span class="preprocessor"></span><span class="preprocessor">#endif</span> <a name="l00179"></a>00179 <span class="preprocessor"></span> <a name="l00180"></a>00180 <a name="l00181"></a>00181 <span class="comment">/*** THE SIX LOGICAL FUNCTIONS ****************************************/</span> <a name="l00182"></a>00182 <span class="comment">/*</span> <a name="l00183"></a>00183 <span class="comment"> * Bit shifting and rotation (used by the six SHA-XYZ logical functions:</span> <a name="l00184"></a>00184 <span class="comment"> *</span> <a name="l00185"></a>00185 <span class="comment"> * NOTE: The naming of R and S appears backwards here (R is a SHIFT and</span> <a name="l00186"></a>00186 <span class="comment"> * S is a ROTATION) because the SHA-256/384/512 description document</span> <a name="l00187"></a>00187 <span class="comment"> * (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this</span> <a name="l00188"></a>00188 <span class="comment"> * same "backwards" definition.</span> <a name="l00189"></a>00189 <span class="comment"> */</span> <a name="l00190"></a>00190 <span class="comment">/* Shift-right (used in SHA-256, SHA-384, and SHA-512): */</span> <a name="l00191"></a><a class="code" href="sha2_8c.html#af299e0982ce71aad5027c4a7315e536f">00191</a> <span class="preprocessor">#define R(b,x) ((x) >> (b))</span> <a name="l00192"></a>00192 <span class="preprocessor"></span><span class="comment">/* 32-bit Rotate-right (used in SHA-256): */</span> <a name="l00193"></a><a class="code" href="sha2_8c.html#afe83c8790798dddd30665496143a853b">00193</a> <span class="preprocessor">#define S32(b,x) (((x) >> (b)) | ((x) << (32 - (b))))</span> <a name="l00194"></a>00194 <span class="preprocessor"></span><span class="comment">/* 64-bit Rotate-right (used in SHA-384 and SHA-512): */</span> <a name="l00195"></a><a class="code" href="sha2_8c.html#a04c57d7554181e15fe6063a477d28236">00195</a> <span class="preprocessor">#define S64(b,x) (((x) >> (b)) | ((x) << (64 - (b))))</span> <a name="l00196"></a>00196 <span class="preprocessor"></span> <a name="l00197"></a>00197 <span class="comment">/* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */</span> <a name="l00198"></a><a class="code" href="sha2_8c.html#ab0a0d1ad13c7e4d3cb38c89f7127c85a">00198</a> <span class="preprocessor">#define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))</span> <a name="l00199"></a><a class="code" href="sha2_8c.html#a447cd1d7fb703b62666400976be24bf3">00199</a> <span class="preprocessor"></span><span class="preprocessor">#define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))</span> <a name="l00200"></a>00200 <span class="preprocessor"></span> <a name="l00201"></a>00201 <span class="comment">/* Four of six logical functions used in SHA-256: */</span> <a name="l00202"></a><a class="code" href="sha2_8c.html#a1d3b933bf48a1c763775d358313f44a6">00202</a> <span class="preprocessor">#define Sigma0_256(x) (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x)))</span> <a name="l00203"></a><a class="code" href="sha2_8c.html#a98f25dda8f234733acf0386d6d5187fd">00203</a> <span class="preprocessor"></span><span class="preprocessor">#define Sigma1_256(x) (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x)))</span> <a name="l00204"></a><a class="code" href="sha2_8c.html#afdc787683d72110451742acd5bf41d16">00204</a> <span class="preprocessor"></span><span class="preprocessor">#define sigma0_256(x) (S32(7, (x)) ^ S32(18, (x)) ^ R(3 , (x)))</span> <a name="l00205"></a><a class="code" href="sha2_8c.html#a6daf5f3524cf68e6a2e19de772070e77">00205</a> <span class="preprocessor"></span><span class="preprocessor">#define sigma1_256(x) (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x)))</span> <a name="l00206"></a>00206 <span class="preprocessor"></span> <a name="l00207"></a>00207 <span class="comment">/* Four of six logical functions used in SHA-384 and SHA-512: */</span> <a name="l00208"></a><a class="code" href="sha2_8c.html#a07b4672fd77b0b5f2bf0041e2faf0b45">00208</a> <span class="preprocessor">#define Sigma0_512(x) (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))</span> <a name="l00209"></a><a class="code" href="sha2_8c.html#a6a0406d09af6eaa6d933070801f74002">00209</a> <span class="preprocessor"></span><span class="preprocessor">#define Sigma1_512(x) (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))</span> <a name="l00210"></a><a class="code" href="sha2_8c.html#a449da15ce26bdf3bf58925e17c7864b3">00210</a> <span class="preprocessor"></span><span class="preprocessor">#define sigma0_512(x) (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7, (x)))</span> <a name="l00211"></a><a class="code" href="sha2_8c.html#adcf0e492a43c6f34a3a5023a0a448e2d">00211</a> <span class="preprocessor"></span><span class="preprocessor">#define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ R( 6, (x)))</span> <a name="l00212"></a>00212 <span class="preprocessor"></span> <a name="l00213"></a>00213 <span class="comment">/*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/</span> <a name="l00214"></a>00214 <span class="comment">/* Hash constant words K for SHA-256: */</span> <a name="l00215"></a>00215 <span class="keyword">static</span> <span class="keyword">const</span> <a class="code" href="sha2_8c.html#a0f9949252a32367cfad50583447e3f5f">sha2_word32</a> K256[64] = { <a name="l00216"></a>00216 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, <a name="l00217"></a>00217 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, <a name="l00218"></a>00218 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL, <a name="l00219"></a>00219 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL, <a name="l00220"></a>00220 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL, <a name="l00221"></a>00221 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, <a name="l00222"></a>00222 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, <a name="l00223"></a>00223 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL, <a name="l00224"></a>00224 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL, <a name="l00225"></a>00225 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL, <a name="l00226"></a>00226 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, <a name="l00227"></a>00227 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, <a name="l00228"></a>00228 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL, <a name="l00229"></a>00229 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL, <a name="l00230"></a>00230 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL, <a name="l00231"></a>00231 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL <a name="l00232"></a>00232 }; <a name="l00233"></a>00233 <a name="l00234"></a>00234 <span class="comment">/* initial hash value H for SHA-256: */</span> <a name="l00235"></a>00235 <span class="keyword">static</span> <span class="keyword">const</span> <a class="code" href="sha2_8c.html#a0f9949252a32367cfad50583447e3f5f">sha2_word32</a> ldns_sha256_initial_hash_value[8] = { <a name="l00236"></a>00236 0x6a09e667UL, <a name="l00237"></a>00237 0xbb67ae85UL, <a name="l00238"></a>00238 0x3c6ef372UL, <a name="l00239"></a>00239 0xa54ff53aUL, <a name="l00240"></a>00240 0x510e527fUL, <a name="l00241"></a>00241 0x9b05688cUL, <a name="l00242"></a>00242 0x1f83d9abUL, <a name="l00243"></a>00243 0x5be0cd19UL <a name="l00244"></a>00244 }; <a name="l00245"></a>00245 <a name="l00246"></a>00246 <span class="comment">/* Hash constant words K for SHA-384 and SHA-512: */</span> <a name="l00247"></a>00247 <span class="keyword">static</span> <span class="keyword">const</span> <a class="code" href="sha2_8c.html#aa546cbc3e1ac5d6426230fbf71e7caa9">sha2_word64</a> K512[80] = { <a name="l00248"></a>00248 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, <a name="l00249"></a>00249 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, <a name="l00250"></a>00250 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, <a name="l00251"></a>00251 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, <a name="l00252"></a>00252 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, <a name="l00253"></a>00253 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, <a name="l00254"></a>00254 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, <a name="l00255"></a>00255 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL, <a name="l00256"></a>00256 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, <a name="l00257"></a>00257 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, <a name="l00258"></a>00258 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, <a name="l00259"></a>00259 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, <a name="l00260"></a>00260 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, <a name="l00261"></a>00261 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, <a name="l00262"></a>00262 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, <a name="l00263"></a>00263 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, <a name="l00264"></a>00264 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, <a name="l00265"></a>00265 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, <a name="l00266"></a>00266 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, <a name="l00267"></a>00267 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL, <a name="l00268"></a>00268 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, <a name="l00269"></a>00269 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, <a name="l00270"></a>00270 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, <a name="l00271"></a>00271 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, <a name="l00272"></a>00272 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, <a name="l00273"></a>00273 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, <a name="l00274"></a>00274 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, <a name="l00275"></a>00275 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, <a name="l00276"></a>00276 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, <a name="l00277"></a>00277 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, <a name="l00278"></a>00278 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, <a name="l00279"></a>00279 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL, <a name="l00280"></a>00280 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, <a name="l00281"></a>00281 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, <a name="l00282"></a>00282 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, <a name="l00283"></a>00283 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, <a name="l00284"></a>00284 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, <a name="l00285"></a>00285 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL, <a name="l00286"></a>00286 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, <a name="l00287"></a>00287 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL <a name="l00288"></a>00288 }; <a name="l00289"></a>00289 <a name="l00290"></a>00290 <span class="comment">/* initial hash value H for SHA-384 */</span> <a name="l00291"></a>00291 <span class="keyword">static</span> <span class="keyword">const</span> <a class="code" href="sha2_8c.html#aa546cbc3e1ac5d6426230fbf71e7caa9">sha2_word64</a> sha384_initial_hash_value[8] = { <a name="l00292"></a>00292 0xcbbb9d5dc1059ed8ULL, <a name="l00293"></a>00293 0x629a292a367cd507ULL, <a name="l00294"></a>00294 0x9159015a3070dd17ULL, <a name="l00295"></a>00295 0x152fecd8f70e5939ULL, <a name="l00296"></a>00296 0x67332667ffc00b31ULL, <a name="l00297"></a>00297 0x8eb44a8768581511ULL, <a name="l00298"></a>00298 0xdb0c2e0d64f98fa7ULL, <a name="l00299"></a>00299 0x47b5481dbefa4fa4ULL <a name="l00300"></a>00300 }; <a name="l00301"></a>00301 <a name="l00302"></a>00302 <span class="comment">/* initial hash value H for SHA-512 */</span> <a name="l00303"></a>00303 <span class="keyword">static</span> <span class="keyword">const</span> <a class="code" href="sha2_8c.html#aa546cbc3e1ac5d6426230fbf71e7caa9">sha2_word64</a> sha512_initial_hash_value[8] = { <a name="l00304"></a>00304 0x6a09e667f3bcc908ULL, <a name="l00305"></a>00305 0xbb67ae8584caa73bULL, <a name="l00306"></a>00306 0x3c6ef372fe94f82bULL, <a name="l00307"></a>00307 0xa54ff53a5f1d36f1ULL, <a name="l00308"></a>00308 0x510e527fade682d1ULL, <a name="l00309"></a>00309 0x9b05688c2b3e6c1fULL, <a name="l00310"></a>00310 0x1f83d9abfb41bd6bULL, <a name="l00311"></a>00311 0x5be0cd19137e2179ULL <a name="l00312"></a>00312 }; <a name="l00313"></a>00313 <a name="l00314"></a>00314 <span class="comment">/*** SHA-256: *********************************************************/</span> <a name="l00315"></a><a class="code" href="sha2_8h.html#ae60ce644de9c673168a871544c20f387">00315</a> <span class="keywordtype">void</span> <a class="code" href="sha2_8c.html#a7247a2faf1bb3e33b9535ebe24d5a78f">ldns_sha256_init</a>(<a class="code" href="struct__ldns__sha256__CTX.html">ldns_sha256_CTX</a>* context) { <a name="l00316"></a>00316 <span class="keywordflow">if</span> (context == (<a class="code" href="struct__ldns__sha256__CTX.html">ldns_sha256_CTX</a>*)0) { <a name="l00317"></a>00317 <span class="keywordflow">return</span>; <a name="l00318"></a>00318 } <a name="l00319"></a>00319 <a class="code" href="sha2_8c.html#aa357cebb0a4ac20de95565894de5f793">MEMCPY_BCOPY</a>(context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>, ldns_sha256_initial_hash_value, <a class="code" href="sha2_8h.html#ac16c6fa6913b570cefc43082b682544a">LDNS_SHA256_DIGEST_LENGTH</a>); <a name="l00320"></a>00320 <a class="code" href="sha2_8c.html#a8e212a8c6f84340a49f10895833e2f2d">MEMSET_BZERO</a>(context-><a class="code" href="struct__ldns__sha256__CTX.html#a80e25345466a6904985c686a2fa35583">buffer</a>, <a class="code" href="sha2_8h.html#a7c6404b9f43eb361e69034412cf42de8">LDNS_SHA256_BLOCK_LENGTH</a>); <a name="l00321"></a>00321 context-><a class="code" href="struct__ldns__sha256__CTX.html#a4206a624bd4cf9e8e99627399fd1d0b5">bitcount</a> = 0; <a name="l00322"></a>00322 } <a name="l00323"></a>00323 <a name="l00324"></a>00324 <span class="preprocessor">#ifdef SHA2_UNROLL_TRANSFORM</span> <a name="l00325"></a>00325 <span class="preprocessor"></span> <a name="l00326"></a>00326 <span class="comment">/* Unrolled SHA-256 round macros: */</span> <a name="l00327"></a>00327 <a name="l00328"></a>00328 <span class="preprocessor">#if BYTE_ORDER == LITTLE_ENDIAN</span> <a name="l00329"></a>00329 <span class="preprocessor"></span> <a name="l00330"></a>00330 <span class="preprocessor">#define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \</span> <a name="l00331"></a>00331 <span class="preprocessor"> REVERSE32(*data++, W256[j]); \</span> <a name="l00332"></a>00332 <span class="preprocessor"> T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \</span> <a name="l00333"></a>00333 <span class="preprocessor"> K256[j] + W256[j]; \</span> <a name="l00334"></a>00334 <span class="preprocessor"> (d) += T1; \</span> <a name="l00335"></a>00335 <span class="preprocessor"> (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \</span> <a name="l00336"></a>00336 <span class="preprocessor"> j++</span> <a name="l00337"></a>00337 <span class="preprocessor"></span> <a name="l00338"></a>00338 <a name="l00339"></a>00339 <span class="preprocessor">#else </span><span class="comment">/* BYTE_ORDER == LITTLE_ENDIAN */</span> <a name="l00340"></a>00340 <a name="l00341"></a>00341 <span class="preprocessor">#define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \</span> <a name="l00342"></a>00342 <span class="preprocessor"> T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \</span> <a name="l00343"></a>00343 <span class="preprocessor"> K256[j] + (W256[j] = *data++); \</span> <a name="l00344"></a>00344 <span class="preprocessor"> (d) += T1; \</span> <a name="l00345"></a>00345 <span class="preprocessor"> (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \</span> <a name="l00346"></a>00346 <span class="preprocessor"> j++</span> <a name="l00347"></a>00347 <span class="preprocessor"></span> <a name="l00348"></a>00348 <span class="preprocessor">#endif </span><span class="comment">/* BYTE_ORDER == LITTLE_ENDIAN */</span> <a name="l00349"></a>00349 <a name="l00350"></a>00350 <span class="preprocessor">#define ROUND256(a,b,c,d,e,f,g,h) \</span> <a name="l00351"></a>00351 <span class="preprocessor"> s0 = W256[(j+1)&0x0f]; \</span> <a name="l00352"></a>00352 <span class="preprocessor"> s0 = sigma0_256(s0); \</span> <a name="l00353"></a>00353 <span class="preprocessor"> s1 = W256[(j+14)&0x0f]; \</span> <a name="l00354"></a>00354 <span class="preprocessor"> s1 = sigma1_256(s1); \</span> <a name="l00355"></a>00355 <span class="preprocessor"> T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + \</span> <a name="l00356"></a>00356 <span class="preprocessor"> (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \</span> <a name="l00357"></a>00357 <span class="preprocessor"> (d) += T1; \</span> <a name="l00358"></a>00358 <span class="preprocessor"> (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \</span> <a name="l00359"></a>00359 <span class="preprocessor"> j++</span> <a name="l00360"></a>00360 <span class="preprocessor"></span> <a name="l00361"></a>00361 <span class="keyword">static</span> <span class="keywordtype">void</span> ldns_sha256_Transform(<a class="code" href="struct__ldns__sha256__CTX.html">ldns_sha256_CTX</a>* context, <a name="l00362"></a>00362 <span class="keyword">const</span> <a class="code" href="sha2_8c.html#a0f9949252a32367cfad50583447e3f5f">sha2_word32</a>* data) { <a name="l00363"></a>00363 <a class="code" href="sha2_8c.html#a0f9949252a32367cfad50583447e3f5f">sha2_word32</a> a, b, c, d, e, f, g, h, s0, s1; <a name="l00364"></a>00364 <a class="code" href="sha2_8c.html#a0f9949252a32367cfad50583447e3f5f">sha2_word32</a> T1, *W256; <a name="l00365"></a>00365 <span class="keywordtype">int</span> j; <a name="l00366"></a>00366 <a name="l00367"></a>00367 W256 = (<a class="code" href="sha2_8c.html#a0f9949252a32367cfad50583447e3f5f">sha2_word32</a>*)context-><a class="code" href="struct__ldns__sha256__CTX.html#a80e25345466a6904985c686a2fa35583">buffer</a>; <a name="l00368"></a>00368 <a name="l00369"></a>00369 <span class="comment">/* initialize registers with the prev. intermediate value */</span> <a name="l00370"></a>00370 a = context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[0]; <a name="l00371"></a>00371 b = context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[1]; <a name="l00372"></a>00372 c = context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[2]; <a name="l00373"></a>00373 d = context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[3]; <a name="l00374"></a>00374 e = context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[4]; <a name="l00375"></a>00375 f = context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[5]; <a name="l00376"></a>00376 g = context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[6]; <a name="l00377"></a>00377 h = context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[7]; <a name="l00378"></a>00378 <a name="l00379"></a>00379 j = 0; <a name="l00380"></a>00380 <span class="keywordflow">do</span> { <a name="l00381"></a>00381 <span class="comment">/* Rounds 0 to 15 (unrolled): */</span> <a name="l00382"></a>00382 ROUND256_0_TO_15(a,b,c,d,e,f,g,h); <a name="l00383"></a>00383 ROUND256_0_TO_15(h,a,b,c,d,e,f,g); <a name="l00384"></a>00384 ROUND256_0_TO_15(g,h,a,b,c,d,e,f); <a name="l00385"></a>00385 ROUND256_0_TO_15(f,g,h,a,b,c,d,e); <a name="l00386"></a>00386 ROUND256_0_TO_15(e,f,g,h,a,b,c,d); <a name="l00387"></a>00387 ROUND256_0_TO_15(d,e,f,g,h,a,b,c); <a name="l00388"></a>00388 ROUND256_0_TO_15(c,d,e,f,g,h,a,b); <a name="l00389"></a>00389 ROUND256_0_TO_15(b,c,d,e,f,g,h,a); <a name="l00390"></a>00390 } <span class="keywordflow">while</span> (j < 16); <a name="l00391"></a>00391 <a name="l00392"></a>00392 <span class="comment">/* Now for the remaining rounds to 64: */</span> <a name="l00393"></a>00393 <span class="keywordflow">do</span> { <a name="l00394"></a>00394 ROUND256(a,b,c,d,e,f,g,h); <a name="l00395"></a>00395 ROUND256(h,a,b,c,d,e,f,g); <a name="l00396"></a>00396 ROUND256(g,h,a,b,c,d,e,f); <a name="l00397"></a>00397 ROUND256(f,g,h,a,b,c,d,e); <a name="l00398"></a>00398 ROUND256(e,f,g,h,a,b,c,d); <a name="l00399"></a>00399 ROUND256(d,e,f,g,h,a,b,c); <a name="l00400"></a>00400 ROUND256(c,d,e,f,g,h,a,b); <a name="l00401"></a>00401 ROUND256(b,c,d,e,f,g,h,a); <a name="l00402"></a>00402 } <span class="keywordflow">while</span> (j < 64); <a name="l00403"></a>00403 <a name="l00404"></a>00404 <span class="comment">/* Compute the current intermediate hash value */</span> <a name="l00405"></a>00405 context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[0] += a; <a name="l00406"></a>00406 context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[1] += b; <a name="l00407"></a>00407 context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[2] += c; <a name="l00408"></a>00408 context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[3] += d; <a name="l00409"></a>00409 context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[4] += e; <a name="l00410"></a>00410 context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[5] += f; <a name="l00411"></a>00411 context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[6] += g; <a name="l00412"></a>00412 context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[7] += h; <a name="l00413"></a>00413 <a name="l00414"></a>00414 <span class="comment">/* Clean up */</span> <a name="l00415"></a>00415 a = b = c = d = e = f = g = h = T1 = 0; <a name="l00416"></a>00416 } <a name="l00417"></a>00417 <a name="l00418"></a>00418 <span class="preprocessor">#else </span><span class="comment">/* SHA2_UNROLL_TRANSFORM */</span> <a name="l00419"></a>00419 <a name="l00420"></a>00420 <span class="keyword">static</span> <span class="keywordtype">void</span> ldns_sha256_Transform(<a class="code" href="struct__ldns__sha256__CTX.html">ldns_sha256_CTX</a>* context, <a name="l00421"></a>00421 <span class="keyword">const</span> <a class="code" href="sha2_8c.html#a0f9949252a32367cfad50583447e3f5f">sha2_word32</a>* data) { <a name="l00422"></a>00422 <a class="code" href="sha2_8c.html#a0f9949252a32367cfad50583447e3f5f">sha2_word32</a> a, b, c, d, e, f, g, h, s0, s1; <a name="l00423"></a>00423 <a class="code" href="sha2_8c.html#a0f9949252a32367cfad50583447e3f5f">sha2_word32</a> T1, T2, *W256; <a name="l00424"></a>00424 <span class="keywordtype">int</span> j; <a name="l00425"></a>00425 <a name="l00426"></a>00426 W256 = (<a class="code" href="sha2_8c.html#a0f9949252a32367cfad50583447e3f5f">sha2_word32</a>*)context-><a class="code" href="struct__ldns__sha256__CTX.html#a80e25345466a6904985c686a2fa35583">buffer</a>; <a name="l00427"></a>00427 <a name="l00428"></a>00428 <span class="comment">/* initialize registers with the prev. intermediate value */</span> <a name="l00429"></a>00429 a = context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[0]; <a name="l00430"></a>00430 b = context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[1]; <a name="l00431"></a>00431 c = context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[2]; <a name="l00432"></a>00432 d = context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[3]; <a name="l00433"></a>00433 e = context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[4]; <a name="l00434"></a>00434 f = context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[5]; <a name="l00435"></a>00435 g = context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[6]; <a name="l00436"></a>00436 h = context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[7]; <a name="l00437"></a>00437 <a name="l00438"></a>00438 j = 0; <a name="l00439"></a>00439 <span class="keywordflow">do</span> { <a name="l00440"></a>00440 #<span class="keywordflow">if</span> <a class="code" href="config_8h.html#a1771b7fb65ee640524d0052f229768c3">BYTE_ORDER</a> == <a class="code" href="config_8h.html#a8782a401fbf55261460863fc2f8df1ce">LITTLE_ENDIAN</a> <a name="l00441"></a>00441 <span class="comment">/* Copy data while converting to host byte order */</span> <a name="l00442"></a>00442 <a class="code" href="sha2_8c.html#a7164c79bb6bc80abcb61cb96eef4194e">REVERSE32</a>(*data++,W256[j]); <a name="l00443"></a>00443 <span class="comment">/* Apply the SHA-256 compression function to update a..h */</span> <a name="l00444"></a>00444 T1 = h + <a class="code" href="sha2_8c.html#a98f25dda8f234733acf0386d6d5187fd">Sigma1_256</a>(e) + <a class="code" href="sha2_8c.html#ab0a0d1ad13c7e4d3cb38c89f7127c85a">Ch</a>(e, f, g) + K256[j] + W256[j]; <a name="l00445"></a>00445 <span class="preprocessor">#else </span><span class="comment">/* BYTE_ORDER == LITTLE_ENDIAN */</span> <a name="l00446"></a>00446 <span class="comment">/* Apply the SHA-256 compression function to update a..h with copy */</span> <a name="l00447"></a>00447 T1 = h + <a class="code" href="sha2_8c.html#a98f25dda8f234733acf0386d6d5187fd">Sigma1_256</a>(e) + <a class="code" href="sha2_8c.html#ab0a0d1ad13c7e4d3cb38c89f7127c85a">Ch</a>(e, f, g) + K256[j] + (W256[j] = *data++); <a name="l00448"></a>00448 <span class="preprocessor">#endif </span><span class="comment">/* BYTE_ORDER == LITTLE_ENDIAN */</span> <a name="l00449"></a>00449 T2 = <a class="code" href="sha2_8c.html#a1d3b933bf48a1c763775d358313f44a6">Sigma0_256</a>(a) + <a class="code" href="sha2_8c.html#a447cd1d7fb703b62666400976be24bf3">Maj</a>(a, b, c); <a name="l00450"></a>00450 h = g; <a name="l00451"></a>00451 g = f; <a name="l00452"></a>00452 f = e; <a name="l00453"></a>00453 e = d + T1; <a name="l00454"></a>00454 d = c; <a name="l00455"></a>00455 c = b; <a name="l00456"></a>00456 b = a; <a name="l00457"></a>00457 a = T1 + T2; <a name="l00458"></a>00458 <a name="l00459"></a>00459 j++; <a name="l00460"></a>00460 } <span class="keywordflow">while</span> (j < 16); <a name="l00461"></a>00461 <a name="l00462"></a>00462 <span class="keywordflow">do</span> { <a name="l00463"></a>00463 <span class="comment">/* Part of the message block expansion: */</span> <a name="l00464"></a>00464 s0 = W256[(j+1)&0x0f]; <a name="l00465"></a>00465 s0 = <a class="code" href="sha2_8c.html#afdc787683d72110451742acd5bf41d16">sigma0_256</a>(s0); <a name="l00466"></a>00466 s1 = W256[(j+14)&0x0f]; <a name="l00467"></a>00467 s1 = <a class="code" href="sha2_8c.html#a6daf5f3524cf68e6a2e19de772070e77">sigma1_256</a>(s1); <a name="l00468"></a>00468 <a name="l00469"></a>00469 <span class="comment">/* Apply the SHA-256 compression function to update a..h */</span> <a name="l00470"></a>00470 T1 = h + <a class="code" href="sha2_8c.html#a98f25dda8f234733acf0386d6d5187fd">Sigma1_256</a>(e) + <a class="code" href="sha2_8c.html#ab0a0d1ad13c7e4d3cb38c89f7127c85a">Ch</a>(e, f, g) + K256[j] + <a name="l00471"></a>00471 (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); <a name="l00472"></a>00472 T2 = <a class="code" href="sha2_8c.html#a1d3b933bf48a1c763775d358313f44a6">Sigma0_256</a>(a) + <a class="code" href="sha2_8c.html#a447cd1d7fb703b62666400976be24bf3">Maj</a>(a, b, c); <a name="l00473"></a>00473 h = g; <a name="l00474"></a>00474 g = f; <a name="l00475"></a>00475 f = e; <a name="l00476"></a>00476 e = d + T1; <a name="l00477"></a>00477 d = c; <a name="l00478"></a>00478 c = b; <a name="l00479"></a>00479 b = a; <a name="l00480"></a>00480 a = T1 + T2; <a name="l00481"></a>00481 <a name="l00482"></a>00482 j++; <a name="l00483"></a>00483 } <span class="keywordflow">while</span> (j < 64); <a name="l00484"></a>00484 <a name="l00485"></a>00485 <span class="comment">/* Compute the current intermediate hash value */</span> <a name="l00486"></a>00486 context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[0] += a; <a name="l00487"></a>00487 context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[1] += b; <a name="l00488"></a>00488 context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[2] += c; <a name="l00489"></a>00489 context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[3] += d; <a name="l00490"></a>00490 context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[4] += e; <a name="l00491"></a>00491 context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[5] += f; <a name="l00492"></a>00492 context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[6] += g; <a name="l00493"></a>00493 context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[7] += h; <a name="l00494"></a>00494 <a name="l00495"></a>00495 <span class="comment">/* Clean up */</span> <a name="l00496"></a>00496 a = b = c = d = e = f = g = h = T1 = T2 = 0; <a name="l00497"></a>00497 } <a name="l00498"></a>00498 <a name="l00499"></a>00499 <span class="preprocessor">#endif </span><span class="comment">/* SHA2_UNROLL_TRANSFORM */</span> <a name="l00500"></a>00500 <a name="l00501"></a><a class="code" href="sha2_8h.html#abe2d681f7b3044aeb3e28cabd3321164">00501</a> <span class="keywordtype">void</span> <a class="code" href="sha2_8c.html#a262db1dfb48ef54e404afd5ea3d0f912">ldns_sha256_update</a>(<a class="code" href="struct__ldns__sha256__CTX.html">ldns_sha256_CTX</a>* context, <span class="keyword">const</span> <a class="code" href="sha2_8c.html#a078da424bb2de5634b5e52fff5a73815">sha2_byte</a> *data, <span class="keywordtype">size_t</span> len) { <a name="l00502"></a>00502 <span class="keywordtype">size_t</span> freespace, usedspace; <a name="l00503"></a>00503 <a name="l00504"></a>00504 <span class="keywordflow">if</span> (len == 0) { <a name="l00505"></a>00505 <span class="comment">/* Calling with no data is valid - we do nothing */</span> <a name="l00506"></a>00506 <span class="keywordflow">return</span>; <a name="l00507"></a>00507 } <a name="l00508"></a>00508 <a name="l00509"></a>00509 <span class="comment">/* Sanity check: */</span> <a name="l00510"></a>00510 assert(context != (<a class="code" href="struct__ldns__sha256__CTX.html">ldns_sha256_CTX</a>*)0 && data != (<a class="code" href="sha2_8c.html#a078da424bb2de5634b5e52fff5a73815">sha2_byte</a>*)0); <a name="l00511"></a>00511 <a name="l00512"></a>00512 usedspace = (context-><a class="code" href="struct__ldns__sha256__CTX.html#a4206a624bd4cf9e8e99627399fd1d0b5">bitcount</a> >> 3) % <a class="code" href="sha2_8h.html#a7c6404b9f43eb361e69034412cf42de8">LDNS_SHA256_BLOCK_LENGTH</a>; <a name="l00513"></a>00513 <span class="keywordflow">if</span> (usedspace > 0) { <a name="l00514"></a>00514 <span class="comment">/* Calculate how much free space is available in the buffer */</span> <a name="l00515"></a>00515 freespace = <a class="code" href="sha2_8h.html#a7c6404b9f43eb361e69034412cf42de8">LDNS_SHA256_BLOCK_LENGTH</a> - usedspace; <a name="l00516"></a>00516 <a name="l00517"></a>00517 <span class="keywordflow">if</span> (len >= freespace) { <a name="l00518"></a>00518 <span class="comment">/* Fill the buffer completely and process it */</span> <a name="l00519"></a>00519 <a class="code" href="sha2_8c.html#aa357cebb0a4ac20de95565894de5f793">MEMCPY_BCOPY</a>(&context-><a class="code" href="struct__ldns__sha256__CTX.html#a80e25345466a6904985c686a2fa35583">buffer</a>[usedspace], data, freespace); <a name="l00520"></a>00520 context-><a class="code" href="struct__ldns__sha256__CTX.html#a4206a624bd4cf9e8e99627399fd1d0b5">bitcount</a> += freespace << 3; <a name="l00521"></a>00521 len -= freespace; <a name="l00522"></a>00522 data += freespace; <a name="l00523"></a>00523 ldns_sha256_Transform(context, (<a class="code" href="sha2_8c.html#a0f9949252a32367cfad50583447e3f5f">sha2_word32</a>*)context-><a class="code" href="struct__ldns__sha256__CTX.html#a80e25345466a6904985c686a2fa35583">buffer</a>); <a name="l00524"></a>00524 } <span class="keywordflow">else</span> { <a name="l00525"></a>00525 <span class="comment">/* The buffer is not yet full */</span> <a name="l00526"></a>00526 <a class="code" href="sha2_8c.html#aa357cebb0a4ac20de95565894de5f793">MEMCPY_BCOPY</a>(&context-><a class="code" href="struct__ldns__sha256__CTX.html#a80e25345466a6904985c686a2fa35583">buffer</a>[usedspace], data, len); <a name="l00527"></a>00527 context-><a class="code" href="struct__ldns__sha256__CTX.html#a4206a624bd4cf9e8e99627399fd1d0b5">bitcount</a> += len << 3; <a name="l00528"></a>00528 <span class="comment">/* Clean up: */</span> <a name="l00529"></a>00529 usedspace = freespace = 0; <a name="l00530"></a>00530 <span class="keywordflow">return</span>; <a name="l00531"></a>00531 } <a name="l00532"></a>00532 } <a name="l00533"></a>00533 <span class="keywordflow">while</span> (len >= <a class="code" href="sha2_8h.html#a7c6404b9f43eb361e69034412cf42de8">LDNS_SHA256_BLOCK_LENGTH</a>) { <a name="l00534"></a>00534 <span class="comment">/* Process as many complete blocks as we can */</span> <a name="l00535"></a>00535 ldns_sha256_Transform(context, (<a class="code" href="sha2_8c.html#a0f9949252a32367cfad50583447e3f5f">sha2_word32</a>*)data); <a name="l00536"></a>00536 context-><a class="code" href="struct__ldns__sha256__CTX.html#a4206a624bd4cf9e8e99627399fd1d0b5">bitcount</a> += <a class="code" href="sha2_8h.html#a7c6404b9f43eb361e69034412cf42de8">LDNS_SHA256_BLOCK_LENGTH</a> << 3; <a name="l00537"></a>00537 len -= <a class="code" href="sha2_8h.html#a7c6404b9f43eb361e69034412cf42de8">LDNS_SHA256_BLOCK_LENGTH</a>; <a name="l00538"></a>00538 data += <a class="code" href="sha2_8h.html#a7c6404b9f43eb361e69034412cf42de8">LDNS_SHA256_BLOCK_LENGTH</a>; <a name="l00539"></a>00539 } <a name="l00540"></a>00540 <span class="keywordflow">if</span> (len > 0) { <a name="l00541"></a>00541 <span class="comment">/* There's left-overs, so save 'em */</span> <a name="l00542"></a>00542 <a class="code" href="sha2_8c.html#aa357cebb0a4ac20de95565894de5f793">MEMCPY_BCOPY</a>(context-><a class="code" href="struct__ldns__sha256__CTX.html#a80e25345466a6904985c686a2fa35583">buffer</a>, data, len); <a name="l00543"></a>00543 context-><a class="code" href="struct__ldns__sha256__CTX.html#a4206a624bd4cf9e8e99627399fd1d0b5">bitcount</a> += len << 3; <a name="l00544"></a>00544 } <a name="l00545"></a>00545 <span class="comment">/* Clean up: */</span> <a name="l00546"></a>00546 usedspace = freespace = 0; <a name="l00547"></a>00547 } <a name="l00548"></a>00548 <a name="l00549"></a><a class="code" href="sha2_8c.html#adb27a52671cc00f00a3eb601deb19070">00549</a> <span class="keywordtype">void</span> <a class="code" href="sha2_8c.html#adb27a52671cc00f00a3eb601deb19070">ldns_sha256_final</a>(<a class="code" href="sha2_8c.html#a078da424bb2de5634b5e52fff5a73815">sha2_byte</a> digest[], <a class="code" href="struct__ldns__sha256__CTX.html">ldns_sha256_CTX</a>* context) { <a name="l00550"></a>00550 <a class="code" href="sha2_8c.html#a0f9949252a32367cfad50583447e3f5f">sha2_word32</a> *d = (<a class="code" href="sha2_8c.html#a0f9949252a32367cfad50583447e3f5f">sha2_word32</a>*)digest; <a name="l00551"></a>00551 <span class="keywordtype">size_t</span> usedspace; <a name="l00552"></a>00552 <a name="l00553"></a>00553 <span class="comment">/* Sanity check: */</span> <a name="l00554"></a>00554 assert(context != (<a class="code" href="struct__ldns__sha256__CTX.html">ldns_sha256_CTX</a>*)0); <a name="l00555"></a>00555 <a name="l00556"></a>00556 <span class="comment">/* If no digest buffer is passed, we don't bother doing this: */</span> <a name="l00557"></a>00557 <span class="keywordflow">if</span> (digest != (<a class="code" href="sha2_8c.html#a078da424bb2de5634b5e52fff5a73815">sha2_byte</a>*)0) { <a name="l00558"></a>00558 usedspace = (context-><a class="code" href="struct__ldns__sha256__CTX.html#a4206a624bd4cf9e8e99627399fd1d0b5">bitcount</a> >> 3) % <a class="code" href="sha2_8h.html#a7c6404b9f43eb361e69034412cf42de8">LDNS_SHA256_BLOCK_LENGTH</a>; <a name="l00559"></a>00559 <span class="preprocessor">#if BYTE_ORDER == LITTLE_ENDIAN</span> <a name="l00560"></a>00560 <span class="preprocessor"></span> <span class="comment">/* Convert FROM host byte order */</span> <a name="l00561"></a>00561 <a class="code" href="sha2_8c.html#a5399a7e8dab676a6c36fe85c91ae1366">REVERSE64</a>(context-><a class="code" href="struct__ldns__sha256__CTX.html#a4206a624bd4cf9e8e99627399fd1d0b5">bitcount</a>,context-><a class="code" href="struct__ldns__sha256__CTX.html#a4206a624bd4cf9e8e99627399fd1d0b5">bitcount</a>); <a name="l00562"></a>00562 <span class="preprocessor">#endif</span> <a name="l00563"></a>00563 <span class="preprocessor"></span> <span class="keywordflow">if</span> (usedspace > 0) { <a name="l00564"></a>00564 <span class="comment">/* Begin padding with a 1 bit: */</span> <a name="l00565"></a>00565 context-><a class="code" href="struct__ldns__sha256__CTX.html#a80e25345466a6904985c686a2fa35583">buffer</a>[usedspace++] = 0x80; <a name="l00566"></a>00566 <a name="l00567"></a>00567 <span class="keywordflow">if</span> (usedspace <= <a class="code" href="sha2_8c.html#a30f0f7734f48f233ee4ea8b328342067">ldns_sha256_SHORT_BLOCK_LENGTH</a>) { <a name="l00568"></a>00568 <span class="comment">/* Set-up for the last transform: */</span> <a name="l00569"></a>00569 <a class="code" href="sha2_8c.html#a8e212a8c6f84340a49f10895833e2f2d">MEMSET_BZERO</a>(&context-><a class="code" href="struct__ldns__sha256__CTX.html#a80e25345466a6904985c686a2fa35583">buffer</a>[usedspace], <a class="code" href="sha2_8c.html#a30f0f7734f48f233ee4ea8b328342067">ldns_sha256_SHORT_BLOCK_LENGTH</a> - usedspace); <a name="l00570"></a>00570 } <span class="keywordflow">else</span> { <a name="l00571"></a>00571 <span class="keywordflow">if</span> (usedspace < <a class="code" href="sha2_8h.html#a7c6404b9f43eb361e69034412cf42de8">LDNS_SHA256_BLOCK_LENGTH</a>) { <a name="l00572"></a>00572 <a class="code" href="sha2_8c.html#a8e212a8c6f84340a49f10895833e2f2d">MEMSET_BZERO</a>(&context-><a class="code" href="struct__ldns__sha256__CTX.html#a80e25345466a6904985c686a2fa35583">buffer</a>[usedspace], <a class="code" href="sha2_8h.html#a7c6404b9f43eb361e69034412cf42de8">LDNS_SHA256_BLOCK_LENGTH</a> - usedspace); <a name="l00573"></a>00573 } <a name="l00574"></a>00574 <span class="comment">/* Do second-to-last transform: */</span> <a name="l00575"></a>00575 ldns_sha256_Transform(context, (<a class="code" href="sha2_8c.html#a0f9949252a32367cfad50583447e3f5f">sha2_word32</a>*)context-><a class="code" href="struct__ldns__sha256__CTX.html#a80e25345466a6904985c686a2fa35583">buffer</a>); <a name="l00576"></a>00576 <a name="l00577"></a>00577 <span class="comment">/* And set-up for the last transform: */</span> <a name="l00578"></a>00578 <a class="code" href="sha2_8c.html#a8e212a8c6f84340a49f10895833e2f2d">MEMSET_BZERO</a>(context-><a class="code" href="struct__ldns__sha256__CTX.html#a80e25345466a6904985c686a2fa35583">buffer</a>, <a class="code" href="sha2_8c.html#a30f0f7734f48f233ee4ea8b328342067">ldns_sha256_SHORT_BLOCK_LENGTH</a>); <a name="l00579"></a>00579 } <a name="l00580"></a>00580 } <span class="keywordflow">else</span> { <a name="l00581"></a>00581 <span class="comment">/* Set-up for the last transform: */</span> <a name="l00582"></a>00582 <a class="code" href="sha2_8c.html#a8e212a8c6f84340a49f10895833e2f2d">MEMSET_BZERO</a>(context-><a class="code" href="struct__ldns__sha256__CTX.html#a80e25345466a6904985c686a2fa35583">buffer</a>, <a class="code" href="sha2_8c.html#a30f0f7734f48f233ee4ea8b328342067">ldns_sha256_SHORT_BLOCK_LENGTH</a>); <a name="l00583"></a>00583 <a name="l00584"></a>00584 <span class="comment">/* Begin padding with a 1 bit: */</span> <a name="l00585"></a>00585 *context-><a class="code" href="struct__ldns__sha256__CTX.html#a80e25345466a6904985c686a2fa35583">buffer</a> = 0x80; <a name="l00586"></a>00586 } <a name="l00587"></a>00587 <span class="comment">/* Set the bit count: */</span> <a name="l00588"></a>00588 *(<a class="code" href="sha2_8c.html#aa546cbc3e1ac5d6426230fbf71e7caa9">sha2_word64</a>*)&context-><a class="code" href="struct__ldns__sha256__CTX.html#a80e25345466a6904985c686a2fa35583">buffer</a>[<a class="code" href="sha2_8c.html#a30f0f7734f48f233ee4ea8b328342067">ldns_sha256_SHORT_BLOCK_LENGTH</a>] = context-><a class="code" href="struct__ldns__sha256__CTX.html#a4206a624bd4cf9e8e99627399fd1d0b5">bitcount</a>; <a name="l00589"></a>00589 <a name="l00590"></a>00590 <span class="comment">/* final transform: */</span> <a name="l00591"></a>00591 ldns_sha256_Transform(context, (<a class="code" href="sha2_8c.html#a0f9949252a32367cfad50583447e3f5f">sha2_word32</a>*)context-><a class="code" href="struct__ldns__sha256__CTX.html#a80e25345466a6904985c686a2fa35583">buffer</a>); <a name="l00592"></a>00592 <a name="l00593"></a>00593 <span class="preprocessor">#if BYTE_ORDER == LITTLE_ENDIAN</span> <a name="l00594"></a>00594 <span class="preprocessor"></span> { <a name="l00595"></a>00595 <span class="comment">/* Convert TO host byte order */</span> <a name="l00596"></a>00596 <span class="keywordtype">int</span> j; <a name="l00597"></a>00597 <span class="keywordflow">for</span> (j = 0; j < 8; j++) { <a name="l00598"></a>00598 <a class="code" href="sha2_8c.html#a7164c79bb6bc80abcb61cb96eef4194e">REVERSE32</a>(context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[j],context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[j]); <a name="l00599"></a>00599 *d++ = context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>[j]; <a name="l00600"></a>00600 } <a name="l00601"></a>00601 } <a name="l00602"></a>00602 <span class="preprocessor">#else</span> <a name="l00603"></a>00603 <span class="preprocessor"></span> <a class="code" href="sha2_8c.html#aa357cebb0a4ac20de95565894de5f793">MEMCPY_BCOPY</a>(d, context-><a class="code" href="struct__ldns__sha256__CTX.html#a1dc73904c3d74ec1af34b5781f1b641d">state</a>, <a class="code" href="sha2_8h.html#ac16c6fa6913b570cefc43082b682544a">LDNS_SHA256_DIGEST_LENGTH</a>); <a name="l00604"></a>00604 <span class="preprocessor">#endif</span> <a name="l00605"></a>00605 <span class="preprocessor"></span> } <a name="l00606"></a>00606 <a name="l00607"></a>00607 <span class="comment">/* Clean up state data: */</span> <a name="l00608"></a>00608 <a class="code" href="sha2_8c.html#a8e212a8c6f84340a49f10895833e2f2d">MEMSET_BZERO</a>(context, <span class="keyword">sizeof</span>(<a class="code" href="struct__ldns__sha256__CTX.html">ldns_sha256_CTX</a>)); <a name="l00609"></a>00609 usedspace = 0; <a name="l00610"></a>00610 } <a name="l00611"></a>00611 <a name="l00612"></a>00612 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> * <a name="l00613"></a><a class="code" href="sha2_8h.html#af8f3852a5a417bc3fe786a477ec86f0f">00613</a> <a class="code" href="sha2_8c.html#af8f3852a5a417bc3fe786a477ec86f0f" title="Convenience function to digest a fixed block of data at once.">ldns_sha256</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *data, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> data_len, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *digest) <a name="l00614"></a>00614 { <a name="l00615"></a>00615 <a class="code" href="struct__ldns__sha256__CTX.html">ldns_sha256_CTX</a> ctx; <a name="l00616"></a>00616 <a class="code" href="sha2_8c.html#a7247a2faf1bb3e33b9535ebe24d5a78f">ldns_sha256_init</a>(&ctx); <a name="l00617"></a>00617 <a class="code" href="sha2_8c.html#a262db1dfb48ef54e404afd5ea3d0f912">ldns_sha256_update</a>(&ctx, data, data_len); <a name="l00618"></a>00618 <a class="code" href="sha2_8c.html#adb27a52671cc00f00a3eb601deb19070">ldns_sha256_final</a>(digest, &ctx); <a name="l00619"></a>00619 <span class="keywordflow">return</span> digest; <a name="l00620"></a>00620 } <a name="l00621"></a>00621 <a name="l00622"></a>00622 <span class="comment">/*** SHA-512: *********************************************************/</span> <a name="l00623"></a><a class="code" href="sha2_8h.html#a5b0c26644fdbd777d1c03d9231105d11">00623</a> <span class="keywordtype">void</span> <a class="code" href="sha2_8c.html#a38a7d2743cb7a0d4eedfdd835f7b6157">ldns_sha512_init</a>(<a class="code" href="struct__ldns__sha512__CTX.html">ldns_sha512_CTX</a>* context) { <a name="l00624"></a>00624 <span class="keywordflow">if</span> (context == (<a class="code" href="struct__ldns__sha512__CTX.html">ldns_sha512_CTX</a>*)0) { <a name="l00625"></a>00625 <span class="keywordflow">return</span>; <a name="l00626"></a>00626 } <a name="l00627"></a>00627 <a class="code" href="sha2_8c.html#aa357cebb0a4ac20de95565894de5f793">MEMCPY_BCOPY</a>(context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>, sha512_initial_hash_value, <a class="code" href="sha2_8h.html#a6f2bac73208f59e48a09e4d7db6e960d">LDNS_SHA512_DIGEST_LENGTH</a>); <a name="l00628"></a>00628 <a class="code" href="sha2_8c.html#a8e212a8c6f84340a49f10895833e2f2d">MEMSET_BZERO</a>(context-><a class="code" href="struct__ldns__sha512__CTX.html#a31366ad9937ae43ae63db0a468e99395">buffer</a>, <a class="code" href="sha2_8h.html#a14689e48f416ecc1938c064d4b6e1fe6">LDNS_SHA512_BLOCK_LENGTH</a>); <a name="l00629"></a>00629 context-><a class="code" href="struct__ldns__sha512__CTX.html#aff26561cccdd57baa1e7362dd35f15ae">bitcount</a>[0] = context-><a class="code" href="struct__ldns__sha512__CTX.html#aff26561cccdd57baa1e7362dd35f15ae">bitcount</a>[1] = 0; <a name="l00630"></a>00630 } <a name="l00631"></a>00631 <a name="l00632"></a>00632 <span class="preprocessor">#ifdef SHA2_UNROLL_TRANSFORM</span> <a name="l00633"></a>00633 <span class="preprocessor"></span> <a name="l00634"></a>00634 <span class="comment">/* Unrolled SHA-512 round macros: */</span> <a name="l00635"></a>00635 <span class="preprocessor">#if BYTE_ORDER == LITTLE_ENDIAN</span> <a name="l00636"></a>00636 <span class="preprocessor"></span> <a name="l00637"></a>00637 <span class="preprocessor">#define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \</span> <a name="l00638"></a>00638 <span class="preprocessor"> REVERSE64(*data++, W512[j]); \</span> <a name="l00639"></a>00639 <span class="preprocessor"> T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \</span> <a name="l00640"></a>00640 <span class="preprocessor"> K512[j] + W512[j]; \</span> <a name="l00641"></a>00641 <span class="preprocessor"> (d) += T1, \</span> <a name="l00642"></a>00642 <span class="preprocessor"> (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)), \</span> <a name="l00643"></a>00643 <span class="preprocessor"> j++</span> <a name="l00644"></a>00644 <span class="preprocessor"></span> <a name="l00645"></a>00645 <a name="l00646"></a>00646 <span class="preprocessor">#else </span><span class="comment">/* BYTE_ORDER == LITTLE_ENDIAN */</span> <a name="l00647"></a>00647 <a name="l00648"></a>00648 <span class="preprocessor">#define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \</span> <a name="l00649"></a>00649 <span class="preprocessor"> T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \</span> <a name="l00650"></a>00650 <span class="preprocessor"> K512[j] + (W512[j] = *data++); \</span> <a name="l00651"></a>00651 <span class="preprocessor"> (d) += T1; \</span> <a name="l00652"></a>00652 <span class="preprocessor"> (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \</span> <a name="l00653"></a>00653 <span class="preprocessor"> j++</span> <a name="l00654"></a>00654 <span class="preprocessor"></span> <a name="l00655"></a>00655 <span class="preprocessor">#endif </span><span class="comment">/* BYTE_ORDER == LITTLE_ENDIAN */</span> <a name="l00656"></a>00656 <a name="l00657"></a>00657 <span class="preprocessor">#define ROUND512(a,b,c,d,e,f,g,h) \</span> <a name="l00658"></a>00658 <span class="preprocessor"> s0 = W512[(j+1)&0x0f]; \</span> <a name="l00659"></a>00659 <span class="preprocessor"> s0 = sigma0_512(s0); \</span> <a name="l00660"></a>00660 <span class="preprocessor"> s1 = W512[(j+14)&0x0f]; \</span> <a name="l00661"></a>00661 <span class="preprocessor"> s1 = sigma1_512(s1); \</span> <a name="l00662"></a>00662 <span class="preprocessor"> T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + \</span> <a name="l00663"></a>00663 <span class="preprocessor"> (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \</span> <a name="l00664"></a>00664 <span class="preprocessor"> (d) += T1; \</span> <a name="l00665"></a>00665 <span class="preprocessor"> (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \</span> <a name="l00666"></a>00666 <span class="preprocessor"> j++</span> <a name="l00667"></a>00667 <span class="preprocessor"></span> <a name="l00668"></a>00668 <span class="keyword">static</span> <span class="keywordtype">void</span> ldns_sha512_Transform(<a class="code" href="struct__ldns__sha512__CTX.html">ldns_sha512_CTX</a>* context, <a name="l00669"></a>00669 <span class="keyword">const</span> <a class="code" href="sha2_8c.html#aa546cbc3e1ac5d6426230fbf71e7caa9">sha2_word64</a>* data) { <a name="l00670"></a>00670 <a class="code" href="sha2_8c.html#aa546cbc3e1ac5d6426230fbf71e7caa9">sha2_word64</a> a, b, c, d, e, f, g, h, s0, s1; <a name="l00671"></a>00671 <a class="code" href="sha2_8c.html#aa546cbc3e1ac5d6426230fbf71e7caa9">sha2_word64</a> T1, *W512 = (<a class="code" href="sha2_8c.html#aa546cbc3e1ac5d6426230fbf71e7caa9">sha2_word64</a>*)context-><a class="code" href="struct__ldns__sha512__CTX.html#a31366ad9937ae43ae63db0a468e99395">buffer</a>; <a name="l00672"></a>00672 <span class="keywordtype">int</span> j; <a name="l00673"></a>00673 <a name="l00674"></a>00674 <span class="comment">/* initialize registers with the prev. intermediate value */</span> <a name="l00675"></a>00675 a = context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[0]; <a name="l00676"></a>00676 b = context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[1]; <a name="l00677"></a>00677 c = context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[2]; <a name="l00678"></a>00678 d = context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[3]; <a name="l00679"></a>00679 e = context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[4]; <a name="l00680"></a>00680 f = context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[5]; <a name="l00681"></a>00681 g = context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[6]; <a name="l00682"></a>00682 h = context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[7]; <a name="l00683"></a>00683 <a name="l00684"></a>00684 j = 0; <a name="l00685"></a>00685 <span class="keywordflow">do</span> { <a name="l00686"></a>00686 ROUND512_0_TO_15(a,b,c,d,e,f,g,h); <a name="l00687"></a>00687 ROUND512_0_TO_15(h,a,b,c,d,e,f,g); <a name="l00688"></a>00688 ROUND512_0_TO_15(g,h,a,b,c,d,e,f); <a name="l00689"></a>00689 ROUND512_0_TO_15(f,g,h,a,b,c,d,e); <a name="l00690"></a>00690 ROUND512_0_TO_15(e,f,g,h,a,b,c,d); <a name="l00691"></a>00691 ROUND512_0_TO_15(d,e,f,g,h,a,b,c); <a name="l00692"></a>00692 ROUND512_0_TO_15(c,d,e,f,g,h,a,b); <a name="l00693"></a>00693 ROUND512_0_TO_15(b,c,d,e,f,g,h,a); <a name="l00694"></a>00694 } <span class="keywordflow">while</span> (j < 16); <a name="l00695"></a>00695 <a name="l00696"></a>00696 <span class="comment">/* Now for the remaining rounds up to 79: */</span> <a name="l00697"></a>00697 <span class="keywordflow">do</span> { <a name="l00698"></a>00698 ROUND512(a,b,c,d,e,f,g,h); <a name="l00699"></a>00699 ROUND512(h,a,b,c,d,e,f,g); <a name="l00700"></a>00700 ROUND512(g,h,a,b,c,d,e,f); <a name="l00701"></a>00701 ROUND512(f,g,h,a,b,c,d,e); <a name="l00702"></a>00702 ROUND512(e,f,g,h,a,b,c,d); <a name="l00703"></a>00703 ROUND512(d,e,f,g,h,a,b,c); <a name="l00704"></a>00704 ROUND512(c,d,e,f,g,h,a,b); <a name="l00705"></a>00705 ROUND512(b,c,d,e,f,g,h,a); <a name="l00706"></a>00706 } <span class="keywordflow">while</span> (j < 80); <a name="l00707"></a>00707 <a name="l00708"></a>00708 <span class="comment">/* Compute the current intermediate hash value */</span> <a name="l00709"></a>00709 context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[0] += a; <a name="l00710"></a>00710 context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[1] += b; <a name="l00711"></a>00711 context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[2] += c; <a name="l00712"></a>00712 context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[3] += d; <a name="l00713"></a>00713 context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[4] += e; <a name="l00714"></a>00714 context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[5] += f; <a name="l00715"></a>00715 context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[6] += g; <a name="l00716"></a>00716 context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[7] += h; <a name="l00717"></a>00717 <a name="l00718"></a>00718 <span class="comment">/* Clean up */</span> <a name="l00719"></a>00719 a = b = c = d = e = f = g = h = T1 = 0; <a name="l00720"></a>00720 } <a name="l00721"></a>00721 <a name="l00722"></a>00722 <span class="preprocessor">#else </span><span class="comment">/* SHA2_UNROLL_TRANSFORM */</span> <a name="l00723"></a>00723 <a name="l00724"></a>00724 <span class="keyword">static</span> <span class="keywordtype">void</span> ldns_sha512_Transform(<a class="code" href="struct__ldns__sha512__CTX.html">ldns_sha512_CTX</a>* context, <a name="l00725"></a>00725 <span class="keyword">const</span> <a class="code" href="sha2_8c.html#aa546cbc3e1ac5d6426230fbf71e7caa9">sha2_word64</a>* data) { <a name="l00726"></a>00726 <a class="code" href="sha2_8c.html#aa546cbc3e1ac5d6426230fbf71e7caa9">sha2_word64</a> a, b, c, d, e, f, g, h, s0, s1; <a name="l00727"></a>00727 <a class="code" href="sha2_8c.html#aa546cbc3e1ac5d6426230fbf71e7caa9">sha2_word64</a> T1, T2, *W512 = (<a class="code" href="sha2_8c.html#aa546cbc3e1ac5d6426230fbf71e7caa9">sha2_word64</a>*)context-><a class="code" href="struct__ldns__sha512__CTX.html#a31366ad9937ae43ae63db0a468e99395">buffer</a>; <a name="l00728"></a>00728 <span class="keywordtype">int</span> j; <a name="l00729"></a>00729 <a name="l00730"></a>00730 <span class="comment">/* initialize registers with the prev. intermediate value */</span> <a name="l00731"></a>00731 a = context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[0]; <a name="l00732"></a>00732 b = context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[1]; <a name="l00733"></a>00733 c = context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[2]; <a name="l00734"></a>00734 d = context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[3]; <a name="l00735"></a>00735 e = context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[4]; <a name="l00736"></a>00736 f = context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[5]; <a name="l00737"></a>00737 g = context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[6]; <a name="l00738"></a>00738 h = context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[7]; <a name="l00739"></a>00739 <a name="l00740"></a>00740 j = 0; <a name="l00741"></a>00741 <span class="keywordflow">do</span> { <a name="l00742"></a>00742 #<span class="keywordflow">if</span> <a class="code" href="config_8h.html#a1771b7fb65ee640524d0052f229768c3">BYTE_ORDER</a> == <a class="code" href="config_8h.html#a8782a401fbf55261460863fc2f8df1ce">LITTLE_ENDIAN</a> <a name="l00743"></a>00743 <span class="comment">/* Convert TO host byte order */</span> <a name="l00744"></a>00744 <a class="code" href="sha2_8c.html#a5399a7e8dab676a6c36fe85c91ae1366">REVERSE64</a>(*data++, W512[j]); <a name="l00745"></a>00745 <span class="comment">/* Apply the SHA-512 compression function to update a..h */</span> <a name="l00746"></a>00746 T1 = h + <a class="code" href="sha2_8c.html#a6a0406d09af6eaa6d933070801f74002">Sigma1_512</a>(e) + <a class="code" href="sha2_8c.html#ab0a0d1ad13c7e4d3cb38c89f7127c85a">Ch</a>(e, f, g) + K512[j] + W512[j]; <a name="l00747"></a>00747 <span class="preprocessor">#else </span><span class="comment">/* BYTE_ORDER == LITTLE_ENDIAN */</span> <a name="l00748"></a>00748 <span class="comment">/* Apply the SHA-512 compression function to update a..h with copy */</span> <a name="l00749"></a>00749 T1 = h + <a class="code" href="sha2_8c.html#a6a0406d09af6eaa6d933070801f74002">Sigma1_512</a>(e) + <a class="code" href="sha2_8c.html#ab0a0d1ad13c7e4d3cb38c89f7127c85a">Ch</a>(e, f, g) + K512[j] + (W512[j] = *data++); <a name="l00750"></a>00750 <span class="preprocessor">#endif </span><span class="comment">/* BYTE_ORDER == LITTLE_ENDIAN */</span> <a name="l00751"></a>00751 T2 = <a class="code" href="sha2_8c.html#a07b4672fd77b0b5f2bf0041e2faf0b45">Sigma0_512</a>(a) + <a class="code" href="sha2_8c.html#a447cd1d7fb703b62666400976be24bf3">Maj</a>(a, b, c); <a name="l00752"></a>00752 h = g; <a name="l00753"></a>00753 g = f; <a name="l00754"></a>00754 f = e; <a name="l00755"></a>00755 e = d + T1; <a name="l00756"></a>00756 d = c; <a name="l00757"></a>00757 c = b; <a name="l00758"></a>00758 b = a; <a name="l00759"></a>00759 a = T1 + T2; <a name="l00760"></a>00760 <a name="l00761"></a>00761 j++; <a name="l00762"></a>00762 } <span class="keywordflow">while</span> (j < 16); <a name="l00763"></a>00763 <a name="l00764"></a>00764 <span class="keywordflow">do</span> { <a name="l00765"></a>00765 <span class="comment">/* Part of the message block expansion: */</span> <a name="l00766"></a>00766 s0 = W512[(j+1)&0x0f]; <a name="l00767"></a>00767 s0 = <a class="code" href="sha2_8c.html#a449da15ce26bdf3bf58925e17c7864b3">sigma0_512</a>(s0); <a name="l00768"></a>00768 s1 = W512[(j+14)&0x0f]; <a name="l00769"></a>00769 s1 = <a class="code" href="sha2_8c.html#adcf0e492a43c6f34a3a5023a0a448e2d">sigma1_512</a>(s1); <a name="l00770"></a>00770 <a name="l00771"></a>00771 <span class="comment">/* Apply the SHA-512 compression function to update a..h */</span> <a name="l00772"></a>00772 T1 = h + <a class="code" href="sha2_8c.html#a6a0406d09af6eaa6d933070801f74002">Sigma1_512</a>(e) + <a class="code" href="sha2_8c.html#ab0a0d1ad13c7e4d3cb38c89f7127c85a">Ch</a>(e, f, g) + K512[j] + <a name="l00773"></a>00773 (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); <a name="l00774"></a>00774 T2 = <a class="code" href="sha2_8c.html#a07b4672fd77b0b5f2bf0041e2faf0b45">Sigma0_512</a>(a) + <a class="code" href="sha2_8c.html#a447cd1d7fb703b62666400976be24bf3">Maj</a>(a, b, c); <a name="l00775"></a>00775 h = g; <a name="l00776"></a>00776 g = f; <a name="l00777"></a>00777 f = e; <a name="l00778"></a>00778 e = d + T1; <a name="l00779"></a>00779 d = c; <a name="l00780"></a>00780 c = b; <a name="l00781"></a>00781 b = a; <a name="l00782"></a>00782 a = T1 + T2; <a name="l00783"></a>00783 <a name="l00784"></a>00784 j++; <a name="l00785"></a>00785 } <span class="keywordflow">while</span> (j < 80); <a name="l00786"></a>00786 <a name="l00787"></a>00787 <span class="comment">/* Compute the current intermediate hash value */</span> <a name="l00788"></a>00788 context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[0] += a; <a name="l00789"></a>00789 context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[1] += b; <a name="l00790"></a>00790 context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[2] += c; <a name="l00791"></a>00791 context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[3] += d; <a name="l00792"></a>00792 context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[4] += e; <a name="l00793"></a>00793 context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[5] += f; <a name="l00794"></a>00794 context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[6] += g; <a name="l00795"></a>00795 context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[7] += h; <a name="l00796"></a>00796 <a name="l00797"></a>00797 <span class="comment">/* Clean up */</span> <a name="l00798"></a>00798 a = b = c = d = e = f = g = h = T1 = T2 = 0; <a name="l00799"></a>00799 } <a name="l00800"></a>00800 <a name="l00801"></a>00801 <span class="preprocessor">#endif </span><span class="comment">/* SHA2_UNROLL_TRANSFORM */</span> <a name="l00802"></a>00802 <a name="l00803"></a><a class="code" href="sha2_8h.html#a126a37d38e177244ccb9c5e7834deb9e">00803</a> <span class="keywordtype">void</span> <a class="code" href="sha2_8c.html#a8d3c22ce0c1e995b63fc025ce0acd919">ldns_sha512_update</a>(<a class="code" href="struct__ldns__sha512__CTX.html">ldns_sha512_CTX</a>* context, <span class="keyword">const</span> <a class="code" href="sha2_8c.html#a078da424bb2de5634b5e52fff5a73815">sha2_byte</a> *data, <span class="keywordtype">size_t</span> len) { <a name="l00804"></a>00804 <span class="keywordtype">size_t</span> freespace, usedspace; <a name="l00805"></a>00805 <a name="l00806"></a>00806 <span class="keywordflow">if</span> (len == 0) { <a name="l00807"></a>00807 <span class="comment">/* Calling with no data is valid - we do nothing */</span> <a name="l00808"></a>00808 <span class="keywordflow">return</span>; <a name="l00809"></a>00809 } <a name="l00810"></a>00810 <a name="l00811"></a>00811 <span class="comment">/* Sanity check: */</span> <a name="l00812"></a>00812 assert(context != (<a class="code" href="struct__ldns__sha512__CTX.html">ldns_sha512_CTX</a>*)0 && data != (<a class="code" href="sha2_8c.html#a078da424bb2de5634b5e52fff5a73815">sha2_byte</a>*)0); <a name="l00813"></a>00813 <a name="l00814"></a>00814 usedspace = (context-><a class="code" href="struct__ldns__sha512__CTX.html#aff26561cccdd57baa1e7362dd35f15ae">bitcount</a>[0] >> 3) % <a class="code" href="sha2_8h.html#a14689e48f416ecc1938c064d4b6e1fe6">LDNS_SHA512_BLOCK_LENGTH</a>; <a name="l00815"></a>00815 <span class="keywordflow">if</span> (usedspace > 0) { <a name="l00816"></a>00816 <span class="comment">/* Calculate how much free space is available in the buffer */</span> <a name="l00817"></a>00817 freespace = <a class="code" href="sha2_8h.html#a14689e48f416ecc1938c064d4b6e1fe6">LDNS_SHA512_BLOCK_LENGTH</a> - usedspace; <a name="l00818"></a>00818 <a name="l00819"></a>00819 <span class="keywordflow">if</span> (len >= freespace) { <a name="l00820"></a>00820 <span class="comment">/* Fill the buffer completely and process it */</span> <a name="l00821"></a>00821 <a class="code" href="sha2_8c.html#aa357cebb0a4ac20de95565894de5f793">MEMCPY_BCOPY</a>(&context-><a class="code" href="struct__ldns__sha512__CTX.html#a31366ad9937ae43ae63db0a468e99395">buffer</a>[usedspace], data, freespace); <a name="l00822"></a>00822 <a class="code" href="sha2_8c.html#a32ce6a800376f90c7cee70ac2e7255d6">ADDINC128</a>(context-><a class="code" href="struct__ldns__sha512__CTX.html#aff26561cccdd57baa1e7362dd35f15ae">bitcount</a>, freespace << 3); <a name="l00823"></a>00823 len -= freespace; <a name="l00824"></a>00824 data += freespace; <a name="l00825"></a>00825 ldns_sha512_Transform(context, (<a class="code" href="sha2_8c.html#aa546cbc3e1ac5d6426230fbf71e7caa9">sha2_word64</a>*)context-><a class="code" href="struct__ldns__sha512__CTX.html#a31366ad9937ae43ae63db0a468e99395">buffer</a>); <a name="l00826"></a>00826 } <span class="keywordflow">else</span> { <a name="l00827"></a>00827 <span class="comment">/* The buffer is not yet full */</span> <a name="l00828"></a>00828 <a class="code" href="sha2_8c.html#aa357cebb0a4ac20de95565894de5f793">MEMCPY_BCOPY</a>(&context-><a class="code" href="struct__ldns__sha512__CTX.html#a31366ad9937ae43ae63db0a468e99395">buffer</a>[usedspace], data, len); <a name="l00829"></a>00829 <a class="code" href="sha2_8c.html#a32ce6a800376f90c7cee70ac2e7255d6">ADDINC128</a>(context-><a class="code" href="struct__ldns__sha512__CTX.html#aff26561cccdd57baa1e7362dd35f15ae">bitcount</a>, len << 3); <a name="l00830"></a>00830 <span class="comment">/* Clean up: */</span> <a name="l00831"></a>00831 usedspace = freespace = 0; <a name="l00832"></a>00832 <span class="keywordflow">return</span>; <a name="l00833"></a>00833 } <a name="l00834"></a>00834 } <a name="l00835"></a>00835 <span class="keywordflow">while</span> (len >= <a class="code" href="sha2_8h.html#a14689e48f416ecc1938c064d4b6e1fe6">LDNS_SHA512_BLOCK_LENGTH</a>) { <a name="l00836"></a>00836 <span class="comment">/* Process as many complete blocks as we can */</span> <a name="l00837"></a>00837 ldns_sha512_Transform(context, (<a class="code" href="sha2_8c.html#aa546cbc3e1ac5d6426230fbf71e7caa9">sha2_word64</a>*)data); <a name="l00838"></a>00838 <a class="code" href="sha2_8c.html#a32ce6a800376f90c7cee70ac2e7255d6">ADDINC128</a>(context-><a class="code" href="struct__ldns__sha512__CTX.html#aff26561cccdd57baa1e7362dd35f15ae">bitcount</a>, <a class="code" href="sha2_8h.html#a14689e48f416ecc1938c064d4b6e1fe6">LDNS_SHA512_BLOCK_LENGTH</a> << 3); <a name="l00839"></a>00839 len -= <a class="code" href="sha2_8h.html#a14689e48f416ecc1938c064d4b6e1fe6">LDNS_SHA512_BLOCK_LENGTH</a>; <a name="l00840"></a>00840 data += <a class="code" href="sha2_8h.html#a14689e48f416ecc1938c064d4b6e1fe6">LDNS_SHA512_BLOCK_LENGTH</a>; <a name="l00841"></a>00841 } <a name="l00842"></a>00842 <span class="keywordflow">if</span> (len > 0) { <a name="l00843"></a>00843 <span class="comment">/* There's left-overs, so save 'em */</span> <a name="l00844"></a>00844 <a class="code" href="sha2_8c.html#aa357cebb0a4ac20de95565894de5f793">MEMCPY_BCOPY</a>(context-><a class="code" href="struct__ldns__sha512__CTX.html#a31366ad9937ae43ae63db0a468e99395">buffer</a>, data, len); <a name="l00845"></a>00845 <a class="code" href="sha2_8c.html#a32ce6a800376f90c7cee70ac2e7255d6">ADDINC128</a>(context-><a class="code" href="struct__ldns__sha512__CTX.html#aff26561cccdd57baa1e7362dd35f15ae">bitcount</a>, len << 3); <a name="l00846"></a>00846 } <a name="l00847"></a>00847 <span class="comment">/* Clean up: */</span> <a name="l00848"></a>00848 usedspace = freespace = 0; <a name="l00849"></a>00849 } <a name="l00850"></a>00850 <a name="l00851"></a>00851 <span class="keyword">static</span> <span class="keywordtype">void</span> ldns_sha512_Last(<a class="code" href="struct__ldns__sha512__CTX.html">ldns_sha512_CTX</a>* context) { <a name="l00852"></a>00852 <span class="keywordtype">size_t</span> usedspace; <a name="l00853"></a>00853 <a name="l00854"></a>00854 usedspace = (context-><a class="code" href="struct__ldns__sha512__CTX.html#aff26561cccdd57baa1e7362dd35f15ae">bitcount</a>[0] >> 3) % <a class="code" href="sha2_8h.html#a14689e48f416ecc1938c064d4b6e1fe6">LDNS_SHA512_BLOCK_LENGTH</a>; <a name="l00855"></a>00855 <span class="preprocessor">#if BYTE_ORDER == LITTLE_ENDIAN</span> <a name="l00856"></a>00856 <span class="preprocessor"></span> <span class="comment">/* Convert FROM host byte order */</span> <a name="l00857"></a>00857 <a class="code" href="sha2_8c.html#a5399a7e8dab676a6c36fe85c91ae1366">REVERSE64</a>(context-><a class="code" href="struct__ldns__sha512__CTX.html#aff26561cccdd57baa1e7362dd35f15ae">bitcount</a>[0],context-><a class="code" href="struct__ldns__sha512__CTX.html#aff26561cccdd57baa1e7362dd35f15ae">bitcount</a>[0]); <a name="l00858"></a>00858 <a class="code" href="sha2_8c.html#a5399a7e8dab676a6c36fe85c91ae1366">REVERSE64</a>(context-><a class="code" href="struct__ldns__sha512__CTX.html#aff26561cccdd57baa1e7362dd35f15ae">bitcount</a>[1],context-><a class="code" href="struct__ldns__sha512__CTX.html#aff26561cccdd57baa1e7362dd35f15ae">bitcount</a>[1]); <a name="l00859"></a>00859 <span class="preprocessor">#endif</span> <a name="l00860"></a>00860 <span class="preprocessor"></span> <span class="keywordflow">if</span> (usedspace > 0) { <a name="l00861"></a>00861 <span class="comment">/* Begin padding with a 1 bit: */</span> <a name="l00862"></a>00862 context-><a class="code" href="struct__ldns__sha512__CTX.html#a31366ad9937ae43ae63db0a468e99395">buffer</a>[usedspace++] = 0x80; <a name="l00863"></a>00863 <a name="l00864"></a>00864 <span class="keywordflow">if</span> (usedspace <= <a class="code" href="sha2_8c.html#a53ebe62ce8c8959c921160f16ecf83b0">ldns_sha512_SHORT_BLOCK_LENGTH</a>) { <a name="l00865"></a>00865 <span class="comment">/* Set-up for the last transform: */</span> <a name="l00866"></a>00866 <a class="code" href="sha2_8c.html#a8e212a8c6f84340a49f10895833e2f2d">MEMSET_BZERO</a>(&context-><a class="code" href="struct__ldns__sha512__CTX.html#a31366ad9937ae43ae63db0a468e99395">buffer</a>[usedspace], <a class="code" href="sha2_8c.html#a53ebe62ce8c8959c921160f16ecf83b0">ldns_sha512_SHORT_BLOCK_LENGTH</a> - usedspace); <a name="l00867"></a>00867 } <span class="keywordflow">else</span> { <a name="l00868"></a>00868 <span class="keywordflow">if</span> (usedspace < <a class="code" href="sha2_8h.html#a14689e48f416ecc1938c064d4b6e1fe6">LDNS_SHA512_BLOCK_LENGTH</a>) { <a name="l00869"></a>00869 <a class="code" href="sha2_8c.html#a8e212a8c6f84340a49f10895833e2f2d">MEMSET_BZERO</a>(&context-><a class="code" href="struct__ldns__sha512__CTX.html#a31366ad9937ae43ae63db0a468e99395">buffer</a>[usedspace], <a class="code" href="sha2_8h.html#a14689e48f416ecc1938c064d4b6e1fe6">LDNS_SHA512_BLOCK_LENGTH</a> - usedspace); <a name="l00870"></a>00870 } <a name="l00871"></a>00871 <span class="comment">/* Do second-to-last transform: */</span> <a name="l00872"></a>00872 ldns_sha512_Transform(context, (<a class="code" href="sha2_8c.html#aa546cbc3e1ac5d6426230fbf71e7caa9">sha2_word64</a>*)context-><a class="code" href="struct__ldns__sha512__CTX.html#a31366ad9937ae43ae63db0a468e99395">buffer</a>); <a name="l00873"></a>00873 <a name="l00874"></a>00874 <span class="comment">/* And set-up for the last transform: */</span> <a name="l00875"></a>00875 <a class="code" href="sha2_8c.html#a8e212a8c6f84340a49f10895833e2f2d">MEMSET_BZERO</a>(context-><a class="code" href="struct__ldns__sha512__CTX.html#a31366ad9937ae43ae63db0a468e99395">buffer</a>, <a class="code" href="sha2_8h.html#a14689e48f416ecc1938c064d4b6e1fe6">LDNS_SHA512_BLOCK_LENGTH</a> - 2); <a name="l00876"></a>00876 } <a name="l00877"></a>00877 } <span class="keywordflow">else</span> { <a name="l00878"></a>00878 <span class="comment">/* Prepare for final transform: */</span> <a name="l00879"></a>00879 <a class="code" href="sha2_8c.html#a8e212a8c6f84340a49f10895833e2f2d">MEMSET_BZERO</a>(context-><a class="code" href="struct__ldns__sha512__CTX.html#a31366ad9937ae43ae63db0a468e99395">buffer</a>, <a class="code" href="sha2_8c.html#a53ebe62ce8c8959c921160f16ecf83b0">ldns_sha512_SHORT_BLOCK_LENGTH</a>); <a name="l00880"></a>00880 <a name="l00881"></a>00881 <span class="comment">/* Begin padding with a 1 bit: */</span> <a name="l00882"></a>00882 *context-><a class="code" href="struct__ldns__sha512__CTX.html#a31366ad9937ae43ae63db0a468e99395">buffer</a> = 0x80; <a name="l00883"></a>00883 } <a name="l00884"></a>00884 <span class="comment">/* Store the length of input data (in bits): */</span> <a name="l00885"></a>00885 *(<a class="code" href="sha2_8c.html#aa546cbc3e1ac5d6426230fbf71e7caa9">sha2_word64</a>*)&context-><a class="code" href="struct__ldns__sha512__CTX.html#a31366ad9937ae43ae63db0a468e99395">buffer</a>[<a class="code" href="sha2_8c.html#a53ebe62ce8c8959c921160f16ecf83b0">ldns_sha512_SHORT_BLOCK_LENGTH</a>] = context-><a class="code" href="struct__ldns__sha512__CTX.html#aff26561cccdd57baa1e7362dd35f15ae">bitcount</a>[1]; <a name="l00886"></a>00886 *(<a class="code" href="sha2_8c.html#aa546cbc3e1ac5d6426230fbf71e7caa9">sha2_word64</a>*)&context-><a class="code" href="struct__ldns__sha512__CTX.html#a31366ad9937ae43ae63db0a468e99395">buffer</a>[<a class="code" href="sha2_8c.html#a53ebe62ce8c8959c921160f16ecf83b0">ldns_sha512_SHORT_BLOCK_LENGTH</a>+8] = context-><a class="code" href="struct__ldns__sha512__CTX.html#aff26561cccdd57baa1e7362dd35f15ae">bitcount</a>[0]; <a name="l00887"></a>00887 <a name="l00888"></a>00888 <span class="comment">/* final transform: */</span> <a name="l00889"></a>00889 ldns_sha512_Transform(context, (<a class="code" href="sha2_8c.html#aa546cbc3e1ac5d6426230fbf71e7caa9">sha2_word64</a>*)context-><a class="code" href="struct__ldns__sha512__CTX.html#a31366ad9937ae43ae63db0a468e99395">buffer</a>); <a name="l00890"></a>00890 } <a name="l00891"></a>00891 <a name="l00892"></a><a class="code" href="sha2_8c.html#a6222b5f02b5c1222a801be5d6def0340">00892</a> <span class="keywordtype">void</span> <a class="code" href="sha2_8c.html#a6222b5f02b5c1222a801be5d6def0340">ldns_sha512_final</a>(<a class="code" href="sha2_8c.html#a078da424bb2de5634b5e52fff5a73815">sha2_byte</a> digest[], <a class="code" href="struct__ldns__sha512__CTX.html">ldns_sha512_CTX</a>* context) { <a name="l00893"></a>00893 <a class="code" href="sha2_8c.html#aa546cbc3e1ac5d6426230fbf71e7caa9">sha2_word64</a> *d = (<a class="code" href="sha2_8c.html#aa546cbc3e1ac5d6426230fbf71e7caa9">sha2_word64</a>*)digest; <a name="l00894"></a>00894 <a name="l00895"></a>00895 <span class="comment">/* Sanity check: */</span> <a name="l00896"></a>00896 assert(context != (<a class="code" href="struct__ldns__sha512__CTX.html">ldns_sha512_CTX</a>*)0); <a name="l00897"></a>00897 <a name="l00898"></a>00898 <span class="comment">/* If no digest buffer is passed, we don't bother doing this: */</span> <a name="l00899"></a>00899 <span class="keywordflow">if</span> (digest != (<a class="code" href="sha2_8c.html#a078da424bb2de5634b5e52fff5a73815">sha2_byte</a>*)0) { <a name="l00900"></a>00900 ldns_sha512_Last(context); <a name="l00901"></a>00901 <a name="l00902"></a>00902 <span class="comment">/* Save the hash data for output: */</span> <a name="l00903"></a>00903 <span class="preprocessor">#if BYTE_ORDER == LITTLE_ENDIAN</span> <a name="l00904"></a>00904 <span class="preprocessor"></span> { <a name="l00905"></a>00905 <span class="comment">/* Convert TO host byte order */</span> <a name="l00906"></a>00906 <span class="keywordtype">int</span> j; <a name="l00907"></a>00907 <span class="keywordflow">for</span> (j = 0; j < 8; j++) { <a name="l00908"></a>00908 <a class="code" href="sha2_8c.html#a5399a7e8dab676a6c36fe85c91ae1366">REVERSE64</a>(context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[j],context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[j]); <a name="l00909"></a>00909 *d++ = context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[j]; <a name="l00910"></a>00910 } <a name="l00911"></a>00911 } <a name="l00912"></a>00912 <span class="preprocessor">#else</span> <a name="l00913"></a>00913 <span class="preprocessor"></span> <a class="code" href="sha2_8c.html#aa357cebb0a4ac20de95565894de5f793">MEMCPY_BCOPY</a>(d, context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>, <a class="code" href="sha2_8h.html#a6f2bac73208f59e48a09e4d7db6e960d">LDNS_SHA512_DIGEST_LENGTH</a>); <a name="l00914"></a>00914 <span class="preprocessor">#endif</span> <a name="l00915"></a>00915 <span class="preprocessor"></span> } <a name="l00916"></a>00916 <a name="l00917"></a>00917 <span class="comment">/* Zero out state data */</span> <a name="l00918"></a>00918 <a class="code" href="sha2_8c.html#a8e212a8c6f84340a49f10895833e2f2d">MEMSET_BZERO</a>(context, <span class="keyword">sizeof</span>(<a class="code" href="struct__ldns__sha512__CTX.html">ldns_sha512_CTX</a>)); <a name="l00919"></a>00919 } <a name="l00920"></a>00920 <a name="l00921"></a>00921 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> * <a name="l00922"></a><a class="code" href="sha2_8h.html#a8d5fb1fd280a58618420c4a24e83cba8">00922</a> <a class="code" href="sha2_8c.html#a8d5fb1fd280a58618420c4a24e83cba8" title="Convenience function to digest a fixed block of data at once.">ldns_sha512</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *data, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> data_len, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *digest) <a name="l00923"></a>00923 { <a name="l00924"></a>00924 <a class="code" href="struct__ldns__sha512__CTX.html">ldns_sha512_CTX</a> ctx; <a name="l00925"></a>00925 <a class="code" href="sha2_8c.html#a38a7d2743cb7a0d4eedfdd835f7b6157">ldns_sha512_init</a>(&ctx); <a name="l00926"></a>00926 <a class="code" href="sha2_8c.html#a8d3c22ce0c1e995b63fc025ce0acd919">ldns_sha512_update</a>(&ctx, data, data_len); <a name="l00927"></a>00927 <a class="code" href="sha2_8c.html#a6222b5f02b5c1222a801be5d6def0340">ldns_sha512_final</a>(digest, &ctx); <a name="l00928"></a>00928 <span class="keywordflow">return</span> digest; <a name="l00929"></a>00929 } <a name="l00930"></a>00930 <a name="l00931"></a>00931 <span class="comment">/*** SHA-384: *********************************************************/</span> <a name="l00932"></a><a class="code" href="sha2_8h.html#a58f7cadb0709d77f836e655d90140540">00932</a> <span class="keywordtype">void</span> <a class="code" href="sha2_8c.html#afde6a224995abcbeca0eae414326e310">ldns_sha384_init</a>(<a class="code" href="struct__ldns__sha512__CTX.html">ldns_sha384_CTX</a>* context) { <a name="l00933"></a>00933 <span class="keywordflow">if</span> (context == (<a class="code" href="struct__ldns__sha512__CTX.html">ldns_sha384_CTX</a>*)0) { <a name="l00934"></a>00934 <span class="keywordflow">return</span>; <a name="l00935"></a>00935 } <a name="l00936"></a>00936 <a class="code" href="sha2_8c.html#aa357cebb0a4ac20de95565894de5f793">MEMCPY_BCOPY</a>(context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>, sha384_initial_hash_value, <a class="code" href="sha2_8h.html#a6f2bac73208f59e48a09e4d7db6e960d">LDNS_SHA512_DIGEST_LENGTH</a>); <a name="l00937"></a>00937 <a class="code" href="sha2_8c.html#a8e212a8c6f84340a49f10895833e2f2d">MEMSET_BZERO</a>(context-><a class="code" href="struct__ldns__sha512__CTX.html#a31366ad9937ae43ae63db0a468e99395">buffer</a>, <a class="code" href="sha2_8h.html#ab6855c361ff1044d01f61faf26099d22">LDNS_SHA384_BLOCK_LENGTH</a>); <a name="l00938"></a>00938 context-><a class="code" href="struct__ldns__sha512__CTX.html#aff26561cccdd57baa1e7362dd35f15ae">bitcount</a>[0] = context-><a class="code" href="struct__ldns__sha512__CTX.html#aff26561cccdd57baa1e7362dd35f15ae">bitcount</a>[1] = 0; <a name="l00939"></a>00939 } <a name="l00940"></a>00940 <a name="l00941"></a><a class="code" href="sha2_8h.html#ac31250e0c61d91c1fff6eb0c6fbb2adc">00941</a> <span class="keywordtype">void</span> <a class="code" href="sha2_8c.html#a6b9a1c163a057e4c30e8ec1dc283abed">ldns_sha384_update</a>(<a class="code" href="struct__ldns__sha512__CTX.html">ldns_sha384_CTX</a>* context, <span class="keyword">const</span> <a class="code" href="sha2_8c.html#a078da424bb2de5634b5e52fff5a73815">sha2_byte</a>* data, <span class="keywordtype">size_t</span> len) { <a name="l00942"></a>00942 <a class="code" href="sha2_8c.html#a8d3c22ce0c1e995b63fc025ce0acd919">ldns_sha512_update</a>((<a class="code" href="struct__ldns__sha512__CTX.html">ldns_sha512_CTX</a>*)context, data, len); <a name="l00943"></a>00943 } <a name="l00944"></a>00944 <a name="l00945"></a><a class="code" href="sha2_8c.html#a4ee7d4d8b2edffb19851d2a2c1f9844b">00945</a> <span class="keywordtype">void</span> <a class="code" href="sha2_8c.html#a4ee7d4d8b2edffb19851d2a2c1f9844b">ldns_sha384_final</a>(<a class="code" href="sha2_8c.html#a078da424bb2de5634b5e52fff5a73815">sha2_byte</a> digest[], <a class="code" href="struct__ldns__sha512__CTX.html">ldns_sha384_CTX</a>* context) { <a name="l00946"></a>00946 <a class="code" href="sha2_8c.html#aa546cbc3e1ac5d6426230fbf71e7caa9">sha2_word64</a> *d = (<a class="code" href="sha2_8c.html#aa546cbc3e1ac5d6426230fbf71e7caa9">sha2_word64</a>*)digest; <a name="l00947"></a>00947 <a name="l00948"></a>00948 <span class="comment">/* Sanity check: */</span> <a name="l00949"></a>00949 assert(context != (<a class="code" href="struct__ldns__sha512__CTX.html">ldns_sha384_CTX</a>*)0); <a name="l00950"></a>00950 <a name="l00951"></a>00951 <span class="comment">/* If no digest buffer is passed, we don't bother doing this: */</span> <a name="l00952"></a>00952 <span class="keywordflow">if</span> (digest != (<a class="code" href="sha2_8c.html#a078da424bb2de5634b5e52fff5a73815">sha2_byte</a>*)0) { <a name="l00953"></a>00953 ldns_sha512_Last((<a class="code" href="struct__ldns__sha512__CTX.html">ldns_sha512_CTX</a>*)context); <a name="l00954"></a>00954 <a name="l00955"></a>00955 <span class="comment">/* Save the hash data for output: */</span> <a name="l00956"></a>00956 <span class="preprocessor">#if BYTE_ORDER == LITTLE_ENDIAN</span> <a name="l00957"></a>00957 <span class="preprocessor"></span> { <a name="l00958"></a>00958 <span class="comment">/* Convert TO host byte order */</span> <a name="l00959"></a>00959 <span class="keywordtype">int</span> j; <a name="l00960"></a>00960 <span class="keywordflow">for</span> (j = 0; j < 6; j++) { <a name="l00961"></a>00961 <a class="code" href="sha2_8c.html#a5399a7e8dab676a6c36fe85c91ae1366">REVERSE64</a>(context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[j],context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[j]); <a name="l00962"></a>00962 *d++ = context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>[j]; <a name="l00963"></a>00963 } <a name="l00964"></a>00964 } <a name="l00965"></a>00965 <span class="preprocessor">#else</span> <a name="l00966"></a>00966 <span class="preprocessor"></span> <a class="code" href="sha2_8c.html#aa357cebb0a4ac20de95565894de5f793">MEMCPY_BCOPY</a>(d, context-><a class="code" href="struct__ldns__sha512__CTX.html#a57d7ee93f5bab664478396f86eb03805">state</a>, <a class="code" href="sha2_8h.html#ab0512bf849fc2c8d69a2bb630c560151">LDNS_SHA384_DIGEST_LENGTH</a>); <a name="l00967"></a>00967 <span class="preprocessor">#endif</span> <a name="l00968"></a>00968 <span class="preprocessor"></span> } <a name="l00969"></a>00969 <a name="l00970"></a>00970 <span class="comment">/* Zero out state data */</span> <a name="l00971"></a>00971 <a class="code" href="sha2_8c.html#a8e212a8c6f84340a49f10895833e2f2d">MEMSET_BZERO</a>(context, <span class="keyword">sizeof</span>(<a class="code" href="struct__ldns__sha512__CTX.html">ldns_sha384_CTX</a>)); <a name="l00972"></a>00972 } <a name="l00973"></a>00973 <a name="l00974"></a>00974 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> * <a name="l00975"></a><a class="code" href="sha2_8h.html#adb96cbe7b4edf0bd1f162613706996e0">00975</a> <a class="code" href="sha2_8c.html#adb96cbe7b4edf0bd1f162613706996e0" title="Convenience function to digest a fixed block of data at once.">ldns_sha384</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *data, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> data_len, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *digest) <a name="l00976"></a>00976 { <a name="l00977"></a>00977 <a class="code" href="struct__ldns__sha512__CTX.html">ldns_sha384_CTX</a> ctx; <a name="l00978"></a>00978 <a class="code" href="sha2_8c.html#afde6a224995abcbeca0eae414326e310">ldns_sha384_init</a>(&ctx); <a name="l00979"></a>00979 <a class="code" href="sha2_8c.html#a6b9a1c163a057e4c30e8ec1dc283abed">ldns_sha384_update</a>(&ctx, data, data_len); <a name="l00980"></a>00980 <a class="code" href="sha2_8c.html#a4ee7d4d8b2edffb19851d2a2c1f9844b">ldns_sha384_final</a>(digest, &ctx); <a name="l00981"></a>00981 <span class="keywordflow">return</span> digest; <a name="l00982"></a>00982 } </pre></div></div> </div> <hr class="footer"/><address class="footer"><small>Generated on Wed Jan 11 2012 for ldns by  <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.4 </small></address> </body> </html>