xref: /optee_os/core/arch/arm/crypto/sm4_armv8a_ce.c (revision 2be3770e85826ae69748fc3e68920d7293a98c5d)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) Hisilicon Technologies Co., Ltd. 2023. All rights reserved.
4  *
5  * SM4 optimization for ARMv8 by SM4 HW instruction, which is an optional
6  * Cryptographic Extension for ARMv8.2-A.
7  */
8 #include <crypto/crypto_accel.h>
9 #include <kernel/thread.h>
10 
11 #include "sm4_armv8a_ce.h"
12 
crypto_accel_sm4_setkey_enc(uint32_t sk[32],const uint8_t key[16])13 void crypto_accel_sm4_setkey_enc(uint32_t sk[32], const uint8_t key[16])
14 {
15 	uint32_t vfp_state = 0;
16 
17 	assert(sk && key);
18 
19 	vfp_state = thread_kernel_enable_vfp();
20 	ce_sm4_setkey_enc(sk, key);
21 	thread_kernel_disable_vfp(vfp_state);
22 }
23 
crypto_accel_sm4_setkey_dec(uint32_t sk[32],const uint8_t key[16])24 void crypto_accel_sm4_setkey_dec(uint32_t sk[32], const uint8_t key[16])
25 {
26 	uint32_t vfp_state = 0;
27 
28 	assert(sk && key);
29 
30 	vfp_state = thread_kernel_enable_vfp();
31 	ce_sm4_setkey_dec(sk, key);
32 	thread_kernel_disable_vfp(vfp_state);
33 }
34 
crypto_accel_sm4_ecb_enc(void * out,const void * in,const void * key,unsigned int len)35 void crypto_accel_sm4_ecb_enc(void *out, const void *in, const void *key,
36 			      unsigned int len)
37 {
38 	uint32_t vfp_state = 0;
39 
40 	assert(out && in && key && !(len % 16));
41 
42 	vfp_state = thread_kernel_enable_vfp();
43 	ce_sm4_ecb_encrypt(out, in, key, len);
44 	thread_kernel_disable_vfp(vfp_state);
45 }
46 
crypto_accel_sm4_cbc_enc(void * out,const void * in,const void * key,unsigned int len,void * iv)47 void crypto_accel_sm4_cbc_enc(void *out, const void *in, const void *key,
48 			      unsigned int len, void *iv)
49 {
50 	uint32_t vfp_state = 0;
51 
52 	assert(out && in && key && !(len % 16));
53 
54 	vfp_state = thread_kernel_enable_vfp();
55 	ce_sm4_cbc_encrypt(out, in, key, len, iv);
56 	thread_kernel_disable_vfp(vfp_state);
57 }
58 
crypto_accel_sm4_cbc_dec(void * out,const void * in,const void * key,unsigned int len,void * iv)59 void crypto_accel_sm4_cbc_dec(void *out, const void *in, const void *key,
60 			      unsigned int len, void *iv)
61 {
62 	uint32_t vfp_state = 0;
63 
64 	assert(out && in && key && !(len % 16));
65 
66 	vfp_state = thread_kernel_enable_vfp();
67 	ce_sm4_cbc_decrypt(out, in, key, len, iv);
68 	thread_kernel_disable_vfp(vfp_state);
69 }
70 
crypto_accel_sm4_ctr_enc(void * out,const void * in,const void * key,unsigned int len,void * iv)71 void crypto_accel_sm4_ctr_enc(void *out, const void *in, const void *key,
72 			      unsigned int len, void *iv)
73 {
74 	uint32_t vfp_state = 0;
75 
76 	assert(out && in && key && !(len % 16));
77 
78 	vfp_state = thread_kernel_enable_vfp();
79 	ce_sm4_ctr_encrypt(out, in, key, len, iv);
80 	thread_kernel_disable_vfp(vfp_state);
81 }
82 
crypto_accel_sm4_xts_enc(void * out,const void * in,const void * key1,const void * key2,unsigned int len,void * iv)83 void crypto_accel_sm4_xts_enc(void *out, const void *in, const void *key1,
84 			      const void *key2, unsigned int len, void *iv)
85 {
86 	uint32_t vfp_state = 0;
87 
88 	assert(out && in && key1 && key2 && (len >= 16));
89 
90 	vfp_state = thread_kernel_enable_vfp();
91 	ce_sm4_xts_encrypt(out, in, key1, key2, len, iv);
92 	thread_kernel_disable_vfp(vfp_state);
93 }
94 
crypto_accel_sm4_xts_dec(void * out,const void * in,const void * key1,const void * key2,unsigned int len,void * iv)95 void crypto_accel_sm4_xts_dec(void *out, const void *in, const void *key1,
96 			      const void *key2, unsigned int len, void *iv)
97 {
98 	uint32_t vfp_state = 0;
99 
100 	assert(out && in && key1 && key2 && (len >= 16));
101 
102 	vfp_state = thread_kernel_enable_vfp();
103 	ce_sm4_xts_decrypt(out, in, key1, key2, len, iv);
104 	thread_kernel_disable_vfp(vfp_state);
105 }
106