]> git.cryptolib.org Git - avr-crypto-lib.git/blob - salsa20/salsa20.c
fixing E-Mail-Address & Copyright
[avr-crypto-lib.git] / salsa20 / salsa20.c
1 /* salsa20.c */
2 /*
3     This file is part of the AVR-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 "salsa20.h"
24
25
26 #define ROTL32(a,n) (((a)<<(n))|((a)>>(32-(n))))
27
28 static
29 void quaterround(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d){
30         *b ^= ROTL32(*a + *d,  7);
31         *c ^= ROTL32(*b + *a,  9);
32         *d ^= ROTL32(*c + *b, 13);
33         *a ^= ROTL32(*d + *c, 18);
34 }
35
36 static
37 void rowround(uint32_t *a){
38         quaterround(a+ 0, a+ 1, a+ 2, a+ 3);
39         quaterround(a+ 5, a+ 6, a+ 7, a+ 4);
40         quaterround(a+10, a+11, a+ 8, a+ 9);
41         quaterround(a+15, a+12, a+13, a+14);
42 }
43
44 static
45 void columnround(uint32_t *a){
46         quaterround(a+ 0, a+ 4, a+ 8, a+12);
47         quaterround(a+ 5, a+ 9, a+13, a+ 1);
48         quaterround(a+10, a+14, a+ 2, a+ 6);
49         quaterround(a+15, a+ 3, a+ 7, a+11);
50 }
51
52 static
53 void doubleround(uint32_t *a){
54         columnround(a);
55         rowround(a);
56
57 }
58
59
60 void salsa20_hash(uint32_t *a){
61         uint8_t i;
62         uint32_t b[16];
63         memcpy(b, a, 64);
64         for(i=0; i<10; ++i){
65                 doubleround(a);
66         }
67         for(i=0; i<16; ++i){
68                 a[i] += b[i];
69         }
70 }
71
72 const uint8_t sigma[] PROGMEM = {'e','x','p','a','n','d',' ','3','2','-','b','y','t','e',' ','k'};
73 const uint8_t theta[] PROGMEM = {'e','x','p','a','n','d',' ','1','6','-','b','y','t','e',' ','k'};
74
75 void salsa_k32(uint32_t *dest, const uint32_t *k, const uint32_t *n){
76         memcpy_P(dest+ 0, sigma+ 0, 4);
77         memcpy(  dest+ 4, k+ 0, 16);
78         memcpy_P(dest+20, sigma+ 4, 4);
79         memcpy(  dest+24, n+ 0, 16);
80         memcpy_P(dest+40, sigma+ 8, 4);
81         memcpy(  dest+44, k+16, 16);
82         memcpy_P(dest+60, sigma+12, 4);
83         salsa20_hash(dest);
84 }
85
86 void salsa_k16(uint32_t *dest, const uint32_t *k, const uint32_t *n){
87         memcpy_P(dest+ 0, theta+ 0, 4);
88         memcpy(  dest+ 4, k+ 0, 16);
89         memcpy_P(dest+20, theta+ 4, 4);
90         memcpy(  dest+24, n+ 0, 16);
91         memcpy_P(dest+40, theta+ 8, 4);
92         memcpy(  dest+44, k+ 0, 16);
93         memcpy_P(dest+60, theta+12, 4);
94         salsa20_hash(dest);
95 }
96
97 void salsa20_genBlock256(void *dest, const void *k, const void *iv, uint64_t i){
98         uint32_t n[4];
99         memcpy(n, iv, 8);
100         memcpy(n+8, &i, 8);
101         salsa_k32((uint32_t*)dest, (uint32_t*)k, n);
102 }
103
104 void salsa20_genBlock128(void *dest, const void *k, const void *iv, uint64_t i){
105         uint32_t n[4];
106         memcpy(n, iv, 8);
107         memcpy(n+8, &i, 8);
108         salsa_k16((uint32_t*)dest, (uint32_t*)k, n);
109 }
110
111
112 void salsa20_init(void *key, uint16_t keylength_b, void *iv, salsa20_ctx_t *ctx){
113         if(keylength_b==256){
114                 memcpy_P((ctx->a.v8+ 0), sigma+ 0, 4);
115                 memcpy_P((ctx->a.v8+20), sigma+ 4, 4);
116                 memcpy_P((ctx->a.v8+40), sigma+ 8, 4);
117                 memcpy(  (ctx->a.v8+44), (uint8_t*)key+16, 16);
118                 memcpy_P((ctx->a.v8+60), sigma+12, 4);
119         }else{
120                 memcpy_P((ctx->a.v8+ 0), theta+ 0, 4);
121                 memcpy_P((ctx->a.v8+20), theta+ 4, 4);
122                 memcpy_P((ctx->a.v8+40), theta+ 8, 4);
123                 memcpy(  (ctx->a.v8+44), (uint8_t*)key+ 0, 16);
124                 memcpy_P((ctx->a.v8+60), theta+12, 4);
125         }
126         memcpy(  (ctx->a.v8+ 4), key, 16);
127         memset(  (ctx->a.v8+24), 0, 16);
128         if(iv){
129                 memcpy(  (ctx->a.v8+24), iv, 8);
130         }
131         ctx->buffer_idx=64;
132 }
133
134 uint8_t salsa20_gen(salsa20_ctx_t *ctx){
135         if(ctx->buffer_idx==64){
136                 memcpy(ctx->buffer, ctx->a.v8, 64);
137                 salsa20_hash((uint32_t*)(ctx->buffer));
138                 ctx->a.v64[4] += 1;
139                 ctx->buffer_idx = 0;
140         }
141         return ctx->buffer[ctx->buffer_idx++];
142 }
143