fork download
  1.  
  2. // code from http://w...content-available-to-author-only...s.com/downloads/tonga-demo/sources/LGPL/shasum-native/
  3.  
  4. /* sha.h
  5.  *
  6.  * The sha1 and sha256 hash functions.
  7.  */
  8.  
  9. /* nettle, low-level cryptographics library
  10.  *
  11.  * Copyright (C) 2001 Niels Möller
  12.  *
  13.  * The nettle library is free software; you can redistribute it and/or modify
  14.  * it under the terms of the GNU Lesser General Public License as published by
  15.  * the Free Software Foundation; either version 2.1 of the License, or (at your
  16.  * option) any later version.
  17.  *
  18.  * The nettle library is distributed in the hope that it will be useful, but
  19.  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  20.  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  21.  * License for more details.
  22.  *
  23.  * You should have received a copy of the GNU Lesser General Public License
  24.  * along with the nettle library; see the file COPYING.LIB. If not, write to
  25.  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  26.  * MA 02111-1307, USA.
  27.  */
  28.  
  29. #ifndef NETTLE_SHA_H_INCLUDED
  30. #define NETTLE_SHA_H_INCLUDED
  31.  
  32. #include <inttypes.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35.  
  36. typedef uint32_t word32;
  37. typedef unsigned char byte;
  38.  
  39.  
  40. /* SHA256 */
  41.  
  42. #define SHA256_DIGEST_SIZE 32
  43. #define SHA256_DATA_SIZE 64
  44.  
  45. /* Digest is kept internally as 8 32-bit words. */
  46. #define _SHA256_DIGEST_LENGTH 8
  47.  
  48. typedef struct sha256_ctx
  49. {
  50. word32 state[_SHA256_DIGEST_LENGTH]; /* State variables */
  51. word32 count_low, count_high; /* 64-bit block count */
  52. byte block[SHA256_DATA_SIZE]; /* SHA256 data buffer */
  53. unsigned int index; /* index into buffer */
  54. } SHA256_CTX;
  55.  
  56. void
  57. sha256_init(struct sha256_ctx *ctx);
  58.  
  59. void
  60. sha256_update(struct sha256_ctx *ctx, const byte *data, unsigned length);
  61.  
  62. void
  63. sha256_final(struct sha256_ctx *ctx);
  64.  
  65. void
  66. sha256_digest(const struct sha256_ctx *ctx, byte *digest);
  67.  
  68.  
  69. #endif /* NETTLE_SHA_H_INCLUDED */
  70.  
  71. /* sha256.h
  72.  *
  73.  * The sha256 hash function.
  74.  */
  75.  
  76. /* nettle, low-level cryptographics library
  77.  *
  78.  * Copyright (C) 2001 Niels Möller
  79.  *
  80.  * This program is free software; you can redistribute it and/or
  81.  * modify it under the terms of the GNU General Public License as
  82.  * published by the Free Software Foundation; either version 2 of the
  83.  * License, or (at your option) any later version.
  84.  *
  85.  * The nettle library is distributed in the hope that it will be useful, but
  86.  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  87.  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  88.  * License for more details.
  89.  *
  90.  * You should have received a copy of the GNU Lesser General Public License
  91.  * along with the nettle library; see the file COPYING.LIB. If not, write to
  92.  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  93.  * MA 02111-1307, USA.
  94.  */
  95.  
  96. /* Modelled after the sha1.c code by Peter Gutmann. */
  97.  
  98. // #include "mhash_sha256.h"
  99. // #include <stdlib.h>
  100. // #include <string.h>
  101.  
  102.  
  103. #ifndef EXTRACT_UCHAR
  104. #define EXTRACT_UCHAR(p) (*(unsigned char *)(p))
  105. #endif
  106.  
  107. #define STRING2INT(s) ((((((EXTRACT_UCHAR(s) << 8) \
  108. | EXTRACT_UCHAR(s+1)) << 8) \
  109. | EXTRACT_UCHAR(s+2)) << 8) \
  110. | EXTRACT_UCHAR(s+3))
  111.  
  112. /* This has been modified in order to fit in mhash.
  113.  * --nmav.
  114.  */
  115.  
  116. /* A block, treated as a sequence of 32-bit words. */
  117. #define SHA256_DATA_LENGTH 16
  118.  
  119. #define ROTR(n,x) ((x)>>(n) | ((x)<<(32-(n))))
  120. #define SHR(n,x) ((x)>>(n))
  121.  
  122. /* The SHA256 functions. The Choice function is the same as the SHA1
  123.   function f1, and the majority function is the same as the SHA1 f3
  124.   function. They can be optimized to save one boolean operation each
  125.   - thanks to Rich Schroeppel, rcs@cs.arizona.edu for discovering
  126.   this */
  127.  
  128. /* #define Choice(x,y,z) ( ( (x) & (y) ) | ( ~(x) & (z) ) ) */
  129. #define Choice(x,y,z) ( (z) ^ ( (x) & ( (y) ^ (z) ) ) )
  130. /* #define Majority(x,y,z) ( ((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)) ) */
  131. #define Majority(x,y,z) ( ((x) & (y)) ^ ((z) & ((x) ^ (y))) )
  132.  
  133. #define S0(x) (ROTR(2,(x)) ^ ROTR(13,(x)) ^ ROTR(22,(x)))
  134. #define S1(x) (ROTR(6,(x)) ^ ROTR(11,(x)) ^ ROTR(25,(x)))
  135.  
  136. #define s0(x) (ROTR(7,(x)) ^ ROTR(18,(x)) ^ SHR(3,(x)))
  137. #define s1(x) (ROTR(17,(x)) ^ ROTR(19,(x)) ^ SHR(10,(x)))
  138.  
  139. /* Generated by the shadata program. */
  140. static const word32 K[64] = {
  141. 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
  142. 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
  143. 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
  144. 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
  145. 0xe49b69c1UL, 0xefbe4786UL, 0xfc19dc6UL, 0x240ca1ccUL,
  146. 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
  147. 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
  148. 0xc6e00bf3UL, 0xd5a79147UL, 0x6ca6351UL, 0x14292967UL,
  149. 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
  150. 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
  151. 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
  152. 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
  153. 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
  154. 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
  155. 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
  156. 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL,
  157. };
  158.  
  159. /* The initial expanding function. The hash function is defined over an
  160.   64-word expanded input array W, where the first 16 are copies of the input
  161.   data, and the remaining 64 are defined by
  162.  
  163.   W[ t ] = s1(W[t-2] + W[t-7] + s0(W[i-15] + W[i-16]
  164.  
  165.   This implementation generates these values on the fly in a circular
  166.   buffer - thanks to Colin Plumb, colin@nyx10.cs.du.edu for this
  167.   optimization.
  168. */
  169.  
  170. #define EXPAND(W,i) \
  171. ( W[(i) & 15 ] += (s1(W[((i)-2) & 15]) + W[((i)-7) & 15] + s0(W[((i)-15) & 15])) )
  172.  
  173. /* The prototype SHA sub-round. The fundamental sub-round is:
  174.  
  175.   T1 = h + S1(e) + Choice(e,f,g) + K[t] + W[t]
  176. T2 = S0(a) + Majority(a,b,c)
  177. a' = T1+T2
  178. b' = a
  179. c' = b
  180. d' = c
  181. e' = d + T1
  182. f' = e
  183. g' = f
  184. h' = g
  185.  
  186.   but this is implemented by unrolling the loop 8 times and renaming
  187.   the variables
  188.   ( h, a, b, c, d, e, f, g ) = ( a, b, c, d, e, f, g, h ) each
  189.   iteration. This code is then replicated 8, using the next 8 values
  190.   from the W[] array each time */
  191.  
  192. /* FIXME: We can probably reorder this to optimize away at least one
  193.  * of T1 and T2. It's crucial that DATA is only used once, as that
  194.  * argument will have side effects. */
  195. #define ROUND(a,b,c,d,e,f,g,h,k,data) do { \
  196.   word32 T1 = h + S1(e) + Choice(e,f,g) + k + data; \
  197.   word32 T2 = S0(a) + Majority(a,b,c); \
  198.   d += T1; \
  199.   h = T1 + T2; \
  200. } while (0)
  201.  
  202. /* Initialize the SHA values */
  203.  
  204. void sha256_init(struct sha256_ctx *ctx)
  205. {
  206. /* Initial values, also generated by the shadata program. */
  207. static const word32 H0[_SHA256_DIGEST_LENGTH] = {
  208. 0x6a09e667UL, 0xbb67ae85UL, 0x3c6ef372UL, 0xa54ff53aUL,
  209. 0x510e527fUL, 0x9b05688cUL, 0x1f83d9abUL, 0x5be0cd19UL,
  210. };
  211.  
  212. memcpy(ctx->state, H0, sizeof(H0));
  213.  
  214. /* Initialize bit count */
  215. ctx->count_low = ctx->count_high = 0;
  216.  
  217. /* Initialize buffer */
  218. ctx->index = 0;
  219. }
  220.  
  221. /* Perform the SHA transformation. Note that this code, like MD5, seems to
  222.   break some optimizing compilers due to the complexity of the expressions
  223.   and the size of the basic block. It may be necessary to split it into
  224.   sections, e.g. based on the four subrounds
  225.  
  226.   Note that this function destroys the data area */
  227.  
  228. static void sha256_transform(word32 * state, word32 * data)
  229. {
  230. word32 A, B, C, D, E, F, G, H; /* Local vars */
  231. unsigned i;
  232. const word32 *k;
  233. word32 *d;
  234.  
  235. /* Set up first buffer and local data buffer */
  236. A = state[0];
  237. B = state[1];
  238. C = state[2];
  239. D = state[3];
  240. E = state[4];
  241. F = state[5];
  242. G = state[6];
  243. H = state[7];
  244.  
  245. /* Heavy mangling */
  246. /* First 16 subrounds that act on the original data */
  247.  
  248. for (i = 0, k = K, d = data; i < 16; i += 8, k += 8, d += 8) {
  249. ROUND(A, B, C, D, E, F, G, H, k[0], d[0]);
  250. ROUND(H, A, B, C, D, E, F, G, k[1], d[1]);
  251. ROUND(G, H, A, B, C, D, E, F, k[2], d[2]);
  252. ROUND(F, G, H, A, B, C, D, E, k[3], d[3]);
  253. ROUND(E, F, G, H, A, B, C, D, k[4], d[4]);
  254. ROUND(D, E, F, G, H, A, B, C, k[5], d[5]);
  255. ROUND(C, D, E, F, G, H, A, B, k[6], d[6]);
  256. ROUND(B, C, D, E, F, G, H, A, k[7], d[7]);
  257. }
  258.  
  259. for (; i < 64; i += 16, k += 16) {
  260. ROUND(A, B, C, D, E, F, G, H, k[0], EXPAND(data, 0));
  261. ROUND(H, A, B, C, D, E, F, G, k[1], EXPAND(data, 1));
  262. ROUND(G, H, A, B, C, D, E, F, k[2], EXPAND(data, 2));
  263. ROUND(F, G, H, A, B, C, D, E, k[3], EXPAND(data, 3));
  264. ROUND(E, F, G, H, A, B, C, D, k[4], EXPAND(data, 4));
  265. ROUND(D, E, F, G, H, A, B, C, k[5], EXPAND(data, 5));
  266. ROUND(C, D, E, F, G, H, A, B, k[6], EXPAND(data, 6));
  267. ROUND(B, C, D, E, F, G, H, A, k[7], EXPAND(data, 7));
  268. ROUND(A, B, C, D, E, F, G, H, k[8], EXPAND(data, 8));
  269. ROUND(H, A, B, C, D, E, F, G, k[9], EXPAND(data, 9));
  270. ROUND(G, H, A, B, C, D, E, F, k[10], EXPAND(data, 10));
  271. ROUND(F, G, H, A, B, C, D, E, k[11], EXPAND(data, 11));
  272. ROUND(E, F, G, H, A, B, C, D, k[12], EXPAND(data, 12));
  273. ROUND(D, E, F, G, H, A, B, C, k[13], EXPAND(data, 13));
  274. ROUND(C, D, E, F, G, H, A, B, k[14], EXPAND(data, 14));
  275. ROUND(B, C, D, E, F, G, H, A, k[15], EXPAND(data, 15));
  276. }
  277.  
  278. /* Update state */
  279. state[0] += A;
  280. state[1] += B;
  281. state[2] += C;
  282. state[3] += D;
  283. state[4] += E;
  284. state[5] += F;
  285. state[6] += G;
  286. state[7] += H;
  287. }
  288.  
  289. static void sha256_block(struct sha256_ctx *ctx, const byte * block)
  290. {
  291. word32 data[SHA256_DATA_LENGTH];
  292. int i;
  293.  
  294. /* Update block count */
  295. if (!++ctx->count_low)
  296. ++ctx->count_high;
  297.  
  298. /* Endian independent conversion */
  299. for (i = 0; i < SHA256_DATA_LENGTH; i++, block += 4)
  300. data[i] = STRING2INT(block);
  301.  
  302. sha256_transform(ctx->state, data);
  303. }
  304.  
  305. void
  306. sha256_update(struct sha256_ctx *ctx, const byte * buffer, unsigned length)
  307. {
  308. if (ctx->index) { /* Try to fill partial block */
  309. unsigned left = SHA256_DATA_SIZE - ctx->index;
  310. if (length < left) {
  311. memcpy(ctx->block + ctx->index, buffer, length);
  312. ctx->index += length;
  313. return; /* Finished */
  314. } else {
  315. memcpy(ctx->block + ctx->index, buffer, left);
  316. sha256_block(ctx, ctx->block);
  317. buffer += left;
  318. length -= left;
  319. }
  320. }
  321. while (length >= SHA256_DATA_SIZE) {
  322. sha256_block(ctx, buffer);
  323. buffer += SHA256_DATA_SIZE;
  324. length -= SHA256_DATA_SIZE;
  325. }
  326. /* Buffer leftovers */
  327. /* NOTE: The corresponding sha1 code checks for the special case length == 0.
  328. * That seems supoptimal, as I suspect it increases the number of branches. */
  329.  
  330. memcpy(ctx->block, buffer, length);
  331. ctx->index = length;
  332. }
  333.  
  334. /* Final wrapup - pad to SHA1_DATA_SIZE-byte boundary with the bit pattern
  335.   1 0* (64-bit count of bits processed, MSB-first) */
  336.  
  337. void sha256_final(struct sha256_ctx *ctx)
  338. {
  339. word32 data[SHA256_DATA_LENGTH];
  340. int i;
  341. int words;
  342.  
  343. i = ctx->index;
  344.  
  345. /* Set the first char of padding to 0x80. This is safe since there is
  346. always at least one byte free */
  347.  
  348. /* assert(i < SHA256_DATA_SIZE);
  349.  */
  350. ctx->block[i++] = 0x80;
  351.  
  352. /* Fill rest of word */
  353. for (; i & 3; i++)
  354. ctx->block[i] = 0;
  355.  
  356. /* i is now a multiple of the word size 4 */
  357. words = i >> 2;
  358. for (i = 0; i < words; i++)
  359. data[i] = STRING2INT(ctx->block + 4 * i);
  360.  
  361. if (words > (SHA256_DATA_LENGTH - 2)) { /* No room for length in this block. Process it and
  362. * pad with another one */
  363. for (i = words; i < SHA256_DATA_LENGTH; i++)
  364. data[i] = 0;
  365. sha256_transform(ctx->state, data);
  366. for (i = 0; i < (SHA256_DATA_LENGTH - 2); i++)
  367. data[i] = 0;
  368. } else
  369. for (i = words; i < SHA256_DATA_LENGTH - 2; i++)
  370. data[i] = 0;
  371.  
  372. /* There are 512 = 2^9 bits in one block */
  373. data[SHA256_DATA_LENGTH - 2] =
  374. (ctx->count_high << 9) | (ctx->count_low >> 23);
  375. data[SHA256_DATA_LENGTH - 1] =
  376. (ctx->count_low << 9) | (ctx->index << 3);
  377. sha256_transform(ctx->state, data);
  378. }
  379.  
  380. void sha256_digest(const struct sha256_ctx *ctx, byte * s)
  381. {
  382. int i;
  383.  
  384. if (s!=NULL)
  385. for (i = 0; i < _SHA256_DIGEST_LENGTH; i++) {
  386. *s++ = ctx->state[i] >> 24;
  387. *s++ = 0xff & (ctx->state[i] >> 16);
  388. *s++ = 0xff & (ctx->state[i] >> 8);
  389. *s++ = 0xff & ctx->state[i];
  390. }
  391. }
  392.  
  393. // #include <stdio.h>
  394. // #include <stdlib.h>
  395.  
  396. // #include "mhash_sha256.h"
  397.  
  398. /*
  399.  * from driver.c of mhash
  400.  */
  401. static const char hexconvtab[] = "0123456789abcdef";
  402.  
  403. static char *
  404. bin2hex(const unsigned char *old, const size_t oldlen, size_t * newlen)
  405. {
  406. unsigned char *new = NULL;
  407. int i, j;
  408.  
  409. new = (char *) malloc(oldlen * 2 * sizeof(char) + 1);
  410. if (!new)
  411. return (new);
  412.  
  413. for (i = j = 0; i < oldlen; i++) {
  414. new[j++] = hexconvtab[old[i] >> 4];
  415. new[j++] = hexconvtab[old[i] & 15];
  416. }
  417. new[j] = '\0';
  418.  
  419. if (newlen)
  420. *newlen = oldlen * 2 * sizeof(char);
  421.  
  422. return (new);
  423. }
  424.  
  425.  
  426. int main(int argc, char** argv)
  427. {
  428. // FILE *file;
  429. size_t n;
  430. SHA256_CTX ctx;
  431. unsigned char buf[1024] = "Eduard";
  432. byte output[33];
  433.  
  434. /* if ( argc <= 1 ) {
  435.   return EXIT_FAILURE;
  436.   }
  437.  
  438.   if ( (file=fopen(argv[1], "rb")) == NULL ) {
  439.   return EXIT_FAILURE;
  440.   } */
  441.  
  442. sha256_init(&ctx);
  443. n = strlen(buf);
  444.  
  445. // while ( (n=fread( buf, 1, sizeof(buf), file)) > 0 )
  446. sha256_update(&ctx, buf, n );
  447.  
  448. sha256_final(&ctx);
  449. sha256_digest(&ctx, output);
  450.  
  451. // printf("%s ?%s\n", bin2hex(output, 32, &n), argv[1]);
  452. puts(bin2hex(output, 32, &n));
  453. return EXIT_SUCCESS;
  454. }
  455.  
  456.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
feaa3bc385dabc27620335632197749b7ad9a8a5e7a6831ec5887720cd01abef