#define subround(f,w,x,y,z,k,s,ti) \
w = x + rol(w + f(x,y,z) + block[k] + ti, s)
-void MD5_Core_Init(MD5_Core_State * s)
+static void MD5_Core_Init(MD5_Core_State * s)
{
s->h[0] = 0x67452301;
s->h[1] = 0xefcdab89;
s->h[3] = 0x10325476;
}
-void MD5_Block(MD5_Core_State * s, uint32 * block)
+static void MD5_Block(MD5_Core_State * s, uint32 * block)
{
uint32 a, b, c, d;
}
}
+void MD5Simple(void const *p, unsigned len, unsigned char output[16])
+{
+ struct MD5Context s;
+
+ MD5Init(&s);
+ MD5Update(&s, (unsigned char const *)p, len);
+ MD5Final(output, &s);
+ smemclr(&s, sizeof(s));
+}
+
/* ----------------------------------------------------------------------
* The above is the MD5 algorithm itself. Now we implement the
* HMAC wrapper on it.
+ *
+ * Some of these functions are exported directly, because they are
+ * useful elsewhere (SOCKS5 CHAP authentication uses HMAC-MD5).
*/
-static struct MD5Context md5_cs_mac_s1, md5_cs_mac_s2;
-static struct MD5Context md5_sc_mac_s1, md5_sc_mac_s2;
+void *hmacmd5_make_context(void *cipher_ctx)
+{
+ return snewn(3, struct MD5Context);
+}
-static void md5_key(struct MD5Context *s1, struct MD5Context *s2,
- unsigned char *key, int len)
+void hmacmd5_free_context(void *handle)
{
+ smemclr(handle, 3*sizeof(struct MD5Context));
+ sfree(handle);
+}
+
+void hmacmd5_key(void *handle, void const *keyv, int len)
+{
+ struct MD5Context *keys = (struct MD5Context *)handle;
unsigned char foo[64];
+ unsigned char const *key = (unsigned char const *)keyv;
int i;
memset(foo, 0x36, 64);
for (i = 0; i < len && i < 64; i++)
foo[i] ^= key[i];
- MD5Init(s1);
- MD5Update(s1, foo, 64);
+ MD5Init(&keys[0]);
+ MD5Update(&keys[0], foo, 64);
memset(foo, 0x5C, 64);
for (i = 0; i < len && i < 64; i++)
foo[i] ^= key[i];
- MD5Init(s2);
- MD5Update(s2, foo, 64);
+ MD5Init(&keys[1]);
+ MD5Update(&keys[1], foo, 64);
- memset(foo, 0, 64); /* burn the evidence */
+ smemclr(foo, 64); /* burn the evidence */
}
-static void md5_cskey(unsigned char *key)
+static void hmacmd5_key_16(void *handle, unsigned char *key)
{
- md5_key(&md5_cs_mac_s1, &md5_cs_mac_s2, key, 16);
+ hmacmd5_key(handle, key, 16);
}
-static void md5_sckey(unsigned char *key)
+static void hmacmd5_start(void *handle)
{
- md5_key(&md5_sc_mac_s1, &md5_sc_mac_s2, key, 16);
+ struct MD5Context *keys = (struct MD5Context *)handle;
+
+ keys[2] = keys[0]; /* structure copy */
}
-static void md5_do_hmac(struct MD5Context *s1, struct MD5Context *s2,
- unsigned char *blk, int len, unsigned long seq,
- unsigned char *hmac)
+static void hmacmd5_bytes(void *handle, unsigned char const *blk, int len)
{
+ struct MD5Context *keys = (struct MD5Context *)handle;
+ MD5Update(&keys[2], blk, len);
+}
+
+static void hmacmd5_genresult(void *handle, unsigned char *hmac)
+{
+ struct MD5Context *keys = (struct MD5Context *)handle;
struct MD5Context s;
unsigned char intermediate[16];
- intermediate[0] = (unsigned char) ((seq >> 24) & 0xFF);
- intermediate[1] = (unsigned char) ((seq >> 16) & 0xFF);
- intermediate[2] = (unsigned char) ((seq >> 8) & 0xFF);
- intermediate[3] = (unsigned char) ((seq) & 0xFF);
-
- s = *s1; /* structure copy */
- MD5Update(&s, intermediate, 4);
- MD5Update(&s, blk, len);
+ s = keys[2]; /* structure copy */
MD5Final(intermediate, &s);
- s = *s2; /* structure copy */
+ s = keys[1]; /* structure copy */
MD5Update(&s, intermediate, 16);
MD5Final(hmac, &s);
}
-static void md5_generate(unsigned char *blk, int len, unsigned long seq)
+static int hmacmd5_verresult(void *handle, unsigned char const *hmac)
+{
+ unsigned char correct[16];
+ hmacmd5_genresult(handle, correct);
+ return smemeq(correct, hmac, 16);
+}
+
+static void hmacmd5_do_hmac_internal(void *handle,
+ unsigned char const *blk, int len,
+ unsigned char const *blk2, int len2,
+ unsigned char *hmac)
+{
+ hmacmd5_start(handle);
+ hmacmd5_bytes(handle, blk, len);
+ if (blk2) hmacmd5_bytes(handle, blk2, len2);
+ hmacmd5_genresult(handle, hmac);
+}
+
+void hmacmd5_do_hmac(void *handle, unsigned char const *blk, int len,
+ unsigned char *hmac)
+{
+ hmacmd5_do_hmac_internal(handle, blk, len, NULL, 0, hmac);
+}
+
+static void hmacmd5_do_hmac_ssh(void *handle, unsigned char const *blk, int len,
+ unsigned long seq, unsigned char *hmac)
+{
+ unsigned char seqbuf[16];
+
+ PUT_32BIT_MSB_FIRST(seqbuf, seq);
+ hmacmd5_do_hmac_internal(handle, seqbuf, 4, blk, len, hmac);
+}
+
+static void hmacmd5_generate(void *handle, unsigned char *blk, int len,
+ unsigned long seq)
{
- md5_do_hmac(&md5_cs_mac_s1, &md5_cs_mac_s2, blk, len, seq, blk + len);
+ hmacmd5_do_hmac_ssh(handle, blk, len, seq, blk + len);
}
-static int md5_verify(unsigned char *blk, int len, unsigned long seq)
+static int hmacmd5_verify(void *handle, unsigned char *blk, int len,
+ unsigned long seq)
{
unsigned char correct[16];
- md5_do_hmac(&md5_sc_mac_s1, &md5_sc_mac_s2, blk, len, seq, correct);
- return !memcmp(correct, blk + len, 16);
+ hmacmd5_do_hmac_ssh(handle, blk, len, seq, correct);
+ return smemeq(correct, blk + len, 16);
}
-const struct ssh_mac ssh_md5 = {
- md5_cskey, md5_sckey,
- md5_generate,
- md5_verify,
- "hmac-md5",
- 16
+const struct ssh_mac ssh_hmac_md5 = {
+ hmacmd5_make_context, hmacmd5_free_context, hmacmd5_key_16,
+ hmacmd5_generate, hmacmd5_verify,
+ hmacmd5_start, hmacmd5_bytes, hmacmd5_genresult, hmacmd5_verresult,
+ "hmac-md5", "hmac-md5-etm@openssh.com",
+ 16, 16,
+ "HMAC-MD5"
};