summaryrefslogtreecommitdiff
path: root/chip/host/dcrypto/aes.c
blob: 4556b4b5ddc4bf8763b7c35205c4ed6109f1b70c (plain)
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
/* Copyright 2018 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <openssl/evp.h>

#define HIDE_EC_STDLIB

#include "dcrypto.h"
#include "registers.h"

enum dcrypto_result DCRYPTO_aes_ctr(uint8_t *out, const uint8_t *key,
				    uint32_t key_bits, const uint8_t *iv,
				    const uint8_t *in, size_t in_len)
{
	EVP_CIPHER_CTX *ctx;
	enum dcrypto_result ret = DCRYPTO_FAIL;
	int out_len = 0;

	ctx = EVP_CIPHER_CTX_new();
	if (!ctx)
		return DCRYPTO_FAIL;

	if (EVP_EncryptInit_ex(ctx, EVP_aes_256_ctr(), NULL, key, iv) != 1)
		goto cleanup;

	if (EVP_EncryptUpdate(ctx, out, &out_len, in, in_len) != 1)
		goto cleanup;

	if (EVP_EncryptFinal(ctx, out + out_len, &out_len) != 1)
		goto cleanup;
	ret = DCRYPTO_OK;

cleanup:
	EVP_CIPHER_CTX_free(ctx);
	return ret;
}