OLD | NEW |
| (Empty) |
1 /* | |
2 * gf2_8.c | |
3 * | |
4 * GF(256) finite field implementation, with the representation used | |
5 * in the AES cipher. | |
6 * | |
7 * David A. McGrew | |
8 * Cisco Systems, Inc. | |
9 */ | |
10 | |
11 /* | |
12 * | |
13 * Copyright (c) 2001-2006, Cisco Systems, Inc. | |
14 * All rights reserved. | |
15 * | |
16 * Redistribution and use in source and binary forms, with or without | |
17 * modification, are permitted provided that the following conditions | |
18 * are met: | |
19 * | |
20 * Redistributions of source code must retain the above copyright | |
21 * notice, this list of conditions and the following disclaimer. | |
22 * | |
23 * Redistributions in binary form must reproduce the above | |
24 * copyright notice, this list of conditions and the following | |
25 * disclaimer in the documentation and/or other materials provided | |
26 * with the distribution. | |
27 * | |
28 * Neither the name of the Cisco Systems, Inc. nor the names of its | |
29 * contributors may be used to endorse or promote products derived | |
30 * from this software without specific prior written permission. | |
31 * | |
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
35 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
36 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
37 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
38 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
39 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
41 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
42 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
43 * OF THE POSSIBILITY OF SUCH DAMAGE. | |
44 * | |
45 */ | |
46 | |
47 | |
48 #ifdef HAVE_CONFIG_H | |
49 #include <config.h> | |
50 #endif | |
51 | |
52 #include "datatypes.h" | |
53 #include "gf2_8.h" | |
54 | |
55 /* gf2_8_shift() moved to gf2_8.h as an inline function */ | |
56 | |
57 gf2_8 | |
58 gf2_8_multiply(gf2_8 x, gf2_8 y) { | |
59 gf2_8 z = 0; | |
60 | |
61 if (y & 1) z ^= x; x = gf2_8_shift(x); | |
62 if (y & 2) z ^= x; x = gf2_8_shift(x); | |
63 if (y & 4) z ^= x; x = gf2_8_shift(x); | |
64 if (y & 8) z ^= x; x = gf2_8_shift(x); | |
65 if (y & 16) z ^= x; x = gf2_8_shift(x); | |
66 if (y & 32) z ^= x; x = gf2_8_shift(x); | |
67 if (y & 64) z ^= x; x = gf2_8_shift(x); | |
68 if (y & 128) z ^= x; | |
69 | |
70 return z; | |
71 } | |
72 | |
73 | |
74 /* this should use the euclidean algorithm */ | |
75 | |
76 gf2_8 | |
77 gf2_8_compute_inverse(gf2_8 x) { | |
78 unsigned int i; | |
79 | |
80 if (x == 0) return 0; /* zero is a special case */ | |
81 for (i=0; i < 256; i++) | |
82 if (gf2_8_multiply((gf2_8) i, x) == 1) | |
83 return (gf2_8) i; | |
84 | |
85 return 0; | |
86 } | |
87 | |
OLD | NEW |