cipher-chachapoly-libcrypto.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (c) 2013 Damien Miller <djm@mindrot.org>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. /* $OpenBSD: cipher-chachapoly-libcrypto.c,v 1.1 2020/04/03 04:32:21 djm Exp $ */
  17. #include "includes.h"
  18. #ifdef WITH_OPENSSL
  19. #include "openbsd-compat/openssl-compat.h"
  20. #endif
  21. #if defined(HAVE_EVP_CHACHA20) && !defined(HAVE_BROKEN_CHACHA20)
  22. #include <sys/types.h>
  23. #include <stdarg.h> /* needed for log.h */
  24. #include <string.h>
  25. #include <stdio.h> /* needed for misc.h */
  26. #include <openssl/evp.h>
  27. #include "log.h"
  28. #include "sshbuf.h"
  29. #include "ssherr.h"
  30. #include "cipher-chachapoly.h"
  31. struct chachapoly_ctx {
  32. EVP_CIPHER_CTX *main_evp, *header_evp;
  33. };
  34. struct chachapoly_ctx *
  35. chachapoly_new(const u_char *key, u_int keylen)
  36. {
  37. struct chachapoly_ctx *ctx;
  38. if (keylen != (32 + 32)) /* 2 x 256 bit keys */
  39. return NULL;
  40. if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
  41. return NULL;
  42. if ((ctx->main_evp = EVP_CIPHER_CTX_new()) == NULL ||
  43. (ctx->header_evp = EVP_CIPHER_CTX_new()) == NULL)
  44. goto fail;
  45. if (!EVP_CipherInit(ctx->main_evp, EVP_chacha20(), key, NULL, 1))
  46. goto fail;
  47. if (!EVP_CipherInit(ctx->header_evp, EVP_chacha20(), key + 32, NULL, 1))
  48. goto fail;
  49. if (EVP_CIPHER_CTX_iv_length(ctx->header_evp) != 16)
  50. goto fail;
  51. return ctx;
  52. fail:
  53. chachapoly_free(ctx);
  54. return NULL;
  55. }
  56. void
  57. chachapoly_free(struct chachapoly_ctx *cpctx)
  58. {
  59. if (cpctx == NULL)
  60. return;
  61. EVP_CIPHER_CTX_free(cpctx->main_evp);
  62. EVP_CIPHER_CTX_free(cpctx->header_evp);
  63. freezero(cpctx, sizeof(*cpctx));
  64. }
  65. /*
  66. * chachapoly_crypt() operates as following:
  67. * En/decrypt with header key 'aadlen' bytes from 'src', storing result
  68. * to 'dest'. The ciphertext here is treated as additional authenticated
  69. * data for MAC calculation.
  70. * En/decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'. Use
  71. * POLY1305_TAGLEN bytes at offset 'len'+'aadlen' as the authentication
  72. * tag. This tag is written on encryption and verified on decryption.
  73. */
  74. int
  75. chachapoly_crypt(struct chachapoly_ctx *ctx, u_int seqnr, u_char *dest,
  76. const u_char *src, u_int len, u_int aadlen, u_int authlen, int do_encrypt)
  77. {
  78. u_char seqbuf[16]; /* layout: u64 counter || u64 seqno */
  79. int r = SSH_ERR_INTERNAL_ERROR;
  80. u_char expected_tag[POLY1305_TAGLEN], poly_key[POLY1305_KEYLEN];
  81. /*
  82. * Run ChaCha20 once to generate the Poly1305 key. The IV is the
  83. * packet sequence number.
  84. */
  85. memset(seqbuf, 0, sizeof(seqbuf));
  86. POKE_U64(seqbuf + 8, seqnr);
  87. memset(poly_key, 0, sizeof(poly_key));
  88. if (!EVP_CipherInit(ctx->main_evp, NULL, NULL, seqbuf, 1) ||
  89. EVP_Cipher(ctx->main_evp, poly_key,
  90. poly_key, sizeof(poly_key)) < 0) {
  91. r = SSH_ERR_LIBCRYPTO_ERROR;
  92. goto out;
  93. }
  94. /* If decrypting, check tag before anything else */
  95. if (!do_encrypt) {
  96. const u_char *tag = src + aadlen + len;
  97. poly1305_auth(expected_tag, src, aadlen + len, poly_key);
  98. if (timingsafe_bcmp(expected_tag, tag, POLY1305_TAGLEN) != 0) {
  99. r = SSH_ERR_MAC_INVALID;
  100. goto out;
  101. }
  102. }
  103. /* Crypt additional data */
  104. if (aadlen) {
  105. if (!EVP_CipherInit(ctx->header_evp, NULL, NULL, seqbuf, 1) ||
  106. EVP_Cipher(ctx->header_evp, dest, src, aadlen) < 0) {
  107. r = SSH_ERR_LIBCRYPTO_ERROR;
  108. goto out;
  109. }
  110. }
  111. /* Set Chacha's block counter to 1 */
  112. seqbuf[0] = 1;
  113. if (!EVP_CipherInit(ctx->main_evp, NULL, NULL, seqbuf, 1) ||
  114. EVP_Cipher(ctx->main_evp, dest + aadlen, src + aadlen, len) < 0) {
  115. r = SSH_ERR_LIBCRYPTO_ERROR;
  116. goto out;
  117. }
  118. /* If encrypting, calculate and append tag */
  119. if (do_encrypt) {
  120. poly1305_auth(dest + aadlen + len, dest, aadlen + len,
  121. poly_key);
  122. }
  123. r = 0;
  124. out:
  125. explicit_bzero(expected_tag, sizeof(expected_tag));
  126. explicit_bzero(seqbuf, sizeof(seqbuf));
  127. explicit_bzero(poly_key, sizeof(poly_key));
  128. return r;
  129. }
  130. /* Decrypt and extract the encrypted packet length */
  131. int
  132. chachapoly_get_length(struct chachapoly_ctx *ctx,
  133. u_int *plenp, u_int seqnr, const u_char *cp, u_int len)
  134. {
  135. u_char buf[4], seqbuf[16];
  136. if (len < 4)
  137. return SSH_ERR_MESSAGE_INCOMPLETE;
  138. memset(seqbuf, 0, sizeof(seqbuf));
  139. POKE_U64(seqbuf + 8, seqnr);
  140. if (!EVP_CipherInit(ctx->header_evp, NULL, NULL, seqbuf, 0))
  141. return SSH_ERR_LIBCRYPTO_ERROR;
  142. if (EVP_Cipher(ctx->header_evp, buf, (u_char *)cp, sizeof(buf)) < 0)
  143. return SSH_ERR_LIBCRYPTO_ERROR;
  144. *plenp = PEEK_U32(buf);
  145. return 0;
  146. }
  147. #endif /* defined(HAVE_EVP_CHACHA20) && !defined(HAVE_BROKEN_CHACHA20) */