xref: /dflybsd-src/crypto/openssh/hmac.c (revision 0cbfa66cdb87e23928a110d9b02839f403e32c11)
1*0cbfa66cSDaniel Fojt /* $OpenBSD: hmac.c,v 1.14 2020/02/26 13:40:09 jsg Exp $ */
236e94dc5SPeter Avalos /*
336e94dc5SPeter Avalos  * Copyright (c) 2014 Markus Friedl.  All rights reserved.
436e94dc5SPeter Avalos  *
536e94dc5SPeter Avalos  * Permission to use, copy, modify, and distribute this software for any
636e94dc5SPeter Avalos  * purpose with or without fee is hereby granted, provided that the above
736e94dc5SPeter Avalos  * copyright notice and this permission notice appear in all copies.
836e94dc5SPeter Avalos  *
936e94dc5SPeter Avalos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1036e94dc5SPeter Avalos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1136e94dc5SPeter Avalos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1236e94dc5SPeter Avalos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1336e94dc5SPeter Avalos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1436e94dc5SPeter Avalos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1536e94dc5SPeter Avalos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1636e94dc5SPeter Avalos  */
1736e94dc5SPeter Avalos 
1836e94dc5SPeter Avalos #include "includes.h"
1936e94dc5SPeter Avalos 
2036e94dc5SPeter Avalos #include <sys/types.h>
21*0cbfa66cSDaniel Fojt 
22*0cbfa66cSDaniel Fojt #include <stdlib.h>
2336e94dc5SPeter Avalos #include <string.h>
2436e94dc5SPeter Avalos 
25e9778795SPeter Avalos #include "sshbuf.h"
2636e94dc5SPeter Avalos #include "digest.h"
2736e94dc5SPeter Avalos #include "hmac.h"
2836e94dc5SPeter Avalos 
2936e94dc5SPeter Avalos struct ssh_hmac_ctx {
3036e94dc5SPeter Avalos 	int			 alg;
3136e94dc5SPeter Avalos 	struct ssh_digest_ctx	*ictx;
3236e94dc5SPeter Avalos 	struct ssh_digest_ctx	*octx;
3336e94dc5SPeter Avalos 	struct ssh_digest_ctx	*digest;
3436e94dc5SPeter Avalos 	u_char			*buf;
3536e94dc5SPeter Avalos 	size_t			 buf_len;
3636e94dc5SPeter Avalos };
3736e94dc5SPeter Avalos 
3836e94dc5SPeter Avalos size_t
ssh_hmac_bytes(int alg)3936e94dc5SPeter Avalos ssh_hmac_bytes(int alg)
4036e94dc5SPeter Avalos {
4136e94dc5SPeter Avalos 	return ssh_digest_bytes(alg);
4236e94dc5SPeter Avalos }
4336e94dc5SPeter Avalos 
4436e94dc5SPeter Avalos struct ssh_hmac_ctx *
ssh_hmac_start(int alg)4536e94dc5SPeter Avalos ssh_hmac_start(int alg)
4636e94dc5SPeter Avalos {
4736e94dc5SPeter Avalos 	struct ssh_hmac_ctx	*ret;
4836e94dc5SPeter Avalos 
4936e94dc5SPeter Avalos 	if ((ret = calloc(1, sizeof(*ret))) == NULL)
5036e94dc5SPeter Avalos 		return NULL;
5136e94dc5SPeter Avalos 	ret->alg = alg;
5236e94dc5SPeter Avalos 	if ((ret->ictx = ssh_digest_start(alg)) == NULL ||
5336e94dc5SPeter Avalos 	    (ret->octx = ssh_digest_start(alg)) == NULL ||
5436e94dc5SPeter Avalos 	    (ret->digest = ssh_digest_start(alg)) == NULL)
5536e94dc5SPeter Avalos 		goto fail;
5636e94dc5SPeter Avalos 	ret->buf_len = ssh_digest_blocksize(ret->ictx);
5736e94dc5SPeter Avalos 	if ((ret->buf = calloc(1, ret->buf_len)) == NULL)
5836e94dc5SPeter Avalos 		goto fail;
5936e94dc5SPeter Avalos 	return ret;
6036e94dc5SPeter Avalos fail:
6136e94dc5SPeter Avalos 	ssh_hmac_free(ret);
6236e94dc5SPeter Avalos 	return NULL;
6336e94dc5SPeter Avalos }
6436e94dc5SPeter Avalos 
6536e94dc5SPeter Avalos int
ssh_hmac_init(struct ssh_hmac_ctx * ctx,const void * key,size_t klen)6636e94dc5SPeter Avalos ssh_hmac_init(struct ssh_hmac_ctx *ctx, const void *key, size_t klen)
6736e94dc5SPeter Avalos {
6836e94dc5SPeter Avalos 	size_t i;
6936e94dc5SPeter Avalos 
7036e94dc5SPeter Avalos 	/* reset ictx and octx if no is key given */
7136e94dc5SPeter Avalos 	if (key != NULL) {
7236e94dc5SPeter Avalos 		/* truncate long keys */
7336e94dc5SPeter Avalos 		if (klen <= ctx->buf_len)
7436e94dc5SPeter Avalos 			memcpy(ctx->buf, key, klen);
7536e94dc5SPeter Avalos 		else if (ssh_digest_memory(ctx->alg, key, klen, ctx->buf,
7636e94dc5SPeter Avalos 		    ctx->buf_len) < 0)
7736e94dc5SPeter Avalos 			return -1;
7836e94dc5SPeter Avalos 		for (i = 0; i < ctx->buf_len; i++)
7936e94dc5SPeter Avalos 			ctx->buf[i] ^= 0x36;
8036e94dc5SPeter Avalos 		if (ssh_digest_update(ctx->ictx, ctx->buf, ctx->buf_len) < 0)
8136e94dc5SPeter Avalos 			return -1;
8236e94dc5SPeter Avalos 		for (i = 0; i < ctx->buf_len; i++)
8336e94dc5SPeter Avalos 			ctx->buf[i] ^= 0x36 ^ 0x5c;
8436e94dc5SPeter Avalos 		if (ssh_digest_update(ctx->octx, ctx->buf, ctx->buf_len) < 0)
8536e94dc5SPeter Avalos 			return -1;
8636e94dc5SPeter Avalos 		explicit_bzero(ctx->buf, ctx->buf_len);
8736e94dc5SPeter Avalos 	}
8836e94dc5SPeter Avalos 	/* start with ictx */
8936e94dc5SPeter Avalos 	if (ssh_digest_copy_state(ctx->ictx, ctx->digest) < 0)
9036e94dc5SPeter Avalos 		return -1;
9136e94dc5SPeter Avalos 	return 0;
9236e94dc5SPeter Avalos }
9336e94dc5SPeter Avalos 
9436e94dc5SPeter Avalos int
ssh_hmac_update(struct ssh_hmac_ctx * ctx,const void * m,size_t mlen)9536e94dc5SPeter Avalos ssh_hmac_update(struct ssh_hmac_ctx *ctx, const void *m, size_t mlen)
9636e94dc5SPeter Avalos {
9736e94dc5SPeter Avalos 	return ssh_digest_update(ctx->digest, m, mlen);
9836e94dc5SPeter Avalos }
9936e94dc5SPeter Avalos 
10036e94dc5SPeter Avalos int
ssh_hmac_update_buffer(struct ssh_hmac_ctx * ctx,const struct sshbuf * b)101e9778795SPeter Avalos ssh_hmac_update_buffer(struct ssh_hmac_ctx *ctx, const struct sshbuf *b)
10236e94dc5SPeter Avalos {
10336e94dc5SPeter Avalos 	return ssh_digest_update_buffer(ctx->digest, b);
10436e94dc5SPeter Avalos }
10536e94dc5SPeter Avalos 
10636e94dc5SPeter Avalos int
ssh_hmac_final(struct ssh_hmac_ctx * ctx,u_char * d,size_t dlen)10736e94dc5SPeter Avalos ssh_hmac_final(struct ssh_hmac_ctx *ctx, u_char *d, size_t dlen)
10836e94dc5SPeter Avalos {
10936e94dc5SPeter Avalos 	size_t len;
11036e94dc5SPeter Avalos 
11136e94dc5SPeter Avalos 	len = ssh_digest_bytes(ctx->alg);
11236e94dc5SPeter Avalos 	if (dlen < len ||
11336e94dc5SPeter Avalos 	    ssh_digest_final(ctx->digest, ctx->buf, len))
11436e94dc5SPeter Avalos 		return -1;
11536e94dc5SPeter Avalos 	/* switch to octx */
11636e94dc5SPeter Avalos 	if (ssh_digest_copy_state(ctx->octx, ctx->digest) < 0 ||
11736e94dc5SPeter Avalos 	    ssh_digest_update(ctx->digest, ctx->buf, len) < 0 ||
11836e94dc5SPeter Avalos 	    ssh_digest_final(ctx->digest, d, dlen) < 0)
11936e94dc5SPeter Avalos 		return -1;
12036e94dc5SPeter Avalos 	return 0;
12136e94dc5SPeter Avalos }
12236e94dc5SPeter Avalos 
12336e94dc5SPeter Avalos void
ssh_hmac_free(struct ssh_hmac_ctx * ctx)12436e94dc5SPeter Avalos ssh_hmac_free(struct ssh_hmac_ctx *ctx)
12536e94dc5SPeter Avalos {
12636e94dc5SPeter Avalos 	if (ctx != NULL) {
12736e94dc5SPeter Avalos 		ssh_digest_free(ctx->ictx);
12836e94dc5SPeter Avalos 		ssh_digest_free(ctx->octx);
12936e94dc5SPeter Avalos 		ssh_digest_free(ctx->digest);
13036e94dc5SPeter Avalos 		if (ctx->buf) {
13136e94dc5SPeter Avalos 			explicit_bzero(ctx->buf, ctx->buf_len);
13236e94dc5SPeter Avalos 			free(ctx->buf);
13336e94dc5SPeter Avalos 		}
134*0cbfa66cSDaniel Fojt 		freezero(ctx, sizeof(*ctx));
13536e94dc5SPeter Avalos 	}
13636e94dc5SPeter Avalos }
13736e94dc5SPeter Avalos 
13836e94dc5SPeter Avalos #ifdef TEST
13936e94dc5SPeter Avalos 
14036e94dc5SPeter Avalos /* cc -DTEST hmac.c digest.c buffer.c cleanup.c fatal.c log.c xmalloc.c -lcrypto */
14136e94dc5SPeter Avalos static void
hmac_test(void * key,size_t klen,void * m,size_t mlen,u_char * e,size_t elen)14236e94dc5SPeter Avalos hmac_test(void *key, size_t klen, void *m, size_t mlen, u_char *e, size_t elen)
14336e94dc5SPeter Avalos {
14436e94dc5SPeter Avalos 	struct ssh_hmac_ctx	*ctx;
14536e94dc5SPeter Avalos 	size_t			 i;
14636e94dc5SPeter Avalos 	u_char			 digest[16];
14736e94dc5SPeter Avalos 
14836e94dc5SPeter Avalos 	if ((ctx = ssh_hmac_start(SSH_DIGEST_MD5)) == NULL)
14936e94dc5SPeter Avalos 		printf("ssh_hmac_start failed");
15036e94dc5SPeter Avalos 	if (ssh_hmac_init(ctx, key, klen) < 0 ||
15136e94dc5SPeter Avalos 	    ssh_hmac_update(ctx, m, mlen) < 0 ||
15236e94dc5SPeter Avalos 	    ssh_hmac_final(ctx, digest, sizeof(digest)) < 0)
15336e94dc5SPeter Avalos 		printf("ssh_hmac_xxx failed");
15436e94dc5SPeter Avalos 	ssh_hmac_free(ctx);
15536e94dc5SPeter Avalos 
15636e94dc5SPeter Avalos 	if (memcmp(e, digest, elen)) {
15736e94dc5SPeter Avalos 		for (i = 0; i < elen; i++)
158e9778795SPeter Avalos 			printf("[%zu] %2.2x %2.2x\n", i, e[i], digest[i]);
15936e94dc5SPeter Avalos 		printf("mismatch\n");
16036e94dc5SPeter Avalos 	} else
16136e94dc5SPeter Avalos 		printf("ok\n");
16236e94dc5SPeter Avalos }
16336e94dc5SPeter Avalos 
16436e94dc5SPeter Avalos int
main(int argc,char ** argv)16536e94dc5SPeter Avalos main(int argc, char **argv)
16636e94dc5SPeter Avalos {
16736e94dc5SPeter Avalos 	/* try test vectors from RFC 2104 */
16836e94dc5SPeter Avalos 
16936e94dc5SPeter Avalos 	u_char key1[16] = {
17036e94dc5SPeter Avalos 	    0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb,
17136e94dc5SPeter Avalos 	    0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb };
17236e94dc5SPeter Avalos 	u_char *data1 = "Hi There";
17336e94dc5SPeter Avalos 	u_char dig1[16] = {
17436e94dc5SPeter Avalos 	    0x92, 0x94, 0x72, 0x7a, 0x36, 0x38, 0xbb, 0x1c,
17536e94dc5SPeter Avalos 	    0x13, 0xf4, 0x8e, 0xf8, 0x15, 0x8b, 0xfc, 0x9d };
17636e94dc5SPeter Avalos 
17736e94dc5SPeter Avalos 	u_char *key2 = "Jefe";
17836e94dc5SPeter Avalos 	u_char *data2 = "what do ya want for nothing?";
17936e94dc5SPeter Avalos 	u_char dig2[16] = {
18036e94dc5SPeter Avalos 	    0x75, 0x0c, 0x78, 0x3e, 0x6a, 0xb0, 0xb5, 0x03,
18136e94dc5SPeter Avalos 	    0xea, 0xa8, 0x6e, 0x31, 0x0a, 0x5d, 0xb7, 0x38 };
18236e94dc5SPeter Avalos 
18336e94dc5SPeter Avalos 	u_char key3[16];
18436e94dc5SPeter Avalos 	u_char data3[50];
18536e94dc5SPeter Avalos 	u_char dig3[16] = {
18636e94dc5SPeter Avalos 	    0x56, 0xbe, 0x34, 0x52, 0x1d, 0x14, 0x4c, 0x88,
18736e94dc5SPeter Avalos 	    0xdb, 0xb8, 0xc7, 0x33, 0xf0, 0xe8, 0xb3, 0xf6 };
18836e94dc5SPeter Avalos 	memset(key3, 0xaa, sizeof(key3));
18936e94dc5SPeter Avalos 	memset(data3, 0xdd, sizeof(data3));
19036e94dc5SPeter Avalos 
19136e94dc5SPeter Avalos 	hmac_test(key1, sizeof(key1), data1, strlen(data1), dig1, sizeof(dig1));
19236e94dc5SPeter Avalos 	hmac_test(key2, strlen(key2), data2, strlen(data2), dig2, sizeof(dig2));
19336e94dc5SPeter Avalos 	hmac_test(key3, sizeof(key3), data3, sizeof(data3), dig3, sizeof(dig3));
19436e94dc5SPeter Avalos 
19536e94dc5SPeter Avalos 	return 0;
19636e94dc5SPeter Avalos }
19736e94dc5SPeter Avalos 
19836e94dc5SPeter Avalos #endif
199