-
Notifications
You must be signed in to change notification settings - Fork 11
/
omac.c
166 lines (130 loc) · 4.51 KB
/
omac.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
---------------------------------------------------------------------------
Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved.
The redistribution and use of this software (with or without changes)
is allowed without the payment of fees or royalties provided that:
source code distributions include the above copyright notice, this
list of conditions and the following disclaimer;
binary distributions include the above copyright notice, this list
of conditions and the following disclaimer in their documentation.
This software is provided 'as is' with no explicit or implied warranties
in respect of its operation, including, but not limited to, correctness
and fitness for purpose.
---------------------------------------------------------------------------
Issue Date: 20/12/2007
*/
#include "omac.h"
#define BLK_ADR_MASK (BLOCK_SIZE - 1)
void omac_init(const unsigned char key[], unsigned long key_len, omac_ctx ctx[1])
{
memset(ctx, 0, sizeof(omac_ctx));
aes_encrypt_key(key, key_len, ctx->aes);
}
void omac_data(unsigned char buf[], unsigned long len, omac_ctx ctx[1])
{ uint32_t cnt = 0, b_pos = ctx->txt_cnt & BLK_ADR_MASK;
if(!len)
return;
if (ctx->txt_cnt && !b_pos)
b_pos = BLOCK_SIZE;
if(!((buf - (UI8_PTR(ctx->txt_cbc) + b_pos)) & BUF_ADRMASK))
{
while(cnt < len && (b_pos & BUF_ADRMASK))
UI8_PTR(ctx->txt_cbc)[b_pos++] ^= buf[cnt++];
if(cnt + BLOCK_SIZE <= len)
{
while(cnt + BUF_INC <= len && b_pos <= BLOCK_SIZE - BUF_INC)
{
*UNIT_PTR(UI8_PTR(ctx->txt_cbc) + b_pos) ^= *UNIT_PTR(buf + cnt);
cnt += BUF_INC; b_pos += BUF_INC;
}
}
while(cnt + BLOCK_SIZE <= len)
{
aes_encrypt(UI8_PTR(ctx->txt_cbc), UI8_PTR(ctx->txt_cbc), ctx->aes);
xor_block_aligned(ctx->txt_cbc, ctx->txt_cbc, buf + cnt);
cnt += BLOCK_SIZE;
}
}
else
{
while(cnt < len && (b_pos & BLK_ADR_MASK))
UI8_PTR(ctx->txt_cbc)[b_pos++] ^= buf[cnt++];
while(cnt + BLOCK_SIZE <= len)
{
aes_encrypt(UI8_PTR(ctx->txt_cbc), UI8_PTR(ctx->txt_cbc), ctx->aes);
xor_block(ctx->txt_cbc, ctx->txt_cbc, buf + cnt);
cnt += BLOCK_SIZE;
}
}
while(cnt < len)
{
if(b_pos == BLOCK_SIZE)
{
aes_encrypt(UI8_PTR(ctx->txt_cbc), UI8_PTR(ctx->txt_cbc), ctx->aes);
b_pos = 0;
}
UI8_PTR(ctx->txt_cbc)[b_pos++] ^= buf[cnt++];
}
ctx->txt_cnt += cnt;
}
static const unsigned char c_xor[4] = { 0x00, 0x87, 0x0e, 0x89 };
static void gf_mulx(uint8_t pad[BLOCK_SIZE])
{ int i, t = pad[0] >> 7;
for(i = 0; i < BLOCK_SIZE - 1; ++i)
pad[i] = (pad[i] << 1) | (pad[i + 1] >> 7);
pad[BLOCK_SIZE - 1] = (pad[BLOCK_SIZE - 1] << 1) ^ c_xor[t];
}
#if OMAC_VERSION == 1
void gf_mulx2(uint8_t pad[BLOCK_SIZE])
{ int i, t = pad[0] >> 6;
for(i = 0; i < BLOCK_SIZE - 1; ++i)
pad[i] = (pad[i] << 2) | (pad[i + 1] >> 6);
pad[BLOCK_SIZE - 2] ^= (t >> 1);
pad[BLOCK_SIZE - 1] = (pad[BLOCK_SIZE - 1] << 2) ^ c_xor[t];
}
#else
void gf_divx(uint8_t pad[BLOCK_SIZE])
{ int i, t = pad[BLOCK_SIZE - 1];
for(i = BLOCK_SIZE - 1; i > 0; --i)
pad[i] = (pad[i] >> 1) | (pad[i - 1] << 7);
pad[0] = (pad[0] >> 1) ^ ((t & 1) ? 0x80 : 0);
pad[BLOCK_SIZE - 1] ^= ((t & 1) ? 0x43 : 0);
}
#endif
#if 0 /* little endian counters */
void gf_mulx(unsigned char pad[BLOCK_SIZE])
{ int i, t;
for(i = BLOCK_SIZE - 1, t = pad[BLOCK_SIZE - 1]; i > 0; --i)
pad[i] = (pad[i] << 1) | (pad[i - 1] >> 7);
pad[0] = (pad[0] << 1) ^ ((t & 0x80) ? 0x87 : 0);
}
void gf_divx(unsigned char pad[BLOCK_SIZE])
{ int i, t;
for(i = 0, t = pad[0]; i < BLOCK_SIZE - 1; ++i)
pad[i] = (pad[i] >> 1) | (pad[i + 1] << 7);
pad[BLOCK_SIZE - 1] = (pad[BLOCK_SIZE - 1] >> 1) ^ ((t & 1) ? 0x80 : 0);
pad[0] ^= ((t & 1) ? 0x43 : 0);
}
#endif
void omac_end(unsigned char auth_tag[], omac_ctx ctx[1])
{ buf_type pad;
int i;
memset(pad, 0, sizeof(pad));
aes_encrypt(UI8_PTR(pad), UI8_PTR(pad), ctx->aes);
i = ctx->txt_cnt & BLK_ADR_MASK;
if(ctx->txt_cnt == 0 || i)
{
UI8_PTR(ctx->txt_cbc)[i] ^= 0x80;
#if OMAC_VERSION == 1
gf_mulx2(UI8_PTR(pad));
#else
gf_divx(UI8_PTR(pad));
#endif
}
else
gf_mulx(UI8_PTR(pad));
xor_block_aligned(pad, pad, ctx->txt_cbc);
aes_encrypt(UI8_PTR(pad), UI8_PTR(pad), ctx->aes);
for(i = 0; i < BLOCK_SIZE; ++i)
auth_tag[i] = UI8_PTR(pad)[i];
}