]> git.cryptolib.org Git - avr-crypto-lib.git/blob - sha2/sha2_small_common.c
fixing E-Mail-Address & Copyright
[avr-crypto-lib.git] / sha2 / sha2_small_common.c
1 /* sha2_small_common.c */
2 /*
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <stdint.h>
21 #include <string.h>
22 #include <avr/pgmspace.h>
23 #include "sha2_small_common.h"
24
25 #include <stdio.h>
26 #include <inttypes.h>
27
28 #define LITTLE_ENDIAN
29
30 /**
31  * rotate x right by n positions
32  */
33 static
34 uint32_t rotr32(uint32_t x, uint8_t n){
35         return ((x >> n) | (x << (32 - n)));
36 }
37
38 static
39 uint32_t rotl32(uint32_t x, uint8_t n){
40         return ((x << n) | (x >> (32 - n)));
41 }
42
43 /*************************************************************************/
44
45 // #define CHANGE_ENDIAN32(x) (((x)<<24) | ((x)>>24) | (((x)& 0x0000ff00)<<8) | (((x)& 0x00ff0000)>>8))
46 static
47 uint32_t change_endian32(uint32_t x){
48         return (((x) << 24) |
49                 ((x) >> 24) |
50                 (((x) & 0x0000ff00) << 8) |
51                 (((x) & 0x00ff0000) >> 8));
52 }
53
54
55 /* sha256 functions as macros for speed and size, cause they are called only once */
56
57 #define CH(x,y,z)  (((x) & (y)) ^ ((~(x)) & (z)))
58 #define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
59
60 #define SIGMA_0(x) (rotr32((x),  2) ^ rotr32((x), 13) ^ rotl32((x), 10))
61 #define SIGMA_1(x) (rotr32((x),  6) ^ rotr32((x), 11) ^ rotl32((x),  7))
62 #define SIGMA_a(x) (rotr32((x),  7) ^ rotl32((x), 14) ^ ((x) >> 3))
63 #define SIGMA_b(x) (rotl32((x), 15) ^ rotl32((x), 13) ^ ((x) >> 10))
64
65 const
66 uint32_t k[] PROGMEM = {
67         0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
68         0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
69         0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
70         0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
71         0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
72         0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
73         0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
74         0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
75 };
76
77
78
79 /**
80  * block must be, 512, Bit = 64, Byte, long !!!
81  */
82 void sha2_small_common_nextBlock (sha2_small_common_ctx_t *state, const void *block){
83         uint32_t w[16], wx;
84         uint8_t  i;
85         uint32_t a[8],t1,t2;
86
87         /* init w */
88 #if defined LITTLE_ENDIAN
89         for (i=0; i<16; ++i){
90                 w[i]= change_endian32(((uint32_t*)block)[i]);
91         }
92 #elif defined BIG_ENDIAN
93                 memcpy((void*)w, block, 64);
94 #endif
95 /* init working variables */
96         memcpy(&a[0], &state->h[0], sizeof(a));
97
98 /* do the, fun stuff, */
99         for (i = 0; i < 64; ++i){
100                 if(i < 16){
101                         wx = w[i];
102                 }else{
103             wx =    SIGMA_b(w[14])
104                   + w[9]
105                   + SIGMA_a(w[1])
106                   + w[0];
107             memmove(&w[0], &w[1], sizeof(w) - sizeof(w[0]));
108             w[15] = wx;
109                 }
110                 t1 =   a[7]
111                      + SIGMA_1(a[4])
112                      + CH(a[4], a[5], a[6])
113                      + pgm_read_dword(&k[i]) + wx;
114                 t2 =   SIGMA_0(a[0])
115                      + MAJ(a[0], a[1], a[2]);
116                 t2 += t1;
117                 memmove(&a[1], &a[0], sizeof(a) - sizeof(a[0]));
118                 a[4] += t1;
119                 a[0] = t2;
120         }
121
122 /* update, the, state, */
123         for (i = 0; i < 8; ++i){
124                 state->h[i] += a[i];
125         }
126         state->length += 1;
127 }
128
129
130 void sha2_small_common_lastBlock(sha2_small_common_ctx_t *state, const void *block, uint16_t length_b){
131         uint8_t lb[512/8]; /* local block */
132         uint64_t len;
133         while(length_b >= 512){
134                 sha2_small_common_nextBlock(state, block);
135                 length_b -= 512;
136                 block = (uint8_t*)block + 64;
137         }
138         len = state->length * 512 + length_b;
139         memset(lb, 0, 64);
140         memcpy(lb, block, (length_b + 7) / 8);
141
142         /* set the final one bit */
143         lb[length_b / 8] |= 0x80 >> (length_b & 0x7);
144         /* pad with zeros */
145         if (length_b >= 512 - 64){ /* not enough space for 64bit length value */
146                 sha2_small_common_nextBlock(state, lb);
147                 memset(lb, 0, 64);
148         }
149         /* store the 64bit length value */
150 #if defined LITTLE_ENDIAN
151                 /* this is now rolled up */
152         uint8_t i;
153         i = 7;
154         do {
155                 lb[63 - i] = ((uint8_t*)&len)[i];
156         } while(i--);
157 #elif defined BIG_ENDIAN
158         *((uint64_t)&(lb[56])) = len;
159 #endif
160         sha2_small_common_nextBlock(state, lb);
161 }
162