mac.c (e477abf734cc777a55286bfbd6b80a6760c96acf) mac.c (6888a9be566d79246a948dcc4c0a914b1bee0c32)
1/* $OpenBSD: mac.c,v 1.18 2012/06/28 05:07:45 dtucker Exp $ */
1/* $OpenBSD: mac.c,v 1.21 2012/12/11 22:51:45 sthen Exp $ */
2/*
3 * Copyright (c) 2001 Markus Friedl. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.

--- 33 unchanged lines hidden (view full) ---

43#include "misc.h"
44
45#include "umac.h"
46
47#include "openbsd-compat/openssl-compat.h"
48
49#define SSH_EVP 1 /* OpenSSL EVP-based MAC */
50#define SSH_UMAC 2 /* UMAC (not integrated with OpenSSL) */
2/*
3 * Copyright (c) 2001 Markus Friedl. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.

--- 33 unchanged lines hidden (view full) ---

43#include "misc.h"
44
45#include "umac.h"
46
47#include "openbsd-compat/openssl-compat.h"
48
49#define SSH_EVP 1 /* OpenSSL EVP-based MAC */
50#define SSH_UMAC 2 /* UMAC (not integrated with OpenSSL) */
51#define SSH_UMAC128 3
51
52struct {
53 char *name;
54 int type;
55 const EVP_MD * (*mdfunc)(void);
56 int truncatebits; /* truncate digest if != 0 */
57 int key_len; /* just for UMAC */
58 int len; /* just for UMAC */
52
53struct {
54 char *name;
55 int type;
56 const EVP_MD * (*mdfunc)(void);
57 int truncatebits; /* truncate digest if != 0 */
58 int key_len; /* just for UMAC */
59 int len; /* just for UMAC */
60 int etm; /* Encrypt-then-MAC */
59} macs[] = {
61} macs[] = {
60 { "hmac-sha1", SSH_EVP, EVP_sha1, 0, -1, -1 },
61 { "hmac-sha1-96", SSH_EVP, EVP_sha1, 96, -1, -1 },
62 /* Encrypt-and-MAC (encrypt-and-authenticate) variants */
63 { "hmac-sha1", SSH_EVP, EVP_sha1, 0, 0, 0, 0 },
64 { "hmac-sha1-96", SSH_EVP, EVP_sha1, 96, 0, 0, 0 },
62#ifdef HAVE_EVP_SHA256
65#ifdef HAVE_EVP_SHA256
63 { "hmac-sha2-256", SSH_EVP, EVP_sha256, 0, -1, -1 },
64 { "hmac-sha2-512", SSH_EVP, EVP_sha512, 0, -1, -1 },
66 { "hmac-sha2-256", SSH_EVP, EVP_sha256, 0, 0, 0, 0 },
67 { "hmac-sha2-512", SSH_EVP, EVP_sha512, 0, 0, 0, 0 },
65#endif
68#endif
66 { "hmac-md5", SSH_EVP, EVP_md5, 0, -1, -1 },
67 { "hmac-md5-96", SSH_EVP, EVP_md5, 96, -1, -1 },
68 { "hmac-ripemd160", SSH_EVP, EVP_ripemd160, 0, -1, -1 },
69 { "hmac-ripemd160@openssh.com", SSH_EVP, EVP_ripemd160, 0, -1, -1 },
70 { "umac-64@openssh.com", SSH_UMAC, NULL, 0, 128, 64 },
71 { NULL, 0, NULL, 0, -1, -1 }
69 { "hmac-md5", SSH_EVP, EVP_md5, 0, 0, 0, 0 },
70 { "hmac-md5-96", SSH_EVP, EVP_md5, 96, 0, 0, 0 },
71 { "hmac-ripemd160", SSH_EVP, EVP_ripemd160, 0, 0, 0, 0 },
72 { "hmac-ripemd160@openssh.com", SSH_EVP, EVP_ripemd160, 0, 0, 0, 0 },
73 { "umac-64@openssh.com", SSH_UMAC, NULL, 0, 128, 64, 0 },
74 { "umac-128@openssh.com", SSH_UMAC128, NULL, 0, 128, 128, 0 },
75
76 /* Encrypt-then-MAC variants */
77 { "hmac-sha1-etm@openssh.com", SSH_EVP, EVP_sha1, 0, 0, 0, 1 },
78 { "hmac-sha1-96-etm@openssh.com", SSH_EVP, EVP_sha1, 96, 0, 0, 1 },
79#ifdef HAVE_EVP_SHA256
80 { "hmac-sha2-256-etm@openssh.com", SSH_EVP, EVP_sha256, 0, 0, 0, 1 },
81 { "hmac-sha2-512-etm@openssh.com", SSH_EVP, EVP_sha512, 0, 0, 0, 1 },
82#endif
83 { "hmac-md5-etm@openssh.com", SSH_EVP, EVP_md5, 0, 0, 0, 1 },
84 { "hmac-md5-96-etm@openssh.com", SSH_EVP, EVP_md5, 96, 0, 0, 1 },
85 { "hmac-ripemd160-etm@openssh.com", SSH_EVP, EVP_ripemd160, 0, 0, 0, 1 },
86 { "umac-64-etm@openssh.com", SSH_UMAC, NULL, 0, 128, 64, 1 },
87 { "umac-128-etm@openssh.com", SSH_UMAC128, NULL, 0, 128, 128, 1 },
88
89 { NULL, 0, NULL, 0, 0, 0, 0 }
72};
73
74static void
75mac_setup_by_id(Mac *mac, int which)
76{
77 int evp_len;
78 mac->type = macs[which].type;
79 if (mac->type == SSH_EVP) {
80 mac->evp_md = (*macs[which].mdfunc)();
81 if ((evp_len = EVP_MD_size(mac->evp_md)) <= 0)
82 fatal("mac %s len %d", mac->name, evp_len);
83 mac->key_len = mac->mac_len = (u_int)evp_len;
84 } else {
85 mac->mac_len = macs[which].len / 8;
86 mac->key_len = macs[which].key_len / 8;
87 mac->umac_ctx = NULL;
88 }
89 if (macs[which].truncatebits != 0)
90 mac->mac_len = macs[which].truncatebits / 8;
90};
91
92static void
93mac_setup_by_id(Mac *mac, int which)
94{
95 int evp_len;
96 mac->type = macs[which].type;
97 if (mac->type == SSH_EVP) {
98 mac->evp_md = (*macs[which].mdfunc)();
99 if ((evp_len = EVP_MD_size(mac->evp_md)) <= 0)
100 fatal("mac %s len %d", mac->name, evp_len);
101 mac->key_len = mac->mac_len = (u_int)evp_len;
102 } else {
103 mac->mac_len = macs[which].len / 8;
104 mac->key_len = macs[which].key_len / 8;
105 mac->umac_ctx = NULL;
106 }
107 if (macs[which].truncatebits != 0)
108 mac->mac_len = macs[which].truncatebits / 8;
109 mac->etm = macs[which].etm;
91}
92
93int
94mac_setup(Mac *mac, char *name)
95{
96 int i;
97
98 for (i = 0; macs[i].name; i++) {

--- 18 unchanged lines hidden (view full) ---

117 if (mac->evp_md == NULL)
118 return -1;
119 HMAC_CTX_init(&mac->evp_ctx);
120 HMAC_Init(&mac->evp_ctx, mac->key, mac->key_len, mac->evp_md);
121 return 0;
122 case SSH_UMAC:
123 mac->umac_ctx = umac_new(mac->key);
124 return 0;
110}
111
112int
113mac_setup(Mac *mac, char *name)
114{
115 int i;
116
117 for (i = 0; macs[i].name; i++) {

--- 18 unchanged lines hidden (view full) ---

136 if (mac->evp_md == NULL)
137 return -1;
138 HMAC_CTX_init(&mac->evp_ctx);
139 HMAC_Init(&mac->evp_ctx, mac->key, mac->key_len, mac->evp_md);
140 return 0;
141 case SSH_UMAC:
142 mac->umac_ctx = umac_new(mac->key);
143 return 0;
144 case SSH_UMAC128:
145 mac->umac_ctx = umac128_new(mac->key);
146 return 0;
125 default:
126 return -1;
127 }
128}
129
130u_char *
131mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen)
132{

--- 13 unchanged lines hidden (view full) ---

146 HMAC_Update(&mac->evp_ctx, data, datalen);
147 HMAC_Final(&mac->evp_ctx, m, NULL);
148 break;
149 case SSH_UMAC:
150 put_u64(nonce, seqno);
151 umac_update(mac->umac_ctx, data, datalen);
152 umac_final(mac->umac_ctx, m, nonce);
153 break;
147 default:
148 return -1;
149 }
150}
151
152u_char *
153mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen)
154{

--- 13 unchanged lines hidden (view full) ---

168 HMAC_Update(&mac->evp_ctx, data, datalen);
169 HMAC_Final(&mac->evp_ctx, m, NULL);
170 break;
171 case SSH_UMAC:
172 put_u64(nonce, seqno);
173 umac_update(mac->umac_ctx, data, datalen);
174 umac_final(mac->umac_ctx, m, nonce);
175 break;
176 case SSH_UMAC128:
177 put_u64(nonce, seqno);
178 umac128_update(mac->umac_ctx, data, datalen);
179 umac128_final(mac->umac_ctx, m, nonce);
180 break;
154 default:
155 fatal("mac_compute: unknown MAC type");
156 }
157 return (m);
158}
159
160void
161mac_clear(Mac *mac)
162{
163 if (mac->type == SSH_UMAC) {
164 if (mac->umac_ctx != NULL)
165 umac_delete(mac->umac_ctx);
181 default:
182 fatal("mac_compute: unknown MAC type");
183 }
184 return (m);
185}
186
187void
188mac_clear(Mac *mac)
189{
190 if (mac->type == SSH_UMAC) {
191 if (mac->umac_ctx != NULL)
192 umac_delete(mac->umac_ctx);
193 } else if (mac->type == SSH_UMAC128) {
194 if (mac->umac_ctx != NULL)
195 umac128_delete(mac->umac_ctx);
166 } else if (mac->evp_md != NULL)
167 HMAC_cleanup(&mac->evp_ctx);
168 mac->evp_md = NULL;
169 mac->umac_ctx = NULL;
170}
171
172/* XXX copied from ciphers_valid */
173#define MAC_SEP ","

--- 22 unchanged lines hidden ---
196 } else if (mac->evp_md != NULL)
197 HMAC_cleanup(&mac->evp_ctx);
198 mac->evp_md = NULL;
199 mac->umac_ctx = NULL;
200}
201
202/* XXX copied from ciphers_valid */
203#define MAC_SEP ","

--- 22 unchanged lines hidden ---