summaryrefslogtreecommitdiffstats
path: root/src/lib/hmac-cram-md5.c
blob: 46d73c416094b0ec60f1cbc5cb34722b20640ea9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
 * CRAM-MD5 (RFC 2195) compatibility code
 * Copyright (c) 2003 Joshua Goodall <joshua@roughtrade.net>
 *
 * This software is released under the MIT license.
 */

#include "lib.h"
#include "md5.h"
#include "hmac-cram-md5.h"

void hmac_md5_get_cram_context(struct hmac_context *_hmac_ctx,
			unsigned char context_digest[CRAM_MD5_CONTEXTLEN])
{
	struct hmac_context_priv *hmac_ctx = &_hmac_ctx->u.priv;
	unsigned char *cdp;

	struct md5_context *ctx = (void*)hmac_ctx->ctx;
	struct md5_context *ctxo = (void*)hmac_ctx->ctxo;

#define CDPUT(p, c) STMT_START {   \
	*(p)++ = (c) & 0xff;       \
	*(p)++ = (c) >> 8 & 0xff;  \
	*(p)++ = (c) >> 16 & 0xff; \
	*(p)++ = (c) >> 24 & 0xff; \
} STMT_END
	cdp = context_digest;
	CDPUT(cdp, ctxo->a);
	CDPUT(cdp, ctxo->b);
	CDPUT(cdp, ctxo->c);
	CDPUT(cdp, ctxo->d);
	CDPUT(cdp, ctx->a);
	CDPUT(cdp, ctx->b);
	CDPUT(cdp, ctx->c);
	CDPUT(cdp, ctx->d);
}

void hmac_md5_set_cram_context(struct hmac_context *_hmac_ctx,
			const unsigned char context_digest[CRAM_MD5_CONTEXTLEN])
{
	struct hmac_context_priv *hmac_ctx = &_hmac_ctx->u.priv;
	const unsigned char *cdp;

	struct md5_context *ctx = (void*)hmac_ctx->ctx;
	struct md5_context *ctxo = (void*)hmac_ctx->ctxo;

#define CDGET(p, c) STMT_START { \
	(c)  = (*p++);           \
	(c) += (*p++ << 8);      \
	(c) += (*p++ << 16);     \
	(c) += ((uint32_t)(*p++) << 24);     \
} STMT_END
	cdp = context_digest;
	CDGET(cdp, ctxo->a);
	CDGET(cdp, ctxo->b);
	CDGET(cdp, ctxo->c);
	CDGET(cdp, ctxo->d);
	CDGET(cdp, ctx->a);
	CDGET(cdp, ctx->b);
	CDGET(cdp, ctx->c);
	CDGET(cdp, ctx->d);

	ctxo->lo = ctx->lo = 64;
	ctxo->hi = ctx->hi = 0;
}