sshhmac.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. bool keyed;
  10. uint8_t *digest;
  11. strbuf *text_name;
  12. ssh2_mac mac;
  13. };
  14. struct hmac_extra {
  15. const ssh_hashalg *hashalg_base;
  16. const char *suffix, *annotation;
  17. };
  18. static ssh2_mac *hmac_new(const ssh2_macalg *alg, ssh_cipher *cipher)
  19. {
  20. struct hmac *ctx = snew(struct hmac);
  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. ctx->keyed = false;
  29. /*
  30. * HMAC is not well defined as a wrapper on an absolutely general
  31. * hash function; it expects that the function it's wrapping will
  32. * consume data in fixed-size blocks, and it's partially defined
  33. * in terms of that block size. So we insist that the hash we're
  34. * given must have defined a meaningful block size.
  35. */
  36. assert(ctx->hashalg->blocklen);
  37. ctx->digest = snewn(ctx->hashalg->hlen, uint8_t);
  38. ctx->text_name = strbuf_new();
  39. strbuf_catf(ctx->text_name, "HMAC-%s",
  40. ctx->hashalg->text_basename, extra->suffix);
  41. if (extra->annotation || ctx->hashalg->annotation) {
  42. strbuf_catf(ctx->text_name, " (");
  43. const char *sep = "";
  44. if (extra->annotation) {
  45. strbuf_catf(ctx->text_name, "%s%s", sep, extra->annotation);
  46. sep = ", ";
  47. }
  48. if (ctx->hashalg->annotation) {
  49. strbuf_catf(ctx->text_name, "%s%s", sep, ctx->hashalg->annotation);
  50. sep = ", ";
  51. }
  52. strbuf_catf(ctx->text_name, ")");
  53. }
  54. ctx->mac.vt = alg;
  55. BinarySink_DELEGATE_INIT(&ctx->mac, ctx->h_live);
  56. return &ctx->mac;
  57. }
  58. static void hmac_free(ssh2_mac *mac)
  59. {
  60. struct hmac *ctx = container_of(mac, struct hmac, mac);
  61. ssh_hash_free(ctx->h_outer);
  62. ssh_hash_free(ctx->h_inner);
  63. ssh_hash_free(ctx->h_live);
  64. smemclr(ctx->digest, ctx->hashalg->hlen);
  65. sfree(ctx->digest);
  66. strbuf_free(ctx->text_name);
  67. smemclr(ctx, sizeof(*ctx));
  68. sfree(ctx);
  69. }
  70. #define PAD_OUTER 0x5C
  71. #define PAD_INNER 0x36
  72. static void hmac_key(ssh2_mac *mac, ptrlen key)
  73. {
  74. struct hmac *ctx = container_of(mac, struct hmac, mac);
  75. const uint8_t *kp;
  76. size_t klen;
  77. strbuf *sb = NULL;
  78. if (ctx->keyed) {
  79. /*
  80. * If we've already been keyed, throw away the existing hash
  81. * objects and make a fresh pair to put the new key in.
  82. */
  83. ssh_hash_free(ctx->h_outer);
  84. ssh_hash_free(ctx->h_inner);
  85. ctx->h_outer = ssh_hash_new(ctx->hashalg);
  86. ctx->h_inner = ssh_hash_new(ctx->hashalg);
  87. }
  88. ctx->keyed = true;
  89. if (key.len > ctx->hashalg->blocklen) {
  90. /*
  91. * RFC 2104 section 2: if the key exceeds the block length of
  92. * the underlying hash, then we start by hashing the key, and
  93. * use that hash as the 'true' key for the HMAC construction.
  94. */
  95. sb = strbuf_new_nm();
  96. strbuf_append(sb, ctx->hashalg->hlen);
  97. ssh_hash *htmp = ssh_hash_new(ctx->hashalg);
  98. put_datapl(htmp, key);
  99. ssh_hash_final(htmp, sb->u);
  100. kp = sb->u;
  101. klen = sb->len;
  102. } else {
  103. /*
  104. * A short enough key is used as is.
  105. */
  106. kp = (const uint8_t *)key.ptr;
  107. klen = key.len;
  108. }
  109. if (ctx->h_outer)
  110. ssh_hash_free(ctx->h_outer);
  111. if (ctx->h_inner)
  112. ssh_hash_free(ctx->h_inner);
  113. ctx->h_outer = ssh_hash_new(ctx->hashalg);
  114. for (size_t i = 0; i < klen; i++)
  115. put_byte(ctx->h_outer, PAD_OUTER ^ kp[i]);
  116. for (size_t i = klen; i < ctx->hashalg->blocklen; i++)
  117. put_byte(ctx->h_outer, PAD_OUTER);
  118. ctx->h_inner = ssh_hash_new(ctx->hashalg);
  119. for (size_t i = 0; i < klen; i++)
  120. put_byte(ctx->h_inner, PAD_INNER ^ kp[i]);
  121. for (size_t i = klen; i < ctx->hashalg->blocklen; i++)
  122. put_byte(ctx->h_inner, PAD_INNER);
  123. if (sb)
  124. strbuf_free(sb);
  125. }
  126. static void hmac_start(ssh2_mac *mac)
  127. {
  128. struct hmac *ctx = container_of(mac, struct hmac, mac);
  129. ssh_hash_free(ctx->h_live);
  130. ctx->h_live = ssh_hash_copy(ctx->h_inner);
  131. BinarySink_DELEGATE_INIT(&ctx->mac, ctx->h_live);
  132. }
  133. static void hmac_genresult(ssh2_mac *mac, unsigned char *output)
  134. {
  135. struct hmac *ctx = container_of(mac, struct hmac, mac);
  136. ssh_hash *htmp;
  137. /* Leave h_live in place, so that the SSH-2 BPP can continue
  138. * regenerating test results from different-length prefixes of the
  139. * packet */
  140. htmp = ssh_hash_copy(ctx->h_live);
  141. ssh_hash_final(htmp, ctx->digest);
  142. htmp = ssh_hash_copy(ctx->h_outer);
  143. put_data(htmp, ctx->digest, ctx->hashalg->hlen);
  144. ssh_hash_final(htmp, ctx->digest);
  145. /*
  146. * Some instances of HMAC truncate the output hash, so instead of
  147. * writing it directly to 'output' we wrote it to our own
  148. * full-length buffer, and now we copy the required amount.
  149. */
  150. memcpy(output, ctx->digest, mac->vt->len);
  151. smemclr(ctx->digest, ctx->hashalg->hlen);
  152. }
  153. static const char *hmac_text_name(ssh2_mac *mac)
  154. {
  155. struct hmac *ctx = container_of(mac, struct hmac, mac);
  156. return ctx->text_name->s;
  157. }
  158. const struct hmac_extra ssh_hmac_sha256_extra = { &ssh_sha256, "" };
  159. const ssh2_macalg ssh_hmac_sha256 = {
  160. hmac_new, hmac_free, hmac_key,
  161. hmac_start, hmac_genresult, hmac_text_name,
  162. "hmac-sha2-256", "[email protected]",
  163. 32, 32, &ssh_hmac_sha256_extra,
  164. };
  165. const struct hmac_extra ssh_hmac_md5_extra = { &ssh_md5, "" };
  166. const ssh2_macalg ssh_hmac_md5 = {
  167. hmac_new, hmac_free, hmac_key,
  168. hmac_start, hmac_genresult, hmac_text_name,
  169. "hmac-md5", "[email protected]",
  170. 16, 16, &ssh_hmac_md5_extra,
  171. };
  172. const struct hmac_extra ssh_hmac_sha1_extra = { &ssh_sha1, "" };
  173. const ssh2_macalg ssh_hmac_sha1 = {
  174. hmac_new, hmac_free, hmac_key,
  175. hmac_start, hmac_genresult, hmac_text_name,
  176. "hmac-sha1", "[email protected]",
  177. 20, 20, &ssh_hmac_sha1_extra,
  178. };
  179. const struct hmac_extra ssh_hmac_sha1_96_extra = { &ssh_sha1, "-96" };
  180. const ssh2_macalg ssh_hmac_sha1_96 = {
  181. hmac_new, hmac_free, hmac_key,
  182. hmac_start, hmac_genresult, hmac_text_name,
  183. "hmac-sha1-96", "[email protected]",
  184. 12, 20, &ssh_hmac_sha1_96_extra,
  185. };
  186. const struct hmac_extra ssh_hmac_sha1_buggy_extra = {
  187. &ssh_sha1, " (bug-compatible)"
  188. };
  189. const ssh2_macalg ssh_hmac_sha1_buggy = {
  190. hmac_new, hmac_free, hmac_key,
  191. hmac_start, hmac_genresult, hmac_text_name,
  192. "hmac-sha1", NULL,
  193. 20, 16, &ssh_hmac_sha1_buggy_extra,
  194. };
  195. const struct hmac_extra ssh_hmac_sha1_96_buggy_extra = {
  196. &ssh_sha1, "-96 (bug-compatible)"
  197. };
  198. const ssh2_macalg ssh_hmac_sha1_96_buggy = {
  199. hmac_new, hmac_free, hmac_key,
  200. hmac_start, hmac_genresult, hmac_text_name,
  201. "hmac-sha1-96", NULL,
  202. 12, 16, &ssh_hmac_sha1_96_buggy_extra,
  203. };