blob: 5744014acf96a4bcef0930640f0f45bf8e072ae7 [file] [log] [blame]
Etienne Carriere75141172020-05-16 11:58:23 +02001// SPDX-License-Identifier: BSD-2-Clause
Pascal Brandc639ac82015-07-02 08:53:34 +02002/*
3 * Copyright (c) 2014, STMicroelectronics International N.V.
4 * All rights reserved.
Pascal Brandc639ac82015-07-02 08:53:34 +02005 */
6
7#include "aes_taf.h"
8#include "aes_impl.h"
9
10/* Encryption/decryption key */
11const unsigned char key[KEYLENGTH(AES_256)] = {
12 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
13 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
14 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
15 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
16};
17
18/* Encryption/decryption buffer */
19unsigned long rk[RKLENGTH(AES_256)];
20
21TEE_Result ta_entry_aes256ecb_encrypt(uint32_t param_types, TEE_Param params[4])
22{
Etienne Carriere102092e2019-03-28 15:24:22 +010023 size_t n_input_blocks = 0;
24 size_t i = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +020025
Etienne Carriere102092e2019-03-28 15:24:22 +010026 /*
27 * It is expected that memRef[0] is input buffer and memRef[1] is
28 * output buffer.
29 */
Pascal Brandc639ac82015-07-02 08:53:34 +020030 if (param_types !=
31 TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
32 TEE_PARAM_TYPE_MEMREF_OUTPUT, TEE_PARAM_TYPE_NONE,
33 TEE_PARAM_TYPE_NONE)) {
34 return TEE_ERROR_BAD_PARAMETERS;
35 }
36
Etienne Carriere102092e2019-03-28 15:24:22 +010037 /* Check that input buffer is whole mult. of block size, in bits */
Pascal Brandc639ac82015-07-02 08:53:34 +020038 if ((params[0].memref.size << 8) % AES_BLOCK_SIZE != 0)
39 return TEE_ERROR_BAD_PARAMETERS;
40
Etienne Carriere102092e2019-03-28 15:24:22 +010041 /* Check that output buffer is whole mult. of block size, in bits */
Pascal Brandc639ac82015-07-02 08:53:34 +020042 if ((params[1].memref.size << 8) % AES_BLOCK_SIZE != 0)
43 return TEE_ERROR_BAD_PARAMETERS;
44
Etienne Carriere102092e2019-03-28 15:24:22 +010045 /* Set up for encryption */
Pascal Brandc639ac82015-07-02 08:53:34 +020046 (void)rijndaelSetupEncrypt(rk, key, AES_256);
47
48 n_input_blocks = params[0].memref.size / (AES_BLOCK_SIZE / 8);
49
50 for (i = 0; i < n_input_blocks; i++) {
51 const unsigned char *ciphertext = params[0].memref.buffer;
52 unsigned char *plaintext = params[1].memref.buffer;
53
54 rijndaelEncrypt(rk, NROUNDS(AES_256),
55 &ciphertext[i * (AES_BLOCK_SIZE / 8)],
56 &plaintext[i * (AES_BLOCK_SIZE / 8)]);
57 }
58
59 return TEE_SUCCESS;
60}
61
62TEE_Result ta_entry_aes256ecb_decrypt(uint32_t param_types, TEE_Param params[4])
63{
Etienne Carriere102092e2019-03-28 15:24:22 +010064 size_t n_input_blocks = 0;
65 size_t i = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +020066
Etienne Carriere102092e2019-03-28 15:24:22 +010067 /*
68 * It is expected that memRef[0] is input buffer and memRef[1] is
69 * output buffer.
70 */
Pascal Brandc639ac82015-07-02 08:53:34 +020071 if (param_types !=
72 TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
73 TEE_PARAM_TYPE_MEMREF_OUTPUT, TEE_PARAM_TYPE_NONE,
74 TEE_PARAM_TYPE_NONE)) {
75 return TEE_ERROR_BAD_PARAMETERS;
76 }
77
Etienne Carriere102092e2019-03-28 15:24:22 +010078 /* Check that input buffer is whole mult. of block size, in bits */
Pascal Brandc639ac82015-07-02 08:53:34 +020079 if ((params[0].memref.size << 8) % AES_BLOCK_SIZE != 0)
80 return TEE_ERROR_BAD_PARAMETERS;
81
Etienne Carriere102092e2019-03-28 15:24:22 +010082 /* Check that output buffer is whole mult. of block size, in bits */
Pascal Brandc639ac82015-07-02 08:53:34 +020083 if ((params[1].memref.size << 8) % AES_BLOCK_SIZE != 0)
84 return TEE_ERROR_BAD_PARAMETERS;
85
Etienne Carriere102092e2019-03-28 15:24:22 +010086 /* Set up for decryption */
Pascal Brandc639ac82015-07-02 08:53:34 +020087 (void)rijndaelSetupDecrypt(rk, key, AES_256);
88
89 n_input_blocks = params[0].memref.size / (AES_BLOCK_SIZE / 8);
90
91 for (i = 0; i < n_input_blocks; i++) {
92 const unsigned char *ciphertext = params[0].memref.buffer;
93 unsigned char *plaintext = params[1].memref.buffer;
94
95 rijndaelDecrypt(rk, NROUNDS(AES_256),
96 &ciphertext[i * (AES_BLOCK_SIZE / 8)],
97 &plaintext[i * (AES_BLOCK_SIZE / 8)]);
98 }
99
100 return TEE_SUCCESS;
101}