hmac.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* $OpenBSD: hmac.c,v 1.14 2020/02/26 13:40:09 jsg Exp $ */
  2. /*
  3. * Copyright (c) 2014 Markus Friedl. All rights reserved.
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include "includes.h"
  18. #include <sys/types.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "sshbuf.h"
  22. #include "digest.h"
  23. #include "hmac.h"
  24. struct ssh_hmac_ctx {
  25. int alg;
  26. struct ssh_digest_ctx *ictx;
  27. struct ssh_digest_ctx *octx;
  28. struct ssh_digest_ctx *digest;
  29. u_char *buf;
  30. size_t buf_len;
  31. };
  32. size_t
  33. ssh_hmac_bytes(int alg)
  34. {
  35. return ssh_digest_bytes(alg);
  36. }
  37. struct ssh_hmac_ctx *
  38. ssh_hmac_start(int alg)
  39. {
  40. struct ssh_hmac_ctx *ret;
  41. if ((ret = calloc(1, sizeof(*ret))) == NULL)
  42. return NULL;
  43. ret->alg = alg;
  44. if ((ret->ictx = ssh_digest_start(alg)) == NULL ||
  45. (ret->octx = ssh_digest_start(alg)) == NULL ||
  46. (ret->digest = ssh_digest_start(alg)) == NULL)
  47. goto fail;
  48. ret->buf_len = ssh_digest_blocksize(ret->ictx);
  49. if ((ret->buf = calloc(1, ret->buf_len)) == NULL)
  50. goto fail;
  51. return ret;
  52. fail:
  53. ssh_hmac_free(ret);
  54. return NULL;
  55. }
  56. int
  57. ssh_hmac_init(struct ssh_hmac_ctx *ctx, const void *key, size_t klen)
  58. {
  59. size_t i;
  60. /* reset ictx and octx if no is key given */
  61. if (key != NULL) {
  62. /* truncate long keys */
  63. if (klen <= ctx->buf_len)
  64. memcpy(ctx->buf, key, klen);
  65. else if (ssh_digest_memory(ctx->alg, key, klen, ctx->buf,
  66. ctx->buf_len) < 0)
  67. return -1;
  68. for (i = 0; i < ctx->buf_len; i++)
  69. ctx->buf[i] ^= 0x36;
  70. if (ssh_digest_update(ctx->ictx, ctx->buf, ctx->buf_len) < 0)
  71. return -1;
  72. for (i = 0; i < ctx->buf_len; i++)
  73. ctx->buf[i] ^= 0x36 ^ 0x5c;
  74. if (ssh_digest_update(ctx->octx, ctx->buf, ctx->buf_len) < 0)
  75. return -1;
  76. explicit_bzero(ctx->buf, ctx->buf_len);
  77. }
  78. /* start with ictx */
  79. if (ssh_digest_copy_state(ctx->ictx, ctx->digest) < 0)
  80. return -1;
  81. return 0;
  82. }
  83. int
  84. ssh_hmac_update(struct ssh_hmac_ctx *ctx, const void *m, size_t mlen)
  85. {
  86. return ssh_digest_update(ctx->digest, m, mlen);
  87. }
  88. int
  89. ssh_hmac_update_buffer(struct ssh_hmac_ctx *ctx, const struct sshbuf *b)
  90. {
  91. return ssh_digest_update_buffer(ctx->digest, b);
  92. }
  93. int
  94. ssh_hmac_final(struct ssh_hmac_ctx *ctx, u_char *d, size_t dlen)
  95. {
  96. size_t len;
  97. len = ssh_digest_bytes(ctx->alg);
  98. if (dlen < len ||
  99. ssh_digest_final(ctx->digest, ctx->buf, len))
  100. return -1;
  101. /* switch to octx */
  102. if (ssh_digest_copy_state(ctx->octx, ctx->digest) < 0 ||
  103. ssh_digest_update(ctx->digest, ctx->buf, len) < 0 ||
  104. ssh_digest_final(ctx->digest, d, dlen) < 0)
  105. return -1;
  106. return 0;
  107. }
  108. void
  109. ssh_hmac_free(struct ssh_hmac_ctx *ctx)
  110. {
  111. if (ctx != NULL) {
  112. ssh_digest_free(ctx->ictx);
  113. ssh_digest_free(ctx->octx);
  114. ssh_digest_free(ctx->digest);
  115. if (ctx->buf) {
  116. explicit_bzero(ctx->buf, ctx->buf_len);
  117. free(ctx->buf);
  118. }
  119. freezero(ctx, sizeof(*ctx));
  120. }
  121. }
  122. #ifdef TEST
  123. /* cc -DTEST hmac.c digest.c buffer.c cleanup.c fatal.c log.c xmalloc.c -lcrypto */
  124. static void
  125. hmac_test(void *key, size_t klen, void *m, size_t mlen, u_char *e, size_t elen)
  126. {
  127. struct ssh_hmac_ctx *ctx;
  128. size_t i;
  129. u_char digest[16];
  130. if ((ctx = ssh_hmac_start(SSH_DIGEST_MD5)) == NULL)
  131. printf("ssh_hmac_start failed");
  132. if (ssh_hmac_init(ctx, key, klen) < 0 ||
  133. ssh_hmac_update(ctx, m, mlen) < 0 ||
  134. ssh_hmac_final(ctx, digest, sizeof(digest)) < 0)
  135. printf("ssh_hmac_xxx failed");
  136. ssh_hmac_free(ctx);
  137. if (memcmp(e, digest, elen)) {
  138. for (i = 0; i < elen; i++)
  139. printf("[%zu] %2.2x %2.2x\n", i, e[i], digest[i]);
  140. printf("mismatch\n");
  141. } else
  142. printf("ok\n");
  143. }
  144. int
  145. main(int argc, char **argv)
  146. {
  147. /* try test vectors from RFC 2104 */
  148. u_char key1[16] = {
  149. 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb,
  150. 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb };
  151. u_char *data1 = "Hi There";
  152. u_char dig1[16] = {
  153. 0x92, 0x94, 0x72, 0x7a, 0x36, 0x38, 0xbb, 0x1c,
  154. 0x13, 0xf4, 0x8e, 0xf8, 0x15, 0x8b, 0xfc, 0x9d };
  155. u_char *key2 = "Jefe";
  156. u_char *data2 = "what do ya want for nothing?";
  157. u_char dig2[16] = {
  158. 0x75, 0x0c, 0x78, 0x3e, 0x6a, 0xb0, 0xb5, 0x03,
  159. 0xea, 0xa8, 0x6e, 0x31, 0x0a, 0x5d, 0xb7, 0x38 };
  160. u_char key3[16];
  161. u_char data3[50];
  162. u_char dig3[16] = {
  163. 0x56, 0xbe, 0x34, 0x52, 0x1d, 0x14, 0x4c, 0x88,
  164. 0xdb, 0xb8, 0xc7, 0x33, 0xf0, 0xe8, 0xb3, 0xf6 };
  165. memset(key3, 0xaa, sizeof(key3));
  166. memset(data3, 0xdd, sizeof(data3));
  167. hmac_test(key1, sizeof(key1), data1, strlen(data1), dig1, sizeof(dig1));
  168. hmac_test(key2, strlen(key2), data2, strlen(data2), dig2, sizeof(dig2));
  169. hmac_test(key3, sizeof(key3), data3, sizeof(data3), dig3, sizeof(dig3));
  170. return 0;
  171. }
  172. #endif