lib/crypto: optimize aes_ccm_128
[samba.git] / lib / crypto / aes_ccm_128.c
1 /*
2    AES-CCM-128 (rfc 3610)
3
4    Copyright (C) Stefan Metzmacher 2012
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "replace.h"
21 #include "../lib/crypto/crypto.h"
22 #include "lib/util/byteorder.h"
23
24 #define M_ ((AES_CCM_128_M - 2) / 2)
25 #define L_ (AES_CCM_128_L - 1)
26
27 void aes_ccm_128_init(struct aes_ccm_128_context *ctx,
28                       const uint8_t K[AES_BLOCK_SIZE],
29                       const uint8_t N[AES_CCM_128_NONCE_SIZE],
30                       size_t a_total, size_t m_total)
31 {
32         ZERO_STRUCTP(ctx);
33
34         AES_set_encrypt_key(K, 128, &ctx->aes_key);
35         memcpy(ctx->nonce, N, AES_CCM_128_NONCE_SIZE);
36         ctx->a_remain = a_total;
37         ctx->m_remain = m_total;
38
39         /*
40          * prepare B_0
41          */
42         ctx->B_i[0]  = L_;
43         ctx->B_i[0] += 8 * M_;
44         if (a_total > 0) {
45                 ctx->B_i[0] += 64;
46         }
47         memcpy(&ctx->B_i[1], ctx->nonce, AES_CCM_128_NONCE_SIZE);
48         RSIVAL(ctx->B_i, (AES_BLOCK_SIZE - AES_CCM_128_L), m_total);
49
50         /*
51          * prepare X_1
52          */
53         AES_encrypt(ctx->B_i, ctx->X_i, &ctx->aes_key);
54
55         /*
56          * prepare B_1
57          */
58         ZERO_STRUCT(ctx->B_i);
59         if (a_total >= UINT32_MAX) {
60                 RSSVAL(ctx->B_i, 0, 0xFFFF);
61                 RSBVAL(ctx->B_i, 2, (uint64_t)a_total);
62                 ctx->B_i_ofs = 10;
63         } else if (a_total >= 0xFF00) {
64                 RSSVAL(ctx->B_i, 0, 0xFFFE);
65                 RSIVAL(ctx->B_i, 2, a_total);
66                 ctx->B_i_ofs = 6;
67         } else if (a_total > 0) {
68                 RSSVAL(ctx->B_i, 0, a_total);
69                 ctx->B_i_ofs = 2;
70         }
71
72         /*
73          * prepare A_i
74          */
75         ctx->A_i[0]  = L_;
76         memcpy(&ctx->A_i[1], ctx->nonce, AES_CCM_128_NONCE_SIZE);
77
78         ctx->S_i_ofs = AES_BLOCK_SIZE;
79 }
80
81 void aes_ccm_128_update(struct aes_ccm_128_context *ctx,
82                         const uint8_t *v, size_t v_len)
83 {
84         size_t *remain;
85
86         if (v_len == 0) {
87                 return;
88         }
89
90         if (ctx->a_remain > 0) {
91                 remain = &ctx->a_remain;
92         } else {
93                 remain = &ctx->m_remain;
94         }
95
96         if (unlikely(v_len > *remain)) {
97                 abort();
98         }
99
100         if (ctx->B_i_ofs > 0) {
101                 size_t n = MIN(AES_BLOCK_SIZE - ctx->B_i_ofs, v_len);
102
103                 memcpy(&ctx->B_i[ctx->B_i_ofs], v, n);
104                 v += n;
105                 v_len -= n;
106                 ctx->B_i_ofs += n;
107                 *remain -= n;
108         }
109
110         if ((ctx->B_i_ofs == AES_BLOCK_SIZE) || (*remain == 0)) {
111                 aes_block_xor(ctx->X_i, ctx->B_i, ctx->B_i);
112                 AES_encrypt(ctx->B_i, ctx->X_i, &ctx->aes_key);
113                 ctx->B_i_ofs = 0;
114         }
115
116         while (v_len >= AES_BLOCK_SIZE) {
117                 aes_block_xor(ctx->X_i, v, ctx->B_i);
118                 AES_encrypt(ctx->B_i, ctx->X_i, &ctx->aes_key);
119                 v += AES_BLOCK_SIZE;
120                 v_len -= AES_BLOCK_SIZE;
121                 *remain -= AES_BLOCK_SIZE;
122         }
123
124         if (v_len > 0) {
125                 ZERO_STRUCT(ctx->B_i);
126                 memcpy(ctx->B_i, v, v_len);
127                 ctx->B_i_ofs += v_len;
128                 *remain -= v_len;
129                 v = NULL;
130                 v_len = 0;
131         }
132
133         if (*remain > 0) {
134                 return;
135         }
136
137         if (ctx->B_i_ofs > 0) {
138                 aes_block_xor(ctx->X_i, ctx->B_i, ctx->B_i);
139                 AES_encrypt(ctx->B_i, ctx->X_i, &ctx->aes_key);
140                 ctx->B_i_ofs = 0;
141         }
142 }
143
144 static inline void aes_ccm_128_S_i(struct aes_ccm_128_context *ctx,
145                                    uint8_t S_i[AES_BLOCK_SIZE],
146                                    size_t i)
147 {
148         RSIVAL(ctx->A_i, (AES_BLOCK_SIZE - AES_CCM_128_L), i);
149         AES_encrypt(ctx->A_i, S_i, &ctx->aes_key);
150 }
151
152 void aes_ccm_128_crypt(struct aes_ccm_128_context *ctx,
153                        uint8_t *m, size_t m_len)
154 {
155         while (m_len > 0) {
156                 if (ctx->S_i_ofs == AES_BLOCK_SIZE) {
157                         ctx->S_i_ctr += 1;
158                         aes_ccm_128_S_i(ctx, ctx->S_i, ctx->S_i_ctr);
159                         ctx->S_i_ofs = 0;
160                 }
161
162                 if (likely(ctx->S_i_ofs == 0 && m_len >= AES_BLOCK_SIZE)) {
163                         aes_block_xor(m, ctx->S_i, m);
164                         m += AES_BLOCK_SIZE;
165                         m_len -= AES_BLOCK_SIZE;
166                         ctx->S_i_ctr += 1;
167                         aes_ccm_128_S_i(ctx, ctx->S_i, ctx->S_i_ctr);
168                         continue;
169                 }
170
171                 m[0] ^= ctx->S_i[ctx->S_i_ofs];
172                 m += 1;
173                 m_len -= 1;
174                 ctx->S_i_ofs += 1;
175         }
176 }
177
178 void aes_ccm_128_digest(struct aes_ccm_128_context *ctx,
179                         uint8_t digest[AES_BLOCK_SIZE])
180 {
181         if (unlikely(ctx->a_remain != 0)) {
182                 abort();
183         }
184         if (unlikely(ctx->m_remain != 0)) {
185                 abort();
186         }
187
188         /* prepare S_0 */
189         aes_ccm_128_S_i(ctx, ctx->S_i, 0);
190
191         /*
192          * note X_i is T here
193          */
194         aes_block_xor(ctx->X_i, ctx->S_i, digest);
195
196         ZERO_STRUCTP(ctx);
197 }