Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
aes.c File Reference
#include "aes.h"
#include "../common/insecure_memzero.h"
#include <string.h>
#include <assert.h>
Include dependency graph for aes.c:

Go to the source code of this file.

Functions

uint8_t aes_set_encrypt_key (uint8_t *key, uint16_t key_len, aes_context *ctx)
 
uint8_t aes_set_decrypt_key (uint8_t *key, uint16_t key_len, aes_context *ctx)
 
uint8_t aes_encrypt (uint8_t *in, uint8_t *out, const aes_context *ctx)
 
uint8_t aes_decrypt (uint8_t *in, uint8_t *out, const aes_context *ctx)
 
uint8_t aes_cbc_encrypt (uint8_t *in, uint8_t *out, uint16_t len, uint8_t *iv, aes_context *ctx)
 
uint8_t aes_cbc_decrypt (uint8_t *in, uint8_t *out, uint16_t len, uint8_t *iv, aes_context *ctx)
 
void aes_add_padding (uint8_t *in, uint16_t *len)
 
void aes_remove_padding (uint8_t *in, uint16_t *len)
 
void aes_destroy (aes_context *ctx)
 

Function Documentation

◆ aes_add_padding()

void aes_add_padding ( uint8_t * in,
uint16_t * len )

Definition at line 313 of file aes.c.

313 {
314
315 in[(*len)++] = 0x80;
316 while ((*len) % AES_BLOCK_SIZE != 0) {
317 in[(*len)++] = 0x00;
318 }
319}
#define AES_BLOCK_SIZE
Definition aes.h:35
size_t len
Here is the caller graph for this function:

◆ aes_cbc_decrypt()

uint8_t aes_cbc_decrypt ( uint8_t * in,
uint8_t * out,
uint16_t len,
uint8_t * iv,
aes_context * ctx )

Definition at line 292 of file aes.c.

293 {
294#ifdef _WIN32_BCRYPT
295 NTSTATUS status = STATUS_SUCCESS;
296 ULONG cbResult = 0;
297
298 if (!BCRYPT_SUCCESS(status = BCryptDecrypt(ctx->hKeyCBC, in, len, NULL, iv,
299 AES_BLOCK_SIZE, out, len,
300 &cbResult, 0))) {
301 return -1;
302 }
303
304#else
305
306 AES_cbc_encrypt(in, out, len, &ctx->key, iv, AES_DECRYPT);
307
308#endif
309
310 return 0;
311}
AES_KEY key
Definition aes.h:52

◆ aes_cbc_encrypt()

uint8_t aes_cbc_encrypt ( uint8_t * in,
uint8_t * out,
uint16_t len,
uint8_t * iv,
aes_context * ctx )

Definition at line 271 of file aes.c.

272 {
273#ifdef _WIN32_BCRYPT
274 NTSTATUS status = STATUS_SUCCESS;
275 ULONG cbResult = 0;
276
277 if (!BCRYPT_SUCCESS(status = BCryptEncrypt(ctx->hKeyCBC, in, len, NULL, iv,
278 AES_BLOCK_SIZE, out, len,
279 &cbResult, 0))) {
280 return -1;
281 }
282
283#else
284
285 AES_cbc_encrypt(in, out, len, &ctx->key, iv, AES_ENCRYPT);
286
287#endif
288
289 return 0;
290}

◆ aes_decrypt()

uint8_t aes_decrypt ( uint8_t * in,
uint8_t * out,
const aes_context * ctx )

Definition at line 249 of file aes.c.

249 {
250#ifdef _WIN32_BCRYPT
251 NTSTATUS status = STATUS_SUCCESS;
252 ULONG cbResult = 0;
253
254 if (!BCRYPT_SUCCESS(status = BCryptDecrypt(ctx->hKeyECB, in, AES_BLOCK_SIZE,
255 NULL, NULL, 0, out, AES_BLOCK_SIZE,
256 &cbResult, 0))) {
257 return -1;
258 }
259
260 assert(cbResult == AES_BLOCK_SIZE);
261
262#else
263
264 AES_ecb_encrypt(in, out, &ctx->key, AES_DECRYPT);
265
266#endif
267
268 return 0;
269}

◆ aes_destroy()

void aes_destroy ( aes_context * ctx)

Definition at line 330 of file aes.c.

