xref: /OK3568_Linux_fs/external/security/librkcrypto/test/c_mode/des_ecb.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
5 #include "des_core.h"
6 
rk_crypto_ecb128_encrypt(void * ctx,const unsigned char * in,unsigned long length,unsigned char * out,block128_f block)7 static int rk_crypto_ecb128_encrypt(void *ctx, const unsigned char *in, unsigned long length, unsigned char *out,block128_f block)
8 {
9 	unsigned long i = 0;
10 
11 	for (i = 0; i < length / DES_BLOCK_SIZE; i++){
12 		(*block)(in + i * DES_BLOCK_SIZE, out + i * DES_BLOCK_SIZE, ctx);
13 	};
14 	return 0;
15 }
16 
rk_des_ecb_encrypt(const unsigned char * in,unsigned char * out,unsigned long length,const unsigned char * key,const int key_len,const int enc)17 int rk_des_ecb_encrypt(const unsigned char *in, unsigned char *out,
18         unsigned long length, const unsigned char *key, const int key_len,
19         const int enc)
20 {
21 	rk_des_context  ctx;
22 	rk_des3_context ctx3;
23 
24 	if(key_len != 8 && key_len != 16 && key_len != 24)
25 		return -1;
26 
27 	if(length%DES_BLOCK_SIZE)
28 		return -1;
29 
30     if (enc) {
31 		switch(key_len){
32 			case 8:
33 				rk_des_setkey_enc(&ctx, key);
34 				rk_crypto_ecb128_encrypt((void*)&ctx, in, length, out, rk_des_crypt_ecb);
35 				break;
36 			case 16:
37 				rk_des3_set2key_enc(&ctx3, key);
38 				rk_crypto_ecb128_encrypt((void*)&ctx3, in, length, out, rk_des3_crypt_ecb);
39 				break;
40 			case 24:
41 				rk_des3_set3key_enc(&ctx3, key);
42 				rk_crypto_ecb128_encrypt((void*)&ctx3, in, length, out, rk_des3_crypt_ecb);
43 				break;
44 			default:
45 				return -1;
46 		}
47 
48     } else {
49 		switch(key_len){
50 			case 8:
51 				rk_des_setkey_dec(&ctx, key);
52 				rk_crypto_ecb128_encrypt((void*)&ctx, in, length, out, rk_des_crypt_ecb);
53 				break;
54 			case 16:
55 				rk_des3_set2key_dec(&ctx3, key);
56 				rk_crypto_ecb128_encrypt((void*)&ctx3, in, length, out, rk_des3_crypt_ecb);
57 				break;
58 			case 24:
59 				rk_des3_set3key_dec(&ctx3, key);
60 				rk_crypto_ecb128_encrypt((void*)&ctx3, in, length, out, rk_des3_crypt_ecb);
61 				break;
62 			default:
63 				return -1;
64 		}
65     }
66 	return 0;
67 }
68 
69 
70