jcs's openbsd hax
openbsd
1/* $OpenBSD: hmac.c,v 1.16 2026/02/14 00:18:34 jsg Exp $ */
2/*
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 <sys/types.h>
19
20#include <stdlib.h>
21#include <string.h>
22
23#include "digest.h"
24#include "hmac.h"
25
26struct ssh_hmac_ctx {
27 int alg;
28 struct ssh_digest_ctx *ictx;
29 struct ssh_digest_ctx *octx;
30 struct ssh_digest_ctx *digest;
31 u_char *buf;
32 size_t buf_len;
33};
34
35size_t
36ssh_hmac_bytes(int alg)
37{
38 return ssh_digest_bytes(alg);
39}
40
41struct ssh_hmac_ctx *
42ssh_hmac_start(int alg)
43{
44 struct ssh_hmac_ctx *ret;
45
46 if ((ret = calloc(1, sizeof(*ret))) == NULL)
47 return NULL;
48 ret->alg = alg;
49 if ((ret->ictx = ssh_digest_start(alg)) == NULL ||
50 (ret->octx = ssh_digest_start(alg)) == NULL ||
51 (ret->digest = ssh_digest_start(alg)) == NULL)
52 goto fail;
53 ret->buf_len = ssh_digest_blocksize(ret->ictx);
54 if ((ret->buf = calloc(1, ret->buf_len)) == NULL)
55 goto fail;
56 return ret;
57fail:
58 ssh_hmac_free(ret);
59 return NULL;
60}
61
62int
63ssh_hmac_init(struct ssh_hmac_ctx *ctx, const void *key, size_t klen)
64{
65 size_t i;
66
67 /* reset ictx and octx if no is key given */
68 if (key != NULL) {
69 /* truncate long keys */
70 if (klen <= ctx->buf_len)
71 memcpy(ctx->buf, key, klen);
72 else if (ssh_digest_memory(ctx->alg, key, klen, ctx->buf,
73 ctx->buf_len) < 0)
74 return -1;
75 for (i = 0; i < ctx->buf_len; i++)
76 ctx->buf[i] ^= 0x36;
77 if (ssh_digest_update(ctx->ictx, ctx->buf, ctx->buf_len) < 0)
78 return -1;
79 for (i = 0; i < ctx->buf_len; i++)
80 ctx->buf[i] ^= 0x36 ^ 0x5c;
81 if (ssh_digest_update(ctx->octx, ctx->buf, ctx->buf_len) < 0)
82 return -1;
83 explicit_bzero(ctx->buf, ctx->buf_len);
84 }
85 /* start with ictx */
86 if (ssh_digest_copy_state(ctx->ictx, ctx->digest) < 0)
87 return -1;
88 return 0;
89}
90
91int
92ssh_hmac_update(struct ssh_hmac_ctx *ctx, const void *m, size_t mlen)
93{
94 return ssh_digest_update(ctx->digest, m, mlen);
95}
96
97int
98ssh_hmac_update_buffer(struct ssh_hmac_ctx *ctx, const struct sshbuf *b)
99{
100 return ssh_digest_update_buffer(ctx->digest, b);
101}
102
103int
104ssh_hmac_final(struct ssh_hmac_ctx *ctx, u_char *d, size_t dlen)
105{
106 size_t len;
107
108 len = ssh_digest_bytes(ctx->alg);
109 if (dlen < len ||
110 ssh_digest_final(ctx->digest, ctx->buf, len))
111 return -1;
112 /* switch to octx */
113 if (ssh_digest_copy_state(ctx->octx, ctx->digest) < 0 ||
114 ssh_digest_update(ctx->digest, ctx->buf, len) < 0 ||
115 ssh_digest_final(ctx->digest, d, dlen) < 0)
116 return -1;
117 return 0;
118}
119
120void
121ssh_hmac_free(struct ssh_hmac_ctx *ctx)
122{
123 if (ctx != NULL) {
124 ssh_digest_free(ctx->ictx);
125 ssh_digest_free(ctx->octx);
126 ssh_digest_free(ctx->digest);
127 if (ctx->buf) {
128 explicit_bzero(ctx->buf, ctx->buf_len);
129 free(ctx->buf);
130 }
131 freezero(ctx, sizeof(*ctx));
132 }
133}
134
135#ifdef TEST
136
137/* cc -DTEST hmac.c digest.c buffer.c cleanup.c fatal.c log.c xmalloc.c -lcrypto */
138static void
139hmac_test(void *key, size_t klen, void *m, size_t mlen, u_char *e, size_t elen)
140{
141 struct ssh_hmac_ctx *ctx;
142 size_t i;
143 u_char digest[16];
144
145 if ((ctx = ssh_hmac_start(SSH_DIGEST_MD5)) == NULL)
146 printf("ssh_hmac_start failed");
147 if (ssh_hmac_init(ctx, key, klen) < 0 ||
148 ssh_hmac_update(ctx, m, mlen) < 0 ||
149 ssh_hmac_final(ctx, digest, sizeof(digest)) < 0)
150 printf("ssh_hmac_xxx failed");
151 ssh_hmac_free(ctx);
152
153 if (memcmp(e, digest, elen)) {
154 for (i = 0; i < elen; i++)
155 printf("[%zu] %2.2x %2.2x\n", i, e[i], digest[i]);
156 printf("mismatch\n");
157 } else
158 printf("ok\n");
159}
160
161int
162main(int argc, char **argv)
163{
164 /* try test vectors from RFC 2104 */
165
166 u_char key1[16] = {
167 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb,
168 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb };
169 u_char *data1 = "Hi There";
170 u_char dig1[16] = {
171 0x92, 0x94, 0x72, 0x7a, 0x36, 0x38, 0xbb, 0x1c,
172 0x13, 0xf4, 0x8e, 0xf8, 0x15, 0x8b, 0xfc, 0x9d };
173
174 u_char *key2 = "Jefe";
175 u_char *data2 = "what do ya want for nothing?";
176 u_char dig2[16] = {
177 0x75, 0x0c, 0x78, 0x3e, 0x6a, 0xb0, 0xb5, 0x03,
178 0xea, 0xa8, 0x6e, 0x31, 0x0a, 0x5d, 0xb7, 0x38 };
179
180 u_char key3[16];
181 u_char data3[50];
182 u_char dig3[16] = {
183 0x56, 0xbe, 0x34, 0x52, 0x1d, 0x14, 0x4c, 0x88,
184 0xdb, 0xb8, 0xc7, 0x33, 0xf0, 0xe8, 0xb3, 0xf6 };
185 memset(key3, 0xaa, sizeof(key3));
186 memset(data3, 0xdd, sizeof(data3));
187
188 hmac_test(key1, sizeof(key1), data1, strlen(data1), dig1, sizeof(dig1));
189 hmac_test(key2, strlen(key2), data2, strlen(data2), dig2, sizeof(dig2));
190 hmac_test(key3, sizeof(key3), data3, sizeof(data3), dig3, sizeof(dig3));
191
192 return 0;
193}
194
195#endif