xref: /openbsd/usr.bin/ssh/hmac.c (revision c9831b39)
1*c9831b39Sjsg /* $OpenBSD: hmac.c,v 1.14 2020/02/26 13:40:09 jsg Exp $ */
2f7baae32Smarkus /*
3f7baae32Smarkus  * Copyright (c) 2014 Markus Friedl.  All rights reserved.
4f7baae32Smarkus  *
5f7baae32Smarkus  * Permission to use, copy, modify, and distribute this software for any
6f7baae32Smarkus  * purpose with or without fee is hereby granted, provided that the above
7f7baae32Smarkus  * copyright notice and this permission notice appear in all copies.
8f7baae32Smarkus  *
9f7baae32Smarkus  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10f7baae32Smarkus  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11f7baae32Smarkus  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12f7baae32Smarkus  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13f7baae32Smarkus  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14f7baae32Smarkus  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15f7baae32Smarkus  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16f7baae32Smarkus  */
17f7baae32Smarkus 
18f7baae32Smarkus #include <sys/types.h>
19f7baae32Smarkus #include <string.h>
20d04a6061Sdjm #include <stdlib.h>
21f7baae32Smarkus 
222dec1069Smarkus #include "sshbuf.h"
23f7baae32Smarkus #include "digest.h"
24f7baae32Smarkus #include "hmac.h"
25f7baae32Smarkus 
26f7baae32Smarkus struct ssh_hmac_ctx {
27f7baae32Smarkus 	int			 alg;
28f7baae32Smarkus 	struct ssh_digest_ctx	*ictx;
29f7baae32Smarkus 	struct ssh_digest_ctx	*octx;
30f7baae32Smarkus 	struct ssh_digest_ctx	*digest;
31f7baae32Smarkus 	u_char			*buf;
32f7baae32Smarkus 	size_t			 buf_len;
33f7baae32Smarkus };
34f7baae32Smarkus 
35f7baae32Smarkus size_t
ssh_hmac_bytes(int alg)36f7baae32Smarkus ssh_hmac_bytes(int alg)
37f7baae32Smarkus {
38f7baae32Smarkus 	return ssh_digest_bytes(alg);
39f7baae32Smarkus }
40f7baae32Smarkus 
41f7baae32Smarkus struct ssh_hmac_ctx *
ssh_hmac_start(int alg)42f7baae32Smarkus ssh_hmac_start(int alg)
43f7baae32Smarkus {
44f7baae32Smarkus 	struct ssh_hmac_ctx	*ret;
45f7baae32Smarkus 
46f7baae32Smarkus 	if ((ret = calloc(1, sizeof(*ret))) == NULL)
47f7baae32Smarkus 		return NULL;
48f7baae32Smarkus 	ret->alg = alg;
49f7baae32Smarkus 	if ((ret->ictx = ssh_digest_start(alg)) == NULL ||
50f7baae32Smarkus 	    (ret->octx = ssh_digest_start(alg)) == NULL ||
51f7baae32Smarkus 	    (ret->digest = ssh_digest_start(alg)) == NULL)
52f7baae32Smarkus 		goto fail;
53f7baae32Smarkus 	ret->buf_len = ssh_digest_blocksize(ret->ictx);
54f7baae32Smarkus 	if ((ret->buf = calloc(1, ret->buf_len)) == NULL)
55f7baae32Smarkus 		goto fail;
56f7baae32Smarkus 	return ret;
57f7baae32Smarkus fail:
58f7baae32Smarkus 	ssh_hmac_free(ret);
59f7baae32Smarkus 	return NULL;
60f7baae32Smarkus }
61f7baae32Smarkus 
62f7baae32Smarkus int
ssh_hmac_init(struct ssh_hmac_ctx * ctx,const void * key,size_t klen)63f7baae32Smarkus ssh_hmac_init(struct ssh_hmac_ctx *ctx, const void *key, size_t klen)
64f7baae32Smarkus {
65f7baae32Smarkus 	size_t i;
66f7baae32Smarkus 
67f7baae32Smarkus 	/* reset ictx and octx if no is key given */
68f7baae32Smarkus 	if (key != NULL) {
69f7baae32Smarkus 		/* truncate long keys */
70f7baae32Smarkus 		if (klen <= ctx->buf_len)
71f7baae32Smarkus 			memcpy(ctx->buf, key, klen);
72f7baae32Smarkus 		else if (ssh_digest_memory(ctx->alg, key, klen, ctx->buf,
73f7baae32Smarkus 		    ctx->buf_len) < 0)
74f7baae32Smarkus 			return -1;
75f7baae32Smarkus 		for (i = 0; i < ctx->buf_len; i++)
76f7baae32Smarkus 			ctx->buf[i] ^= 0x36;
77f7baae32Smarkus 		if (ssh_digest_update(ctx->ictx, ctx->buf, ctx->buf_len) < 0)
78f7baae32Smarkus 			return -1;
79f7baae32Smarkus 		for (i = 0; i < ctx->buf_len; i++)
80f7baae32Smarkus 			ctx->buf[i] ^= 0x36 ^ 0x5c;
81f7baae32Smarkus 		if (ssh_digest_update(ctx->octx, ctx->buf, ctx->buf_len) < 0)
82f7baae32Smarkus 			return -1;
8369881b76Stedu 		explicit_bzero(ctx->buf, ctx->buf_len);
84f7baae32Smarkus 	}
85f7baae32Smarkus 	/* start with ictx */
86f7baae32Smarkus 	if (ssh_digest_copy_state(ctx->ictx, ctx->digest) < 0)
87f7baae32Smarkus 		return -1;
88f7baae32Smarkus 	return 0;
89f7baae32Smarkus }
90f7baae32Smarkus 
91f7baae32Smarkus int
ssh_hmac_update(struct ssh_hmac_ctx * ctx,const void * m,size_t mlen)92f7baae32Smarkus ssh_hmac_update(struct ssh_hmac_ctx *ctx, const void *m, size_t mlen)
93f7baae32Smarkus {
94f7baae32Smarkus 	return ssh_digest_update(ctx->digest, m, mlen);
95f7baae32Smarkus }
96f7baae32Smarkus 
97f7baae32Smarkus int
ssh_hmac_update_buffer(struct ssh_hmac_ctx * ctx,const struct sshbuf * b)982dec1069Smarkus ssh_hmac_update_buffer(struct ssh_hmac_ctx *ctx, const struct sshbuf *b)
99f7baae32Smarkus {
100f7baae32Smarkus 	return ssh_digest_update_buffer(ctx->digest, b);
101f7baae32Smarkus }
102f7baae32Smarkus 
103f7baae32Smarkus int
ssh_hmac_final(struct ssh_hmac_ctx * ctx,u_char * d,size_t dlen)104f7baae32Smarkus ssh_hmac_final(struct ssh_hmac_ctx *ctx, u_char *d, size_t dlen)
105f7baae32Smarkus {
106f7baae32Smarkus 	size_t len;
107f7baae32Smarkus 
108f7baae32Smarkus 	len = ssh_digest_bytes(ctx->alg);
109f7baae32Smarkus 	if (dlen < len ||
110f7baae32Smarkus 	    ssh_digest_final(ctx->digest, ctx->buf, len))
111f7baae32Smarkus 		return -1;
112f7baae32Smarkus 	/* switch to octx */
113f7baae32Smarkus 	if (ssh_digest_copy_state(ctx->octx, ctx->digest) < 0 ||
114f7baae32Smarkus 	    ssh_digest_update(ctx->digest, ctx->buf, len) < 0 ||
115f7baae32Smarkus 	    ssh_digest_final(ctx->digest, d, dlen) < 0)
116f7baae32Smarkus 		return -1;
117f7baae32Smarkus 	return 0;
118f7baae32Smarkus }
119f7baae32Smarkus 
120f7baae32Smarkus void
ssh_hmac_free(struct ssh_hmac_ctx * ctx)121f7baae32Smarkus ssh_hmac_free(struct ssh_hmac_ctx *ctx)
122f7baae32Smarkus {
123f7baae32Smarkus 	if (ctx != NULL) {
124f7baae32Smarkus 		ssh_digest_free(ctx->ictx);
125f7baae32Smarkus 		ssh_digest_free(ctx->octx);
126f7baae32Smarkus 		ssh_digest_free(ctx->digest);
127f7baae32Smarkus 		if (ctx->buf) {
12869881b76Stedu 			explicit_bzero(ctx->buf, ctx->buf_len);
129f7baae32Smarkus 			free(ctx->buf);
130f7baae32Smarkus 		}
131*c9831b39Sjsg 		freezero(ctx, sizeof(*ctx));
132f7baae32Smarkus 	}
133f7baae32Smarkus }
134f7baae32Smarkus 
135f7baae32Smarkus #ifdef TEST
136f7baae32Smarkus 
137f7baae32Smarkus /* cc -DTEST hmac.c digest.c buffer.c cleanup.c fatal.c log.c xmalloc.c -lcrypto */
138f7baae32Smarkus static void
hmac_test(void * key,size_t klen,void * m,size_t mlen,u_char * e,size_t elen)139f7baae32Smarkus hmac_test(void *key, size_t klen, void *m, size_t mlen, u_char *e, size_t elen)
140f7baae32Smarkus {
141f7baae32Smarkus 	struct ssh_hmac_ctx	*ctx;
142f7baae32Smarkus 	size_t			 i;
143f7baae32Smarkus 	u_char			 digest[16];
144f7baae32Smarkus 
145f7baae32Smarkus 	if ((ctx = ssh_hmac_start(SSH_DIGEST_MD5)) == NULL)
146f7baae32Smarkus 		printf("ssh_hmac_start failed");
147f7baae32Smarkus 	if (ssh_hmac_init(ctx, key, klen) < 0 ||
148f7baae32Smarkus 	    ssh_hmac_update(ctx, m, mlen) < 0 ||
149f7baae32Smarkus 	    ssh_hmac_final(ctx, digest, sizeof(digest)) < 0)
150f7baae32Smarkus 		printf("ssh_hmac_xxx failed");
151f7baae32Smarkus 	ssh_hmac_free(ctx);
152f7baae32Smarkus 
153f7baae32Smarkus 	if (memcmp(e, digest, elen)) {
154f7baae32Smarkus 		for (i = 0; i < elen; i++)
155cfb5d775Smarkus 			printf("[%zu] %2.2x %2.2x\n", i, e[i], digest[i]);
156f7baae32Smarkus 		printf("mismatch\n");
157f7baae32Smarkus 	} else
158f7baae32Smarkus 		printf("ok\n");
159f7baae32Smarkus }
160f7baae32Smarkus 
161f7baae32Smarkus int
main(int argc,char ** argv)162f7baae32Smarkus main(int argc, char **argv)
163f7baae32Smarkus {
164f7baae32Smarkus 	/* try test vectors from RFC 2104 */
165f7baae32Smarkus 
166f7baae32Smarkus 	u_char key1[16] = {
167f7baae32Smarkus 	    0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb,
168f7baae32Smarkus 	    0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb };
169f7baae32Smarkus 	u_char *data1 = "Hi There";
170f7baae32Smarkus 	u_char dig1[16] = {
171f7baae32Smarkus 	    0x92, 0x94, 0x72, 0x7a, 0x36, 0x38, 0xbb, 0x1c,
172f7baae32Smarkus 	    0x13, 0xf4, 0x8e, 0xf8, 0x15, 0x8b, 0xfc, 0x9d };
173f7baae32Smarkus 
174f7baae32Smarkus 	u_char *key2 = "Jefe";
175f7baae32Smarkus 	u_char *data2 = "what do ya want for nothing?";
176f7baae32Smarkus 	u_char dig2[16] = {
177f7baae32Smarkus 	    0x75, 0x0c, 0x78, 0x3e, 0x6a, 0xb0, 0xb5, 0x03,
178f7baae32Smarkus 	    0xea, 0xa8, 0x6e, 0x31, 0x0a, 0x5d, 0xb7, 0x38 };
179f7baae32Smarkus 
180f7baae32Smarkus 	u_char key3[16];
181f7baae32Smarkus 	u_char data3[50];
182f7baae32Smarkus 	u_char dig3[16] = {
183f7baae32Smarkus 	    0x56, 0xbe, 0x34, 0x52, 0x1d, 0x14, 0x4c, 0x88,
184f7baae32Smarkus 	    0xdb, 0xb8, 0xc7, 0x33, 0xf0, 0xe8, 0xb3, 0xf6 };
185f7baae32Smarkus 	memset(key3, 0xaa, sizeof(key3));
186f7baae32Smarkus 	memset(data3, 0xdd, sizeof(data3));
187f7baae32Smarkus 
188f7baae32Smarkus 	hmac_test(key1, sizeof(key1), data1, strlen(data1), dig1, sizeof(dig1));
189f7baae32Smarkus 	hmac_test(key2, strlen(key2), data2, strlen(data2), dig2, sizeof(dig2));
190f7baae32Smarkus 	hmac_test(key3, sizeof(key3), data3, sizeof(data3), dig3, sizeof(dig3));
191f7baae32Smarkus 
192f7baae32Smarkus 	return 0;
193f7baae32Smarkus }
194f7baae32Smarkus 
195f7baae32Smarkus #endif
196