Andrew Boyson / crypto

Dependents:   oldheating gps motorhome heating

Revision:
9:f354b4859b0b
Child:
17:93feb2a51d58
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tls/tls-mac.c	Wed Sep 11 07:24:21 2019 +0000
@@ -0,0 +1,34 @@
+#include <stdint.h>
+#include "hmac-sha1.h"
+
+/*
+Calculate the MAC
+seq_num + TLSCompressed.type + TLSCompressed.version + TLSCompressed.length + TLSCompressed.fragment
+sequence='0000000000000000', rechdr='16 03 03', datalen='00 10'
+*/
+
+void TlsMacSha1(int      macKeyLength,
+                uint8_t* macKey,
+                uint64_t sequence,
+                uint8_t  contentType,
+                uint8_t  versionH,
+                uint8_t  versionL,
+                int      payloadLength,
+                uint8_t* payload,
+                uint8_t* mac)
+{
+    struct HmacSha1Struct md;
+    HmacSha1Start (&md, macKey, macKeyLength);
+    uint8_t prequel[8 + 5];
+    uint8_t *p = prequel;
+    for (int i = 0; i < 7; i++) *p++ = 0; //sequence
+    *p++ = sequence;
+    *p++ = contentType;
+    *p++ = versionH;
+    *p++ = versionL;
+    *p++ = payloadLength >> 8;
+    *p++ = payloadLength;
+    HmacSha1Add   (&md, prequel, sizeof(prequel));
+    HmacSha1Add   (&md, payload, payloadLength);
+    HmacSha1Finish(&md, mac);
+}
\ No newline at end of file