]> git.cryptolib.org Git - avr-crypto-lib.git/blob - shabal/shabal192-asm.S
fixing some decryption bugs in GCM128
[avr-crypto-lib.git] / shabal / shabal192-asm.S
1 /* shabal192-asm.S */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2009  Daniel Otte (daniel.otte@rub.de)
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  * \file    shabal192-asm.S
21  * \author  Daniel Otte
22  * \email   daniel.otte@rub.de
23  * \date    2009-04-27
24  * \license GPLv3 or later
25  */ 
26
27 #include "avr-asm-macros.S"
28
29 shabal192_iv:
30         /* A */
31 .long   0xFD749ED4, 0xB798E530, 0x33904B6F, 0x46BDA85E
32 .long   0x076934B4, 0x454B4058, 0x77F74527, 0xFB4CF465
33 .long   0x62931DA9, 0xE778C8DB, 0x22B3998E, 0xAC15CFB9
34         /* B */
35 .long   0x58BCBAC4, 0xEC47A08E, 0xAEE933B2, 0xDFCBC824
36 .long   0xA7944804, 0xBF65BDB0, 0x5A9D4502, 0x59979AF7
37 .long   0xC5CEA54E, 0x4B6B8150, 0x16E71909, 0x7D632319
38 .long   0x930573A0, 0xF34C63D1, 0xCAF914B4, 0xFDD6612C
39         /* C */
40 .long   0x61550878, 0x89EF2B75, 0xA1660C46, 0x7EF3855B
41 .long   0x7297B58C, 0x1BC67793, 0x7FB1C723, 0xB66FC640
42 .long   0x1A48B71C, 0xF0976D17, 0x088CE80A, 0xA454EDF3
43 .long   0x1C096BF4, 0xAC76224B, 0x5215781C, 0xCD5D2669
44
45
46 /******************************************************************************/
47 /*
48 void shabal192_init(shabal_ctx_t *ctx){
49         uint8_t i;
50         ctx->b = ctx->b_buffer;
51         ctx->c = ctx->c_buffer;
52         ctx->w.w64 = 1LL;
53         for(i=0;i<SHABAL_R;++i){
54                 ctx->a[i] = pgm_read_dword(&(shabal192_iv[i]));
55         }
56         for(i=0;i<16;++i){
57                 ctx->b[i] = pgm_read_dword(&(shabal192_iv[SHABAL_R+i]));
58         }
59         for(i=0;i<16;++i){
60                 ctx->c[i] = pgm_read_dword(&(shabal192_iv[SHABAL_R+16+i]));
61         }
62 }
63 */
64 /*
65  * param ctx: r24,r25
66  */
67 .global shabal192_init
68 shabal192_init:
69         movw r26, r24
70         ldi r24, 1
71         st X+, r24
72         st X+, r1
73         st X+, r1
74         st X+, r1
75         st X+, r1
76         st X+, r1
77         st X+, r1
78         st X+, r1
79         movw r24, r26
80         adiw r24, 12*4+2+2
81         st X+, r24
82         st X+, r25
83         adiw r24, 4*16-1
84         adiw r24, 1
85         st X+, r24
86         st X+, r25 
87         ldi r24, (12+16+16)
88         ldi r30, lo8(shabal192_iv)
89         ldi r31, hi8(shabal192_iv)
90 1:      lpm r0, Z+
91         st X+, r0
92         lpm r0, Z+
93         st X+, r0
94         lpm r0, Z+
95         st X+, r0
96         lpm r0, Z+
97         st X+, r0
98         dec r24
99         brne 1b
100         ret     
101
102 /******************************************************************************/
103 /*
104  * param dest: r24:r25
105  * param ctx:  r22:r23
106  */
107 .global shabal192_ctx2hash
108 shabal192_ctx2hash:
109         movw r26, r24
110         movw r30, r22
111         ldd r24, Z+(8+2)
112         ldd r25, Z+(8+2+1)
113         movw r30, r24
114         adiw r30, (16-192/32)*4
115         ldi r24, 192/8
116 1:  ld r0, Z+
117         st X+, r0
118         dec r24
119         brne 1b
120         ret     
121         
122 /******************************************************************************/
123 /*
124 void shabal192(void *dest, void *msg, uint32_t length_b){
125         shabal_ctx_t ctx;
126         shabal192_init(&ctx);
127         while(length_b>=SHABAL_BLOCKSIZE){
128                 shabal_nextBlock(&ctx, msg);
129                 msg = (uint8_t*)msg+SHABAL_BLOCKSIZE_B;
130                 length_b -= SHABAL_BLOCKSIZE;
131         }
132         shabal_lastBlock(&ctx, msg, length_b);
133         shabal192_ctx2hash(dest, &ctx);
134 }
135 */
136 /*
137  * param dest:      r24:r25
138  * param msg:       r22:r23
139  * param length_b:  r18:r21
140  */
141 DST0 =  8
142 DST1 =  9
143 CTX0 = 10
144 CTX1 = 11
145 MSG0 = 12
146 MSG1 = 13
147 LEN2 = 14
148 LEN3 = 15
149 LEN0 = 16
150 LEN1 = 17
151 .global shabal192
152 shabal192: 
153         push_range 8, 17
154         stack_alloc_large 188
155         adiw r30, 1 /* Z points to stack space (ctx) */
156         movw CTX0, r30
157         movw DST0, r24
158         movw MSG0, r22
159         movw LEN0, r18
160         movw LEN2, r20
161         movw r24, r30
162         rcall shabal192_init
163 2:
164         tst LEN2
165         brne 3f
166         tst LEN3
167         breq 4f
168 3:
169         movw r24, CTX0
170         movw r22, MSG0
171         rcall shabal_nextBlock
172         subi LEN1, 0x02
173         sbc LEN2, r1
174         sbc LEN3, r1
175         ldi r18, 64
176         add MSG0, r18
177         adc MSG1, r1    
178         rjmp 2b
179 4:      
180         movw r24, CTX0
181         movw r22, MSG0
182         movw r20, LEN0
183         rcall shabal_lastBlock
184         movw r24, DST0
185         movw r22, CTX0
186         rcall shabal192_ctx2hash
187         stack_free_large2 188
188         pop_range 8, 17
189         ret
190         
191