luat_crypto_air105.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "luat_base.h"
  22. #include "luat_crypto.h"
  23. #include "app_interface.h"
  24. #define LUAT_LOG_TAG "crypto"
  25. #include "luat_log.h"
  26. //#include "mbedtls/config.h"
  27. #include "mbedtls/cipher.h"
  28. #include "mbedtls/sha1.h"
  29. #include "mbedtls/sha256.h"
  30. #include "mbedtls/sha512.h"
  31. #include "mbedtls/md5.h"
  32. int luat_crypto_trng(char* buff, size_t len) {
  33. size_t t = 0;
  34. uint32_t tmp[4];
  35. int i;
  36. char *temp = tmp;
  37. while (len > t) {
  38. RNG_GetData(tmp);
  39. if ((len - t) >=16)
  40. {
  41. memcpy(buff + t, temp, 16);
  42. t += 16;
  43. }
  44. else
  45. {
  46. i = 0;
  47. while (len > t)
  48. {
  49. buff[t] = temp[i];
  50. t++;
  51. i++;
  52. }
  53. }
  54. }
  55. return 0;
  56. }
  57. struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt,
  58. struct tm *tm_buf )
  59. {
  60. Date_UserDataStruct Date;
  61. Time_UserDataStruct Time;
  62. Tamp2UTC(*tt, &Date, &Time, 0);
  63. tm_buf->tm_year = Date.Year - 1900;
  64. tm_buf->tm_mon = Date.Mon - 1;
  65. tm_buf->tm_mday = Date.Day;
  66. tm_buf->tm_hour = Time.Hour;
  67. tm_buf->tm_min = Time.Min;
  68. tm_buf->tm_sec = Time.Sec;
  69. return tm_buf;
  70. }