chachapoly.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Dropbear SSH
  3. *
  4. * Copyright (c) 2002,2003 Matt Johnston
  5. * Copyright (c) 2020 by Vladislav Grishenko
  6. * All rights reserved.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE. */
  25. #include "includes.h"
  26. #include "algo.h"
  27. #include "dbutil.h"
  28. #include "chachapoly.h"
  29. #if DROPBEAR_CHACHA20POLY1305
  30. #define CHACHA20_KEY_LEN 32
  31. #define CHACHA20_BLOCKSIZE 8
  32. #define POLY1305_KEY_LEN 32
  33. #define POLY1305_TAG_LEN 16
  34. static const struct ltc_cipher_descriptor dummy = {.name = NULL};
  35. static const struct dropbear_hash dropbear_chachapoly_mac =
  36. {NULL, POLY1305_KEY_LEN, POLY1305_TAG_LEN};
  37. const struct dropbear_cipher dropbear_chachapoly =
  38. {&dummy, CHACHA20_KEY_LEN*2, CHACHA20_BLOCKSIZE};
  39. static int dropbear_chachapoly_start(int UNUSED(cipher), const unsigned char* UNUSED(IV),
  40. const unsigned char *key, int keylen,
  41. int UNUSED(num_rounds), dropbear_chachapoly_state *state) {
  42. int err;
  43. TRACE2(("enter dropbear_chachapoly_start"))
  44. if (keylen != CHACHA20_KEY_LEN*2) {
  45. return CRYPT_ERROR;
  46. }
  47. if ((err = chacha_setup(&state->chacha, key,
  48. CHACHA20_KEY_LEN, 20)) != CRYPT_OK) {
  49. return err;
  50. }
  51. if ((err = chacha_setup(&state->header, key + CHACHA20_KEY_LEN,
  52. CHACHA20_KEY_LEN, 20) != CRYPT_OK)) {
  53. return err;
  54. }
  55. TRACE2(("leave dropbear_chachapoly_start"))
  56. return CRYPT_OK;
  57. }
  58. static int dropbear_chachapoly_crypt(unsigned int seq,
  59. const unsigned char *in, unsigned char *out,
  60. unsigned long len, unsigned long taglen,
  61. dropbear_chachapoly_state *state, int direction) {
  62. poly1305_state poly;
  63. unsigned char seqbuf[8], key[POLY1305_KEY_LEN], tag[POLY1305_TAG_LEN];
  64. int err;
  65. TRACE2(("enter dropbear_chachapoly_crypt"))
  66. if (len < 4 || taglen != POLY1305_TAG_LEN) {
  67. return CRYPT_ERROR;
  68. }
  69. STORE64H((uint64_t)seq, seqbuf);
  70. chacha_ivctr64(&state->chacha, seqbuf, sizeof(seqbuf), 0);
  71. if ((err = chacha_keystream(&state->chacha, key, sizeof(key))) != CRYPT_OK) {
  72. return err;
  73. }
  74. poly1305_init(&poly, key, sizeof(key));
  75. if (direction == LTC_DECRYPT) {
  76. poly1305_process(&poly, in, len);
  77. poly1305_done(&poly, tag, &taglen);
  78. if (constant_time_memcmp(in + len, tag, taglen) != 0) {
  79. return CRYPT_ERROR;
  80. }
  81. }
  82. chacha_ivctr64(&state->header, seqbuf, sizeof(seqbuf), 0);
  83. if ((err = chacha_crypt(&state->header, in, 4, out)) != CRYPT_OK) {
  84. return err;
  85. }
  86. chacha_ivctr64(&state->chacha, seqbuf, sizeof(seqbuf), 1);
  87. if ((err = chacha_crypt(&state->chacha, in + 4, len - 4, out + 4)) != CRYPT_OK) {
  88. return err;
  89. }
  90. if (direction == LTC_ENCRYPT) {
  91. poly1305_process(&poly, out, len);
  92. poly1305_done(&poly, out + len, &taglen);
  93. }
  94. TRACE2(("leave dropbear_chachapoly_crypt"))
  95. return CRYPT_OK;
  96. }
  97. static int dropbear_chachapoly_getlength(unsigned int seq,
  98. const unsigned char *in, unsigned int *outlen,
  99. unsigned long len, dropbear_chachapoly_state *state) {
  100. unsigned char seqbuf[8], buf[4];
  101. int err;
  102. TRACE2(("enter dropbear_chachapoly_getlength"))
  103. if (len < sizeof(buf)) {
  104. return CRYPT_ERROR;
  105. }
  106. STORE64H((uint64_t)seq, seqbuf);
  107. chacha_ivctr64(&state->header, seqbuf, sizeof(seqbuf), 0);
  108. if ((err = chacha_crypt(&state->header, in, sizeof(buf), buf)) != CRYPT_OK) {
  109. return err;
  110. }
  111. LOAD32H(*outlen, buf);
  112. TRACE2(("leave dropbear_chachapoly_getlength"))
  113. return CRYPT_OK;
  114. }
  115. const struct dropbear_cipher_mode dropbear_mode_chachapoly =
  116. {(void *)dropbear_chachapoly_start, NULL, NULL,
  117. (void *)dropbear_chachapoly_crypt,
  118. (void *)dropbear_chachapoly_getlength, &dropbear_chachapoly_mac};
  119. #endif /* DROPBEAR_CHACHA20POLY1305 */