mbedtls_adapter.c 1.7 KB

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