| 1 |
/* |
| 2 |
* (C) 2004- TeraTerm Project |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* |
| 9 |
* 1. Redistributions of source code must retain the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer in the |
| 13 |
* documentation and/or other materials provided with the distribution. |
| 14 |
* 3. The name of the author may not be used to endorse or promote products |
| 15 |
* derived from this software without specific prior written permission. |
| 16 |
* |
| 17 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR |
| 18 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 19 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 20 |
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 22 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
*/ |
| 28 |
|
| 29 |
#include "ed25519_crypto_api.h" |
| 30 |
|
| 31 |
#define blocks crypto_hashblocks_sha512 |
| 32 |
|
| 33 |
static const unsigned char iv[64] = { |
| 34 |
0x6a,0x09,0xe6,0x67,0xf3,0xbc,0xc9,0x08, |
| 35 |
0xbb,0x67,0xae,0x85,0x84,0xca,0xa7,0x3b, |
| 36 |
0x3c,0x6e,0xf3,0x72,0xfe,0x94,0xf8,0x2b, |
| 37 |
0xa5,0x4f,0xf5,0x3a,0x5f,0x1d,0x36,0xf1, |
| 38 |
0x51,0x0e,0x52,0x7f,0xad,0xe6,0x82,0xd1, |
| 39 |
0x9b,0x05,0x68,0x8c,0x2b,0x3e,0x6c,0x1f, |
| 40 |
0x1f,0x83,0xd9,0xab,0xfb,0x41,0xbd,0x6b, |
| 41 |
0x5b,0xe0,0xcd,0x19,0x13,0x7e,0x21,0x79 |
| 42 |
} ; |
| 43 |
|
| 44 |
typedef unsigned long long uint64; |
| 45 |
|
| 46 |
int crypto_hash_sha512(unsigned char *out,const unsigned char *in,unsigned long long inlen) |
| 47 |
{ |
| 48 |
unsigned char h[64]; |
| 49 |
unsigned char padded[256]; |
| 50 |
// unsigned int i; |
| 51 |
unsigned long long i; |
| 52 |
unsigned long long bytes = inlen; |
| 53 |
|
| 54 |
for (i = 0;i < 64;++i) h[i] = iv[i]; |
| 55 |
|
| 56 |
blocks(h,in,inlen); |
| 57 |
in += inlen; |
| 58 |
inlen &= 127; |
| 59 |
in -= inlen; |
| 60 |
|
| 61 |
for (i = 0;i < inlen;++i) padded[i] = in[i]; |
| 62 |
padded[inlen] = 0x80; |
| 63 |
|
| 64 |
if (inlen < 112) { |
| 65 |
for (i = inlen + 1;i < 119;++i) padded[i] = 0; |
| 66 |
padded[119] = (unsigned char)(bytes >> 61); |
| 67 |
padded[120] = (unsigned char)(bytes >> 53); |
| 68 |
padded[121] = (unsigned char)(bytes >> 45); |
| 69 |
padded[122] = (unsigned char)(bytes >> 37); |
| 70 |
padded[123] = (unsigned char)(bytes >> 29); |
| 71 |
padded[124] = (unsigned char)(bytes >> 21); |
| 72 |
padded[125] = (unsigned char)(bytes >> 13); |
| 73 |
padded[126] = (unsigned char)(bytes >> 5); |
| 74 |
padded[127] = (unsigned char)(bytes << 3); |
| 75 |
blocks(h,padded,128); |
| 76 |
} else { |
| 77 |
for (i = inlen + 1;i < 247;++i) padded[i] = 0; |
| 78 |
padded[247] = (unsigned char)(bytes >> 61); |
| 79 |
padded[248] = (unsigned char)(bytes >> 53); |
| 80 |
padded[249] = (unsigned char)(bytes >> 45); |
| 81 |
padded[250] = (unsigned char)(bytes >> 37); |
| 82 |
padded[251] = (unsigned char)(bytes >> 29); |
| 83 |
padded[252] = (unsigned char)(bytes >> 21); |
| 84 |
padded[253] = (unsigned char)(bytes >> 13); |
| 85 |
padded[254] = (unsigned char)(bytes >> 5); |
| 86 |
padded[255] = (unsigned char)(bytes << 3); |
| 87 |
blocks(h,padded,256); |
| 88 |
} |
| 89 |
|
| 90 |
for (i = 0;i < 64;++i) out[i] = h[i]; |
| 91 |
|
| 92 |
return 0; |
| 93 |
} |
| 94 |
|