Browse code

pv: changes to SHA string transformations

- addressed memset faulty usage, changed location of sha256 files,
removed sha2utils.h as we can use defines in sha256.h, removed
redundant code

lucian balanceanu authored on 18/03/2014 15:20:36
Showing 6 changed files
... ...
@@ -125,33 +125,27 @@ void compute_md5(char *dst, char *src, int src_len)
125 125
 void compute_sha256(char *dst, u_int8_t *src, int src_len)
126 126
 {
127 127
 	SHA256_CTX ctx256;
128
-	char buf[64];
129 128
 	SHA256_Init(&ctx256);
130 129
 	SHA256_Update(&ctx256, src, src_len);
131
-	SHA256_End(&ctx256, buf);
132
-	strncpy(dst, buf, 64);
130
+	SHA256_End(&ctx256, dst);
133 131
 }
134 132
 
135 133
 /*! \brief Compute SHA384 checksum */
136 134
 void compute_sha384(char *dst, u_int8_t *src, int src_len)
137 135
 {
138 136
 	SHA384_CTX ctx384;
139
-	char buf[96];
140 137
 	SHA384_Init(&ctx384);
141 138
 	SHA384_Update(&ctx384, src, src_len);
142
-	SHA384_End(&ctx384, buf);
143
-	strncpy(dst, buf, 96);
139
+	SHA384_End(&ctx384, dst);
144 140
 }
145 141
 
146 142
 /*! \brief Compute SHA512 checksum */
147 143
 void compute_sha512(char *dst, u_int8_t *src, int src_len)
148 144
 {
149 145
 	SHA512_CTX ctx512;
150
-	char buf[128];
151 146
 	SHA512_Init(&ctx512);
152 147
 	SHA512_Update(&ctx512, src, src_len);
153
-	SHA512_End(&ctx512, buf);
154
-	strncpy(dst, buf, 128);
148
+	SHA512_End(&ctx512, dst);
155 149
 }
156 150
 
157 151
 /*! \brief Unscape all printable ASCII characters */
... ...
@@ -30,7 +30,7 @@
30 30
 
31 31
 #include "../../str.h"
32 32
 #include "../../md5.h"
33
-#include "../../sha256.h"
33
+#include "../srutils/sha256.h"
34 34
 
35 35
 /*
36 36
  * add backslashes to special characters
37 37
similarity index 98%
38 38
rename from sha256.c
39 39
rename to lib/srutils/sha256.c
... ...
@@ -12,7 +12,7 @@
12 12
  *    notice, this list of conditions and the following disclaimer.
13 13
  * 2. Redistributions in binary form must reproduce the above copyright
14 14
  *    notice, this list of conditions and the following disclaimer in the
15
- *    documentation and/or other materials provided with the distribution.
15
+ *    documentation and/or other materials provided w627ith the distribution.
16 16
  * 3. Neither the name of the copyright holder nor the names of contributors
17 17
  *    may be used to endorse or promote products derived from this software
18 18
  *    without specific prior written permission.
... ...
@@ -624,7 +624,7 @@ void SHA256_Final(sha2_byte digest[], SHA256_CTX* context) {
624 624
 	}
625 625
 
626 626
 	/* Clean up state data: */
627
-	MEMSET_BZERO(context, sizeof(context));
627
+	MEMSET_BZERO(context, sizeof(*context));
628 628
 	usedspace = 0;
629 629
 }
630 630
 
... ...
@@ -645,7 +645,7 @@ char *SHA256_End(SHA256_CTX* context, char buffer[]) {
645 645
 		}
646 646
 		*buffer = (char)0;
647 647
 	} else {
648
-		MEMSET_BZERO(context, sizeof(context));
648
+		MEMSET_BZERO(context, sizeof(*context));
649 649
 	}
650 650
 	MEMSET_BZERO(digest, SHA256_DIGEST_LENGTH);
651 651
 	return buffer;
... ...
@@ -954,7 +954,7 @@ void SHA512_Final(sha2_byte digest[], SHA512_CTX* context) {
954 954
 	}
955 955
 
956 956
 	/* Zero out state data */
957
-	MEMSET_BZERO(context, sizeof(context));
957
+	MEMSET_BZERO(context, sizeof(*context));
958 958
 }
959 959
 
960 960
 char *SHA512_End(SHA512_CTX* context, char buffer[]) {
... ...
@@ -974,7 +974,7 @@ char *SHA512_End(SHA512_CTX* context, char buffer[]) {
974 974
 		}
975 975
 		*buffer = (char)0;
976 976
 	} else {
977
-		MEMSET_BZERO(context, sizeof(context));
977
+		MEMSET_BZERO(context, sizeof(*context));
978 978
 	}
979 979
 	MEMSET_BZERO(digest, SHA512_DIGEST_LENGTH);
980 980
 	return buffer;
... ...
@@ -1029,7 +1029,7 @@ void SHA384_Final(sha2_byte digest[], SHA384_CTX* context) {
1029 1029
 	}
1030 1030
 
1031 1031
 	/* Zero out state data */
1032
-	MEMSET_BZERO(context, sizeof(context));
1032
+	MEMSET_BZERO(context, sizeof(*context));
1033 1033
 }
1034 1034
 
1035 1035
 char *SHA384_End(SHA384_CTX* context, char buffer[]) {
... ...
@@ -1049,7 +1049,7 @@ char *SHA384_End(SHA384_CTX* context, char buffer[]) {
1049 1049
 		}
1050 1050
 		*buffer = (char)0;
1051 1051
 	} else {
1052
-		MEMSET_BZERO(context, sizeof(context));
1052
+		MEMSET_BZERO(context, sizeof(*context));
1053 1053
 	}
1054 1054
 	MEMSET_BZERO(digest, SHA384_DIGEST_LENGTH);
1055 1055
 	return buffer;
1056 1056
similarity index 100%
1057 1057
rename from sha256.h
1058 1058
rename to lib/srutils/sha256.h
... ...
@@ -47,7 +47,6 @@
47 47
 #include "../../parser/parse_nameaddr.h"
48 48
 
49 49
 #include "../../lib/kcore/strcommon.h"
50
-#include "../../sha2utils.h"
51 50
 #include "pv_trans.h"
52 51
 
53 52
 
... ...
@@ -240,31 +239,31 @@ int tr_eval_string(struct sip_msg *msg, tr_param_t *tp, int subtype,
240 239
 			if(!(val->flags&PV_VAL_STR))
241 240
 				val->rs.s = int2str(val->ri, &val->rs.len);
242 241
 			compute_sha256(_tr_buffer, (u_int8_t*)val->rs.s, val->rs.len);
243
-			_tr_buffer[SHA256_LEN] = '\0';
242
+			_tr_buffer[SHA256_DIGEST_STRING_LENGTH -1] = '\0';
244 243
 			val->flags = PV_VAL_STR;
245 244
 			val->ri = 0;
246 245
 			val->rs.s = _tr_buffer;
247
-			val->rs.len = SHA256_LEN;
246
+			val->rs.len = SHA256_DIGEST_STRING_LENGTH -1 ;
248 247
 			break;
249 248
 		case TR_S_SHA384:
250 249
 			if(!(val->flags&PV_VAL_STR))
251 250
 				val->rs.s = int2str(val->ri, &val->rs.len);
252 251
 			compute_sha384(_tr_buffer, (u_int8_t*)val->rs.s, val->rs.len);
253
-			_tr_buffer[SHA384_LEN] = '\0';
252
+			_tr_buffer[SHA384_DIGEST_STRING_LENGTH -1] = '\0';
254 253
 			val->flags = PV_VAL_STR;
255 254
 			val->ri = 0;
256 255
 			val->rs.s = _tr_buffer;
257
-			val->rs.len = SHA384_LEN;
256
+			val->rs.len = SHA384_DIGEST_STRING_LENGTH -1;
258 257
 			break;
259 258
 		case TR_S_SHA512:
260 259
 			if(!(val->flags&PV_VAL_STR))
261 260
 				val->rs.s = int2str(val->ri, &val->rs.len);
262 261
 			compute_sha512(_tr_buffer, (u_int8_t*)val->rs.s, val->rs.len);
263
-			_tr_buffer[SHA512_LEN] = '\0';
262
+			_tr_buffer[SHA512_DIGEST_STRING_LENGTH -1] = '\0';
264 263
 			val->flags = PV_VAL_STR;
265 264
 			val->ri = 0;
266 265
 			val->rs.s = _tr_buffer;
267
-			val->rs.len = SHA512_LEN;
266
+			val->rs.len = SHA512_DIGEST_STRING_LENGTH -1;
268 267
 			break;
269 268
 		case TR_S_ENCODEHEXA:
270 269
 			if(!(val->flags&PV_VAL_STR))
271 270
deleted file mode 100644
... ...
@@ -1,17 +0,0 @@
1
-/*!
2
- * \file
3
- * \brief SIP-router core :: sha2 hash support
4
- * \ingroup core
5
- * Module: \ref core
6
- */
7
-
8
-#ifndef _SHA2UTILS_H
9
-#define _SHA2UTILS_H
10
-
11
-#include "str.h"
12
-
13
-#define SHA256_LEN	 64
14
-#define SHA384_LEN	 96
15
-#define SHA512_LEN	128
16
-
17
-#endif /* _SHA2UTILS_H */