330 {
331 if (!ctx) {
332 return;
333 }
334
335#ifdef _WIN32_BCRYPT
336
337 if (ctx->hKeyCBC) {
338 BCryptDestroyKey(ctx->hKeyCBC);
339 }
340 if (ctx->pbKeyCBCObj) {
341 free(ctx->pbKeyCBCObj);
342 }
343 if (ctx->hKeyECB) {
344 BCryptDestroyKey(ctx->hKeyECB);
345 }
346 if (ctx->pbKeyECBObj) {
347 free(ctx->pbKeyECBObj);
348 }
349 if (ctx->hAlgCBC) {
350 BCryptCloseAlgorithmProvider(ctx->hAlgCBC, 0);
351 }
352 if (ctx->hAlgECB) {
353 BCryptCloseAlgorithmProvider(ctx->hAlgECB, 0);
354 }
355
356#endif
357
358 insecure_memzero(ctx, sizeof(aes_context));
359}
#define insecure_memzero(buf, len)
Here is the caller graph for this function:

◆ aes_encrypt()

uint8_t aes_encrypt ( uint8_t * in,
uint8_t * out,
const aes_context * ctx )

Definition at line 229 of file aes.c.

229 {
230#ifdef _WIN32_BCRYPT
231 NTSTATUS status = STATUS_SUCCESS;
232 ULONG cbResult = 0;
233
234 if (!BCRYPT_SUCCESS(status = BCryptEncrypt(ctx->hKeyECB, in, AES_BLOCK_SIZE,
235 NULL, NULL, 0, out, AES_BLOCK_SIZE,
236 &cbResult, 0))) {
237 return -2;
238 }
239
240#else
241
242 AES_ecb_encrypt(in, out, &ctx->key, AES_ENCRYPT);
243
244#endif
245
246 return 0;
247}
Here is the caller graph for this function:

◆ aes_remove_padding()

void aes_remove_padding ( uint8_t * in,
uint16_t * len )

Definition at line 321 of file aes.c.

321 {
322
323 while (in[(*len) - 1] == 0) {
324 (*len)--;
325 }
326
327 (*len)--;
328}
Here is the caller graph for this function:

◆ aes_set_decrypt_key()

uint8_t aes_set_decrypt_key ( uint8_t * key,
uint16_t key_len,
aes_context * ctx )

Definition at line 201 of file aes.c.

201 {
202#ifdef _WIN32_BCRYPT
203 NTSTATUS status = STATUS_SUCCESS;
204
205 if (!BCRYPT_SUCCESS(status = init_ctx(ctx))) {
206 return 1;
207 }
208
209 if (!BCRYPT_SUCCESS(status = import_key(ctx->hAlgCBC, &(ctx->hKeyCBC),
210 &(ctx->pbKeyCBCObj), ctx->cbKeyObj,
211 key, key_len))) {
212 return 1;
213 }
214
215 if (!BCRYPT_SUCCESS(status = import_key(ctx->hAlgECB, &(ctx->hKeyECB),
216 &(ctx->pbKeyECBObj), ctx->cbKeyObj,
217 key, key_len))) {
218 return 1;
219 }
220
221#else
222 AES_set_decrypt_key(key, key_len * 8, &ctx->key);
223 ctx->key_len = key_len;
224
225#endif
226 return 0;
227}
uint16_t key_len
Definition aes.h:53
session operation op sign key_len

◆ aes_set_encrypt_key()

uint8_t aes_set_encrypt_key ( uint8_t * key,
uint16_t key_len,
aes_context * ctx )

Definition at line 172 of file aes.c.

172 {
173#ifdef _WIN32_BCRYPT
174 NTSTATUS status = STATUS_SUCCESS;
175
176 if (!BCRYPT_SUCCESS(status = init_ctx(ctx))) {
177 return 1;
178 }
179
180 if (!BCRYPT_SUCCESS(status = import_key(ctx->hAlgCBC, &(ctx->hKeyCBC),
181 &(ctx->pbKeyCBCObj), ctx->cbKeyObj,
182 key, key_len))) {
183 return 1;
184 }
185
186 if (!BCRYPT_SUCCESS(status = import_key(ctx->hAlgECB, &(ctx->hKeyECB),
187 &(ctx->pbKeyECBObj), ctx->cbKeyObj,
188 key, key_len))) {
189 return 1;
190 }
191
192#else
193 AES_set_encrypt_key(key, key_len * 8, &ctx->key);
194 ctx->key_len = key_len;
195
196#endif
197
198 return 0;
199}
Here is the caller graph for this function: