mbedtls_adapter.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /* This file is part of Graphene Library OS.
  2. Graphene Library OS is free software: you can redistribute it and/or
  3. modify it under the terms of the GNU General Public License
  4. as published by the Free Software Foundation, either version 3 of the
  5. License, or (at your option) any later version.
  6. Graphene Library OS is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  12. #include <stdint.h>
  13. #include "pal.h"
  14. #include "pal_crypto.h"
  15. #include "crypto/mbedtls/mbedtls/sha256.h"
  16. int lib_SHA256Init(LIB_SHA256_CONTEXT *context)
  17. {
  18. mbedtls_sha256_init(context);
  19. mbedtls_sha256_starts(context, 0 /* 0 = use SSH256 */);
  20. return 0;
  21. }
  22. int lib_SHA256Update(LIB_SHA256_CONTEXT *context, const uint8_t *data,
  23. uint64_t len)
  24. {
  25. /* For compatibility with other SHA256 providers, don't support
  26. * large lengths. */
  27. if (len > UINT32_MAX) {
  28. return -1;
  29. }
  30. mbedtls_sha256_update(context, data, len);
  31. return 0;
  32. }
  33. int lib_SHA256Final(LIB_SHA256_CONTEXT *context, uint8_t *output)
  34. {
  35. mbedtls_sha256_finish(context, output);
  36. /* This function is called free, but it doesn't actually free the memory.
  37. * It zeroes out the context to avoid potentially leaking information
  38. * about the hash that was just performed. */
  39. mbedtls_sha256_free(context);
  40. return 0;
  41. }