hmac.c 7.4 KB

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