]> pd.if.org Git - zpackage/blob - crypto/hmac_message.c
remove stray debug fprintf
[zpackage] / crypto / hmac_message.c
1 #define _POSIX_C_SOURCE 200809L
2
3 #include <arpa/inet.h>
4
5 #include "tlse.h"
6
7 #ifndef htonll
8 #define htonll(x) ((1==htonl(1)) ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32))
9 #endif
10
11 #ifndef ntohll
12 #define ntohll(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32))
13 #endif
14
15 unsigned int tls_hmac_message(unsigned char local,
16                                struct TLSContext *context,
17                                const unsigned char *buf,
18                                int buf_len,
19                                const unsigned char *buf2,
20                                int buf_len2, unsigned char *out,
21                                unsigned int outlen
22                                ) {
23         hmac_state hash;
24         int hash_idx;
25
26         int mac_size = outlen;
27
28         if (mac_size == TLS_SHA1_MAC_SIZE) {
29                 hash_idx = find_hash("sha1");
30         } else if (mac_size == TLS_SHA384_MAC_SIZE) {
31                 hash_idx = find_hash("sha384");
32         } else {
33                 hash_idx = find_hash("sha256");
34         }
35
36         if (hmac_init(&hash, hash_idx,
37              local ? context->crypto.ctx_local_mac.local_mac : context->
38              crypto.ctx_remote_mac.remote_mac, mac_size)) {
39                 return 0;
40         }
41
42         uint64_t sn;
43         if (local) {
44                 sn = htonll(context->local_sequence_number);
45         } else {
46                 sn = htonll(context->remote_sequence_number);
47         }
48
49         if (hmac_process(&hash, (unsigned char *)&sn, sizeof sn)) {
50                 return 0;
51         }
52
53         if (hmac_process(&hash, buf, buf_len)) {
54                 return 0;
55         }
56
57         if (buf2 && buf_len2) {
58                 if (hmac_process(&hash, buf2, buf_len2)) {
59                         return 0;
60                 }
61         }
62         unsigned long ref_outlen = outlen;
63         if (hmac_done(&hash, out, &ref_outlen)) {
64                 return 0;
65         }
66
67         return (unsigned int)ref_outlen;
68 }
69