C/Visual C - des3

 
Vista:

des3

Publicado por Hichimoto (44 intervenciones) el 29/02/2024 05:37:39
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <unistd.h>
#include <openssl/des.h>
 
#define BUFSIZE 1024
 
int main(void)
{
    unsigned char in[BUFSIZE], encrypted[BUFSIZE], decrypted[BUFSIZE];
    unsigned char *e = encrypted;
    int len;
 
    DES_cblock key1, key2, key3;
    DES_cblock seed = {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10};
    DES_key_schedule ks1, ks2, ks3;
    DES_cblock ivsetup = {0xE1, 0xE2, 0xE3, 0xD4, 0xD5, 0xC6, 0xC7, 0xA8};
    DES_cblock ivec;
 
    memset(in, 0, sizeof(in));
    memset(encrypted, 0, sizeof(encrypted));
    memset(decrypted, 0, sizeof(decrypted));
 
    RAND_seed(seed, sizeof(DES_cblock));
 
    DES_random_key(&key1);
    DES_random_key(&key2);
    DES_random_key(&key2);
 
    DES_set_key((DES_cblock *)key1, &ks1);
    DES_set_key((DES_cblock *)key2, &ks2);
    DES_set_key((DES_cblock *)key3, &ks3);
 
    /* plaintext */
    strcpy(in, "this is a string encrypted with triple des. it is 64 bytes long.");
 
    printf("Plaintext: [%s]\n", in);
 
    len = strlen(in);
    memcpy(ivec, ivsetup, sizeof(ivsetup));
    DES_ede3_cbc_encrypt(in, encrypted, len, &ks1, &ks2, &ks3, &ivec, DES_ENCRYPT);
 
    printf("Ciphertext:");
    while (*e) printf(" [x]", *e++);
    printf("\n");
 
    len = strlen(encrypted);
    memcpy(ivec, ivsetup, sizeof(ivsetup));
    DES_ede3_cbc_encrypt(encrypted, decrypted, len, &ks1, &ks2, &ks3, &ivec, DES_DECRYPT);
 
    printf("Decrypted Text: [%s]\n", decrypted);
 
    exit(0);
 
 
}
 
 
 
 
//int main() {
//	// set mode
//	/*
//	 * TODO make different code per option
//	 * int opt;
//	while ((opt = getopt(argc, argv, "cof")) != -1) {
//		switch (opt) {
//		case 'c':
//			mode = CBC_MODE;
//			break;
//		case 'o':
//			mode = OFB_MODE;
//			break;
//		case 'f':
//			mode = CFB_MODE;
//			break;
//		default:
//			mode = CBC_MODE;
//			break;
//		}
//	}*/
//
//	const unsigned char plaintext[] = "this is a string encrypted with 3des";
//	int bufsize = sizeof(plaintext);
//
//	//initialize
//	char iv;
//	des_cblock key1, key2, key3, seed = { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54,
//			0x32, 0x10 };
//	DES_key_schedule * ks1, * ks2, * ks3;
//
//	des_random_seed(seed);
//	des_random_key(key1);
//	des_random_key(key2);
//	des_random_key(key3);
//
//	des_set_key((C_Block *)key1, ks1);
//	des_set_key((C_Block *)key2, ks2);
//	des_set_key((C_Block *)key3, ks3);
//
//	memset(iv, 0, bufsize);
//
////	//char key[] = "this is a bad password";
////
//	unsigned char * encrypted;
//	unsigned char * decrypted;
//
//	//compiler doesn't recognize these
////	DES_ed3_cbc_encrypt(plaintext, encrypted, bufsize, ks1, ks2, ks3, iv, 1);
////	DES_ed3_cbc_encrypt(encrypted, decrypted, bufsize, ks1, ks2, ks3, iv, 0);
//
//	DES_ede3_cbc_encrypt(plaintext,
//	        encrypted, (long) bufsize, ks1,
//	        ks2, ks3, iv,
//	        1);
////	DES_ede3_cbc_encrypt(encrypted,
////	        decrypted, (long) bufsize, ks1,
////	        ks2, ks3, iv,
////	        0);
//
//	printf("Decrypted: %s", decrypted);
//
////
////	encrypt(key, plaintext, *encrypted);
////
////	decrypt(key, encrypted, *decrypted);
//
//	return 0;
//}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
CK_RV
sw_des3_cbc(CK_BYTE * in_data,
	    CK_ULONG in_data_len,
	    CK_BYTE *out_data,
	    CK_ULONG *out_data_len,
	    CK_BYTE *init_v,
	    CK_BYTE  *key_value,
	    CK_BYTE  encrypt)
{
	DES_key_schedule des_key1;
	DES_key_schedule des_key2;
	DES_key_schedule des_key3;
 
	const_DES_cblock key_SSL1, key_SSL2, key_SSL3;
	DES_cblock ivec;
 
	// the des decrypt will only fail if the data length is not evenly divisible
	// by 8
	if (in_data_len % 8) {
		TRACE_ERROR("%s\n", ock_err(ERR_DATA_LEN_RANGE));
		return CKR_DATA_LEN_RANGE;
	}
 
	// The key as passed in is a 24 byte string containing 3 keys
	// pick it apart and create the key schedules
	memcpy(&key_SSL1, key_value, (size_t)8);
	memcpy(&key_SSL2, key_value+8, (size_t)8);
	memcpy(&key_SSL3, key_value+16, (size_t)8);
	DES_set_key_unchecked(&key_SSL1, &des_key1);
	DES_set_key_unchecked(&key_SSL2, &des_key2);
	DES_set_key_unchecked(&key_SSL3, &des_key3);
 
	memcpy(ivec, init_v, sizeof(ivec));
 
	// Encrypt or decrypt the data
	if (encrypt) {
		DES_ede3_cbc_encrypt(in_data,
				out_data,
				in_data_len,
				&des_key1,
				&des_key2,
				&des_key3,
				&ivec,
				DES_ENCRYPT);
		*out_data_len = in_data_len;
	} else {
		DES_ede3_cbc_encrypt(in_data,
				out_data,
				in_data_len,
				&des_key1,
				&des_key2,
				&des_key3,
				&ivec,
				DES_DECRYPT);
 
		*out_data_len = in_data_len;
	}
 
	return CKR_OK;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
static void
cbc3_test(char key1[8], char key2[8], char key3[8],
	  char iv[8], char in[24], char out[24])
{
    unsigned char k1[8], k2[8], k3[8],
	indata[24], outdata[24], outdata2[24], ansdata[24];
    DES_key_schedule s1, s2, s3;
    DES_cblock ivdata, ivec_copy;
 
    memcpy(k1, key1, 8);
    memcpy(k2, key2, 8);
    memcpy(k3, key3, 8);
    memcpy(ivdata, iv, 8);
    memcpy(indata, in, 24);
    memcpy(ansdata, out, 24);
    DES_set_odd_parity(&k1);
    DES_set_odd_parity(&k2);
    DES_set_odd_parity(&k3);
    DES_set_key_unchecked(&k1, &s1);
    DES_set_key_unchecked(&k2, &s2);
    DES_set_key_unchecked(&k3, &s3);
    memcpy(&ivec_copy, &ivdata, sizeof(ivec_copy));
    DES_ede3_cbc_encrypt(indata, outdata, 24,
			 &s1, &s2, &s3, &ivec_copy, 1);
    if (memcmp(outdata, ansdata, sizeof(ansdata)) != 0)
	errx(1, "cbc3: encrypt");
    memcpy(&ivec_copy, &ivdata, sizeof(ivec_copy));
    DES_ede3_cbc_encrypt(outdata, outdata2, 24,
			 &s1, &s2, &s3, &ivec_copy, 0);
    if (memcmp(indata, outdata2, sizeof(outdata2)) != 0)
	errx(1, "cbc3: decrypt");
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
int perf()
{
        DES_cblock key1, key2, key3, iv;
        DES_key_schedule ks1, ks2, ks3;
 
        char* payload = (char*)malloc(64);
        int i;
        uint64_t start, end;
 
       	memcpy(payload, "hello world hello world hello world\n", 64);
 
	// 1. Key & IV Extract
        memcpy(key1, "aeaeaeae", 8);
        memcpy(key2, "aeaeaeae", 8);
        memcpy(key3, "aeaeaeae", 8);
 
        memcpy(iv, "aeaeaeae", 8);
 
        DES_set_odd_parity(&key1);
        DES_set_odd_parity(&key2);
        DES_set_odd_parity(&key3);
 
        // Key Validation Check
        if(DES_set_key_checked(&key1, &ks1) ||
           DES_set_key_checked(&key2, &ks2) ||
           DES_set_key_checked(&key3, &ks3))
        {
               printf("DES_set_key_checked Error\n");
        }
 
        start = cpu_tsc();
 
        for(i = 0; i < 1000000; i++)
        {
                DES_ede3_cbc_encrypt((const unsigned char*)payload,
                                (unsigned char*)payload,
                                64 , &ks1, &ks2, &ks3, &iv, DES_ENCRYPT);
        }
 
        end = cpu_tsc();
 
        printf("encrpytion time : %ld\n", (end-start)/10000);
 
	start = cpu_tsc();
 
        for(i = 0; i < 1000000; i++)
        {
                DES_ede3_cbc_encrypt((const unsigned char*)payload,
                                (unsigned char*)payload,
                                64 , &ks1, &ks2, &ks3, &iv, DES_DECRYPT);
        }
 
        end = cpu_tsc();
 
        printf("decryption time : %ld\n", (end-start)/10000);
 
	return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
static int kcdecrypt(unsigned char *key, unsigned char *iv, unsigned char *data)
{
	unsigned char out[CTLEN];
	int pad, n, i;
	DES_cblock key1, key2, key3;
	DES_cblock ivec;
	DES_key_schedule ks1, ks2, ks3;
	memset(out, 0, sizeof(out));
	memcpy(key1, key, 8);
	memcpy(key2, key + 8, 8);
	memcpy(key3, key + 16, 8);
	DES_set_key((C_Block *) key1, &ks1);
	DES_set_key((C_Block *) key2, &ks2);
	DES_set_key((C_Block *) key3, &ks3);
	memcpy(ivec, iv, 8);
	DES_ede3_cbc_encrypt(data, out, CTLEN, &ks1, &ks2, &ks3, &ivec,  DES_DECRYPT);
 
	// now check padding
	pad = out[47];
	if(pad > 8)
		// "Bad padding byte. You probably have a wrong password"
		return -1;
	if(pad != 4) /* possible bug here, is this assumption always valid? */
		return -1;
	n = CTLEN - pad;
	for(i = n; i < CTLEN; i++)
		if(out[i] != pad)
			// "Bad padding. You probably have a wrong password"
			return -1;
	return 0;
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder