Lines 1-100
Link Here
|
1 |
--- librtmp/handshake.h.orig 2016-02-29 01:15:13 UTC |
|
|
2 |
+++ librtmp/handshake.h |
3 |
@@ -31,9 +31,13 @@ |
4 |
#define SHA256_DIGEST_LENGTH 32 |
5 |
#endif |
6 |
#define HMAC_CTX sha2_context |
7 |
-#define HMAC_setup(ctx, key, len) sha2_hmac_starts(&ctx, (unsigned char *)key, len, 0) |
8 |
-#define HMAC_crunch(ctx, buf, len) sha2_hmac_update(&ctx, buf, len) |
9 |
-#define HMAC_finish(ctx, dig, dlen) dlen = SHA256_DIGEST_LENGTH; sha2_hmac_finish(&ctx, dig) |
10 |
+#define HMAC_setup(ctx, key, len) do { \ |
11 |
+ if (ctx == NULL) \ |
12 |
+ ctx = calloc(1, sizeof(*ctx)); \ |
13 |
+ sha2_hmac_starts(ctx, (unsigned char *)key, len, 0); \ |
14 |
+ } while (0) |
15 |
+#define HMAC_crunch(ctx, buf, len) sha2_hmac_update(ctx, buf, len) |
16 |
+#define HMAC_finish(ctx, dig, dlen) dlen = SHA256_DIGEST_LENGTH; sha2_hmac_finish(ctx, dig) |
17 |
|
18 |
typedef arc4_context * RC4_handle; |
19 |
#define RC4_alloc(h) *h = malloc(sizeof(arc4_context)) |
20 |
@@ -50,10 +54,17 @@ typedef arc4_context * RC4_handle; |
21 |
#endif |
22 |
#undef HMAC_CTX |
23 |
#define HMAC_CTX struct hmac_sha256_ctx |
24 |
-#define HMAC_setup(ctx, key, len) hmac_sha256_set_key(&ctx, len, key) |
25 |
-#define HMAC_crunch(ctx, buf, len) hmac_sha256_update(&ctx, len, buf) |
26 |
-#define HMAC_finish(ctx, dig, dlen) dlen = SHA256_DIGEST_LENGTH; hmac_sha256_digest(&ctx, SHA256_DIGEST_LENGTH, dig) |
27 |
-#define HMAC_close(ctx) |
28 |
+#define HMAC_setup(ctx, key, len) do { \ |
29 |
+ if (ctx == NULL) \ |
30 |
+ ctx = calloc(1, sizeof(*ctx)); \ |
31 |
+ hmac_sha256_set_key(ctx, len, key); \ |
32 |
+ } while (0) |
33 |
+#define HMAC_crunch(ctx, buf, len) hmac_sha256_update(ctx, len, buf) |
34 |
+#define HMAC_finish(ctx, dig, dlen) dlen = SHA256_DIGEST_LENGTH; hmac_sha256_digest(ctx, SHA256_DIGEST_LENGTH, dig) |
35 |
+#define HMAC_close(ctx) do { \ |
36 |
+ free(ctx); \ |
37 |
+ ctx = NULL; \ |
38 |
+ } while (0) |
39 |
|
40 |
typedef struct arcfour_ctx* RC4_handle; |
41 |
#define RC4_alloc(h) *h = malloc(sizeof(struct arcfour_ctx)) |
42 |
@@ -69,9 +80,37 @@ typedef struct arcfour_ctx* RC4_handle; |
43 |
#if OPENSSL_VERSION_NUMBER < 0x0090800 || !defined(SHA256_DIGEST_LENGTH) |
44 |
#error Your OpenSSL is too old, need 0.9.8 or newer with SHA256 |
45 |
#endif |
46 |
-#define HMAC_setup(ctx, key, len) HMAC_CTX_init(&ctx); HMAC_Init_ex(&ctx, key, len, EVP_sha256(), 0) |
47 |
-#define HMAC_crunch(ctx, buf, len) HMAC_Update(&ctx, buf, len) |
48 |
-#define HMAC_finish(ctx, dig, dlen) HMAC_Final(&ctx, dig, &dlen); HMAC_CTX_cleanup(&ctx) |
49 |
+#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
50 |
+#define HMAC_setup(ctx, key, len) do { \ |
51 |
+ if (ctx == NULL) \ |
52 |
+ ctx = calloc(1, sizeof(*ctx)); \ |
53 |
+ HMAC_CTX_init(ctx); \ |
54 |
+ HMAC_Init_ex(ctx, key, len, EVP_sha256(), 0); \ |
55 |
+ } while (0) |
56 |
+#else |
57 |
+#define HMAC_setup(ctx, key, len) do { \ |
58 |
+ if (ctx == NULL) \ |
59 |
+ ctx = HMAC_CTX_new(); \ |
60 |
+ else \ |
61 |
+ HMAC_CTX_reset(ctx); \ |
62 |
+ HMAC_Init_ex(ctx, key, len, EVP_sha256(), 0); \ |
63 |
+ } while (0) |
64 |
+#endif |
65 |
+#define HMAC_crunch(ctx, buf, len) HMAC_Update(ctx, buf, len) |
66 |
+#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
67 |
+#define HMAC_finish(ctx, dig, dlen) do { \ |
68 |
+ HMAC_Final(ctx, dig, &dlen); \ |
69 |
+ HMAC_CTX_cleanup(ctx); \ |
70 |
+ free(ctx); \ |
71 |
+ ctx = NULL; \ |
72 |
+ } while (0) |
73 |
+#else |
74 |
+#define HMAC_finish(ctx, dig, dlen) do { \ |
75 |
+ HMAC_Final(ctx, dig, &dlen); \ |
76 |
+ HMAC_CTX_free(ctx); \ |
77 |
+ ctx = NULL; \ |
78 |
+ } while (0) |
79 |
+#endif |
80 |
|
81 |
typedef RC4_KEY * RC4_handle; |
82 |
#define RC4_alloc(h) *h = malloc(sizeof(RC4_KEY)) |
83 |
@@ -117,7 +156,7 @@ static void InitRC4Encryption |
84 |
{ |
85 |
uint8_t digest[SHA256_DIGEST_LENGTH]; |
86 |
unsigned int digestLen = 0; |
87 |
- HMAC_CTX ctx; |
88 |
+ HMAC_CTX *ctx = NULL; |
89 |
|
90 |
RC4_alloc(rc4keyIn); |
91 |
RC4_alloc(rc4keyOut); |
92 |
@@ -266,7 +305,7 @@ HMACsha256(const uint8_t *message, size_ |
93 |
size_t keylen, uint8_t *digest) |
94 |
{ |
95 |
unsigned int digestLen; |
96 |
- HMAC_CTX ctx; |
97 |
+ HMAC_CTX *ctx = NULL; |
98 |
|
99 |
HMAC_setup(ctx, key, keylen); |
100 |
HMAC_crunch(ctx, message, messageLen); |