1 /*
2 * Copyright (c) 2001-2019, Arm Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <stdint.h>
11 #include <string.h>
12
13 #include <limits.h>
14
15 /* cc lib */
16 #include "cc_aes_defs.h"
17 #include "cc_regs.h"
18 #include "dx_crys_kernel.h"
19 #include "cc_hal_plat.h"
20 #if defined(CC_CONFIG_SUPPORT_EXT_DMA)
21 #include "mbedtls_aes_ext_dma.h"
22 #endif
23
24 /* mbedtls lib */
25 #include "mbedtls/cipher.h"
26 #include "mbedtls/timing.h"
27
28 /* local */
29 #include "run_integration_pal_log.h"
30 #include "run_integration_test.h"
31 #include "run_integration_helper.h"
32
33 /************************************************************
34 *
35 * static function prototypes
36 *
37 ************************************************************/
38 static RunItError_t runIt_extDma(void);
39
40 /************************************************************
41 *
42 * static functions
43 *
44 ************************************************************/
runIt_extDma(void)45 static RunItError_t runIt_extDma(void)
46 {
47 RunItError_t rc = RUNIT_ERROR__OK;
48 #if defined(CC_CONFIG_SUPPORT_EXT_DMA)
49 /* https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/AES_CTR.pdf */
50 static const uint8_t KEY[] = { 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE, 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81, 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7, 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4 };
51 static const uint8_t PLAIN[] = { 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96, 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A };
52 static const uint8_t CYPHER[] = { 0x60, 0x1E, 0xC3, 0x13, 0x77, 0x57, 0x89, 0xA5, 0xB7, 0xA7, 0xF5, 0x04, 0xBB, 0xF3, 0xD2, 0x28 };
53 static const uint8_t NONCE[] = { 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF };
54
55 uint32_t dataLen = sizeof(PLAIN);
56 size_t keySize = sizeof(KEY);
57 uint8_t *dataInBuff = NULL;
58 uint32_t *dataInWords = NULL;
59 uint32_t *outputBuffer = NULL;
60 uint32_t k = 0, i =0;
61 uint32_t sizeInWords = 0;
62 uint32_t wordsToRW = 0;
63 CCAesIv_t ivBuff = {0};
64 uint8_t keyBuff[CC_AES_KEY_MAX_SIZE_IN_BYTES] = {0};
65
66 RunItPtr dataInBuffPtr;
67 RunItPtr outputBufferPtr;
68 RunItPtr dataInWordsPtr;
69
70 const char* TEST_NAME = "External DMA";
71 RUNIT_SUB_TEST_START(TEST_NAME);
72
73 sizeInWords = (dataLen + 3) / sizeof(uint32_t);
74 /*
75 * you DO NOT have to allocate the buffers in a DMAble address space.
76 */
77 ALLOC_AND_COPY(dataInBuffPtr, dataInBuff, PLAIN, dataLen);
78 ALLOC32(outputBufferPtr, outputBuffer, dataLen);
79 ALLOC32(dataInWordsPtr, dataInWords, sizeInWords * sizeof(uint32_t));
80
81 memcpy(ivBuff, NONCE, sizeof(ivBuff));
82 memcpy(keyBuff, KEY, sizeof(keyBuff));
83
84 RUNIT_ASSERT_WITH_RESULT(mbedtls_aes_ext_dma_init(keySize * 8, CC_AES_ENCRYPT, CC_AES_MODE_CTR), CC_OK);
85 RUNIT_ASSERT_WITH_RESULT(mbedtls_aes_ext_dma_set_key(CC_AES_MODE_CTR, keyBuff, keySize * 8), CC_OK);
86 RUNIT_ASSERT_WITH_RESULT(mbedtls_aes_ext_dma_set_iv(CC_AES_MODE_CTR, ivBuff, 16), CC_OK);
87 RUNIT_ASSERT_WITH_RESULT(mbedtls_aes_ext_dma_set_data_size(dataLen, CC_AES_MODE_CTR), CC_OK);
88
89 memset(dataInWords, 0, sizeInWords * sizeof(uint32_t));
90 memcpy(dataInWords, dataInBuff, dataLen);
91
92 // DATA DIN and DATA DOUT are written and read in 4 Words chunks.
93 while (k < sizeInWords)
94 {
95 if (sizeInWords - k > 4)
96 {
97 wordsToRW = 4;
98 }
99 else
100 {
101 wordsToRW = (sizeInWords - k);
102 }
103
104 for (i = 0; i < wordsToRW; i++)
105 {
106 CC_HAL_WRITE_REGISTER(CC_REG_OFFSET(HOST_RGF, DIN_BUFFER), dataInWords[i + k]);
107 }
108
109 for (i = 0; i < wordsToRW; i++)
110 {
111 outputBuffer[i + k] = CC_HAL_READ_REGISTER(CC_REG_OFFSET(HOST_RGF, DOUT_BUFFER));
112 }
113
114 k += 4;
115 }
116
117 RUNIT_ASSERT_WITH_RESULT(mbedtls_aes_ext_dma_finish(CC_AES_MODE_CTR, ivBuff, 16), CC_OK);
118
119 RUNIT_PRINT_BUF(outputBuffer, sizeInWords * sizeof(uint32_t), "outputBuffer");
120 RUNIT_ASSERT(memcmp(outputBuffer, CYPHER, sizeInWords * sizeof(uint32_t)) == 0);
121
122 bail:
123
124 FREE_IF_NOT_NULL(dataInBuffPtr);
125 FREE_IF_NOT_NULL(outputBufferPtr);
126 FREE_IF_NOT_NULL(dataInWordsPtr);
127
128 RUNIT_SUB_TEST_RESULT_W_PARAMS(TEST_NAME, "KEY[%"PRIu32"b] PLAIN[%"PRIu32"B]",
129 (uint32_t)keySize, (uint32_t)dataLen);
130 #endif /* CC_CONFIG_SUPPORT_EXT_DMA */
131 return rc;
132 }
133
134 /************************************************************
135 *
136 * public functions
137 *
138 ************************************************************/
runIt_extDmaTest(void)139 RunItError_t runIt_extDmaTest(void)
140 {
141 RunItError_t rc = RUNIT_ERROR__OK;
142
143 const char* TEST_NAME = "External DMA";
144 RUNIT_TEST_START(TEST_NAME);
145
146 RUNIT_ASSERT(runIt_extDma() == RUNIT_ERROR__OK);
147
148 bail:
149
150 RUNIT_TEST_RESULT(TEST_NAME);
151 return rc;
152 }
153