s2n_blob.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #pragma once
  16. #include <stdbool.h>
  17. #include <stdint.h>
  18. #include "utils/s2n_result.h"
  19. struct s2n_blob {
  20. /* The data for the s2n_blob */
  21. uint8_t *data;
  22. /* The size of the data */
  23. uint32_t size;
  24. /* The amount of memory allocated for this blob (i.e. the amount of memory
  25. * which needs to be freed when the blob is cleaned up). If this blob was
  26. * created with s2n_blob_init(), this value is 0. If s2n_alloc() was called,
  27. * this value will be greater than 0.
  28. */
  29. uint32_t allocated;
  30. /* Can this blob be resized */
  31. unsigned growable : 1;
  32. };
  33. bool s2n_blob_is_growable(const struct s2n_blob *b);
  34. S2N_RESULT s2n_blob_validate(const struct s2n_blob *b);
  35. int s2n_blob_init(struct s2n_blob *b, uint8_t *data, uint32_t size);
  36. int s2n_blob_zero(struct s2n_blob *b);
  37. int s2n_blob_char_to_lower(struct s2n_blob *b);
  38. int s2n_hex_string_to_bytes(const uint8_t *str, struct s2n_blob *blob);
  39. int s2n_blob_slice(const struct s2n_blob *b, struct s2n_blob *slice, uint32_t offset, uint32_t size);
  40. #define s2n_stack_blob(name, requested_size, maximum) \
  41. size_t name##_requested_size = (requested_size); \
  42. uint8_t name##_buf[(maximum)] = { 0 }; \
  43. POSIX_ENSURE_LTE(name##_requested_size, (maximum)); \
  44. struct s2n_blob name = { 0 }; \
  45. POSIX_GUARD(s2n_blob_init(&name, name##_buf, name##_requested_size))
  46. #define RESULT_STACK_BLOB(name, requested_size, maximum) \
  47. size_t name##_requested_size = (requested_size); \
  48. uint8_t name##_buf[(maximum)] = { 0 }; \
  49. RESULT_ENSURE_LTE(name##_requested_size, (maximum)); \
  50. struct s2n_blob name = { 0 }; \
  51. RESULT_GUARD_POSIX(s2n_blob_init(&name, name##_buf, name##_requested_size))
  52. #define S2N_BLOB_LABEL(name, str) \
  53. static uint8_t name##_data[] = str; \
  54. const struct s2n_blob name = { .data = name##_data, .size = sizeof(name##_data) - 1 };
  55. /* The S2N_BLOB_FROM_HEX macro creates a s2n_blob with the contents of a hex string.
  56. * It is allocated on a stack so there no need to free after use.
  57. * hex should be a const char[]. This function checks against using char*,
  58. * because sizeof needs to refer to the buffer length rather than a pointer size */
  59. #define S2N_BLOB_FROM_HEX(name, hex) \
  60. s2n_stack_blob(name, (sizeof(hex) - 1) / 2, (sizeof(hex) - 1) / 2); \
  61. POSIX_GUARD(s2n_hex_string_to_bytes((const uint8_t *) hex, &name));