chacha.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*!
  2. \ingroup ChaCha
  3. \brief This function sets the initialization vector (nonce) for a ChaCha
  4. object, initializing it for use as a cipher. It should be called after the
  5. key has been set, using wc_Chacha_SetKey. A difference nonce should be
  6. used for each round of encryption.
  7. \return 0 Returned upon successfully setting the initialization vector
  8. \return BAD_FUNC_ARG returned if there is an error processing the ctx
  9. input argument
  10. \param ctx pointer to the ChaCha structure on which to set the iv
  11. \param inIv pointer to a buffer containing the 12 byte initialization
  12. vector with which to initialize the ChaCha structure
  13. \param counter the value at which the block counter should start--usually
  14. zero.
  15. _Example_
  16. \code
  17. ChaCha enc;
  18. // initialize enc with wc_Chacha_SetKey
  19. byte iv[12];
  20. // initialize iv
  21. if( wc_Chacha_SetIV(&enc, iv, 0) != 0) {
  22. // error initializing ChaCha structure
  23. }
  24. \endcode
  25. \sa wc_Chacha_SetKey
  26. \sa wc_Chacha_Process
  27. */
  28. WOLFSSL_API int wc_Chacha_SetIV(ChaCha* ctx, const byte* inIv, word32 counter);
  29. /*!
  30. \ingroup ChaCha
  31. \brief This function processes the text from the buffer input, encrypts
  32. or decrypts it, and stores the result in the buffer output.
  33. \return 0 Returned upon successfully encrypting or decrypting the input
  34. \return BAD_FUNC_ARG returned if there is an error processing the ctx
  35. input argument
  36. \param ctx pointer to the ChaCha structure on which to set the iv
  37. \param output pointer to a buffer in which to store the output ciphertext
  38. or decrypted plaintext
  39. \param input pointer to the buffer containing the input plaintext to
  40. encrypt or the input ciphertext to decrypt
  41. \param msglen length of the message to encrypt or the ciphertext to decrypt
  42. _Example_
  43. \code
  44. ChaCha enc;
  45. // initialize enc with wc_Chacha_SetKey and wc_Chacha_SetIV
  46. byte plain[] = { // initialize plaintext };
  47. byte cipher[sizeof(plain)];
  48. if( wc_Chacha_Process(&enc, cipher, plain, sizeof(plain)) != 0) {
  49. // error processing ChaCha cipher
  50. }
  51. \endcode
  52. \sa wc_Chacha_SetKey
  53. \sa wc_Chacha_Process
  54. */
  55. WOLFSSL_API int wc_Chacha_Process(ChaCha* ctx, byte* cipher, const byte* plain,
  56. word32 msglen);
  57. /*!
  58. \ingroup ChaCha
  59. \brief This function sets the key for a ChaCha object, initializing it for
  60. use as a cipher. It should be called before setting the nonce with
  61. wc_Chacha_SetIV, and before using it for encryption with wc_Chacha_Process.
  62. \return 0 Returned upon successfully setting the key
  63. \return BAD_FUNC_ARG returned if there is an error processing the ctx
  64. input argument or if the key is not 16 or 32 bytes long
  65. \param ctx pointer to the ChaCha structure in which to set the key
  66. \param key pointer to a buffer containing the 16 or 32 byte key with
  67. which to initialize the ChaCha structure
  68. \param keySz the length of the key passed in
  69. _Example_
  70. \code
  71. ChaCha enc;
  72. byte key[] = { // initialize key };
  73. if( wc_Chacha_SetKey(&enc, key, sizeof(key)) != 0) {
  74. // error initializing ChaCha structure
  75. }
  76. \endcode
  77. \sa wc_Chacha_SetIV
  78. \sa wc_Chacha_Process
  79. */
  80. WOLFSSL_API int wc_Chacha_SetKey(ChaCha* ctx, const byte* key, word32 keySz);