]> git.cryptolib.org Git - avr-crypto-lib.git/blob - sha256.c
new, derived from old avr/crypto + cast5
[avr-crypto-lib.git] / sha256.c
1 /**
2  * File:                sha256.c
3  * Author:      Daniel Otte 
4  * Date:                16.05.2006
5  * License:     GPL
6  * 
7  */
8
9 #include <stdint.h>
10 #include <string.h> /* for memcpy, memmove, memset */
11 #include "sha256.h"
12
13 #define LITTLE_ENDIAN
14
15 #if defined LITTLE_ENDIAN
16 #elif defined BIG_ENDIAN
17 #else
18         #error specify endianess!!!
19 #endif
20
21
22 uint32_t sha256_init_vector[]={
23         0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
24     0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19 };
25
26 void sha256_init(sha256_ctx_t *state){
27         state->length=0;
28         memcpy(state->h, sha256_init_vector, 8*4);
29 }
30
31 /*
32  * rotate x right by n positions
33  */
34
35 uint32_t rotr32( uint32_t x, uint8_t n){
36         return ((x>>n) | (x<<(32-n)));
37 }
38
39 // #define CHANGE_ENDIAN32(x) (((x)<<24) | ((x)>>24) | (((x)& 0x0000ff00)<<8) | (((x)& 0x00ff0000)>>8))
40
41 uint32_t change_endian32(uint32_t x){
42         return (((x)<<24) | ((x)>>24) | (((x)& 0x0000ff00)<<8) | (((x)& 0x00ff0000)>>8));
43 }
44
45
46 /* sha256 functions as macros for speed and size, cause they are called only once */
47
48 #define CH(x,y,z)  (((x)&(y)) ^ ((~(x))&(z)))
49 #define MAJ(x,y,z) (((x)&(y)) ^ ((x)&(z)) ^ ((y)&(z)))
50
51 #define SIGMA0(x) (rotr32((x),2) ^ rotr32((x),13) ^ rotr32((x),22))
52 #define SIGMA1(x) (rotr32((x),6) ^ rotr32((x),11) ^ rotr32((x),25))
53 #define SIGMA_a(x) (rotr32((x),7)  ^ rotr32((x),18) ^ ((x)>>3))
54 #define SIGMA_b(x) (rotr32((x),17) ^ rotr32((x),19) ^ ((x)>>10))
55
56
57 uint32_t k[]={
58         0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
59         0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
60         0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
61         0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
62         0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
63         0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
64         0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
65         0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
66 };
67
68
69 /**
70  * block must be, 512, Bit = 64, Byte, long !!!
71  */
72 void sha256_nextBlock (sha256_ctx_t *state, void* block){
73         uint32_t w[64]; /* this is 256, byte, large, */
74         uint8_t  i;
75         uint32_t a[8],t1,t2;
76
77         /* init w */
78 #if defined LITTLE_ENDIAN
79                 for (i=0; i<16; ++i){
80                         w[i]= change_endian32(((uint32_t*)block)[i]);
81                 }
82 #elif defined BIG_ENDIAN
83                 memcpy((void*)w, block, 64);
84 #endif
85                 for (i=16; i<64; ++i){
86                         w[i] = SIGMA_b(w[i-2]) + w[i-7] + SIGMA_a(w[i-15]) + w[i-16];   
87                 }
88
89         /* init working variables */    
90                 memcpy((void*)a,(void*)(state->h), 8*4);
91
92         /* do the, fun stuff, */
93                 for (i=0; i<64; ++i){
94                         t1 = a[7] + SIGMA1(a[4]) + CH(a[4],a[5],a[6]) + k[i] + w[i];
95                         t2 = SIGMA0(a[0]) + MAJ(a[0],a[1],a[2]);
96                         memmove(&(a[1]), &(a[0]), 7*4);         /* a[7]=a[6]; a[6]=a[5]; a[5]=a[4]; a[4]=a[3]; a[3]=a[2]; a[2]=a[1]; a[1]=a[0]; */
97                         a[4] += t1;
98                         a[0] = t1 + t2;
99                 }
100
101         /* update, the, state, */
102                 for (i=0; i<8; ++i){
103                         state->h[i] += a[i];
104                 }       
105                 state->length += 512;
106
107
108 /*
109  * length is the length of only THIS block in BITS not in bytes!
110  *  bits are big endian, meaning high bits come first.
111  *      if you have a message with bits at the end, the byte must be padded with zeros 
112  * */
113 void sha256_lastBlock(sha256_ctx_t *state, void* block, uint16_t length){
114         uint8_t lb[SHA256_BLOCK_BITS/8]; /* local block */
115         state->length += length;
116         memcpy (&(lb[0]), block, length/8);
117         
118         /* set the final one bit */
119         if (length & 0x3){ // if we have single bits at the end
120                 lb[length/8] = ((uint8_t*)(block))[length/8];
121         } else {
122                 lb[length/8] = 0;
123         }
124         lb[length/8] |= 0x80>>(length & 0x3);
125         length =(length >> 3) + 1; /* from now on length contains the number of BYTES in lb*/
126         /* pad with zeros */
127         if (length>64-8){ /* not enouth space for 64bit length value */
128                 memset((void*)(&(lb[length])), 0, 64-length);
129                 sha256_nextBlock(state, lb);
130                 state->length -= 512;
131                 length = 0;     
132         }
133         memset((void*)(&(lb[length])), 0, 56-length);
134         /* store the 64bit length value */
135 #if defined LITTLE_ENDIAN
136                 /* this is now rolled up */
137         uint8_t i;      
138         for (i=1; i<=8; ++i){
139                 lb[55+i] = (uint8_t)(state->length>>(64- 8*i));
140         }
141 #elif defined BIG_ENDIAN
142         *((uint64_t)&(lb[56])) = state->length;
143 #endif
144         sha256_nextBlock(state, lb);
145 }
146
147
148 /*
149  * length in bits!
150  */
151
152 void sha256(sha256_hash_t *dest, void* msg, uint32_t length){ /* length could be choosen longer but this is for ?C */
153         sha256_ctx_t s;
154         sha256_init(&s);
155         while(length >= SHA256_BLOCK_BITS){
156                 sha256_nextBlock(&s, msg);
157                 msg += SHA256_BLOCK_BITS/8;
158                 length -= SHA256_BLOCK_BITS;
159         }
160         sha256_lastBlock(&s, msg, length);
161         sha256_ctx2hash(dest,&s);
162 }
163
164
165
166
167 #ifdef sha256_ctx2hash_in_C
168 void sha256_ctx2hash(sha256_hash_t *dest, sha256_ctx_t *state){
169 #if defined LITTLE_ENDIAN
170         uint8_t i;
171         for(i=0; i<8; ++i){
172                 ((uint32_t*)dest)[i] = change_endian32(state->h[i]);
173         }
174 #elif BIG_ENDIAN
175         if (dest != state->h)
176                 memcpy(dest, state->h, SHA256_HASH_BITS/8);
177 #else
178 # error unsupported endian type!
179 #endif
180 }
181 #endif
182
183