buffer.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/buffer.h>
  12. /*
  13. * LIMIT_BEFORE_EXPANSION is the maximum n such that (n+3)/3*4 < 2**31. That
  14. * function is applied in several functions in this file and this limit
  15. * ensures that the result fits in an int.
  16. */
  17. #define LIMIT_BEFORE_EXPANSION 0x5ffffffc
  18. BUF_MEM *BUF_MEM_new_ex(unsigned long flags)
  19. {
  20. BUF_MEM *ret;
  21. ret = BUF_MEM_new();
  22. if (ret != NULL)
  23. ret->flags = flags;
  24. return (ret);
  25. }
  26. BUF_MEM *BUF_MEM_new(void)
  27. {
  28. BUF_MEM *ret;
  29. ret = OPENSSL_zalloc(sizeof(*ret));
  30. if (ret == NULL) {
  31. BUFerr(BUF_F_BUF_MEM_NEW, ERR_R_MALLOC_FAILURE);
  32. return (NULL);
  33. }
  34. return (ret);
  35. }
  36. void BUF_MEM_free(BUF_MEM *a)
  37. {
  38. if (a == NULL)
  39. return;
  40. if (a->data != NULL) {
  41. if (a->flags & BUF_MEM_FLAG_SECURE)
  42. OPENSSL_secure_free(a->data);
  43. else
  44. OPENSSL_clear_free(a->data, a->max);
  45. }
  46. OPENSSL_free(a);
  47. }
  48. /* Allocate a block of secure memory; copy over old data if there
  49. * was any, and then free it. */
  50. static char *sec_alloc_realloc(BUF_MEM *str, size_t len)
  51. {
  52. char *ret;
  53. ret = OPENSSL_secure_malloc(len);
  54. if (str->data != NULL) {
  55. if (ret != NULL)
  56. memcpy(ret, str->data, str->length);
  57. OPENSSL_secure_free(str->data);
  58. }
  59. return (ret);
  60. }
  61. size_t BUF_MEM_grow(BUF_MEM *str, size_t len)
  62. {
  63. char *ret;
  64. size_t n;
  65. if (str->length >= len) {
  66. str->length = len;
  67. return (len);
  68. }
  69. if (str->max >= len) {
  70. if (str->data != NULL)
  71. memset(&str->data[str->length], 0, len - str->length);
  72. str->length = len;
  73. return (len);
  74. }
  75. /* This limit is sufficient to ensure (len+3)/3*4 < 2**31 */
  76. if (len > LIMIT_BEFORE_EXPANSION) {
  77. BUFerr(BUF_F_BUF_MEM_GROW, ERR_R_MALLOC_FAILURE);
  78. return 0;
  79. }
  80. n = (len + 3) / 3 * 4;
  81. if ((str->flags & BUF_MEM_FLAG_SECURE))
  82. ret = sec_alloc_realloc(str, n);
  83. else
  84. ret = OPENSSL_realloc(str->data, n);
  85. if (ret == NULL) {
  86. BUFerr(BUF_F_BUF_MEM_GROW, ERR_R_MALLOC_FAILURE);
  87. len = 0;
  88. } else {
  89. str->data = ret;
  90. str->max = n;
  91. memset(&str->data[str->length], 0, len - str->length);
  92. str->length = len;
  93. }
  94. return (len);
  95. }
  96. size_t BUF_MEM_grow_clean(BUF_MEM *str, size_t len)
  97. {
  98. char *ret;
  99. size_t n;
  100. if (str->length >= len) {
  101. if (str->data != NULL)
  102. memset(&str->data[len], 0, str->length - len);
  103. str->length = len;
  104. return (len);
  105. }
  106. if (str->max >= len) {
  107. memset(&str->data[str->length], 0, len - str->length);
  108. str->length = len;
  109. return (len);
  110. }
  111. /* This limit is sufficient to ensure (len+3)/3*4 < 2**31 */
  112. if (len > LIMIT_BEFORE_EXPANSION) {
  113. BUFerr(BUF_F_BUF_MEM_GROW_CLEAN, ERR_R_MALLOC_FAILURE);
  114. return 0;
  115. }
  116. n = (len + 3) / 3 * 4;
  117. if ((str->flags & BUF_MEM_FLAG_SECURE))
  118. ret = sec_alloc_realloc(str, n);
  119. else
  120. ret = OPENSSL_clear_realloc(str->data, str->max, n);
  121. if (ret == NULL) {
  122. BUFerr(BUF_F_BUF_MEM_GROW_CLEAN, ERR_R_MALLOC_FAILURE);
  123. len = 0;
  124. } else {
  125. str->data = ret;
  126. str->max = n;
  127. memset(&str->data[str->length], 0, len - str->length);
  128. str->length = len;
  129. }
  130. return (len);
  131. }
  132. void BUF_reverse(unsigned char *out, const unsigned char *in, size_t size)
  133. {
  134. size_t i;
  135. if (in) {
  136. out += size - 1;
  137. for (i = 0; i < size; i++)
  138. *out-- = *in++;
  139. } else {
  140. unsigned char *q;
  141. char c;
  142. q = out + size - 1;
  143. for (i = 0; i < size / 2; i++) {
  144. c = *q;
  145. *q-- = *out;
  146. *out++ = c;
  147. }
  148. }
  149. }