Etienne Carriere | 7514117 | 2020-05-16 11:58:23 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: BSD-2-Clause |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014, STMicroelectronics International N.V. |
| 4 | * All rights reserved. |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 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 | |
| 21 | TEE_Result ta_entry_aes256ecb_encrypt(uint32_t param_types, TEE_Param params[4]) |
| 22 | { |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame] | 23 | size_t n_input_blocks = 0; |
| 24 | size_t i = 0; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 25 | |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame] | 26 | /* |
| 27 | * It is expected that memRef[0] is input buffer and memRef[1] is |
| 28 | * output buffer. |
| 29 | */ |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 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 | |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame] | 37 | /* Check that input buffer is whole mult. of block size, in bits */ |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 38 | if ((params[0].memref.size << 8) % AES_BLOCK_SIZE != 0) |
| 39 | return TEE_ERROR_BAD_PARAMETERS; |
| 40 | |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame] | 41 | /* Check that output buffer is whole mult. of block size, in bits */ |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 42 | if ((params[1].memref.size << 8) % AES_BLOCK_SIZE != 0) |
| 43 | return TEE_ERROR_BAD_PARAMETERS; |
| 44 | |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame] | 45 | /* Set up for encryption */ |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 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 | |
| 62 | TEE_Result ta_entry_aes256ecb_decrypt(uint32_t param_types, TEE_Param params[4]) |
| 63 | { |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame] | 64 | size_t n_input_blocks = 0; |
| 65 | size_t i = 0; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 66 | |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame] | 67 | /* |
| 68 | * It is expected that memRef[0] is input buffer and memRef[1] is |
| 69 | * output buffer. |
| 70 | */ |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 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 | |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame] | 78 | /* Check that input buffer is whole mult. of block size, in bits */ |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 79 | if ((params[0].memref.size << 8) % AES_BLOCK_SIZE != 0) |
| 80 | return TEE_ERROR_BAD_PARAMETERS; |
| 81 | |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame] | 82 | /* Check that output buffer is whole mult. of block size, in bits */ |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 83 | if ((params[1].memref.size << 8) % AES_BLOCK_SIZE != 0) |
| 84 | return TEE_ERROR_BAD_PARAMETERS; |
| 85 | |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame] | 86 | /* Set up for decryption */ |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 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 | } |