sshhmac.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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();
  96. strbuf_append(sb, ctx->hashalg->hlen);
  97. { // WINSCP
  98. ssh_hash *htmp = ssh_hash_new(ctx->hashalg);
  99. put_datapl(htmp, key);
  100. ssh_hash_final(htmp, sb->u);
  101. kp = sb->u;
  102. klen = sb->len;
  103. } // WINSCP
  104. } else {
  105. /*
  106. * A short enough key is used as is.
  107. */
  108. kp = (const uint8_t *)key.ptr;
  109. klen = key.len;
  110. }
  111. if (ctx->h_outer)
  112. ssh_hash_free(ctx->h_outer);
  113. if (ctx->h_inner)
  114. ssh_hash_free(ctx->h_inner);
  115. ctx->h_outer = ssh_hash_new(ctx->hashalg);
  116. { // WINSCP
  117. size_t i; // WINSCP
  118. for (i = 0; i < klen; i++)
  119. put_byte(ctx->h_outer, PAD_OUTER ^ kp[i]);
  120. for (i = klen; i < ctx->hashalg->blocklen; i++)
  121. put_byte(ctx->h_outer, PAD_OUTER);
  122. ctx->h_inner = ssh_hash_new(ctx->hashalg);
  123. for (i = 0; i < klen; i++)
  124. put_byte(ctx->h_inner, PAD_INNER ^ kp[i]);
  125. for (i = klen; i < ctx->hashalg->blocklen; i++)
  126. put_byte(ctx->h_inner, PAD_INNER);
  127. if (sb)
  128. strbuf_free(sb);
  129. } // WINSCP
  130. }
  131. static void hmac_start(ssh2_mac *mac)
  132. {
  133. struct hmac *ctx = container_of(mac, struct hmac, mac);
  134. ssh_hash_free(ctx->h_live);
  135. ctx->h_live = ssh_hash_copy(ctx->h_inner);
  136. BinarySink_DELEGATE_INIT(&ctx->mac, ctx->h_live);
  137. }
  138. static void hmac_genresult(ssh2_mac *mac, unsigned char *output)
  139. {
  140. struct hmac *ctx = container_of(mac, struct hmac, mac);
  141. ssh_hash *htmp;
  142. /* Leave h_live in place, so that the SSH-2 BPP can continue
  143. * regenerating test results from different-length prefixes of the
  144. * packet */
  145. htmp = ssh_hash_copy(ctx->h_live);
  146. ssh_hash_final(htmp, ctx->digest);
  147. htmp = ssh_hash_copy(ctx->h_outer);
  148. put_data(htmp, ctx->digest, ctx->hashalg->hlen);
  149. ssh_hash_final(htmp, ctx->digest);
  150. /*
  151. * Some instances of HMAC truncate the output hash, so instead of
  152. * writing it directly to 'output' we wrote it to our own
  153. * full-length buffer, and now we copy the required amount.
  154. */
  155. memcpy(output, ctx->digest, mac->vt->len);
  156. smemclr(ctx->digest, ctx->hashalg->hlen);
  157. }
  158. static const char *hmac_text_name(ssh2_mac *mac)
  159. {
  160. struct hmac *ctx = container_of(mac, struct hmac, mac);
  161. return ctx->text_name->s;
  162. }
  163. const struct hmac_extra ssh_hmac_sha256_extra = { &ssh_sha256, "" };
  164. const ssh2_macalg ssh_hmac_sha256 = {
  165. hmac_new, hmac_free, hmac_key,
  166. hmac_start, hmac_genresult, hmac_text_name,
  167. "hmac-sha2-256", "[email protected]",
  168. 32, 32, &ssh_hmac_sha256_extra,
  169. };
  170. const struct hmac_extra ssh_hmac_md5_extra = { &ssh_md5, "" };
  171. const ssh2_macalg ssh_hmac_md5 = {
  172. hmac_new, hmac_free, hmac_key,
  173. hmac_start, hmac_genresult, hmac_text_name,
  174. "hmac-md5", "[email protected]",
  175. 16, 16, &ssh_hmac_md5_extra,
  176. };
  177. const struct hmac_extra ssh_hmac_sha1_extra = { &ssh_sha1, "" };
  178. const ssh2_macalg ssh_hmac_sha1 = {
  179. hmac_new, hmac_free, hmac_key,
  180. hmac_start, hmac_genresult, hmac_text_name,
  181. "hmac-sha1", "[email protected]",
  182. 20, 20, &ssh_hmac_sha1_extra,
  183. };
  184. const struct hmac_extra ssh_hmac_sha1_96_extra = { &ssh_sha1, "-96" };
  185. const ssh2_macalg ssh_hmac_sha1_96 = {
  186. hmac_new, hmac_free, hmac_key,
  187. hmac_start, hmac_genresult, hmac_text_name,
  188. "hmac-sha1-96", "[email protected]",
  189. 12, 20, &ssh_hmac_sha1_96_extra,
  190. };
  191. const struct hmac_extra ssh_hmac_sha1_buggy_extra = {
  192. &ssh_sha1, " (bug-compatible)"
  193. };
  194. const ssh2_macalg ssh_hmac_sha1_buggy = {
  195. hmac_new, hmac_free, hmac_key,
  196. hmac_start, hmac_genresult, hmac_text_name,
  197. "hmac-sha1", NULL,
  198. 20, 16, &ssh_hmac_sha1_buggy_extra,
  199. };
  200. const struct hmac_extra ssh_hmac_sha1_96_buggy_extra = {
  201. &ssh_sha1, "-96 (bug-compatible)"
  202. };
  203. const ssh2_macalg ssh_hmac_sha1_96_buggy = {
  204. hmac_new, hmac_free, hmac_key,
  205. hmac_start, hmac_genresult, hmac_text_name,
  206. "hmac-sha1-96", NULL,
  207. 12, 16, &ssh_hmac_sha1_96_buggy_extra,
  208. };