]> git.cryptolib.org Git - avr-crypto-lib.git/blob - noekeon_omac.S
fixed small bug in MD5
[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 #include "avr-asm-macros.S"
31
32
33 push_r18_r27_func:
34         pop r31
35         pop r30
36         push_range 18, 27
37         ijmp
38
39 pop_r18_r27_func:
40         pop r31
41         pop r30
42         pop_range 18, 27
43         ijmp
44
45 .extern noekeon_enc
46
47 /*
48  * void noekeon_omac(void* dest, const void* msg, uint16_t msglength_b, 
49  *                   const void* key, uint8_t t)  
50  */
51
52 /* param dest        is passed in r24:r25
53  * param msg         is passed in r22:r23
54  * param msglength_b is passed in r20:r21
55  * param key         is passed in r18:r19
56  * param t           is passed in r16
57  */
58 .global noekeon_omac
59 noekeon_omac:
60         stack_alloc 48
61         adiw r30, 1
62         ldi r17, 48
63 1:
64         st Z+, r1
65         dec r17
66         brne 1b
67         sbiw r30, 48
68         movw r26, r22
69         movw r22, r18
70         movw r18, r24
71         movw r24, r30
72         cpi r16, 0xff
73         breq 2f
74         st Z, r16
75         rcall push_r18_r27_func
76         rcall noekeon_enc
77         rcall pop_r18_r27_func
78         movw r30, r24  
79 2:      
80         tst r21
81         brne fullblock
82         cpi r20, 128+1
83         brlo lastblock
84 fullblock:      
85         /* copy block to stack buffer */
86         ldi r16, 16
87 1:      
88         ld r0, X+
89         ld r17, Z
90         eor r0, r17
91         st Z+, r0
92         dec r16
93         brne 1b
94         rcall push_r18_r27_func
95         rcall noekeon_enc
96         rcall pop_r18_r27_func
97         movw r30, r24
98         subi r20, 128
99         sbci r21, 0
100         rjmp 2 
101 lastblock:
102         adiw r24, 16
103         rcall push_r18_r27_func
104         rcall noekeon_enc
105         rcall pop_r18_r27_func
106
107         bst r20, 6 /* set t bit if msglength_b%128==0*/
108 2:
109         ldi r16, 16
110         clc
111         movw r30, r24 /* z points to encrypted null  vector (L) */      
112 1:      
113         ld r0, Z
114         rol r0
115         st Z+, r0
116         dec r16
117         brne 1b
118         brtc 2f
119         clt
120         rjmp 2b
121 2: /* B/P has been calculated */
122         ldi r16, 16
123         sbiw r30, 32
124 3:
125         ld r0, Z
126         ldd r17, Z+16
127         eor r0, r17
128         st Z+, r0
129         dec r16
130         brne 3b
131    /* B/P has been xored into stack buffer */
132    /* now we have to xor-in the remaining message */
133     mov r16, r20
134     subi r16, -7
135     lsr r16
136     lsr r16
137     lsr r16  /* r 1*/
138     sbiw r30, 16
139 4: 
140     ld r0, X+
141     ld r17, Z
142     eor r0, r17
143     st Z+, r0
144     dec r16
145     brne 4b
146     /* now we have only to insert the 1 at the end of message if msglength_b%128 != 0 */
147     sbiw r30, 1
148     andi r20, 0x07
149     breq 7f
150     ldi r17, 1
151 5:
152     dec r20
153     breq 6f
154     lsl r17
155     rjmp 5b  
156 6:
157         ld r0, Z
158         eor r0, r17
159         st Z, r0
160 7:
161         call noekeon_enc
162         stack_free 48
163         ret
164
165
166
167
168
169
170
171
172
173
174
175