Coverage Report

Created: 2026-02-24 06:38

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