-
Notifications
You must be signed in to change notification settings - Fork 0
/
sec_crypto.c
137 lines (116 loc) · 4.41 KB
/
sec_crypto.c
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
/*
===========================================================================
Copyright (C) 2010-2013 Ninja and TheKelm of the IceOps-Team
This file is part of CoD4X17a-Server source code.
CoD4X17a-Server source code is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
CoD4X17a-Server source code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
===========================================================================
*/
#include "sec_crypto.h"
#include "sec_common.h"
struct ltc_hash_descriptor sec_hashes[SEC_HASH_SIZE__];
qboolean Sec_BinaryToHex(char *in,unsigned long inSize,char *out, unsigned long *outSize){
if(*outSize < inSize * 2+1){
//printf("Sec_BinaryToHex: DEBUG: %lu, %lu\n",*outSize,inSize * 4);
SecCryptErr = CRYPT_MEM;
return qfalse;
}
int i,j;
char hex[17]="0123456789abcdef";
for(i=0,j=0;i<inSize && j < *outSize;i++,j+=2){
//Sec_CharToHex(in[i],out+(i*2));
out[j+1]= hex[in[i]&0xF];
out[j]= hex[(in[i]>>4)&0xF];
out[j+2]= 0;
//printf("%d,%d,%c%c |\n",i,j,out[j],out[j+1]);
}
//printf("\n\n");
return qtrue;
}
qboolean Sec_HashMemory(sec_hash_e algo, void *in, size_t inSize, void *out, long unsigned int *outSize,qboolean binaryOutput){
//__asm__("int $3");
if(in == NULL || out == NULL || *outSize < 1 || inSize < 1 || algo >= SEC_HASH_SIZE__){
SecCryptErr = CRYPT_INVALID_ARG;
return qfalse;
}
if(!Sec_Initialized()){
return qfalse;
}
hash_state md;
int result;
long unsigned int size;
unsigned char *buff = NULL,*buff2 = NULL;
struct ltc_hash_descriptor *hs = &sec_hashes[algo];
SecCryptErr = CRYPT_OK;
size = (binaryOutput ? hs->hashsize / 4: hs->hashsize);
if(size > *outSize){
SecCryptErr = CRYPT_MEM;
return qfalse;
}
if(!binaryOutput) {buff = (unsigned char *)Sec_Malloc(sizeof(unsigned char) * size); buff2 = buff; }
else {buff2 = NULL; buff = out; }
if((result = hs->init(&md)) != CRYPT_OK) { Sec_Free(buff2); SecCryptErr = result; return qfalse; }
if((result = hs->process(&md, in, inSize)) != CRYPT_OK){ Sec_Free(buff2); SecCryptErr = result; return qfalse; }
if((result = hs->done(&md,buff)) != CRYPT_OK) { Sec_Free(buff2); SecCryptErr = result; return qfalse; }
if(!binaryOutput){
Sec_BinaryToHex((char *)buff,hs->hashsize,out,outSize);
}
else *outSize = hs->hashsize;
Sec_Free(buff2);
return (SecCryptErr == CRYPT_OK);
}
qboolean Sec_HashFile(sec_hash_e algo, const char *fname, void *out, long unsigned *outSize,qboolean binaryOutput)
{
if(fname == NULL || out == NULL || outSize == NULL || *outSize < 1 || algo < 0 || algo >= SEC_HASH_SIZE__){
SecCryptErr = CRYPT_INVALID_ARG;
return qfalse;
}
if(!Sec_Initialized()){
return qfalse;
}
hash_state md;
FILE *fp;
unsigned char buff[1024];
unsigned char *ptr;
int x,result,size;
struct ltc_hash_descriptor *hs = &sec_hashes[algo];
SecCryptErr = CRYPT_OK;
size = binaryOutput ? hs->hashsize / 4 : hs->hashsize;
if(size > *outSize){
SecCryptErr = CRYPT_MEM;
return qfalse;
}
unsigned long dbg = 0;
if((result = hs->init(&md))!=CRYPT_OK){ SecCryptErr=result; return qfalse; }
fp = fopen(fname,"rb");
if(fp==NULL){
SecCryptErr = CRYPT_INVALID_ARG;
return qfalse;
}
do{
x = fread(buff, 1, sizeof(buff), fp);
if ((result = hs->process(&md, buff, x)) != CRYPT_OK){
SecCryptErr = result;
fclose(fp);
return qfalse;
}
dbg += x;
}while (x == sizeof(buff));
//printf("File size: %lu\n",dbg);
fclose(fp);
ptr = binaryOutput ? out : buff;
if((result = hs->done(&md,buff))!=CRYPT_OK){ SecCryptErr = result; return qfalse; }
if(!binaryOutput){
Sec_BinaryToHex((char *)ptr,hs->hashsize,out,outSize);
}
else *outSize = hs->hashsize;
return (SecCryptErr == CRYPT_OK);
}