ed25519.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Dropbear - a SSH2 server
  3. *
  4. * Copyright (c) 2002,2003 Matt Johnston
  5. * All rights reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE. */
  24. /* Perform Ed25519 operations on data, including reading keys, signing and
  25. * verification. */
  26. #include "includes.h"
  27. #include "dbutil.h"
  28. #include "buffer.h"
  29. #include "ssh.h"
  30. #include "curve25519.h"
  31. #include "ed25519.h"
  32. #if DROPBEAR_ED25519
  33. /* Load a public ed25519 key from a buffer, initialising the values.
  34. * The key will have the same format as buf_put_ed25519_key.
  35. * These should be freed with ed25519_key_free.
  36. * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
  37. int buf_get_ed25519_pub_key(buffer *buf, dropbear_ed25519_key *key,
  38. enum signkey_type expect_keytype) {
  39. unsigned int len, typelen;
  40. char *keytype = NULL;
  41. enum signkey_type buf_keytype;
  42. TRACE(("enter buf_get_ed25519_pub_key"))
  43. dropbear_assert(key != NULL);
  44. /* consume and check the key string */
  45. keytype = buf_getstring(buf, &typelen);
  46. buf_keytype = signkey_type_from_name(keytype, typelen);
  47. m_free(keytype);
  48. if (buf_keytype != expect_keytype) {
  49. TRACE(("leave buf_get_ed25519_pub_key: mismatch key type"))
  50. return DROPBEAR_FAILURE;
  51. }
  52. len = buf_getint(buf);
  53. if (len != CURVE25519_LEN || buf->len - buf->pos < len) {
  54. TRACE(("leave buf_get_ed25519_pub_key: failure"))
  55. return DROPBEAR_FAILURE;
  56. }
  57. m_burn(key->priv, CURVE25519_LEN);
  58. memcpy(key->pub, buf_getptr(buf, CURVE25519_LEN), CURVE25519_LEN);
  59. buf_incrpos(buf, CURVE25519_LEN);
  60. TRACE(("leave buf_get_ed25519_pub_key: success"))
  61. return DROPBEAR_SUCCESS;
  62. }
  63. /* Same as buf_get_ed25519_pub_key, but reads private key at the end.
  64. * Loads a public and private ed25519 key from a buffer
  65. * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
  66. int buf_get_ed25519_priv_key(buffer *buf, dropbear_ed25519_key *key) {
  67. unsigned int len;
  68. TRACE(("enter buf_get_ed25519_priv_key"))
  69. dropbear_assert(key != NULL);
  70. buf_incrpos(buf, 4+SSH_SIGNKEY_ED25519_LEN); /* int + "ssh-ed25519" */
  71. len = buf_getint(buf);
  72. if (len != CURVE25519_LEN*2 || buf->len - buf->pos < len) {
  73. TRACE(("leave buf_get_ed25519_priv_key: failure"))
  74. return DROPBEAR_FAILURE;
  75. }
  76. memcpy(key->priv, buf_getptr(buf, CURVE25519_LEN), CURVE25519_LEN);
  77. buf_incrpos(buf, CURVE25519_LEN);
  78. memcpy(key->pub, buf_getptr(buf, CURVE25519_LEN), CURVE25519_LEN);
  79. buf_incrpos(buf, CURVE25519_LEN);
  80. TRACE(("leave buf_get_ed25519_priv_key: success"))
  81. return DROPBEAR_SUCCESS;
  82. }
  83. /* Clear and free the memory used by a public or private key */
  84. void ed25519_key_free(dropbear_ed25519_key *key) {
  85. TRACE2(("enter ed25519_key_free"))
  86. if (key == NULL) {
  87. TRACE2(("leave ed25519_key_free: key == NULL"))
  88. return;
  89. }
  90. m_burn(key->priv, CURVE25519_LEN);
  91. m_free(key);
  92. TRACE2(("leave ed25519_key_free"))
  93. }
  94. /* Put the public ed25519 key into the buffer in the required format */
  95. void buf_put_ed25519_pub_key(buffer *buf, const dropbear_ed25519_key *key) {
  96. TRACE(("enter buf_put_ed25519_pub_key"))
  97. dropbear_assert(key != NULL);
  98. buf_putstring(buf, SSH_SIGNKEY_ED25519, SSH_SIGNKEY_ED25519_LEN);
  99. buf_putstring(buf, key->pub, CURVE25519_LEN);
  100. TRACE(("leave buf_put_ed25519_pub_key"))
  101. }
  102. /* Put the public and private ed25519 key into the buffer in the required format */
  103. void buf_put_ed25519_priv_key(buffer *buf, const dropbear_ed25519_key *key) {
  104. TRACE(("enter buf_put_ed25519_priv_key"))
  105. dropbear_assert(key != NULL);
  106. buf_putstring(buf, SSH_SIGNKEY_ED25519, SSH_SIGNKEY_ED25519_LEN);
  107. buf_putint(buf, CURVE25519_LEN*2);
  108. buf_putbytes(buf, key->priv, CURVE25519_LEN);
  109. buf_putbytes(buf, key->pub, CURVE25519_LEN);
  110. TRACE(("leave buf_put_ed25519_priv_key"))
  111. }
  112. /* Sign the data presented with key, writing the signature contents
  113. * to the buffer */
  114. void buf_put_ed25519_sign(buffer* buf, const dropbear_ed25519_key *key, const buffer *data_buf) {
  115. unsigned char s[64];
  116. unsigned long slen = sizeof(s);
  117. TRACE(("enter buf_put_ed25519_sign"))
  118. dropbear_assert(key != NULL);
  119. dropbear_ed25519_sign(data_buf->data, data_buf->len, s, &slen, key->priv, key->pub);
  120. buf_putstring(buf, SSH_SIGNKEY_ED25519, SSH_SIGNKEY_ED25519_LEN);
  121. buf_putstring(buf, s, slen);
  122. TRACE(("leave buf_put_ed25519_sign"))
  123. }
  124. #if DROPBEAR_SIGNKEY_VERIFY
  125. /* Verify a signature in buf, made on data by the key given.
  126. * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
  127. int buf_ed25519_verify(buffer *buf, const dropbear_ed25519_key *key, const buffer *data_buf) {
  128. int ret = DROPBEAR_FAILURE;
  129. unsigned char *s;
  130. unsigned long slen;
  131. TRACE(("enter buf_ed25519_verify"))
  132. dropbear_assert(key != NULL);
  133. slen = buf_getint(buf);
  134. if (slen != 64 || buf->len - buf->pos < slen) {
  135. TRACE(("leave buf_ed25519_verify: bad size"))
  136. goto out;
  137. }
  138. s = buf_getptr(buf, slen);
  139. if (dropbear_ed25519_verify(data_buf->data, data_buf->len,
  140. s, slen, key->pub) == 0) {
  141. /* signature is valid */
  142. TRACE(("leave buf_ed25519_verify: success!"))
  143. ret = DROPBEAR_SUCCESS;
  144. }
  145. out:
  146. TRACE(("leave buf_ed25519_verify: ret %d", ret))
  147. return ret;
  148. }
  149. #endif /* DROPBEAR_SIGNKEY_VERIFY */
  150. #endif /* DROPBEAR_ED25519 */