2 ** Copyright 2005 Double Precision, Inc.
3 ** See COPYING for distribution information.
12 static const char rcsid
[]="$Id: sha256.c,v 1.1 2005/02/21 03:18:30 mrsam Exp $";
14 #define ROTR(x,n) ((SHA256_WORD)(((SHA256_WORD)(x) >> (n))|((x) << (32-(n)))))
16 #define ROTL(x,n) ((SHA256_WORD)(((SHA256_WORD)(x) << (n))|((x) >> (32-(n)))))
19 #define CH(x,y,z) ((SHA256_WORD)(((x) & (y)) ^ ((~(x))&(z))))
20 #define MAJ(x,y,z) ((SHA256_WORD)(((x)&(y))^((x)&(z))^((y)&(z))))
22 #define SUM0(x) ((SHA256_WORD)(ROTR((x),2)^ROTR((x),13)^ROTR((x),22)))
23 #define SUM1(x) ((SHA256_WORD)(ROTR((x),6)^ROTR((x),11)^ROTR((x),25)))
25 #define TH0(x) ((SHA256_WORD)(ROTR((x),7)^ROTR((x),18)^((SHA256_WORD)(x)>>3)))
26 #define TH1(x) ((SHA256_WORD)(ROTR((x),17)^ROTR((x),19)^((SHA256_WORD)(x)>>10)))
28 static const SHA256_WORD K
[64]=
29 {0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
30 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
31 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
32 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
33 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
34 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
35 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
36 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2};
38 void sha256_context_init(struct SHA256_CONTEXT
*c
)
40 if (sizeof(SHA256_WORD
) != 4)
54 void sha256_context_hash(struct SHA256_CONTEXT
*cc
,
55 const unsigned char blk
[SHA256_BLOCK_SIZE
])
59 SHA256_WORD a
,b
,c
,d
,e
,f
,g
,h
;
61 for (i
=t
=0; t
<16; t
++)
63 SHA256_WORD x
=blk
[i
]; i
++;
65 x
=(x
<< 8) | blk
[i
]; i
++;
66 x
=(x
<< 8) | blk
[i
]; i
++;
67 W
[t
]=(x
<< 8) | blk
[i
]; i
++;
71 W
[t
]= TH1(W
[t
-2]) + W
[t
-7] + TH0(W
[t
-15]) + W
[t
-16];
84 SHA256_WORD T1
=h
+ SUM1(e
) + CH(e
,f
,g
) + K
[t
] + W
[t
];
85 SHA256_WORD T2
=SUM0(a
)+MAJ(a
,b
,c
);
106 void sha256_context_hashstream(struct SHA256_CONTEXT
*c
, const void *p
, unsigned l
)
108 const unsigned char *cp
=(const unsigned char *)p
;
113 if (c
->blk_ptr
== 0 && l
>= SHA256_BLOCK_SIZE
)
115 sha256_context_hash(c
, cp
);
116 cp
+= SHA256_BLOCK_SIZE
;
117 l
-= SHA256_BLOCK_SIZE
;
122 if (ll
> SHA256_BLOCK_SIZE
- c
->blk_ptr
)
123 ll
=SHA256_BLOCK_SIZE
- c
->blk_ptr
;
124 memcpy(c
->blk
+ c
->blk_ptr
, cp
, ll
);
128 if (c
->blk_ptr
>= SHA256_BLOCK_SIZE
)
130 sha256_context_hash(c
, c
->blk
);
136 void sha256_context_endstream(struct SHA256_CONTEXT
*c
, unsigned long l
)
138 unsigned char buf
[8];
139 static unsigned char zero
[SHA256_BLOCK_SIZE
-8];
142 sha256_context_hashstream(c
, &buf
, 1);
143 while (c
->blk_ptr
!= SHA256_BLOCK_SIZE
-8)
145 if (c
->blk_ptr
> SHA256_BLOCK_SIZE
-8)
147 sha256_context_hashstream(c
, zero
,
148 SHA256_BLOCK_SIZE
- c
->blk_ptr
);
151 sha256_context_hashstream(c
, zero
,
152 SHA256_BLOCK_SIZE
-8-c
->blk_ptr
);
160 buf
[3]=buf
[2]=buf
[1]=buf
[0]=0;
162 sha256_context_hashstream(c
, buf
, 8);
165 void sha256_context_digest(struct SHA256_CONTEXT
*c
, SHA256_DIGEST d
)
167 unsigned char *dp
=d
+ SHA256_DIGEST_SIZE
;
172 SHA256_WORD w
=c
->H
[--i
];
181 void sha256_context_restore(struct SHA256_CONTEXT
*c
, const SHA256_DIGEST d
)
183 const unsigned char *dp
=d
;
188 SHA256_WORD w
= *dp
++;
198 void sha256_digest(const void *msg
, unsigned len
, SHA256_DIGEST d
)
200 struct SHA256_CONTEXT c
;
202 sha256_context_init( &c
);
203 sha256_context_hashstream(&c
, msg
, len
);
204 sha256_context_endstream(&c
, len
);
205 sha256_context_digest( &c
, d
);