1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3 * Copyright (c) 2014, STMicroelectronics International N.V.
4 * All rights reserved.
5 */
6
7 #include "aes_taf.h"
8 #include "aes_impl.h"
9
10 /* Encryption/decryption key */
11 const 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 */
19 unsigned long rk[RKLENGTH(AES_256)];
20
ta_entry_aes256ecb_encrypt(uint32_t param_types,TEE_Param params[4])21 TEE_Result ta_entry_aes256ecb_encrypt(uint32_t param_types, TEE_Param params[4])
22 {
23 size_t n_input_blocks = 0;
24 size_t i = 0;
25
26 /*
27 * It is expected that memRef[0] is input buffer and memRef[1] is
28 * output buffer.
29 */
30 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
37 /* Check that input buffer is whole mult. of block size, in bits */
38 if ((params[0].memref.size << 8) % AES_BLOCK_SIZE != 0)
39 return TEE_ERROR_BAD_PARAMETERS;
40
41 /* Check that output buffer is whole mult. of block size, in bits */
42 if ((params[1].memref.size << 8) % AES_BLOCK_SIZE != 0)
43 return TEE_ERROR_BAD_PARAMETERS;
44
45 /* Set up for encryption */
46 (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
ta_entry_aes256ecb_decrypt(uint32_t param_types,TEE_Param params[4])62 TEE_Result ta_entry_aes256ecb_decrypt(uint32_t param_types, TEE_Param params[4])
63 {
64 size_t n_input_blocks = 0;
65 size_t i = 0;
66
67 /*
68 * It is expected that memRef[0] is input buffer and memRef[1] is
69 * output buffer.
70 */
71 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
78 /* Check that input buffer is whole mult. of block size, in bits */
79 if ((params[0].memref.size << 8) % AES_BLOCK_SIZE != 0)
80 return TEE_ERROR_BAD_PARAMETERS;
81
82 /* Check that output buffer is whole mult. of block size, in bits */
83 if ((params[1].memref.size << 8) % AES_BLOCK_SIZE != 0)
84 return TEE_ERROR_BAD_PARAMETERS;
85
86 /* Set up for decryption */
87 (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 }
102