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
|
/**
* crypto.c
* Crypto calls.
*
* Matej Pfajfar <mp292@cam.ac.uk>
*/
/*
* Changes :
* $Log$
* Revision 1.1 2002/06/26 22:45:50 arma
* Initial revision
*
* Revision 1.1 2002/04/02 14:28:01 badbytes
* Final finishes.
*
*/
#include <malloc.h>
#include <unistd.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include "../common/log.h"
#include "crypto.h"
int crypt_f(unsigned char *buf, size_t buflen, crypt_path_t **cpath, size_t cpathlen)
{
int i=0;
int retval = 0;
unsigned char *ciphertext = NULL;
crypt_path_t *thishop;
/* allocate the ciphertext buffer */
ciphertext = (unsigned char *)malloc(buflen);
if (!ciphertext)
{
log(LOG_ERR,"Error allocating memory.");
return -1;
}
for (i=0; i < cpathlen; i++) /* moving from last to first hop
* Remember : cpath is in reverse order, i.e. last hop first
*/
{
log(LOG_DEBUG,"crypt_f() : Processing hop %u",cpathlen-i);
thishop = cpath[i];
/* encrypt */
retval = EVP_EncryptUpdate(&thishop->f_ctx,ciphertext, &buflen, buf, buflen);
if (!retval) /* error */
{
log(LOG_ERR,"Error performing encryption:%s",ERR_reason_error_string(ERR_get_error()));
free(ciphertext);
return -1;
}
/* copy ciphertext back to buf */
memcpy((void *)buf,(void *)ciphertext,buflen);
}
free((void *)ciphertext);
return 0;
}
int crypt_b(unsigned char *buf, size_t buflen, crypt_path_t **cpath, size_t cpathlen)
{
int i=0;
int retval=0;
unsigned char *plaintext=NULL;
crypt_path_t *thishop;
/* allocate the plaintext buffer */
plaintext = (unsigned char *)malloc(buflen);
if (!plaintext)
{
log(LOG_ERR,"Error allocating memory.");
return -1;
}
for (i=cpathlen-1; i >= 0; i--) /* moving from first to last hop
* Remember : cpath is in reverse order, i.e. last hop first
*/
{
thishop = cpath[i];
/* encrypt */
retval = EVP_DecryptUpdate(&thishop->b_ctx,plaintext, &buflen, buf, buflen);
if (!retval) /* error */
{
log(LOG_ERR,"Error performing decryption:%s",ERR_reason_error_string(ERR_get_error()));
free(plaintext);
return -1;
}
/* copy plaintext back to buf */
memcpy((void *)buf,(void *)plaintext,buflen);
}
free(plaintext);
return 0;
}
|