xref: /openssh-portable/hmac.c (revision d5ba1c03)
1*d5ba1c03Sjsg@openbsd.org /* $OpenBSD: hmac.c,v 1.14 2020/02/26 13:40:09 jsg Exp $ */
24e8d937aSDamien Miller /*
34e8d937aSDamien Miller  * Copyright (c) 2014 Markus Friedl.  All rights reserved.
44e8d937aSDamien Miller  *
54e8d937aSDamien Miller  * Permission to use, copy, modify, and distribute this software for any
64e8d937aSDamien Miller  * purpose with or without fee is hereby granted, provided that the above
74e8d937aSDamien Miller  * copyright notice and this permission notice appear in all copies.
84e8d937aSDamien Miller  *
94e8d937aSDamien Miller  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
104e8d937aSDamien Miller  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
114e8d937aSDamien Miller  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
124e8d937aSDamien Miller  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
134e8d937aSDamien Miller  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
144e8d937aSDamien Miller  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
154e8d937aSDamien Miller  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
164e8d937aSDamien Miller  */
174e8d937aSDamien Miller 
184e8d937aSDamien Miller #include "includes.h"
194e8d937aSDamien Miller 
204e8d937aSDamien Miller #include <sys/types.h>
212ea60312SDarren Tucker 
222ea60312SDarren Tucker #include <stdlib.h>
234e8d937aSDamien Miller #include <string.h>
244e8d937aSDamien Miller 
25e17ac01fSmarkus@openbsd.org #include "sshbuf.h"
264e8d937aSDamien Miller #include "digest.h"
274e8d937aSDamien Miller #include "hmac.h"
284e8d937aSDamien Miller 
294e8d937aSDamien Miller struct ssh_hmac_ctx {
304e8d937aSDamien Miller 	int			 alg;
314e8d937aSDamien Miller 	struct ssh_digest_ctx	*ictx;
324e8d937aSDamien Miller 	struct ssh_digest_ctx	*octx;
334e8d937aSDamien Miller 	struct ssh_digest_ctx	*digest;
344e8d937aSDamien Miller 	u_char			*buf;
354e8d937aSDamien Miller 	size_t			 buf_len;
364e8d937aSDamien Miller };
374e8d937aSDamien Miller 
384e8d937aSDamien Miller size_t
ssh_hmac_bytes(int alg)394e8d937aSDamien Miller ssh_hmac_bytes(int alg)
404e8d937aSDamien Miller {
414e8d937aSDamien Miller 	return ssh_digest_bytes(alg);
424e8d937aSDamien Miller }
434e8d937aSDamien Miller 
444e8d937aSDamien Miller struct ssh_hmac_ctx *
ssh_hmac_start(int alg)454e8d937aSDamien Miller ssh_hmac_start(int alg)
464e8d937aSDamien Miller {
474e8d937aSDamien Miller 	struct ssh_hmac_ctx	*ret;
484e8d937aSDamien Miller 
494e8d937aSDamien Miller 	if ((ret = calloc(1, sizeof(*ret))) == NULL)
504e8d937aSDamien Miller 		return NULL;
514e8d937aSDamien Miller 	ret->alg = alg;
524e8d937aSDamien Miller 	if ((ret->ictx = ssh_digest_start(alg)) == NULL ||
534e8d937aSDamien Miller 	    (ret->octx = ssh_digest_start(alg)) == NULL ||
544e8d937aSDamien Miller 	    (ret->digest = ssh_digest_start(alg)) == NULL)
554e8d937aSDamien Miller 		goto fail;
564e8d937aSDamien Miller 	ret->buf_len = ssh_digest_blocksize(ret->ictx);
574e8d937aSDamien Miller 	if ((ret->buf = calloc(1, ret->buf_len)) == NULL)
584e8d937aSDamien Miller 		goto fail;
594e8d937aSDamien Miller 	return ret;
604e8d937aSDamien Miller fail:
614e8d937aSDamien Miller 	ssh_hmac_free(ret);
624e8d937aSDamien Miller 	return NULL;
634e8d937aSDamien Miller }
644e8d937aSDamien Miller 
654e8d937aSDamien Miller int
ssh_hmac_init(struct ssh_hmac_ctx * ctx,const void * key,size_t klen)664e8d937aSDamien Miller ssh_hmac_init(struct ssh_hmac_ctx *ctx, const void *key, size_t klen)
674e8d937aSDamien Miller {
684e8d937aSDamien Miller 	size_t i;
694e8d937aSDamien Miller 
704e8d937aSDamien Miller 	/* reset ictx and octx if no is key given */
714e8d937aSDamien Miller 	if (key != NULL) {
724e8d937aSDamien Miller 		/* truncate long keys */
734e8d937aSDamien Miller 		if (klen <= ctx->buf_len)
744e8d937aSDamien Miller 			memcpy(ctx->buf, key, klen);
754e8d937aSDamien Miller 		else if (ssh_digest_memory(ctx->alg, key, klen, ctx->buf,
764e8d937aSDamien Miller 		    ctx->buf_len) < 0)
774e8d937aSDamien Miller 			return -1;
784e8d937aSDamien Miller 		for (i = 0; i < ctx->buf_len; i++)
794e8d937aSDamien Miller 			ctx->buf[i] ^= 0x36;
804e8d937aSDamien Miller 		if (ssh_digest_update(ctx->ictx, ctx->buf, ctx->buf_len) < 0)
814e8d937aSDamien Miller 			return -1;
824e8d937aSDamien Miller 		for (i = 0; i < ctx->buf_len; i++)
834e8d937aSDamien Miller 			ctx->buf[i] ^= 0x36 ^ 0x5c;
844e8d937aSDamien Miller 		if (ssh_digest_update(ctx->octx, ctx->buf, ctx->buf_len) < 0)
854e8d937aSDamien Miller 			return -1;
864e8d937aSDamien Miller 		explicit_bzero(ctx->buf, ctx->buf_len);
874e8d937aSDamien Miller 	}
884e8d937aSDamien Miller 	/* start with ictx */
894e8d937aSDamien Miller 	if (ssh_digest_copy_state(ctx->ictx, ctx->digest) < 0)
904e8d937aSDamien Miller 		return -1;
914e8d937aSDamien Miller 	return 0;
924e8d937aSDamien Miller }
934e8d937aSDamien Miller 
944e8d937aSDamien Miller int
ssh_hmac_update(struct ssh_hmac_ctx * ctx,const void * m,size_t mlen)954e8d937aSDamien Miller ssh_hmac_update(struct ssh_hmac_ctx *ctx, const void *m, size_t mlen)
964e8d937aSDamien Miller {
974e8d937aSDamien Miller 	return ssh_digest_update(ctx->digest, m, mlen);
984e8d937aSDamien Miller }
994e8d937aSDamien Miller 
1004e8d937aSDamien Miller int
ssh_hmac_update_buffer(struct ssh_hmac_ctx * ctx,const struct sshbuf * b)101e17ac01fSmarkus@openbsd.org ssh_hmac_update_buffer(struct ssh_hmac_ctx *ctx, const struct sshbuf *b)
1024e8d937aSDamien Miller {
1034e8d937aSDamien Miller 	return ssh_digest_update_buffer(ctx->digest, b);
1044e8d937aSDamien Miller }
1054e8d937aSDamien Miller 
1064e8d937aSDamien Miller int
ssh_hmac_final(struct ssh_hmac_ctx * ctx,u_char * d,size_t dlen)1074e8d937aSDamien Miller ssh_hmac_final(struct ssh_hmac_ctx *ctx, u_char *d, size_t dlen)
1084e8d937aSDamien Miller {
1094e8d937aSDamien Miller 	size_t len;
1104e8d937aSDamien Miller 
1114e8d937aSDamien Miller 	len = ssh_digest_bytes(ctx->alg);
1124e8d937aSDamien Miller 	if (dlen < len ||
1134e8d937aSDamien Miller 	    ssh_digest_final(ctx->digest, ctx->buf, len))
1144e8d937aSDamien Miller 		return -1;
1154e8d937aSDamien Miller 	/* switch to octx */
1164e8d937aSDamien Miller 	if (ssh_digest_copy_state(ctx->octx, ctx->digest) < 0 ||
1174e8d937aSDamien Miller 	    ssh_digest_update(ctx->digest, ctx->buf, len) < 0 ||
1184e8d937aSDamien Miller 	    ssh_digest_final(ctx->digest, d, dlen) < 0)
1194e8d937aSDamien Miller 		return -1;
1204e8d937aSDamien Miller 	return 0;
1214e8d937aSDamien Miller }
1224e8d937aSDamien Miller 
1234e8d937aSDamien Miller void
ssh_hmac_free(struct ssh_hmac_ctx * ctx)1244e8d937aSDamien Miller ssh_hmac_free(struct ssh_hmac_ctx *ctx)
1254e8d937aSDamien Miller {
1264e8d937aSDamien Miller 	if (ctx != NULL) {
1274e8d937aSDamien Miller 		ssh_digest_free(ctx->ictx);
1284e8d937aSDamien Miller 		ssh_digest_free(ctx->octx);
1294e8d937aSDamien Miller 		ssh_digest_free(ctx->digest);
1304e8d937aSDamien Miller 		if (ctx->buf) {
1314e8d937aSDamien Miller 			explicit_bzero(ctx->buf, ctx->buf_len);
1324e8d937aSDamien Miller 			free(ctx->buf);
1334e8d937aSDamien Miller 		}
134*d5ba1c03Sjsg@openbsd.org 		freezero(ctx, sizeof(*ctx));
1354e8d937aSDamien Miller 	}
1364e8d937aSDamien Miller }
1374e8d937aSDamien Miller 
1384e8d937aSDamien Miller #ifdef TEST
1394e8d937aSDamien Miller 
1404e8d937aSDamien Miller /* cc -DTEST hmac.c digest.c buffer.c cleanup.c fatal.c log.c xmalloc.c -lcrypto */
1414e8d937aSDamien Miller static void
hmac_test(void * key,size_t klen,void * m,size_t mlen,u_char * e,size_t elen)1424e8d937aSDamien Miller hmac_test(void *key, size_t klen, void *m, size_t mlen, u_char *e, size_t elen)
1434e8d937aSDamien Miller {
1444e8d937aSDamien Miller 	struct ssh_hmac_ctx	*ctx;
1454e8d937aSDamien Miller 	size_t			 i;
1464e8d937aSDamien Miller 	u_char			 digest[16];
1474e8d937aSDamien Miller 
1484e8d937aSDamien Miller 	if ((ctx = ssh_hmac_start(SSH_DIGEST_MD5)) == NULL)
1494e8d937aSDamien Miller 		printf("ssh_hmac_start failed");
1504e8d937aSDamien Miller 	if (ssh_hmac_init(ctx, key, klen) < 0 ||
1514e8d937aSDamien Miller 	    ssh_hmac_update(ctx, m, mlen) < 0 ||
1524e8d937aSDamien Miller 	    ssh_hmac_final(ctx, digest, sizeof(digest)) < 0)
1534e8d937aSDamien Miller 		printf("ssh_hmac_xxx failed");
1544e8d937aSDamien Miller 	ssh_hmac_free(ctx);
1554e8d937aSDamien Miller 
1564e8d937aSDamien Miller 	if (memcmp(e, digest, elen)) {
1574e8d937aSDamien Miller 		for (i = 0; i < elen; i++)
158df100be5Smarkus@openbsd.org 			printf("[%zu] %2.2x %2.2x\n", i, e[i], digest[i]);
1594e8d937aSDamien Miller 		printf("mismatch\n");
1604e8d937aSDamien Miller 	} else
1614e8d937aSDamien Miller 		printf("ok\n");
1624e8d937aSDamien Miller }
1634e8d937aSDamien Miller 
1644e8d937aSDamien Miller int
main(int argc,char ** argv)1654e8d937aSDamien Miller main(int argc, char **argv)
1664e8d937aSDamien Miller {
1674e8d937aSDamien Miller 	/* try test vectors from RFC 2104 */
1684e8d937aSDamien Miller 
1694e8d937aSDamien Miller 	u_char key1[16] = {
1704e8d937aSDamien Miller 	    0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb,
1714e8d937aSDamien Miller 	    0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb };
1724e8d937aSDamien Miller 	u_char *data1 = "Hi There";
1734e8d937aSDamien Miller 	u_char dig1[16] = {
1744e8d937aSDamien Miller 	    0x92, 0x94, 0x72, 0x7a, 0x36, 0x38, 0xbb, 0x1c,
1754e8d937aSDamien Miller 	    0x13, 0xf4, 0x8e, 0xf8, 0x15, 0x8b, 0xfc, 0x9d };
1764e8d937aSDamien Miller 
1774e8d937aSDamien Miller 	u_char *key2 = "Jefe";
1784e8d937aSDamien Miller 	u_char *data2 = "what do ya want for nothing?";
1794e8d937aSDamien Miller 	u_char dig2[16] = {
1804e8d937aSDamien Miller 	    0x75, 0x0c, 0x78, 0x3e, 0x6a, 0xb0, 0xb5, 0x03,
1814e8d937aSDamien Miller 	    0xea, 0xa8, 0x6e, 0x31, 0x0a, 0x5d, 0xb7, 0x38 };
1824e8d937aSDamien Miller 
1834e8d937aSDamien Miller 	u_char key3[16];
1844e8d937aSDamien Miller 	u_char data3[50];
1854e8d937aSDamien Miller 	u_char dig3[16] = {
1864e8d937aSDamien Miller 	    0x56, 0xbe, 0x34, 0x52, 0x1d, 0x14, 0x4c, 0x88,
1874e8d937aSDamien Miller 	    0xdb, 0xb8, 0xc7, 0x33, 0xf0, 0xe8, 0xb3, 0xf6 };
1884e8d937aSDamien Miller 	memset(key3, 0xaa, sizeof(key3));
1894e8d937aSDamien Miller 	memset(data3, 0xdd, sizeof(data3));
1904e8d937aSDamien Miller 
1914e8d937aSDamien Miller 	hmac_test(key1, sizeof(key1), data1, strlen(data1), dig1, sizeof(dig1));
1924e8d937aSDamien Miller 	hmac_test(key2, strlen(key2), data2, strlen(data2), dig2, sizeof(dig2));
1934e8d937aSDamien Miller 	hmac_test(key3, sizeof(key3), data3, sizeof(data3), dig3, sizeof(dig3));
1944e8d937aSDamien Miller 
1954e8d937aSDamien Miller 	return 0;
1964e8d937aSDamien Miller }
1974e8d937aSDamien Miller 
1984e8d937aSDamien Miller #endif
199