-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccm.c
177 lines (126 loc) · 3.98 KB
/
ccm.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
167
168
169
170
171
172
173
#include "asmUtil.h"
#include "printf.h"
#include "aes.h"
#include "cpu.h"
#include "ccm.h"
//TI does not provide nearly enough docs to get AES-CCM working on the CC1110. Le Sigh...
static uint8_t __xdata mBlockOut[AES_BLOCK_SIZE];
static uint8_t __xdata mBlockIn[AES_BLOCK_SIZE];
static uint8_t __xdata mMic[4];
//sdcc cannot inline things so we do for it
#define aesCcmPrvCopyNonceSetHalfword(_firstByte, nonce, _val) \
do { \
mBlockOut[0] = (_firstByte); \
xMemCopyShort(mBlockOut + 1, nonce, 13); \
mBlockOut[14] = 0; /* normally val.hi, but for us - zero */ \
mBlockOut[15] = (_val); \
} while(0)
//leaves result in mBlockOut
#pragma callee_saves aesCcmPrvCalcUnencryptedMic
static void aesCcmPrvCalcUnencryptedMic(const uint8_t __xdata *src, const struct AesCcmInfo __xdata *ccmInfo) __reentrant
{
uint8_t done;
//create block 0
aesCcmPrvCopyNonceSetHalfword(ccmInfo->authSrcLen ? 0x49 : 0x09, ccmInfo->nonce, ccmInfo->encDataLen);
//encrypt it
aesEnc(mBlockOut);
if (ccmInfo->authSrcLen) {
uint8_t __xdata *blk = mBlockIn;
uint8_t now, already = 2;
*blk++ = 0; //authSrcLen.hi
*blk++ = ccmInfo->authSrcLen;
now = 14; //since we already used 2
done = 0;
while (done < ccmInfo->authSrcLen) {
uint8_t i;
if (now > (uint8_t)(ccmInfo->authSrcLen - done))
now = (uint8_t)(ccmInfo->authSrcLen - done);
xMemCopyShort(blk, src, now);
src += now;
xMemSet(blk + now, 0, 16 - already - now);
for (i = 0; i < 16; i++)
mBlockOut[i] ^= mBlockIn[i];
aesEnc(mBlockOut);
blk = mBlockIn;
done += now;
now = 16;
already = 0;
}
}
done = 0;
while (done < ccmInfo->encDataLen) {
uint8_t i, now;
now = 16;
if (now > ccmInfo->encDataLen - done)
now = ccmInfo->encDataLen - done;
for (i = 0; i < now; i++)
mBlockOut[i] ^= *src++;
done += now;
aesEnc(mBlockOut);
}
}
void aesCcmEnc(void __xdata *dstP, const void __xdata *srcP, const struct AesCcmInfo __xdata *ccmInfo) __reentrant
{
const uint8_t __xdata *src = (const uint8_t __xdata*)srcP;
uint8_t __xdata *dst = (uint8_t*)dstP;
uint8_t i, done = 0, now, ctr = 0;
aesSetKey(ccmInfo->key);
//it goes after encrypted data
aesCcmPrvCalcUnencryptedMic(src, ccmInfo);
xMemCopyShort(mMic, mBlockOut, sizeof(mMic));
//copy authed data
xMemCopyShort(dst, src, ccmInfo->authSrcLen);
src += ccmInfo->authSrcLen;
dst += ccmInfo->authSrcLen;
//now we encrypt
now = 0; //first block not used
while (done < ccmInfo->encDataLen) {
if (now > (uint8_t)(ccmInfo->encDataLen - done))
now = (uint8_t)(ccmInfo->encDataLen - done);
aesCcmPrvCopyNonceSetHalfword(1, ccmInfo->nonce, ctr++);
aesEnc(mBlockOut);
if (!now) //first block
for (i = 0; i < sizeof(mMic); i++)
mMic[i] ^= mBlockOut[i];
else {
for (i = 0; i < now; i++)
*dst++ = *src++ ^ mBlockOut[i];
}
done += now;
now = 16;
}
xMemCopyShort(dst, mMic, sizeof(mMic));
}
__bit aesCcmDec(void __xdata *dstP, const void __xdata *srcP, const struct AesCcmInfo __xdata *ccmInfo) __reentrant
{
const uint8_t __xdata *src = (const uint8_t __xdata*)srcP;
uint8_t __xdata *dst = (uint8_t*)dstP;
uint8_t i, done, now, ctr = 0;
aesSetKey(ccmInfo->key);
//copy authed data
xMemCopyShort(dst, src, ccmInfo->authSrcLen);
src += ccmInfo->authSrcLen;
dst += ccmInfo->authSrcLen;
//then we decrypt
done = 0;
now = 0; //first block not used
while (done < ccmInfo->encDataLen) {
if (now > (uint8_t)(ccmInfo->encDataLen - done))
now = (uint8_t)(ccmInfo->encDataLen - done);
aesCcmPrvCopyNonceSetHalfword(1, ccmInfo->nonce, ctr++);
aesEnc(mBlockOut);
if (!now) { //first block
//given mic is after data
for (i = 0; i < sizeof(mMic); i++)
mMic[i] = src[ccmInfo->encDataLen + i] ^ mBlockOut[i];
}
else {
for (i = 0; i < now; i++)
*dst++ = *src++ ^ mBlockOut[i];
}
done += now;
now = 16;
}
aesCcmPrvCalcUnencryptedMic(dstP, ccmInfo);
return xMemEqual(mMic, mBlockOut, sizeof(mMic));
}