You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

chacha_cryptobox.c 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 2015, Vsevolod Stakhov
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY AUTHOR ''AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
  17. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  19. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  20. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include "config.h"
  25. #include "ottery-internal.h"
  26. #include "libcryptobox/chacha20/chacha.h"
  27. #define STATE_LEN (sizeof(chacha_state))
  28. #define STATE_BYTES 40
  29. #define IDX_STEP 16
  30. #define OUTPUT_LEN (IDX_STEP * 64)
  31. static void
  32. chacha20_cryptobox_state_setup (void *state_, const uint8_t *bytes)
  33. {
  34. chacha_state *x = state_;
  35. chacha_init (x, (chacha_key *)bytes, (chacha_iv *)(bytes + 32), 20);
  36. }
  37. static void
  38. chacha20_cryptobox_generate (void *state_, uint8_t *output, uint32_t idx)
  39. {
  40. chacha_state *x = state_;
  41. memset (output, 0, OUTPUT_LEN);
  42. memcpy (output, &idx, sizeof (idx));
  43. chacha_update (x, output, output, OUTPUT_LEN);
  44. }
  45. #define PRF_CHACHA(r) { \
  46. "CHACHA" #r "-CRYPTOBOX", \
  47. "CHACHA" #r "-CRYPTOBOX", \
  48. "CHACHA" #r "-CRYPTOBOX", \
  49. STATE_LEN, \
  50. STATE_BYTES, \
  51. OUTPUT_LEN, \
  52. 0, \
  53. chacha ## r ## _cryptobox_state_setup, \
  54. chacha ## r ## _cryptobox_generate \
  55. }
  56. const struct ottery_prf ottery_prf_chacha20_cryptobox_ = PRF_CHACHA(20);