mac.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /* $OpenBSD: mac.c,v 1.35 2019/09/06 04:53:27 djm 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.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "includes.h"
  26. #include <sys/types.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <stdio.h>
  30. #include "digest.h"
  31. #include "hmac.h"
  32. #include "umac.h"
  33. #include "mac.h"
  34. #include "misc.h"
  35. #include "ssherr.h"
  36. #include "sshbuf.h"
  37. #include "openbsd-compat/openssl-compat.h"
  38. #define SSH_DIGEST 1 /* SSH_DIGEST_XXX */
  39. #define SSH_UMAC 2 /* UMAC (not integrated with OpenSSL) */
  40. #define SSH_UMAC128 3
  41. struct macalg {
  42. char *name;
  43. int type;
  44. int alg;
  45. int truncatebits; /* truncate digest if != 0 */
  46. int key_len; /* just for UMAC */
  47. int len; /* just for UMAC */
  48. int etm; /* Encrypt-then-MAC */
  49. };
  50. static const struct macalg macs[] = {
  51. /* Encrypt-and-MAC (encrypt-and-authenticate) variants */
  52. { "hmac-sha1", SSH_DIGEST, SSH_DIGEST_SHA1, 0, 0, 0, 0 },
  53. { "hmac-sha1-96", SSH_DIGEST, SSH_DIGEST_SHA1, 96, 0, 0, 0 },
  54. { "hmac-sha2-256", SSH_DIGEST, SSH_DIGEST_SHA256, 0, 0, 0, 0 },
  55. { "hmac-sha2-512", SSH_DIGEST, SSH_DIGEST_SHA512, 0, 0, 0, 0 },
  56. { "hmac-md5", SSH_DIGEST, SSH_DIGEST_MD5, 0, 0, 0, 0 },
  57. { "hmac-md5-96", SSH_DIGEST, SSH_DIGEST_MD5, 96, 0, 0, 0 },
  58. { "none", SSH_DIGEST, SSH_DIGEST_NULL, 0, 0, 0, 0 },
  59. { "umac-64@openssh.com", SSH_UMAC, 0, 0, 128, 64, 0 },
  60. { "umac-128@openssh.com", SSH_UMAC128, 0, 0, 128, 128, 0 },
  61. /* Encrypt-then-MAC variants */
  62. { "hmac-sha1-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_SHA1, 0, 0, 0, 1 },
  63. { "hmac-sha1-96-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_SHA1, 96, 0, 0, 1 },
  64. { "hmac-sha2-256-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_SHA256, 0, 0, 0, 1 },
  65. { "hmac-sha2-512-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_SHA512, 0, 0, 0, 1 },
  66. { "hmac-md5-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_MD5, 0, 0, 0, 1 },
  67. { "hmac-md5-96-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_MD5, 96, 0, 0, 1 },
  68. { "umac-64-etm@openssh.com", SSH_UMAC, 0, 0, 128, 64, 1 },
  69. { "umac-128-etm@openssh.com", SSH_UMAC128, 0, 0, 128, 128, 1 },
  70. { NULL, 0, 0, 0, 0, 0, 0 }
  71. };
  72. /* Returns a list of supported MACs separated by the specified char. */
  73. char *
  74. mac_alg_list(char sep)
  75. {
  76. char *ret = NULL, *tmp;
  77. size_t nlen, rlen = 0;
  78. const struct macalg *m;
  79. for (m = macs; m->name != NULL; m++) {
  80. if (ret != NULL)
  81. ret[rlen++] = sep;
  82. nlen = strlen(m->name);
  83. if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
  84. free(ret);
  85. return NULL;
  86. }
  87. ret = tmp;
  88. memcpy(ret + rlen, m->name, nlen + 1);
  89. rlen += nlen;
  90. }
  91. return ret;
  92. }
  93. static int
  94. mac_setup_by_alg(struct sshmac *mac, const struct macalg *macalg)
  95. {
  96. mac->type = macalg->type;
  97. if (mac->type == SSH_DIGEST) {
  98. if ((mac->hmac_ctx = ssh_hmac_start(macalg->alg)) == NULL)
  99. return SSH_ERR_ALLOC_FAIL;
  100. mac->key_len = mac->mac_len = ssh_hmac_bytes(macalg->alg);
  101. } else {
  102. mac->mac_len = macalg->len / 8;
  103. mac->key_len = macalg->key_len / 8;
  104. mac->umac_ctx = NULL;
  105. }
  106. if (macalg->truncatebits != 0)
  107. mac->mac_len = macalg->truncatebits / 8;
  108. mac->etm = macalg->etm;
  109. return 0;
  110. }
  111. int
  112. mac_setup(struct sshmac *mac, char *name)
  113. {
  114. const struct macalg *m;
  115. for (m = macs; m->name != NULL; m++) {
  116. if (strcmp(name, m->name) != 0)
  117. continue;
  118. if (mac != NULL)
  119. return mac_setup_by_alg(mac, m);
  120. return 0;
  121. }
  122. return SSH_ERR_INVALID_ARGUMENT;
  123. }
  124. int
  125. mac_init(struct sshmac *mac)
  126. {
  127. if (mac->key == NULL)
  128. return SSH_ERR_INVALID_ARGUMENT;
  129. switch (mac->type) {
  130. case SSH_DIGEST:
  131. if (mac->hmac_ctx == NULL ||
  132. ssh_hmac_init(mac->hmac_ctx, mac->key, mac->key_len) < 0)
  133. return SSH_ERR_INVALID_ARGUMENT;
  134. return 0;
  135. case SSH_UMAC:
  136. if ((mac->umac_ctx = umac_new(mac->key)) == NULL)
  137. return SSH_ERR_ALLOC_FAIL;
  138. return 0;
  139. case SSH_UMAC128:
  140. if ((mac->umac_ctx = umac128_new(mac->key)) == NULL)
  141. return SSH_ERR_ALLOC_FAIL;
  142. return 0;
  143. default:
  144. return SSH_ERR_INVALID_ARGUMENT;
  145. }
  146. }
  147. int
  148. mac_compute(struct sshmac *mac, u_int32_t seqno,
  149. const u_char *data, int datalen,
  150. u_char *digest, size_t dlen)
  151. {
  152. static union {
  153. u_char m[SSH_DIGEST_MAX_LENGTH];
  154. u_int64_t for_align;
  155. } u;
  156. u_char b[4];
  157. u_char nonce[8];
  158. if (mac->mac_len > sizeof(u))
  159. return SSH_ERR_INTERNAL_ERROR;
  160. switch (mac->type) {
  161. case SSH_DIGEST:
  162. put_u32(b, seqno);
  163. /* reset HMAC context */
  164. if (ssh_hmac_init(mac->hmac_ctx, NULL, 0) < 0 ||
  165. ssh_hmac_update(mac->hmac_ctx, b, sizeof(b)) < 0 ||
  166. ssh_hmac_update(mac->hmac_ctx, data, datalen) < 0 ||
  167. ssh_hmac_final(mac->hmac_ctx, u.m, sizeof(u.m)) < 0) {
  168. debug("(ssh_hmac_init(mac->hmac_ctx, NULL, 0) < 0 || ssh_hmac_update(mac->hmac_ctx, b, sizeof(b)) < 0 || ssh_hmac_update(mac->hmac_ctx, data, datalen) < 0 || ssh_hmac_final(mac->hmac_ctx, u.m, sizeof(u.m)) < 0)");
  169. return SSH_ERR_LIBCRYPTO_ERROR;
  170. }
  171. break;
  172. case SSH_UMAC:
  173. POKE_U64(nonce, seqno);
  174. umac_update(mac->umac_ctx, data, datalen);
  175. umac_final(mac->umac_ctx, u.m, nonce);
  176. break;
  177. case SSH_UMAC128:
  178. put_u64(nonce, seqno);
  179. umac128_update(mac->umac_ctx, data, datalen);
  180. umac128_final(mac->umac_ctx, u.m, nonce);
  181. break;
  182. default:
  183. return SSH_ERR_INVALID_ARGUMENT;
  184. }
  185. if (digest != NULL) {
  186. if (dlen > mac->mac_len)
  187. dlen = mac->mac_len;
  188. memcpy(digest, u.m, dlen);
  189. }
  190. return 0;
  191. }
  192. int
  193. mac_check(struct sshmac *mac, u_int32_t seqno,
  194. const u_char *data, size_t dlen,
  195. const u_char *theirmac, size_t mlen)
  196. {
  197. u_char ourmac[SSH_DIGEST_MAX_LENGTH];
  198. int r;
  199. if (mac->mac_len > mlen)
  200. return SSH_ERR_INVALID_ARGUMENT;
  201. if ((r = mac_compute(mac, seqno, data, dlen,
  202. ourmac, sizeof(ourmac))) != 0)
  203. return r;
  204. if (timingsafe_bcmp(ourmac, theirmac, mac->mac_len) != 0)
  205. return SSH_ERR_MAC_INVALID;
  206. return 0;
  207. }
  208. void
  209. mac_clear(struct sshmac *mac)
  210. {
  211. if (mac->type == SSH_UMAC) {
  212. if (mac->umac_ctx != NULL)
  213. umac_delete(mac->umac_ctx);
  214. } else if (mac->type == SSH_UMAC128) {
  215. if (mac->umac_ctx != NULL)
  216. umac128_delete(mac->umac_ctx);
  217. } else if (mac->hmac_ctx != NULL)
  218. ssh_hmac_free(mac->hmac_ctx);
  219. mac->hmac_ctx = NULL;
  220. mac->umac_ctx = NULL;
  221. }
  222. void
  223. mac_destroy(struct sshmac *mac)
  224. {
  225. if (mac == NULL)
  226. return;
  227. if (mac->key) {
  228. memset(mac->key, 0, mac->key_len);
  229. free(mac->key);
  230. }
  231. memset(mac, 0, sizeof(*mac));
  232. }
  233. /* XXX copied from ciphers_valid */
  234. #define MAC_SEP ","
  235. int
  236. mac_valid(const char *names)
  237. {
  238. char *maclist, *cp, *p;
  239. if (names == NULL || strcmp(names, "") == 0)
  240. return 0;
  241. if ((maclist = cp = strdup(names)) == NULL)
  242. return 0;
  243. for ((p = strsep(&cp, MAC_SEP)); p && *p != '\0';
  244. (p = strsep(&cp, MAC_SEP))) {
  245. if (mac_setup(NULL, p) < 0) {
  246. free(maclist);
  247. return 0;
  248. }
  249. }
  250. free(maclist);
  251. return 1;
  252. }