Coverage Report

Created: 2026-06-08 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/BearSSL/src/symcipher/aes_small_enc.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
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
693k
#define S   br_aes_S
28
29
static void
30
add_round_key(unsigned *state, const uint32_t *skeys)
31
47.2k
{
32
47.2k
  int i;
33
34
236k
  for (i = 0; i < 16; i += 4) {
35
188k
    uint32_t k;
36
37
188k
    k = *skeys ++;
38
188k
    state[i + 0] ^= (unsigned)(k >> 24);
39
188k
    state[i + 1] ^= (unsigned)(k >> 16) & 0xFF;
40
188k
    state[i + 2] ^= (unsigned)(k >> 8) & 0xFF;
41
188k
    state[i + 3] ^= (unsigned)k & 0xFF;
42
188k
  }
43
47.2k
}
44
45
static void
46
sub_bytes(unsigned *state)
47
43.3k
{
48
43.3k
  int i;
49
50
737k
  for (i = 0; i < 16; i ++) {
51
693k
    state[i] = S[state[i]];
52
693k
  }
53
43.3k
}
54
55
static void
56
shift_rows(unsigned *state)
57
43.3k
{
58
43.3k
  unsigned tmp;
59
60
43.3k
  tmp = state[1];
61
43.3k
  state[1] = state[5];
62
43.3k
  state[5] = state[9];
63
43.3k
  state[9] = state[13];
64
43.3k
  state[13] = tmp;
65
66
43.3k
  tmp = state[2];
67
43.3k
  state[2] = state[10];
68
43.3k
  state[10] = tmp;
69
43.3k
  tmp = state[6];
70
43.3k
  state[6] = state[14];
71
43.3k
  state[14] = tmp;
72
73
43.3k
  tmp = state[15];
74
43.3k
  state[15] = state[11];
75
43.3k
  state[11] = state[7];
76
43.3k
  state[7] = state[3];
77
43.3k
  state[3] = tmp;
78
43.3k
}
79
80
static void
81
mix_columns(unsigned *state)
82
39.5k
{
83
39.5k
  int i;
84
85
197k
  for (i = 0; i < 16; i += 4) {
86
158k
    unsigned s0, s1, s2, s3;
87
158k
    unsigned t0, t1, t2, t3;
88
89
158k
    s0 = state[i + 0];
90
158k
    s1 = state[i + 1];
91
158k
    s2 = state[i + 2];
92
158k
    s3 = state[i + 3];
93
158k
    t0 = (s0 << 1) ^ s1 ^ (s1 << 1) ^ s2 ^ s3;
94
158k
    t1 = s0 ^ (s1 << 1) ^ s2 ^ (s2 << 1) ^ s3;
95
158k
    t2 = s0 ^ s1 ^ (s2 << 1) ^ s3 ^ (s3 << 1);
96
158k
    t3 = s0 ^ (s0 << 1) ^ s1 ^ s2 ^ (s3 << 1);
97
158k
    state[i + 0] = t0 ^ ((unsigned)(-(int)(t0 >> 8)) & 0x11B);
98
158k
    state[i + 1] = t1 ^ ((unsigned)(-(int)(t1 >> 8)) & 0x11B);
99
158k
    state[i + 2] = t2 ^ ((unsigned)(-(int)(t2 >> 8)) & 0x11B);
100
158k
    state[i + 3] = t3 ^ ((unsigned)(-(int)(t3 >> 8)) & 0x11B);
101
158k
  }
102
39.5k
}
103
104
/* see inner.h */
105
void
106
br_aes_small_encrypt(unsigned num_rounds, const uint32_t *skey, void *data)
107
3.86k
{
108
3.86k
  unsigned char *buf;
109
3.86k
  unsigned state[16];
110
3.86k
  unsigned u;
111
112
3.86k
  buf = data;
113
65.7k
  for (u = 0; u < 16; u ++) {
114
61.8k
    state[u] = buf[u];
115
61.8k
  }
116
3.86k
  add_round_key(state, skey);
117
43.3k
  for (u = 1; u < num_rounds; u ++) {
118
39.5k
    sub_bytes(state);
119
39.5k
    shift_rows(state);
120
39.5k
    mix_columns(state);
121
39.5k
    add_round_key(state, skey + (u << 2));
122
39.5k
  }
123
3.86k
  sub_bytes(state);
124
3.86k
  shift_rows(state);
125
3.86k
  add_round_key(state, skey + (num_rounds << 2));
126
65.7k
  for (u = 0; u < 16; u ++) {
127
61.8k
    buf[u] = state[u];
128
61.8k
  }
129
3.86k
}