1 /*
2 * Copyright (c) 2021-2023, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8 /** \file cc3xx_internal_aes.c
9 *
10 * This file contains the implementation of the internal functions to
11 * perform symmetric encryption and decryption using the AES algorithm
12 *
13 */
14
15 #include "cc3xx_internal_aes.h"
16
17 #include "cc_pal_types.h"
18 #include "cc_pal_mem.h"
19 #include "cc_pal_abort.h"
20
21 /**
22 * \ingroup internal_aes
23 */
cc3xx_aes_init(AesContext_t * ctx)24 void cc3xx_aes_init(AesContext_t *ctx)
25 {
26 if (NULL == ctx) {
27 CC_PalAbort("ctx cannot be NULL");
28 return;
29 }
30
31 ctx->padType = CRYPTO_PADDING_NONE;
32 ctx->dataBlockType = FIRST_BLOCK;
33 ctx->inputDataAddrType = DLLI_ADDR;
34 ctx->outputDataAddrType = DLLI_ADDR;
35 }
36
37 /**
38 * \ingroup internal_aes
39 */
cc3xx_aes_free(AesContext_t * ctx)40 void cc3xx_aes_free(AesContext_t *ctx)
41 {
42 if (NULL == ctx) {
43 CC_PAL_LOG_ERR("ctx cannot be NULL");
44 return;
45 }
46
47 CC_PalMemSet(ctx, 0, sizeof(AesContext_t));
48 }
49
aes_setkey(AesContext_t * ctx,const uint8_t * key,size_t key_bits,cryptoDirection_t direction)50 static psa_status_t aes_setkey(
51 AesContext_t *ctx,
52 const uint8_t *key,
53 size_t key_bits,
54 cryptoDirection_t direction)
55 {
56 if ((NULL == ctx) || (NULL == key)) {
57 CC_PAL_LOG_ERR("ctx or key cannot be NULL");
58 return PSA_ERROR_INVALID_ARGUMENT;
59 }
60
61 ctx->dir = direction;
62 ctx->cryptoKey = USER_KEY;
63
64 switch (key_bits) {
65 case 128:
66 ctx->keySizeId = KEY_SIZE_128_BIT;
67 break;
68 case 192:
69 ctx->keySizeId = KEY_SIZE_192_BIT;
70 break;
71 case 256:
72 ctx->keySizeId = KEY_SIZE_256_BIT;
73 break;
74 default:
75 CC_PAL_LOG_ERR("key_bits (%d) not supported", key_bits);
76 return PSA_ERROR_INVALID_ARGUMENT;
77 }
78
79 CC_PalMemCopy(ctx->keyBuf, key, key_bits/8);
80
81 return PSA_SUCCESS;
82 }
83
84 /** \defgroup internal_aes Internal AES module
85 *
86 * Internal functions used by the driver to perform AES cipher encryption
87 * and decryption
88 *
89 * @{
90 */
cc3xx_aes_setkey_enc(AesContext_t * ctx,const uint8_t * key,size_t key_bits)91 psa_status_t cc3xx_aes_setkey_enc(
92 AesContext_t *ctx,
93 const uint8_t *key,
94 size_t key_bits)
95 {
96 return aes_setkey(ctx, key, key_bits, CRYPTO_DIRECTION_ENCRYPT);
97 }
98
cc3xx_aes_setkey_dec(AesContext_t * ctx,const uint8_t * key,size_t key_bits)99 psa_status_t cc3xx_aes_setkey_dec(
100 AesContext_t *ctx,
101 const uint8_t *key,
102 size_t key_bits)
103 {
104 return aes_setkey(ctx, key, key_bits, CRYPTO_DIRECTION_DECRYPT);
105 }
106
cc3xx_aes_crypt(AesContext_t * ctx,aesMode_t mode,size_t length,uint8_t iv[AES_IV_SIZE],const uint8_t * input,uint8_t * output)107 psa_status_t cc3xx_aes_crypt(
108 AesContext_t *ctx,
109 aesMode_t mode,
110 size_t length,
111 uint8_t iv[AES_IV_SIZE],
112 const uint8_t *input,
113 uint8_t *output)
114 {
115 drvError_t drvRet;
116 CCBuffInfo_t inBuffInfo;
117 CCBuffInfo_t outBuffInfo;
118
119 if (0 == length) {
120 return PSA_SUCCESS;
121 }
122
123 if (NULL == ctx || NULL == input || NULL == output || NULL == iv) {
124 CC_PAL_LOG_ERR("Null pointer exception");
125 return PSA_ERROR_INVALID_ARGUMENT;
126 }
127
128 if (mode != CIPHER_CTR && length % AES_BLOCK_SIZE) {
129 CC_PAL_LOG_ERR("Length %d not a multiple of the block size", length);
130 return PSA_ERROR_INVALID_ARGUMENT;
131 }
132
133 ctx->mode = mode;
134
135 if (mode != CIPHER_ECB) {
136 CC_PalMemCopy(ctx->ivBuf, iv, AES_IV_SIZE);
137 }
138
139 drvRet = SetDataBuffersInfo(input, length, &inBuffInfo,
140 output, length, &outBuffInfo);
141 if (drvRet != 0) {
142 CC_PAL_LOG_ERR("Bad i/o buffers");
143 return PSA_ERROR_INVALID_ARGUMENT;
144 }
145
146 drvRet = ProcessAesDrv(ctx, &inBuffInfo, &outBuffInfo, length);
147 if (drvRet != AES_DRV_OK) {
148 CC_PAL_LOG_ERR("cc3xx_aes_crypt failed: %d", drvRet);
149 return PSA_ERROR_INVALID_ARGUMENT;
150 }
151
152 if (mode != CIPHER_ECB) {
153 CC_PalMemCopy(iv, ctx->ivBuf, AES_IV_SIZE);
154 }
155
156 return PSA_SUCCESS;
157 }
158 /** @} */ // end of internal_aes
159