s2n_blob.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://aws.amazon.com/apache2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #include "utils/s2n_blob.h"
  16. #include <ctype.h>
  17. #include <string.h>
  18. #include <sys/param.h>
  19. #include "api/s2n.h"
  20. #include "error/s2n_errno.h"
  21. #include "utils/s2n_safety.h"
  22. S2N_RESULT s2n_blob_validate(const struct s2n_blob *b)
  23. {
  24. RESULT_ENSURE_REF(b);
  25. RESULT_DEBUG_ENSURE(S2N_IMPLIES(b->data == NULL, b->size == 0), S2N_ERR_SAFETY);
  26. RESULT_DEBUG_ENSURE(S2N_IMPLIES(b->data == NULL, b->allocated == 0), S2N_ERR_SAFETY);
  27. RESULT_DEBUG_ENSURE(S2N_IMPLIES(b->growable == 0, b->allocated == 0), S2N_ERR_SAFETY);
  28. RESULT_DEBUG_ENSURE(S2N_IMPLIES(b->growable != 0, b->size <= b->allocated), S2N_ERR_SAFETY);
  29. RESULT_DEBUG_ENSURE(S2N_MEM_IS_READABLE(b->data, b->allocated), S2N_ERR_SAFETY);
  30. RESULT_DEBUG_ENSURE(S2N_MEM_IS_READABLE(b->data, b->size), S2N_ERR_SAFETY);
  31. return S2N_RESULT_OK;
  32. }
  33. int s2n_blob_init(struct s2n_blob *b, uint8_t *data, uint32_t size)
  34. {
  35. POSIX_ENSURE_REF(b);
  36. POSIX_ENSURE(S2N_MEM_IS_READABLE(data, size), S2N_ERR_SAFETY);
  37. *b = (struct s2n_blob){ .data = data, .size = size, .allocated = 0, .growable = 0 };
  38. POSIX_POSTCONDITION(s2n_blob_validate(b));
  39. return S2N_SUCCESS;
  40. }
  41. int s2n_blob_zero(struct s2n_blob *b)
  42. {
  43. POSIX_PRECONDITION(s2n_blob_validate(b));
  44. POSIX_CHECKED_MEMSET(b->data, 0, MAX(b->allocated, b->size));
  45. POSIX_POSTCONDITION(s2n_blob_validate(b));
  46. return S2N_SUCCESS;
  47. }
  48. int s2n_blob_slice(const struct s2n_blob *b, struct s2n_blob *slice, uint32_t offset, uint32_t size)
  49. {
  50. POSIX_PRECONDITION(s2n_blob_validate(b));
  51. POSIX_PRECONDITION(s2n_blob_validate(slice));
  52. uint32_t slice_size = 0;
  53. POSIX_GUARD(s2n_add_overflow(offset, size, &slice_size));
  54. POSIX_ENSURE(b->size >= slice_size, S2N_ERR_SIZE_MISMATCH);
  55. slice->data = (b->data) ? (b->data + offset) : NULL;
  56. slice->size = size;
  57. slice->growable = 0;
  58. slice->allocated = 0;
  59. POSIX_POSTCONDITION(s2n_blob_validate(slice));
  60. return S2N_SUCCESS;
  61. }
  62. int s2n_blob_char_to_lower(struct s2n_blob *b)
  63. {
  64. POSIX_PRECONDITION(s2n_blob_validate(b));
  65. for (size_t i = 0; i < b->size; i++) {
  66. b->data[i] = tolower(b->data[i]);
  67. }
  68. POSIX_POSTCONDITION(s2n_blob_validate(b));
  69. return S2N_SUCCESS;
  70. }
  71. /* An inverse map from an ascii value to a hexidecimal nibble value
  72. * accounts for all possible char values, where 255 is invalid value */
  73. static const uint8_t hex_inverse[256] = {
  74. /* clang-format off */
  75. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  76. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  77. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  78. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, 255, 255, 255, 255, 255,
  79. 255, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  80. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  81. 255, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  82. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  83. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  84. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  85. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  86. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  87. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  88. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  89. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  90. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255
  91. /* clang-format on */
  92. };
  93. /* takes a hex string and writes values in the s2n_blob
  94. * string needs to a valid hex and blob needs to be large enough */
  95. int s2n_hex_string_to_bytes(const uint8_t *str, struct s2n_blob *blob)
  96. {
  97. POSIX_ENSURE_REF(str);
  98. POSIX_PRECONDITION(s2n_blob_validate(blob));
  99. uint32_t len_with_spaces = strlen((const char *) str);
  100. size_t i = 0, j = 0;
  101. while (j < len_with_spaces) {
  102. if (str[j] == ' ') {
  103. j++;
  104. continue;
  105. }
  106. uint8_t high_nibble = hex_inverse[str[j]];
  107. POSIX_ENSURE(high_nibble != 255, S2N_ERR_INVALID_HEX);
  108. uint8_t low_nibble = hex_inverse[str[j + 1]];
  109. POSIX_ENSURE(low_nibble != 255, S2N_ERR_INVALID_HEX);
  110. POSIX_ENSURE(i < blob->size, S2N_ERR_INVALID_HEX);
  111. blob->data[i] = high_nibble << 4 | low_nibble;
  112. i++;
  113. j += 2;
  114. }
  115. blob->size = i;
  116. POSIX_POSTCONDITION(s2n_blob_validate(blob));
  117. return S2N_SUCCESS;
  118. }