hmac.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Implementation of HMAC (RFC 2104) for PuTTY, in a general form that
  3. * can wrap any underlying hash function.
  4. */
  5. #include "ssh.h"
  6. struct hmac {
  7. const ssh_hashalg *hashalg;
  8. ssh_hash *h_outer, *h_inner, *h_live;
  9. uint8_t *digest;
  10. strbuf *text_name;
  11. ssh2_mac mac;
  12. };
  13. struct hmac_extra {
  14. const ssh_hashalg *hashalg_base;
  15. const char *suffix, *annotation;
  16. };
  17. /* Most of hmac_new(). Takes the actual 'struct hmac' as a parameter,
  18. * because sometimes it will have been allocated in a special way. */
  19. static ssh2_mac *hmac_new_inner(struct hmac *ctx, const ssh2_macalg *alg)
  20. {
  21. const struct hmac_extra *extra = (const struct hmac_extra *)alg->extra;
  22. ctx->h_outer = ssh_hash_new(extra->hashalg_base);
  23. /* In case that hashalg was a selector vtable, we'll now switch to
  24. * using whatever real one it selected, for all future purposes. */
  25. ctx->hashalg = ssh_hash_alg(ctx->h_outer);
  26. ctx->h_inner = ssh_hash_new(ctx->hashalg);
  27. ctx->h_live = ssh_hash_new(ctx->hashalg);
  28. /*
  29. * HMAC is not well defined as a wrapper on an absolutely general
  30. * hash function; it expects that the function it's wrapping will
  31. * consume data in fixed-size blocks, and it's partially defined
  32. * in terms of that block size. So we insist that the hash we're
  33. * given must have defined a meaningful block size.
  34. */
  35. assert(ctx->hashalg->blocklen);
  36. ctx->digest = snewn(ctx->hashalg->hlen, uint8_t);
  37. ctx->text_name = strbuf_new();
  38. put_fmt(ctx->text_name, "HMAC-%s%s",
  39. ctx->hashalg->text_basename, extra->suffix);
  40. if (extra->annotation || ctx->hashalg->annotation) {
  41. put_fmt(ctx->text_name, " (");
  42. const char *sep = "";
  43. if (extra->annotation) {
  44. put_fmt(ctx->text_name, "%s%s", sep, extra->annotation);
  45. sep = ", ";
  46. }
  47. if (ctx->hashalg->annotation) {
  48. put_fmt(ctx->text_name, "%s%s", sep, ctx->hashalg->annotation);
  49. sep = ", ";
  50. }
  51. put_fmt(ctx->text_name, ")");
  52. }
  53. ctx->mac.vt = alg;
  54. BinarySink_DELEGATE_INIT(&ctx->mac, ctx->h_live);
  55. return &ctx->mac;
  56. }
  57. static ssh2_mac *hmac_new(const ssh2_macalg *alg, ssh_cipher *cipher)
  58. {
  59. return hmac_new_inner(snew(struct hmac), alg); /* cipher isn't needed */
  60. }
  61. static void hmac_free(ssh2_mac *mac)
  62. {
  63. struct hmac *ctx = container_of(mac, struct hmac, mac);
  64. ssh_hash_free(ctx->h_outer);
  65. ssh_hash_free(ctx->h_inner);
  66. ssh_hash_free(ctx->h_live);
  67. smemclr(ctx->digest, ctx->hashalg->hlen);
  68. sfree(ctx->digest);
  69. strbuf_free(ctx->text_name);
  70. smemclr(ctx, sizeof(*ctx));
  71. sfree(ctx);
  72. }
  73. #define PAD_OUTER 0x5C
  74. #define PAD_INNER 0x36
  75. static void hmac_key(ssh2_mac *mac, ptrlen key)
  76. {
  77. struct hmac *ctx = container_of(mac, struct hmac, mac);
  78. const uint8_t *kp;
  79. size_t klen;
  80. strbuf *sb = NULL;
  81. if (key.len > ctx->hashalg->blocklen) {
  82. /*
  83. * RFC 2104 section 2: if the key exceeds the block length of
  84. * the underlying hash, then we start by hashing the key, and
  85. * use that hash as the 'true' key for the HMAC construction.
  86. */
  87. sb = strbuf_new_nm();
  88. strbuf_append(sb, ctx->hashalg->hlen);
  89. hash_simple(ctx->hashalg, key, sb->u);
  90. kp = sb->u;
  91. klen = sb->len;
  92. } else {
  93. /*
  94. * A short enough key is used as is.
  95. */
  96. kp = (const uint8_t *)key.ptr;
  97. klen = key.len;
  98. }
  99. ssh_hash_reset(ctx->h_outer);
  100. for (size_t i = 0; i < klen; i++)
  101. put_byte(ctx->h_outer, PAD_OUTER ^ kp[i]);
  102. for (size_t i = klen; i < ctx->hashalg->blocklen; i++)
  103. put_byte(ctx->h_outer, PAD_OUTER);
  104. ssh_hash_reset(ctx->h_inner);
  105. for (size_t i = 0; i < klen; i++)
  106. put_byte(ctx->h_inner, PAD_INNER ^ kp[i]);
  107. for (size_t i = klen; i < ctx->hashalg->blocklen; i++)
  108. put_byte(ctx->h_inner, PAD_INNER);
  109. if (sb)
  110. strbuf_free(sb);
  111. }
  112. static void hmac_start(ssh2_mac *mac)
  113. {
  114. struct hmac *ctx = container_of(mac, struct hmac, mac);
  115. ssh_hash_copyfrom(ctx->h_live, ctx->h_inner);
  116. }
  117. static void hmac_genresult(ssh2_mac *mac, unsigned char *output)
  118. {
  119. struct hmac *ctx = container_of(mac, struct hmac, mac);
  120. ssh_hash *htmp;
  121. /* Leave h_live and h_outer in place, so that the SSH-2 BPP can
  122. * continue regenerating test results from different-length
  123. * prefixes of the packet */
  124. ssh_hash_digest_nondestructive(ctx->h_live, ctx->digest);
  125. htmp = ssh_hash_copy(ctx->h_outer);
  126. put_data(htmp, ctx->digest, ctx->hashalg->hlen);
  127. ssh_hash_final(htmp, ctx->digest);
  128. /*
  129. * Some instances of HMAC truncate the output hash, so instead of
  130. * writing it directly to 'output' we wrote it to our own
  131. * full-length buffer, and now we copy the required amount.
  132. */
  133. memcpy(output, ctx->digest, mac->vt->len);
  134. smemclr(ctx->digest, ctx->hashalg->hlen);
  135. }
  136. static const char *hmac_text_name(ssh2_mac *mac)
  137. {
  138. struct hmac *ctx = container_of(mac, struct hmac, mac);
  139. return ctx->text_name->s;
  140. }
  141. static const struct hmac_extra ssh_hmac_sha512_extra = { &ssh_sha512, "" };
  142. const ssh2_macalg ssh_hmac_sha512 = {
  143. .new = hmac_new,
  144. .free = hmac_free,
  145. .setkey = hmac_key,
  146. .start = hmac_start,
  147. .genresult = hmac_genresult,
  148. .next_message = nullmac_next_message,
  149. .text_name = hmac_text_name,
  150. .name = "hmac-sha2-512",
  151. .etm_name = "hmac-sha2-512-etm@openssh.com",
  152. .len = 64,
  153. .keylen = 64,
  154. .extra = &ssh_hmac_sha512_extra,
  155. };
  156. static const struct hmac_extra ssh_hmac_sha256_extra = { &ssh_sha256, "" };
  157. const ssh2_macalg ssh_hmac_sha256 = {
  158. .new = hmac_new,
  159. .free = hmac_free,
  160. .setkey = hmac_key,
  161. .start = hmac_start,
  162. .genresult = hmac_genresult,
  163. .next_message = nullmac_next_message,
  164. .text_name = hmac_text_name,
  165. .name = "hmac-sha2-256",
  166. .etm_name = "hmac-sha2-256-etm@openssh.com",
  167. .len = 32,
  168. .keylen = 32,
  169. .extra = &ssh_hmac_sha256_extra,
  170. };
  171. static const struct hmac_extra ssh_hmac_md5_extra = { &ssh_md5, "" };
  172. const ssh2_macalg ssh_hmac_md5 = {
  173. .new = hmac_new,
  174. .free = hmac_free,
  175. .setkey = hmac_key,
  176. .start = hmac_start,
  177. .genresult = hmac_genresult,
  178. .next_message = nullmac_next_message,
  179. .text_name = hmac_text_name,
  180. .name = "hmac-md5",
  181. .etm_name = "hmac-md5-etm@openssh.com",
  182. .len = 16,
  183. .keylen = 16,
  184. .extra = &ssh_hmac_md5_extra,
  185. };
  186. static const struct hmac_extra ssh_hmac_sha1_extra = { &ssh_sha1, "" };
  187. const ssh2_macalg ssh_hmac_sha1 = {
  188. .new = hmac_new,
  189. .free = hmac_free,
  190. .setkey = hmac_key,
  191. .start = hmac_start,
  192. .genresult = hmac_genresult,
  193. .next_message = nullmac_next_message,
  194. .text_name = hmac_text_name,
  195. .name = "hmac-sha1",
  196. .etm_name = "hmac-sha1-etm@openssh.com",
  197. .len = 20,
  198. .keylen = 20,
  199. .extra = &ssh_hmac_sha1_extra,
  200. };
  201. static const struct hmac_extra ssh_hmac_sha1_96_extra = { &ssh_sha1, "-96" };
  202. const ssh2_macalg ssh_hmac_sha1_96 = {
  203. .new = hmac_new,
  204. .free = hmac_free,
  205. .setkey = hmac_key,
  206. .start = hmac_start,
  207. .genresult = hmac_genresult,
  208. .next_message = nullmac_next_message,
  209. .text_name = hmac_text_name,
  210. .name = "hmac-sha1-96",
  211. .etm_name = "hmac-sha1-96-etm@openssh.com",
  212. .len = 12,
  213. .keylen = 20,
  214. .extra = &ssh_hmac_sha1_96_extra,
  215. };
  216. static const struct hmac_extra ssh_hmac_sha1_buggy_extra = {
  217. &ssh_sha1, "", "bug-compatible"
  218. };
  219. const ssh2_macalg ssh_hmac_sha1_buggy = {
  220. .new = hmac_new,
  221. .free = hmac_free,
  222. .setkey = hmac_key,
  223. .start = hmac_start,
  224. .genresult = hmac_genresult,
  225. .next_message = nullmac_next_message,
  226. .text_name = hmac_text_name,
  227. .name = "hmac-sha1",
  228. .len = 20,
  229. .keylen = 16,
  230. .extra = &ssh_hmac_sha1_buggy_extra,
  231. };
  232. static const struct hmac_extra ssh_hmac_sha1_96_buggy_extra = {
  233. &ssh_sha1, "-96", "bug-compatible"
  234. };
  235. const ssh2_macalg ssh_hmac_sha1_96_buggy = {
  236. .new = hmac_new,
  237. .free = hmac_free,
  238. .setkey = hmac_key,
  239. .start = hmac_start,
  240. .genresult = hmac_genresult,
  241. .next_message = nullmac_next_message,
  242. .text_name = hmac_text_name,
  243. .name = "hmac-sha1-96",
  244. .len = 12,
  245. .keylen = 16,
  246. .extra = &ssh_hmac_sha1_96_buggy_extra,
  247. };
  248. ssh2_mac *hmac_new_from_hash(const ssh_hashalg *hash)
  249. {
  250. /*
  251. * Construct a custom ssh2_macalg, derived directly from the
  252. * provided hash vtable. It's included in the same memory
  253. * allocation as the struct hmac, so that it all gets freed
  254. * together.
  255. */
  256. struct alloc {
  257. struct hmac hmac;
  258. ssh2_macalg alg;
  259. struct hmac_extra extra;
  260. };
  261. struct alloc *alloc = snew(struct alloc);
  262. alloc->alg.new = hmac_new;
  263. alloc->alg.free = hmac_free;
  264. alloc->alg.setkey = hmac_key;
  265. alloc->alg.start = hmac_start;
  266. alloc->alg.genresult = hmac_genresult;
  267. alloc->alg.next_message = nullmac_next_message;
  268. alloc->alg.text_name = hmac_text_name;
  269. alloc->alg.name = NULL;
  270. alloc->alg.etm_name = NULL;
  271. alloc->alg.len = hash->hlen;
  272. alloc->alg.keylen = hash->hlen;
  273. alloc->alg.extra = &alloc->extra;
  274. alloc->extra.hashalg_base = hash;
  275. alloc->extra.suffix = "";
  276. alloc->extra.annotation = NULL;
  277. return hmac_new_inner(&alloc->hmac, &alloc->alg);
  278. }