sshhmac.c 7.2 KB

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