]> git.cryptolib.org Git - avr-crypto-lib.git/blob - noekeon_omac.S
some testvectors
[avr-crypto-lib.git] / noekeon_omac.S
1 /* noekeon_omac.S */
2 /*
3     This file is part of the Crypto-avr-lib/microcrypt-lib.
4     Copyright (C) 2008  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  * \author  Daniel Otte
21  * \email   daniel.otte@rub.de
22  * \date    2008-08-06
23  * \license GPLv3 or later
24  * 
25  * 
26  * 
27  */
28
29 #include <avr/io.h>
30
31
32 .macro push_ p1:req, p2:vararg
33         push \p1
34 .ifnb \p2       
35         push_ \p2
36 .endif
37 .endm
38
39 .macro pop_ p1:req, p2:vararg
40         pop \p1
41 .ifnb \p2       
42         pop_ \p2
43 .endif
44 .endm
45
46 .macro push_range from:req, to:req
47         push \from
48 .if     \to-\from
49         push_range "(\from+1)",\to
50 .endif          
51 .endm
52
53 .macro pop_range from:req, to:req
54         pop \to
55 .if     \to-\from
56         pop_range \from,"(\to-1)"       
57 .endif
58 .endm
59
60 .macro stack_alloc size:req, reg1=r30, reg2=r31
61         in \reg1, _SFR_IO_ADDR(SPL)
62         in \reg2, _SFR_IO_ADDR(SPH)
63         sbiw r30, \size 
64         out  _SFR_IO_ADDR(SPH), \reg2
65         out  _SFR_IO_ADDR(SPL), \reg1
66 .endm
67
68 .macro stack_free size:req, reg1=r30, reg2=r31
69         in \reg1, _SFR_IO_ADDR(SPL)
70         in \reg2, _SFR_IO_ADDR(SPH)
71         adiw r30, \size 
72         out  _SFR_IO_ADDR(SPH), \reg2
73         out  _SFR_IO_ADDR(SPL), \reg1
74 .endm
75
76 push_r18_r27_func:
77         pop r31
78         pop r30
79         push_range 18, 27
80         ijmp
81
82 pop_r18_r27_func:
83         pop r31
84         pop r30
85         pop_range 18, 27
86         ijmp
87
88 .extern noekeon_enc
89
90 /*
91  * void noekeon_omac(void* dest, const void* msg, uint16_t msglength_b, 
92  *                   const void* key, uint8_t t)  
93  */
94
95 /* param dest        is passed in r24:r25
96  * param msg         is passed in r22:r23
97  * param msglength_b is passed in r20:r21
98  * param key         is passed in r18:r19
99  * param t           is passed in r16
100  */
101 .global noekeon_omac
102 noekeon_omac:
103         stack_alloc 48
104         adiw r30, 1
105         ldi r17, 48
106 1:
107         st Z+, r1
108         dec r17
109         brne 1b
110         sbiw r30, 48
111         mov r26, r22
112         mov r22, r18
113         mov r18, r24
114         mov r24, r30
115         cpi r16, 0xff
116         breq 2f
117         st Z, r16
118         rcall push_r18_r27_func
119         rcall noekeon_enc
120         rcall pop_r18_r27_func
121         movw r30, r24  
122 2:      
123         tst r21
124         brne fullblock
125         cpi r20, 128+1
126         brlo lastblock
127 fullblock:      
128         /* copy block to stack buffer */
129         ldi r16, 16
130 1:      
131         ld r0, X+
132         ld r17, Z
133         eor r0, r17
134         st Z+, r0
135         dec r16
136         brne 1b
137         rcall push_r18_r27_func
138         rcall noekeon_enc
139         rcall pop_r18_r27_func
140         movw r30, r24
141         subi r20, 128
142         sbci r21, 0
143         rjmp 2 
144 lastblock:
145         adiw r24, 16
146         rcall push_r18_r27_func
147         rcall noekeon_enc
148         rcall pop_r18_r27_func
149
150         bst r20, 6 /* set t bit if msglength_b%128==0*/
151 2:
152         ldi r16, 16
153         clc
154         movw r30, r24 /* z points to encrypted null  vector (L) */      
155 1:      
156         ld r0, Z
157         rol r0
158         st Z+, r0
159         dec r16
160         brne 1b
161         brtc 2f
162         clt
163         rjmp 2b
164 2: /* B/P has been calculated */
165         ldi r16, 16
166         sbiw r30, 32
167 3:
168         ld r0, Z
169         ldd r17, Z+16
170         eor r0, r17
171         st Z+, r0
172         dec r16
173         brne 3b
174    /* B/P has been xored into stack buffer */
175    /* now we have to xor-in the remaining message */
176     mov r16, r20
177     subi r16, -7
178     lsr r16
179     lsr r16
180     lsr r16  /* r 1*/
181     sbiw r30, 16
182 4: 
183     ld r0, X+
184     ld r17, Z
185     eor r0, r17
186     st Z+, r0
187     dec r16
188     brne 4b
189     /* now we have only to insert the 1 at the end of message if msglength_b%128 != 0 */
190     sbiw r30, 1
191     andi r20, 0x07
192     breq 7f
193     ldi r17, 1
194 5:
195     dec r20
196     breq 6f
197     lsl r17
198     rjmp 5b  
199 6:
200         ld r0, Z
201         eor r0, r17
202         st Z, r0
203 7:
204         call noekeon_enc
205         stack_free 48
206         ret
207
208
209
210
211
212
213
214
215
216
217
218