xref: /OK3568_Linux_fs/external/security/librkcrypto/test/c_mode/sm4_ecb.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "sm4_core.h"
5 
rk_crypto_ecb128_encrypt(void * ctx,const unsigned char * in,unsigned int length,unsigned char * out,block128_f block)6 static int rk_crypto_ecb128_encrypt(void *ctx, const unsigned char *in, unsigned int length, unsigned char *out,block128_f block)
7 {
8 	unsigned int i = 0;
9 
10 	for(i=0; i<length/SM4_BLOCK_SIZE; i++){
11 		(*block)(in+i*SM4_BLOCK_SIZE, out+i*SM4_BLOCK_SIZE, ctx);
12 	};
13 	return 0;
14 }
15 
rk_sm4_ecb_encrypt(const unsigned char * in,unsigned char * out,unsigned int length,const unsigned char * key,const int key_len,const int enc)16 int rk_sm4_ecb_encrypt(const unsigned char *in, unsigned char *out,
17         unsigned int length, const unsigned char *key, const int key_len,
18         const int enc)
19 {
20 	sm4_context  ctx;
21 
22 	if(key_len != 16)
23 		return -1;
24 
25 	if(length%SM4_BLOCK_SIZE || length == 0)
26 		return -1;
27 
28     if (enc) {
29 		rk_sm4_setkey_enc(&ctx, key);
30     } else {
31 		rk_sm4_setkey_dec(&ctx, key);
32     }
33 
34 	rk_crypto_ecb128_encrypt((void*)&ctx, in, length, out, rk_rk_sm4_crypt_ecb);
35 	return 0;
36 }
37 
38 
39