oberon_backend_chacha_poly_aead.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * Copyright (c) 2018 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(NRF_CRYPTO)
  42. #include <stdbool.h>
  43. #include "oberon_backend_chacha_poly_aead.h"
  44. #if NRF_MODULE_ENABLED(NRF_CRYPTO_OBERON_CHACHA_POLY_AEAD)
  45. static ret_code_t backend_cc310_init(void * const p_context, uint8_t * p_key)
  46. {
  47. nrf_crypto_backend_chacha_poly_context_t * p_ctx =
  48. (nrf_crypto_backend_chacha_poly_context_t *)p_context;
  49. if (p_ctx->header.p_info->key_size != NRF_CRYPTO_KEY_SIZE_256)
  50. {
  51. return NRF_ERROR_CRYPTO_KEY_SIZE;
  52. }
  53. memcpy(p_ctx->key, p_key, sizeof(p_ctx->key));
  54. return NRF_SUCCESS;
  55. }
  56. static inline ret_code_t backend_cc310_uninit(void * const p_context)
  57. {
  58. return NRF_SUCCESS;
  59. }
  60. static ret_code_t backend_cc310_crypt(void * const p_context,
  61. nrf_crypto_operation_t operation,
  62. uint8_t * p_nonce,
  63. uint8_t nonce_size,
  64. uint8_t * p_adata,
  65. size_t adata_size,
  66. uint8_t * p_data_in,
  67. size_t data_in_size,
  68. uint8_t * p_data_out,
  69. uint8_t * p_mac,
  70. uint8_t mac_size)
  71. {
  72. int result;
  73. nrf_crypto_backend_chacha_poly_context_t * p_ctx =
  74. (nrf_crypto_backend_chacha_poly_context_t *)p_context;
  75. if ((adata_size == 0) || (data_in_size == 0))
  76. {
  77. return NRF_ERROR_CRYPTO_INPUT_LENGTH;
  78. }
  79. if (mac_size != NRF_CRYPTO_CHACHA_POLY_MAC_SIZE)
  80. {
  81. return NRF_ERROR_CRYPTO_AEAD_MAC_SIZE;
  82. }
  83. if (nonce_size != NRF_CRYPTO_CHACHA_POLY_NONCE_SIZE)
  84. {
  85. return NRF_ERROR_CRYPTO_AEAD_NONCE_SIZE;
  86. }
  87. if (operation == NRF_CRYPTO_ENCRYPT)
  88. {
  89. ocrypto_chacha20_poly1305_encrypt_aad(p_mac,
  90. p_data_out,
  91. p_data_in,
  92. data_in_size,
  93. p_adata,
  94. adata_size,
  95. p_nonce,
  96. (size_t)nonce_size,
  97. p_ctx->key);
  98. }
  99. else if (operation == NRF_CRYPTO_DECRYPT)
  100. {
  101. result = ocrypto_chacha20_poly1305_decrypt_aad(p_mac,
  102. p_data_out,
  103. p_data_in,
  104. data_in_size,
  105. p_adata,
  106. adata_size,
  107. p_nonce,
  108. (size_t)nonce_size,
  109. p_ctx->key);
  110. if (result != 0)
  111. {
  112. return NRF_ERROR_CRYPTO_AEAD_INVALID_MAC;
  113. }
  114. }
  115. else
  116. {
  117. return NRF_ERROR_CRYPTO_INVALID_PARAM;
  118. }
  119. return NRF_SUCCESS;
  120. }
  121. nrf_crypto_aead_info_t const g_nrf_crypto_chacha_poly_256_info =
  122. {
  123. .key_size = NRF_CRYPTO_KEY_SIZE_256,
  124. .mode = NRF_CRYPTO_AEAD_MODE_CHACHA_POLY,
  125. .init_fn = backend_cc310_init,
  126. .uninit_fn = backend_cc310_uninit,
  127. .crypt_fn = backend_cc310_crypt
  128. };
  129. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_CC310_CHACHA_POLY_AEAD)
  130. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO)