gpt(8): Add HAMMER and HAMMER2 support
[dragonfly.git] / crypto / openssh / hmac.c
blob3815bf668aa44736e9c0bf69b840b9a1a5caaaf0
1 /* $OpenBSD: hmac.c,v 1.12 2015/03/24 20:03:44 markus Exp $ */
2 /*
3 * Copyright (c) 2014 Markus Friedl. All rights reserved.
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.
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.
18 #include "includes.h"
20 #include <sys/types.h>
21 #include <string.h>
23 #include "sshbuf.h"
24 #include "digest.h"
25 #include "hmac.h"
27 struct ssh_hmac_ctx {
28 int alg;
29 struct ssh_digest_ctx *ictx;
30 struct ssh_digest_ctx *octx;
31 struct ssh_digest_ctx *digest;
32 u_char *buf;
33 size_t buf_len;
36 size_t
37 ssh_hmac_bytes(int alg)
39 return ssh_digest_bytes(alg);
42 struct ssh_hmac_ctx *
43 ssh_hmac_start(int alg)
45 struct ssh_hmac_ctx *ret;
47 if ((ret = calloc(1, sizeof(*ret))) == NULL)
48 return NULL;
49 ret->alg = alg;
50 if ((ret->ictx = ssh_digest_start(alg)) == NULL ||
51 (ret->octx = ssh_digest_start(alg)) == NULL ||
52 (ret->digest = ssh_digest_start(alg)) == NULL)
53 goto fail;
54 ret->buf_len = ssh_digest_blocksize(ret->ictx);
55 if ((ret->buf = calloc(1, ret->buf_len)) == NULL)
56 goto fail;
57 return ret;
58 fail:
59 ssh_hmac_free(ret);
60 return NULL;
63 int
64 ssh_hmac_init(struct ssh_hmac_ctx *ctx, const void *key, size_t klen)
66 size_t i;
68 /* reset ictx and octx if no is key given */
69 if (key != NULL) {
70 /* truncate long keys */
71 if (klen <= ctx->buf_len)
72 memcpy(ctx->buf, key, klen);
73 else if (ssh_digest_memory(ctx->alg, key, klen, ctx->buf,
74 ctx->buf_len) < 0)
75 return -1;
76 for (i = 0; i < ctx->buf_len; i++)
77 ctx->buf[i] ^= 0x36;
78 if (ssh_digest_update(ctx->ictx, ctx->buf, ctx->buf_len) < 0)
79 return -1;
80 for (i = 0; i < ctx->buf_len; i++)
81 ctx->buf[i] ^= 0x36 ^ 0x5c;
82 if (ssh_digest_update(ctx->octx, ctx->buf, ctx->buf_len) < 0)
83 return -1;
84 explicit_bzero(ctx->buf, ctx->buf_len);
86 /* start with ictx */
87 if (ssh_digest_copy_state(ctx->ictx, ctx->digest) < 0)
88 return -1;
89 return 0;
92 int
93 ssh_hmac_update(struct ssh_hmac_ctx *ctx, const void *m, size_t mlen)
95 return ssh_digest_update(ctx->digest, m, mlen);
98 int
99 ssh_hmac_update_buffer(struct ssh_hmac_ctx *ctx, const struct sshbuf *b)
101 return ssh_digest_update_buffer(ctx->digest, b);
105 ssh_hmac_final(struct ssh_hmac_ctx *ctx, u_char *d, size_t dlen)
107 size_t len;
109 len = ssh_digest_bytes(ctx->alg);
110 if (dlen < len ||
111 ssh_digest_final(ctx->digest, ctx->buf, len))
112 return -1;
113 /* switch to octx */
114 if (ssh_digest_copy_state(ctx->octx, ctx->digest) < 0 ||
115 ssh_digest_update(ctx->digest, ctx->buf, len) < 0 ||
116 ssh_digest_final(ctx->digest, d, dlen) < 0)
117 return -1;
118 return 0;
121 void
122 ssh_hmac_free(struct ssh_hmac_ctx *ctx)
124 if (ctx != NULL) {
125 ssh_digest_free(ctx->ictx);
126 ssh_digest_free(ctx->octx);
127 ssh_digest_free(ctx->digest);
128 if (ctx->buf) {
129 explicit_bzero(ctx->buf, ctx->buf_len);
130 free(ctx->buf);
132 explicit_bzero(ctx, sizeof(*ctx));
133 free(ctx);
137 #ifdef TEST
140 * cc -DTEST hmac.c digest.c buffer.c cleanup.c fatal.c log.c xmalloc.c -lprivate_crypto
142 static void
143 hmac_test(void *key, size_t klen, void *m, size_t mlen, u_char *e, size_t elen)
145 struct ssh_hmac_ctx *ctx;
146 size_t i;
147 u_char digest[16];
149 if ((ctx = ssh_hmac_start(SSH_DIGEST_MD5)) == NULL)
150 printf("ssh_hmac_start failed");
151 if (ssh_hmac_init(ctx, key, klen) < 0 ||
152 ssh_hmac_update(ctx, m, mlen) < 0 ||
153 ssh_hmac_final(ctx, digest, sizeof(digest)) < 0)
154 printf("ssh_hmac_xxx failed");
155 ssh_hmac_free(ctx);
157 if (memcmp(e, digest, elen)) {
158 for (i = 0; i < elen; i++)
159 printf("[%zu] %2.2x %2.2x\n", i, e[i], digest[i]);
160 printf("mismatch\n");
161 } else
162 printf("ok\n");
166 main(int argc, char **argv)
168 /* try test vectors from RFC 2104 */
170 u_char key1[16] = {
171 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb,
172 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb };
173 u_char *data1 = "Hi There";
174 u_char dig1[16] = {
175 0x92, 0x94, 0x72, 0x7a, 0x36, 0x38, 0xbb, 0x1c,
176 0x13, 0xf4, 0x8e, 0xf8, 0x15, 0x8b, 0xfc, 0x9d };
178 u_char *key2 = "Jefe";
179 u_char *data2 = "what do ya want for nothing?";
180 u_char dig2[16] = {
181 0x75, 0x0c, 0x78, 0x3e, 0x6a, 0xb0, 0xb5, 0x03,
182 0xea, 0xa8, 0x6e, 0x31, 0x0a, 0x5d, 0xb7, 0x38 };
184 u_char key3[16];
185 u_char data3[50];
186 u_char dig3[16] = {
187 0x56, 0xbe, 0x34, 0x52, 0x1d, 0x14, 0x4c, 0x88,
188 0xdb, 0xb8, 0xc7, 0x33, 0xf0, 0xe8, 0xb3, 0xf6 };
189 memset(key3, 0xaa, sizeof(key3));
190 memset(data3, 0xdd, sizeof(data3));
192 hmac_test(key1, sizeof(key1), data1, strlen(data1), dig1, sizeof(dig1));
193 hmac_test(key2, strlen(key2), data2, strlen(data2), dig2, sizeof(dig2));
194 hmac_test(key3, sizeof(key3), data3, sizeof(data3), dig3, sizeof(dig3));
196 return 0;
199 #endif