File Coverage

src/symcipher/aes_small_enc.c
Criterion Covered Total %
statement 0 61 0.0
branch 0 12 0.0
condition n/a
subroutine n/a
pod n/a
total 0 73 0.0


line stmt bran cond sub pod time code
1             /*
2             * Copyright (c) 2016 Thomas Pornin
3             *
4             * Permission is hereby granted, free of charge, to any person obtaining
5             * a copy of this software and associated documentation files (the
6             * "Software"), to deal in the Software without restriction, including
7             * without limitation the rights to use, copy, modify, merge, publish,
8             * distribute, sublicense, and/or sell copies of the Software, and to
9             * permit persons to whom the Software is furnished to do so, subject to
10             * the following conditions:
11             *
12             * The above copyright notice and this permission notice shall be
13             * included in all copies or substantial portions of the Software.
14             *
15             * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16             * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17             * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18             * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19             * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20             * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21             * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22             * SOFTWARE.
23             */
24              
25             #include "inner.h"
26              
27             #define S br_aes_S
28              
29             static void
30 0           add_round_key(unsigned *state, const uint32_t *skeys)
31             {
32             int i;
33              
34 0 0         for (i = 0; i < 16; i += 4) {
35             uint32_t k;
36              
37 0           k = *skeys ++;
38 0           state[i + 0] ^= (unsigned)(k >> 24);
39 0           state[i + 1] ^= (unsigned)(k >> 16) & 0xFF;
40 0           state[i + 2] ^= (unsigned)(k >> 8) & 0xFF;
41 0           state[i + 3] ^= (unsigned)k & 0xFF;
42             }
43 0           }
44              
45             static void
46 0           sub_bytes(unsigned *state)
47             {
48             int i;
49              
50 0 0         for (i = 0; i < 16; i ++) {
51 0           state[i] = S[state[i]];
52             }
53 0           }
54              
55             static void
56 0           shift_rows(unsigned *state)
57             {
58             unsigned tmp;
59              
60 0           tmp = state[1];
61 0           state[1] = state[5];
62 0           state[5] = state[9];
63 0           state[9] = state[13];
64 0           state[13] = tmp;
65              
66 0           tmp = state[2];
67 0           state[2] = state[10];
68 0           state[10] = tmp;
69 0           tmp = state[6];
70 0           state[6] = state[14];
71 0           state[14] = tmp;
72              
73 0           tmp = state[15];
74 0           state[15] = state[11];
75 0           state[11] = state[7];
76 0           state[7] = state[3];
77 0           state[3] = tmp;
78 0           }
79              
80             static void
81 0           mix_columns(unsigned *state)
82             {
83             int i;
84              
85 0 0         for (i = 0; i < 16; i += 4) {
86             unsigned s0, s1, s2, s3;
87             unsigned t0, t1, t2, t3;
88              
89 0           s0 = state[i + 0];
90 0           s1 = state[i + 1];
91 0           s2 = state[i + 2];
92 0           s3 = state[i + 3];
93 0           t0 = (s0 << 1) ^ s1 ^ (s1 << 1) ^ s2 ^ s3;
94 0           t1 = s0 ^ (s1 << 1) ^ s2 ^ (s2 << 1) ^ s3;
95 0           t2 = s0 ^ s1 ^ (s2 << 1) ^ s3 ^ (s3 << 1);
96 0           t3 = s0 ^ (s0 << 1) ^ s1 ^ s2 ^ (s3 << 1);
97 0           state[i + 0] = t0 ^ ((unsigned)(-(int)(t0 >> 8)) & 0x11B);
98 0           state[i + 1] = t1 ^ ((unsigned)(-(int)(t1 >> 8)) & 0x11B);
99 0           state[i + 2] = t2 ^ ((unsigned)(-(int)(t2 >> 8)) & 0x11B);
100 0           state[i + 3] = t3 ^ ((unsigned)(-(int)(t3 >> 8)) & 0x11B);
101             }
102 0           }
103              
104             /* see inner.h */
105             void
106 0           br_aes_small_encrypt(unsigned num_rounds, const uint32_t *skey, void *data)
107             {
108             unsigned char *buf;
109             unsigned state[16];
110             unsigned u;
111              
112 0           buf = data;
113 0 0         for (u = 0; u < 16; u ++) {
114 0           state[u] = buf[u];
115             }
116 0           add_round_key(state, skey);
117 0 0         for (u = 1; u < num_rounds; u ++) {
118 0           sub_bytes(state);
119 0           shift_rows(state);
120 0           mix_columns(state);
121 0           add_round_key(state, skey + (u << 2));
122             }
123 0           sub_bytes(state);
124 0           shift_rows(state);
125 0           add_round_key(state, skey + (num_rounds << 2));
126 0 0         for (u = 0; u < 16; u ++) {
127 0           buf[u] = state[u];
128             }
129 0           }