base64.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * RFC 1521 base64 encoding/decoding
  3. *
  4. * Copyright (C) 2018, Nordic Semiconductor ASA
  5. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  6. * SPDX-License-Identifier: Apache-2.0
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  9. * not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * Adapted for Zephyr by Carles Cufi (carles.cufi@nordicsemi.no)
  21. * - Removed mbedtls_ prefixes
  22. * - Reworked coding style
  23. */
  24. #include <stdint.h>
  25. #include <errno.h>
  26. #include <sys/base64.h>
  27. static const uint8_t base64_enc_map[64] = {
  28. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
  29. 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
  30. 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
  31. 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  32. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
  33. 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
  34. '8', '9', '+', '/'
  35. };
  36. static const uint8_t base64_dec_map[128] = {
  37. 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
  38. 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
  39. 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
  40. 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
  41. 127, 127, 127, 62, 127, 127, 127, 63, 52, 53,
  42. 54, 55, 56, 57, 58, 59, 60, 61, 127, 127,
  43. 127, 64, 127, 127, 127, 0, 1, 2, 3, 4,
  44. 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  45. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
  46. 25, 127, 127, 127, 127, 127, 127, 26, 27, 28,
  47. 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
  48. 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
  49. 49, 50, 51, 127, 127, 127, 127, 127
  50. };
  51. #define BASE64_SIZE_T_MAX ((size_t) -1) /* SIZE_T_MAX is not standard */
  52. /*
  53. * Encode a buffer into base64 format
  54. */
  55. int base64_encode(uint8_t *dst, size_t dlen, size_t *olen, const uint8_t *src,
  56. size_t slen)
  57. {
  58. size_t i, n;
  59. int C1, C2, C3;
  60. uint8_t *p;
  61. if (slen == 0) {
  62. *olen = 0;
  63. return 0;
  64. }
  65. n = slen / 3 + (slen % 3 != 0);
  66. if (n > (BASE64_SIZE_T_MAX - 1) / 4) {
  67. *olen = BASE64_SIZE_T_MAX;
  68. return -ENOMEM;
  69. }
  70. n *= 4;
  71. if ((dlen < n + 1) || (!dst)) {
  72. *olen = n + 1;
  73. return -ENOMEM;
  74. }
  75. n = (slen / 3) * 3;
  76. for (i = 0, p = dst; i < n; i += 3) {
  77. C1 = *src++;
  78. C2 = *src++;
  79. C3 = *src++;
  80. *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
  81. *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
  82. *p++ = base64_enc_map[(((C2 & 15) << 2) + (C3 >> 6)) & 0x3F];
  83. *p++ = base64_enc_map[C3 & 0x3F];
  84. }
  85. if (i < slen) {
  86. C1 = *src++;
  87. C2 = ((i + 1) < slen) ? *src++ : 0;
  88. *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
  89. *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
  90. if ((i + 1) < slen) {
  91. *p++ = base64_enc_map[((C2 & 15) << 2) & 0x3F];
  92. } else {
  93. *p++ = '=';
  94. }
  95. *p++ = '=';
  96. }
  97. *olen = p - dst;
  98. *p = 0U;
  99. return 0;
  100. }
  101. /*
  102. * Decode a base64-formatted buffer
  103. */
  104. int base64_decode(uint8_t *dst, size_t dlen, size_t *olen, const uint8_t *src,
  105. size_t slen)
  106. {
  107. size_t i, n;
  108. uint32_t j, x;
  109. uint8_t *p;
  110. /* First pass: check for validity and get output length */
  111. for (i = n = j = 0U; i < slen; i++) {
  112. /* Skip spaces before checking for EOL */
  113. x = 0U;
  114. while (i < slen && src[i] == ' ') {
  115. ++i;
  116. ++x;
  117. }
  118. /* Spaces at end of buffer are OK */
  119. if (i == slen) {
  120. break;
  121. }
  122. if ((slen - i) >= 2 && src[i] == '\r' && src[i + 1] == '\n') {
  123. continue;
  124. }
  125. if (src[i] == '\n') {
  126. continue;
  127. }
  128. /* Space inside a line is an error */
  129. if (x != 0U) {
  130. return -EINVAL;
  131. }
  132. if (src[i] == '=' && ++j > 2) {
  133. return -EINVAL;
  134. }
  135. if (src[i] > 127 || base64_dec_map[src[i]] == 127U) {
  136. return -EINVAL;
  137. }
  138. if (base64_dec_map[src[i]] < 64 && j != 0U) {
  139. return -EINVAL;
  140. }
  141. n++;
  142. }
  143. if (n == 0) {
  144. *olen = 0;
  145. return 0;
  146. }
  147. /* The following expression is to calculate the following formula
  148. * without risk of integer overflow in n:
  149. * n = ( ( n * 6 ) + 7 ) >> 3;
  150. */
  151. n = (6 * (n >> 3)) + ((6 * (n & 0x7) + 7) >> 3);
  152. n -= j;
  153. if (dst == NULL || dlen < n) {
  154. *olen = n;
  155. return -ENOMEM;
  156. }
  157. for (j = 3U, n = x = 0U, p = dst; i > 0; i--, src++) {
  158. if (*src == '\r' || *src == '\n' || *src == ' ') {
  159. continue;
  160. }
  161. j -= (base64_dec_map[*src] == 64U);
  162. x = (x << 6) | (base64_dec_map[*src] & 0x3F);
  163. if (++n == 4) {
  164. n = 0;
  165. if (j > 0) {
  166. *p++ = (unsigned char)(x >> 16);
  167. }
  168. if (j > 1) {
  169. *p++ = (unsigned char)(x >> 8);
  170. }
  171. if (j > 2) {
  172. *p++ = (unsigned char)(x);
  173. }
  174. }
  175. }
  176. *olen = p - dst;
  177. return 0;
  178. }