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