X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=crypto%2Fhmac_message.c;fp=crypto%2Fhmac_message.c;h=5f7249e964dccdad463cf4520553267914eebc92;hb=66bc25938679f1d6a1d1200f329093d82a5e99b4;hp=0000000000000000000000000000000000000000;hpb=a52ee0733f420ca20224049260d6fc5cf7d8f621;p=zpackage diff --git a/crypto/hmac_message.c b/crypto/hmac_message.c new file mode 100644 index 0000000..5f7249e --- /dev/null +++ b/crypto/hmac_message.c @@ -0,0 +1,69 @@ +#define _POSIX_C_SOURCE 200809L + +#include + +#include "tlse.h" + +#ifndef htonll +#define htonll(x) ((1==htonl(1)) ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32)) +#endif + +#ifndef ntohll +#define ntohll(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32)) +#endif + +unsigned int tls_hmac_message(unsigned char local, + struct TLSContext *context, + const unsigned char *buf, + int buf_len, + const unsigned char *buf2, + int buf_len2, unsigned char *out, + unsigned int outlen + ) { + hmac_state hash; + int hash_idx; + + int mac_size = outlen; + + if (mac_size == TLS_SHA1_MAC_SIZE) { + hash_idx = find_hash("sha1"); + } else if (mac_size == TLS_SHA384_MAC_SIZE) { + hash_idx = find_hash("sha384"); + } else { + hash_idx = find_hash("sha256"); + } + + if (hmac_init(&hash, hash_idx, + local ? context->crypto.ctx_local_mac.local_mac : context-> + crypto.ctx_remote_mac.remote_mac, mac_size)) { + return 0; + } + + uint64_t sn; + if (local) { + sn = htonll(context->local_sequence_number); + } else { + sn = htonll(context->remote_sequence_number); + } + + if (hmac_process(&hash, (unsigned char *)&sn, sizeof sn)) { + return 0; + } + + if (hmac_process(&hash, buf, buf_len)) { + return 0; + } + + if (buf2 && buf_len2) { + if (hmac_process(&hash, buf2, buf_len2)) { + return 0; + } + } + unsigned long ref_outlen = outlen; + if (hmac_done(&hash, out, &ref_outlen)) { + return 0; + } + + return (unsigned int)ref_outlen; +} +