utils.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* utils.c - TinyCrypt platform-dependent run-time operations */
  2. /*
  3. * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * - Neither the name of Intel Corporation nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <tinycrypt/utils.h>
  32. #include <tinycrypt/constants.h>
  33. #include <string.h>
  34. #define MASK_TWENTY_SEVEN 0x1b
  35. unsigned int _copy(uint8_t *to, unsigned int to_len,
  36. const uint8_t *from, unsigned int from_len)
  37. {
  38. if (from_len <= to_len) {
  39. (void)memcpy(to, from, from_len);
  40. return from_len;
  41. } else {
  42. return TC_CRYPTO_FAIL;
  43. }
  44. }
  45. void _set(void *to, uint8_t val, unsigned int len)
  46. {
  47. (void)memset(to, val, len);
  48. }
  49. /*
  50. * Doubles the value of a byte for values up to 127.
  51. */
  52. uint8_t _double_byte(uint8_t a)
  53. {
  54. return ((a<<1) ^ ((a>>7) * MASK_TWENTY_SEVEN));
  55. }
  56. int _compare(const uint8_t *a, const uint8_t *b, size_t size)
  57. {
  58. const uint8_t *tempa = a;
  59. const uint8_t *tempb = b;
  60. uint8_t result = 0;
  61. for (unsigned int i = 0; i < size; i++) {
  62. result |= tempa[i] ^ tempb[i];
  63. }
  64. return result;
  65. }