xref: /OK3568_Linux_fs/external/security/librkcrypto/test/c_mode/des_cbc.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_cbc128_encrypt(void * ctx,const unsigned char * in,unsigned char * out,int len,unsigned char * ivec,block128_f block)7 static void rk_crypto_cbc128_encrypt(void *ctx, const unsigned char *in, unsigned char *out,
8 			int len, unsigned char *ivec, block128_f block)
9 {
10 	int n;
11 	const unsigned char *iv = ivec;
12 
13 	while (len) {
14 		for(n=0; n<DES_BLOCK_SIZE && n<len; ++n)
15 			out[n] = in[n] ^ iv[n];
16 		for(; n<DES_BLOCK_SIZE; ++n)
17 			out[n] = iv[n];
18 		(*block)(out, out, ctx);
19 		iv = out;
20 		if (len<=DES_BLOCK_SIZE) break;
21 		len -= DES_BLOCK_SIZE;
22 		in  += DES_BLOCK_SIZE;
23 		out += DES_BLOCK_SIZE;
24 	}
25 	memcpy(ivec,iv,DES_BLOCK_SIZE);
26 }
27 
rk_crypto_cbc128_decrypt(void * ctx,const unsigned char * in,unsigned char * out,int len,unsigned char * ivec,block128_f block)28 static void rk_crypto_cbc128_decrypt(void *ctx, const unsigned char *in, unsigned char *out,
29 			int len, unsigned char *ivec, block128_f block)
30 {
31 	int n;
32 	unsigned char c;
33 	unsigned char tmp_buf[DES_BLOCK_SIZE];
34 
35 	memset(tmp_buf, 0x00, sizeof(tmp_buf));
36 
37 	while (len) {
38 		(*block)(in, tmp_buf,ctx);
39 		for(n=0; n<DES_BLOCK_SIZE && n<len; ++n) {
40 			c = in[n];
41 			out[n] = tmp_buf[n] ^ ivec[n];
42 			ivec[n] = c;
43 		}
44 		if (len<=DES_BLOCK_SIZE) {
45 			for (; n<DES_BLOCK_SIZE; ++n)
46 				ivec[n] = in[n];
47 			break;
48 		}
49 		len -= DES_BLOCK_SIZE;
50 		in	+= DES_BLOCK_SIZE;
51 		out += DES_BLOCK_SIZE;
52 	}
53 
54 }
55 
rk_des_cbc_encrypt(const unsigned char * in,unsigned char * out,unsigned long length,const unsigned char * key,const int key_len,unsigned char * ivec,const int enc)56 int rk_des_cbc_encrypt(const unsigned char *in, unsigned char *out,
57         unsigned long length, const unsigned char *key, const int key_len,
58         unsigned char *ivec, const int enc)
59 {
60 		rk_des_context  ctx;
61 		rk_des3_context ctx3;
62 
63 		if(key_len != 8 && key_len != 16 && key_len != 24)
64 			return -1;
65 
66 		if(length%DES_BLOCK_SIZE)
67 			return -1;
68 
69         if (enc) {
70 			switch(key_len){
71 				case 8:
72 					rk_des_setkey_enc(&ctx, key);
73 					rk_crypto_cbc128_encrypt((void*)&ctx, in, out, length, ivec, rk_des_crypt_ecb);
74 					break;
75 				case 16:
76 					rk_des3_set2key_enc(&ctx3, key);
77 					rk_crypto_cbc128_encrypt((void*)&ctx3, in, out, length, ivec, rk_des3_crypt_ecb);
78 					break;
79 				case 24:
80 					rk_des3_set3key_enc(&ctx3, key);
81 					rk_crypto_cbc128_encrypt((void*)&ctx3, in, out, length, ivec, rk_des3_crypt_ecb);
82 					break;
83 				default:
84 					return -1;
85 			}
86 
87         } else {
88 			switch(key_len){
89 				case 8:
90 					rk_des_setkey_dec(&ctx, key);
91 					rk_crypto_cbc128_decrypt((void*)&ctx, in, out, length, ivec, rk_des_crypt_ecb);
92 					break;
93 				case 16:
94 					rk_des3_set2key_dec(&ctx3, key);
95 					rk_crypto_cbc128_decrypt((void*)&ctx3, in, out, length, ivec, rk_des3_crypt_ecb);
96 					break;
97 				case 24:
98 					rk_des3_set3key_dec(&ctx3, key);
99 					rk_crypto_cbc128_decrypt((void*)&ctx3, in, out, length, ivec, rk_des3_crypt_ecb);
100 					break;
101 				default:
102 					return -1;
103 			}
104         }
105 		return 0;
106 }
107 
108 
109