ssh-ed25519-sk.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* $OpenBSD: ssh-ed25519-sk.c,v 1.5 2020/02/26 13:40:09 jsg Exp $ */
  2. /*
  3. * Copyright (c) 2019 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. /* #define DEBUG_SK 1 */
  18. #include "includes.h"
  19. #define SSHKEY_INTERNAL
  20. #include <sys/types.h>
  21. #include <limits.h>
  22. #include "crypto_api.h"
  23. #include <string.h>
  24. #include <stdarg.h>
  25. #include "log.h"
  26. #include "sshbuf.h"
  27. #include "sshkey.h"
  28. #include "ssherr.h"
  29. #include "ssh.h"
  30. #include "digest.h"
  31. int
  32. ssh_ed25519_sk_verify(const struct sshkey *key,
  33. const u_char *signature, size_t signaturelen,
  34. const u_char *data, size_t datalen, u_int compat,
  35. struct sshkey_sig_details **detailsp)
  36. {
  37. struct sshbuf *b = NULL;
  38. struct sshbuf *encoded = NULL;
  39. char *ktype = NULL;
  40. const u_char *sigblob;
  41. const u_char *sm;
  42. u_char *m = NULL;
  43. u_char apphash[32];
  44. u_char msghash[32];
  45. u_char sig_flags;
  46. u_int sig_counter;
  47. size_t len;
  48. unsigned long long smlen = 0, mlen = 0;
  49. int r = SSH_ERR_INTERNAL_ERROR;
  50. int ret;
  51. struct sshkey_sig_details *details = NULL;
  52. if (detailsp != NULL)
  53. *detailsp = NULL;
  54. if (key == NULL ||
  55. sshkey_type_plain(key->type) != KEY_ED25519_SK ||
  56. key->ed25519_pk == NULL ||
  57. signature == NULL || signaturelen == 0)
  58. return SSH_ERR_INVALID_ARGUMENT;
  59. if ((b = sshbuf_from(signature, signaturelen)) == NULL)
  60. return SSH_ERR_ALLOC_FAIL;
  61. if (sshbuf_get_cstring(b, &ktype, NULL) != 0 ||
  62. sshbuf_get_string_direct(b, &sigblob, &len) != 0 ||
  63. sshbuf_get_u8(b, &sig_flags) != 0 ||
  64. sshbuf_get_u32(b, &sig_counter) != 0) {
  65. r = SSH_ERR_INVALID_FORMAT;
  66. goto out;
  67. }
  68. #ifdef DEBUG_SK
  69. fprintf(stderr, "%s: data:\n", __func__);
  70. /* sshbuf_dump_data(data, datalen, stderr); */
  71. fprintf(stderr, "%s: sigblob:\n", __func__);
  72. sshbuf_dump_data(sigblob, len, stderr);
  73. fprintf(stderr, "%s: sig_flags = 0x%02x, sig_counter = %u\n",
  74. __func__, sig_flags, sig_counter);
  75. #endif
  76. if (strcmp(sshkey_ssh_name_plain(key), ktype) != 0) {
  77. r = SSH_ERR_KEY_TYPE_MISMATCH;
  78. goto out;
  79. }
  80. if (sshbuf_len(b) != 0) {
  81. r = SSH_ERR_UNEXPECTED_TRAILING_DATA;
  82. goto out;
  83. }
  84. if (len > crypto_sign_ed25519_BYTES) {
  85. r = SSH_ERR_INVALID_FORMAT;
  86. goto out;
  87. }
  88. if (ssh_digest_memory(SSH_DIGEST_SHA256, key->sk_application,
  89. strlen(key->sk_application), apphash, sizeof(apphash)) != 0 ||
  90. ssh_digest_memory(SSH_DIGEST_SHA256, data, datalen,
  91. msghash, sizeof(msghash)) != 0) {
  92. r = SSH_ERR_INVALID_ARGUMENT;
  93. goto out;
  94. }
  95. #ifdef DEBUG_SK
  96. fprintf(stderr, "%s: hashed application:\n", __func__);
  97. sshbuf_dump_data(apphash, sizeof(apphash), stderr);
  98. fprintf(stderr, "%s: hashed message:\n", __func__);
  99. sshbuf_dump_data(msghash, sizeof(msghash), stderr);
  100. #endif
  101. if ((details = calloc(1, sizeof(*details))) == NULL) {
  102. r = SSH_ERR_ALLOC_FAIL;
  103. goto out;
  104. }
  105. details->sk_counter = sig_counter;
  106. details->sk_flags = sig_flags;
  107. if ((encoded = sshbuf_new()) == NULL) {
  108. r = SSH_ERR_ALLOC_FAIL;
  109. goto out;
  110. }
  111. if (sshbuf_put(encoded, sigblob, len) != 0 ||
  112. sshbuf_put(encoded, apphash, sizeof(apphash)) != 0 ||
  113. sshbuf_put_u8(encoded, sig_flags) != 0 ||
  114. sshbuf_put_u32(encoded, sig_counter) != 0 ||
  115. sshbuf_put(encoded, msghash, sizeof(msghash)) != 0) {
  116. r = SSH_ERR_ALLOC_FAIL;
  117. goto out;
  118. }
  119. #ifdef DEBUG_SK
  120. fprintf(stderr, "%s: signed buf:\n", __func__);
  121. sshbuf_dump(encoded, stderr);
  122. #endif
  123. sm = sshbuf_ptr(encoded);
  124. smlen = sshbuf_len(encoded);
  125. mlen = smlen;
  126. if ((m = malloc(smlen)) == NULL) {
  127. r = SSH_ERR_ALLOC_FAIL;
  128. goto out;
  129. }
  130. if ((ret = crypto_sign_ed25519_open(m, &mlen, sm, smlen,
  131. key->ed25519_pk)) != 0) {
  132. debug2("%s: crypto_sign_ed25519_open failed: %d",
  133. __func__, ret);
  134. }
  135. if (ret != 0 || mlen != smlen - len) {
  136. r = SSH_ERR_SIGNATURE_INVALID;
  137. goto out;
  138. }
  139. /* XXX compare 'm' and 'sm + len' ? */
  140. /* success */
  141. r = 0;
  142. if (detailsp != NULL) {
  143. *detailsp = details;
  144. details = NULL;
  145. }
  146. out:
  147. if (m != NULL)
  148. freezero(m, smlen); /* NB mlen may be invalid if r != 0 */
  149. sshkey_sig_details_free(details);
  150. sshbuf_free(b);
  151. sshbuf_free(encoded);
  152. free(ktype);
  153. return r;
  154. }