-
Notifications
You must be signed in to change notification settings - Fork 2
/
crypto.cpp
188 lines (140 loc) · 3.21 KB
/
crypto.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Package: Crypto-PAn 1.0
// File: crypto.cpp
// Last Update: Aug 8, 2005
// Author: Jinliang Fan, David Stott ([email protected])
#include <string.h>
#include <stdlib.h>
#include "crypto.h"
#include <openssl/bio.h>
#include <openssl/err.h>
#include <iostream>
static BIO* bio_err = NULL;
static int Ciphersloaded = 0;
static BIO*
init_bio_err()
{
if (bio_err == NULL && (bio_err=BIO_new(BIO_s_file())) != NULL) {
BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
ERR_load_crypto_strings();
}
return bio_err;
}
void
bio_err_print_errors()
{
init_bio_err();
ERR_print_errors( bio_err );
}
/**
* initializes ciphernames (i.e., calls OpenSSL_add_all_ciphers), if needed
*/
static void
init_ciphernames()
{
if ( Ciphersloaded == 0 ) {
OpenSSL_add_all_ciphers();
Ciphersloaded = 1;
}
}
Crypto::Crypto()
{
initialized = 0;
dir = 1;
if ( bio_err == NULL ) {
init_bio_err();
}
}
Crypto::Crypto( const char* _ciphername, const unsigned char* _key, const unsigned char* _iv )
{
initialized = 0;
dir = 1;
if ( SetCipher( _ciphername ) ) {
return;
}
SetKey( _key );
SetIV( _iv );
}
Crypto::~Crypto()
{
delete key;
key = NULL;
delete iv;
iv = NULL;
EVP_CIPHER_CTX_cleanup( ctx );
}
int
Crypto::SetCipher( const char* _ciphername )
{
ciphername = strdup(_ciphername);
init_ciphernames();
/* finds cipher structure matching session key's ciphername */
if ( (cipher = EVP_get_cipherbyname( ciphername )) == NULL ) {
std::cerr << "Crypto::SetCipher(): Unable to understand cipher "<< ciphername << std::endl;
return -1;
}
initialized = 0;
return 0;
}
// void
// Crypto::Initialize()
// {
// if ( initialized == 0 ) {
// EVP_CIPHER_CTX_init( ctx );
// if ( EVP_CipherInit_ex( ctx, cipher, NULL, key, iv, dir ) != 1 ) {
// std::cerr << "Crypto::Initialize(): Error on EVP_CipherInit_ex" << std::endl;
// return;
// }
// initialized = 1;
// }
// }
void
Crypto::SetDir( int _dir )
{
dir = _dir;
}
void
Crypto::SetKey( const unsigned char* _key )
{
if ( _key != NULL ) {
key = new unsigned char[GetKeyLength()];
memcpy( key, _key, GetKeyLength() );
}
}
void
Crypto::SetIV( const unsigned char* _iv )
{
if ( _iv != NULL ) {
iv = new unsigned char[GetIVLength()];
memcpy( iv, _iv, GetIVLength() );
}
}
int
Crypto::EncryptInit(void)
{
ctx = EVP_CIPHER_CTX_new();
EVP_CIPHER_CTX_init( ctx );
if ( EVP_CipherInit_ex( ctx, cipher, NULL, key, iv, dir ) != 1 ) {
std::cerr << "Crypto::Initialize(): Error on EVP_CipherInit_ex" << std::endl;
return -1;
}
initialized = 1;
return 0;
}
int
Crypto::EncryptUpdate( unsigned char *out, int &outl, const unsigned char *in, int inl )
{
if ( EVP_CipherUpdate( ctx, out, &outl, in, inl ) == 0 ) {
std::cerr << "Crypto::CipherUpdate() failed" << std::endl;
return -1;
}
return 0;
}
int
Crypto::EncryptFinal( unsigned char *out, int &outl )
{
if ( EVP_CipherFinal_ex( ctx, out, &outl ) == 0 ) {
std::cerr << "Crypto::EncryptFinal() failed" << std::endl;
return -1;
}
return 0;
}