]> git.cryptolib.org Git - avr-crypto-lib.git/blob - gf256mul/gf256mul.S
d202d84fed5014f62aa6a89080b7efbb12229d93
[avr-crypto-lib.git] / gf256mul / gf256mul.S
1 /* gf256mul.S */
2 /*
3     This file is part of the AVR-Crypto-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 /* 
21  * File:        gf256mul.S
22  * Author:      Daniel Otte
23  * Date:        2008-12-19
24  * License:     GPLv3 or later
25  * Description: peasant's algorithm for multiplication in GF(2^8)
26  * 
27  */
28
29 #include <avr/io.h>
30 #define OPTIMIZE_SMALL_A
31
32 /*
33  * param a: r24
34  * param b: r22
35  * param reducer: r20
36  */
37 A = 23
38 B = 22
39 P = 24
40 .global gf256mul
41
42 #ifdef OPTIMIZE_SMALL_A
43 gf256mul:
44         mov A, r24
45         clr r24
46 1:      
47         lsr A
48         breq 4f
49         brcc 2f
50         eor P, B
51 2:
52         lsl B
53         brcc 3f
54         eor B, r20      
55 3:
56         rjmp 1b
57 4:
58         brcc 2f
59         eor P, B
60 2:
61         ret
62
63 #else
64
65 /*
66
67 uint8_t gf256mul(uint8_t a, uint8_t b, uint8_t p) {
68         uint8_t r = 0, c = 8;
69         do {
70             if (a & 1) {
71                 r ^= b;
72             }
73             a >>= 1;
74             if (b & 0x80) {
75                 b ^= p;
76             }
77             b <<= 1;
78         } while (--c);
79         return r;
80 }
81
82  */
83 gf256mul:
84         mov A, r24
85         clr r24
86         ldi r25, 8
87 1:      
88         lsr A
89         brcc 2f
90         eor P, B
91 2:
92         lsl B
93         brcc 3f
94         eor B, r20      
95 3:
96         dec r25
97         brne 1b
98         ret
99
100 #endif