b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | #include <common.h> |
| 2 | #include <tee.h> |
| 3 | #include <malloc.h> |
| 4 | #include <asm/arch/cpu.h> |
| 5 | #include "asr_hw_aes.h" |
| 6 | #ifdef CONFIG_TEE_OS |
| 7 | #include "cipher_optee.h" |
| 8 | #endif |
| 9 | #ifdef CONFIG_ASR_TE200 |
| 10 | #include "te200_cipher.h" |
| 11 | #endif |
| 12 | #ifdef CONFIG_ASR_BCM |
| 13 | #include "bcm/asr_cipher.h" |
| 14 | #endif |
| 15 | #include "../geu/asr_geu.h" |
| 16 | |
| 17 | int aes_ecb_encrypt(const uint8_t *key, uint32_t keylen, bool use_rkek, |
| 18 | const void *plaintext, void *ciphertext, size_t size) |
| 19 | { |
| 20 | #ifdef CONFIG_ASR_TE200 |
| 21 | if (cpu_is_asr1903_b0()) { |
| 22 | #ifdef CONFIG_TEE_OS |
| 23 | return aes_ecb_encrypt_optee((uint8_t *)key, keylen, use_rkek, |
| 24 | (void *)plaintext, ciphertext, size); |
| 25 | #else |
| 26 | return aes_ecb_encrypt_te200((uint8_t *)key, keylen, use_rkek, |
| 27 | (void *)plaintext, ciphertext, size); |
| 28 | #endif |
| 29 | } |
| 30 | #endif |
| 31 | |
| 32 | #ifdef CONFIG_ASR_BCM |
| 33 | #ifdef CONFIG_TEE_OS |
| 34 | return aes_ecb_encrypt_optee((uint8_t *)key, keylen, use_rkek, |
| 35 | (void *)plaintext, ciphertext, size); |
| 36 | #else |
| 37 | return aes_ecb_encrypt_bcm((uint8_t *)key, keylen, use_rkek, |
| 38 | (void *)plaintext, ciphertext, size); |
| 39 | #endif |
| 40 | #endif |
| 41 | |
| 42 | return aes_ecb_encrypt_geu(key, keylen, use_rkek, plaintext, ciphertext, size); |
| 43 | } |
| 44 | |
| 45 | |
| 46 | int aes_ecb_decrypt(const uint8_t *key, uint32_t keylen, bool use_rkek, |
| 47 | const void *ciphertext, void *plaintext, size_t size) |
| 48 | { |
| 49 | #ifdef CONFIG_ASR_TE200 |
| 50 | if (cpu_is_asr1903_b0()) { |
| 51 | #ifdef CONFIG_TEE_OS |
| 52 | return aes_ecb_decrypt_optee((uint8_t *)key, keylen, use_rkek, |
| 53 | (void *)ciphertext, plaintext, size); |
| 54 | #else |
| 55 | return aes_ecb_decrypt_te200((uint8_t *)key, keylen, use_rkek, |
| 56 | (void *)ciphertext, plaintext, size); |
| 57 | #endif |
| 58 | } |
| 59 | #endif |
| 60 | |
| 61 | #ifdef CONFIG_ASR_BCM |
| 62 | #ifdef CONFIG_TEE_OS |
| 63 | return aes_ecb_decrypt_optee((uint8_t *)key, keylen, use_rkek, |
| 64 | (void *)ciphertext, plaintext, size); |
| 65 | #else |
| 66 | return aes_ecb_decrypt_bcm((uint8_t *)key, keylen, use_rkek, |
| 67 | (void *)ciphertext, plaintext, size); |
| 68 | #endif |
| 69 | #endif |
| 70 | |
| 71 | return aes_ecb_decrypt_geu(key, keylen, use_rkek, ciphertext, plaintext, size); |
| 72 | } |
| 73 | |
| 74 | int aes_cbc_encrypt(const uint8_t *iv, const uint8_t *key, uint32_t keylen, |
| 75 | bool use_rkek, const void *plaintext, void *ciphertext, size_t size) |
| 76 | { |
| 77 | uint8_t in_iv[16] = {0}; |
| 78 | memcpy(in_iv, iv, 16); |
| 79 | |
| 80 | #ifdef CONFIG_ASR_TE200 |
| 81 | if (cpu_is_asr1903_b0()) { |
| 82 | #ifdef CONFIG_TEE_OS |
| 83 | return aes_cbc_encrypt_optee(in_iv, (uint8_t *)key, keylen, |
| 84 | use_rkek, (void *)plaintext, ciphertext, size); |
| 85 | #else |
| 86 | return aes_cbc_encrypt_te200(in_iv, (uint8_t *)key, keylen, |
| 87 | use_rkek, (void *)plaintext, ciphertext, size); |
| 88 | #endif |
| 89 | } |
| 90 | #endif |
| 91 | |
| 92 | #ifdef CONFIG_ASR_BCM |
| 93 | #ifdef CONFIG_TEE_OS |
| 94 | return aes_cbc_encrypt_optee(in_iv, (uint8_t *)key, keylen, |
| 95 | use_rkek, (void *)plaintext, ciphertext, size); |
| 96 | #else |
| 97 | return aes_cbc_encrypt_bcm(in_iv, (uint8_t *)key, keylen, |
| 98 | use_rkek, (void *)plaintext, ciphertext, size); |
| 99 | #endif |
| 100 | #endif |
| 101 | |
| 102 | return aes_cbc_encrypt_geu(in_iv, key, keylen, use_rkek, plaintext, ciphertext, size); |
| 103 | } |
| 104 | |
| 105 | int aes_cbc_decrypt(const uint8_t *iv, const uint8_t *key, uint32_t keylen, |
| 106 | bool use_rkek, const void *ciphertext, void *plaintext, size_t size) |
| 107 | { |
| 108 | uint8_t in_iv[16] = {0}; |
| 109 | memcpy(in_iv, iv, 16); |
| 110 | |
| 111 | #ifdef CONFIG_ASR_TE200 |
| 112 | if (cpu_is_asr1903_b0()) { |
| 113 | #ifdef CONFIG_TEE_OS |
| 114 | return aes_cbc_decrypt_optee(in_iv, (uint8_t *)key, keylen, |
| 115 | use_rkek, (void *)ciphertext, plaintext, size); |
| 116 | #else |
| 117 | return aes_cbc_decrypt_te200(in_iv, (uint8_t *)key, keylen, |
| 118 | use_rkek, (void *)ciphertext, plaintext, size); |
| 119 | #endif |
| 120 | } |
| 121 | #endif |
| 122 | |
| 123 | #ifdef CONFIG_ASR_BCM |
| 124 | #ifdef CONFIG_TEE_OS |
| 125 | return aes_cbc_decrypt_optee(in_iv, (uint8_t *)key, keylen, |
| 126 | use_rkek, (void *)ciphertext, plaintext, size); |
| 127 | #else |
| 128 | return aes_cbc_decrypt_bcm(in_iv, (uint8_t *)key, keylen, |
| 129 | use_rkek, (void *)ciphertext, plaintext, size); |
| 130 | #endif |
| 131 | #endif |
| 132 | |
| 133 | return aes_cbc_decrypt_geu(in_iv, key, keylen, use_rkek, ciphertext, plaintext, size); |
| 134 | } |
| 135 | |
| 136 | // #define ASR_HW_AES_TEST |
| 137 | |
| 138 | #ifdef ASR_HW_AES_TEST |
| 139 | __attribute__ ((aligned (16))) |
| 140 | static unsigned int key128_ecb[] = { |
| 141 | 0x67452301, 0xefcdab89, 0x67452301, 0xefcdab89 |
| 142 | }; |
| 143 | |
| 144 | __attribute__ ((aligned (16))) |
| 145 | static unsigned int pt_ecb[] = { |
| 146 | 0x03020100, 0x07060504, 0x0b0a0908, 0x0f0e0d0c, |
| 147 | 0x13121110, 0x17161514, 0x1b1a1918, 0x1f1e1d1c, |
| 148 | 0x23222120, 0x27262524, 0x2b2a2928, 0x2f2e2d2c, |
| 149 | 0x33323130, 0x37363534, 0x3b3a3938, 0x3f3e3d3c, |
| 150 | }; |
| 151 | |
| 152 | __attribute__ ((aligned (16))) |
| 153 | static unsigned int ct_ecb[] = { |
| 154 | 0x246295ad, 0x6426631f, 0x62d522a4, 0xa3aa53f7, |
| 155 | 0x41c3c69f, 0xda7c6ad1, 0xe647f9a0, 0x32abc4c9, |
| 156 | 0xb51b9cdf, 0x5139d75b, 0xa643b208, 0x94e6f543, |
| 157 | 0x38da0179, 0xd7bc88bb, 0x52ac77a6, 0x05ed84b4, |
| 158 | }; |
| 159 | |
| 160 | static int hw_aes_ecb_test(void *pverfiy_buf) |
| 161 | { |
| 162 | int ret = 0; |
| 163 | |
| 164 | /* step1: aes ecb encrypt test*/ |
| 165 | ret = aes_ecb_encrypt(key128_ecb, sizeof(key128_ecb), false, |
| 166 | pt_ecb, pverfiy_buf, sizeof(pt_ecb)); |
| 167 | if (ret) { |
| 168 | printf("aes ecb encrypt err\r\n"); |
| 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | if (memcmp(pverfiy_buf, ct_ecb, sizeof(ct_ecb))) { |
| 173 | printf("aes ecb encrypt err\r\n"); |
| 174 | return ret; |
| 175 | } |
| 176 | printf("aes ecb encrypt test: pass\r\n"); |
| 177 | |
| 178 | /* step2: aes ecb decrypt test*/ |
| 179 | ret = aes_ecb_decrypt(key128_ecb, sizeof(key128_ecb), false, |
| 180 | ct_ecb, pverfiy_buf, sizeof(ct_ecb)); |
| 181 | if (ret) { |
| 182 | printf("aes ecb decrypt err\r\n"); |
| 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | if (memcmp(pverfiy_buf, pt_ecb, sizeof(pt_ecb))) { |
| 187 | printf("aes ecb decrypt err\r\n"); |
| 188 | return ret; |
| 189 | } |
| 190 | printf("aes ecb decrypt test: pass\r\n"); |
| 191 | return ret; |
| 192 | } |
| 193 | |
| 194 | static int hw_aes_ecb_rkek_test(void) |
| 195 | { |
| 196 | int ret = 0; |
| 197 | uint32_t data_size = 512; |
| 198 | |
| 199 | uint8_t *plaintext = NULL; |
| 200 | uint8_t *ciphertext = NULL; |
| 201 | plaintext = (uint8_t *)malloc(data_size); |
| 202 | if (plaintext == NULL) { |
| 203 | ret = -1; |
| 204 | goto exit; |
| 205 | } |
| 206 | |
| 207 | ciphertext = (uint8_t *)malloc(data_size); |
| 208 | if (ciphertext == NULL) { |
| 209 | ret = -1; |
| 210 | goto exit; |
| 211 | } |
| 212 | |
| 213 | /* test aes ecb without hwkey encrypt/decrypt */ |
| 214 | memset(plaintext, 0xAB, data_size); |
| 215 | memset(ciphertext, 0, data_size); |
| 216 | |
| 217 | if (0 > aes_ecb_encrypt(NULL, 32, true, plaintext, ciphertext, data_size)){ |
| 218 | ret = -1; |
| 219 | goto exit; |
| 220 | } |
| 221 | |
| 222 | if (0 > aes_ecb_decrypt(NULL, 32, true, ciphertext, ciphertext, data_size)){ |
| 223 | ret = -1; |
| 224 | goto exit; |
| 225 | } |
| 226 | |
| 227 | if (memcmp(plaintext, ciphertext, data_size)) { |
| 228 | printf("aes ecb rkek test: failed\r\n"); |
| 229 | ret = -1; |
| 230 | goto exit; |
| 231 | } |
| 232 | printf("aes ecb rkek test: pass\r\n"); |
| 233 | |
| 234 | exit: |
| 235 | if (plaintext != NULL) { |
| 236 | free(plaintext); |
| 237 | } |
| 238 | if (ciphertext != NULL) { |
| 239 | free(ciphertext); |
| 240 | } |
| 241 | return ret; |
| 242 | } |
| 243 | |
| 244 | void hw_aes_test(void) |
| 245 | { |
| 246 | void *pverfiy_buf = (void *)malloc(256); |
| 247 | if (!pverfiy_buf) { |
| 248 | printf("te200 aes test: malloc failed\r\n"); |
| 249 | } |
| 250 | memset(pverfiy_buf, 0 , 256); |
| 251 | |
| 252 | hw_aes_ecb_test(pverfiy_buf); |
| 253 | hw_aes_ecb_rkek_test(); |
| 254 | |
| 255 | free(pverfiy_buf); |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | #endif |