armor.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/errno.h>
  3. int ceph_armor(char *dst, const char *src, const char *end);
  4. int ceph_unarmor(char *dst, const char *src, const char *end);
  5. /*
  6. * base64 encode/decode.
  7. */
  8. static const char *pem_key =
  9. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  10. static int encode_bits(int c)
  11. {
  12. return pem_key[c];
  13. }
  14. static int decode_bits(char c)
  15. {
  16. if (c >= 'A' && c <= 'Z')
  17. return c - 'A';
  18. if (c >= 'a' && c <= 'z')
  19. return c - 'a' + 26;
  20. if (c >= '0' && c <= '9')
  21. return c - '0' + 52;
  22. if (c == '+')
  23. return 62;
  24. if (c == '/')
  25. return 63;
  26. if (c == '=')
  27. return 0; /* just non-negative, please */
  28. return -EINVAL;
  29. }
  30. int ceph_armor(char *dst, const char *src, const char *end)
  31. {
  32. int olen = 0;
  33. int line = 0;
  34. while (src < end) {
  35. unsigned char a, b, c;
  36. a = *src++;
  37. *dst++ = encode_bits(a >> 2);
  38. if (src < end) {
  39. b = *src++;
  40. *dst++ = encode_bits(((a & 3) << 4) | (b >> 4));
  41. if (src < end) {
  42. c = *src++;
  43. *dst++ = encode_bits(((b & 15) << 2) |
  44. (c >> 6));
  45. *dst++ = encode_bits(c & 63);
  46. } else {
  47. *dst++ = encode_bits((b & 15) << 2);
  48. *dst++ = '=';
  49. }
  50. } else {
  51. *dst++ = encode_bits(((a & 3) << 4));
  52. *dst++ = '=';
  53. *dst++ = '=';
  54. }
  55. olen += 4;
  56. line += 4;
  57. if (line == 64) {
  58. line = 0;
  59. *(dst++) = '\n';
  60. olen++;
  61. }
  62. }
  63. return olen;
  64. }
  65. int ceph_unarmor(char *dst, const char *src, const char *end)
  66. {
  67. int olen = 0;
  68. while (src < end) {
  69. int a, b, c, d;
  70. if (src[0] == '\n') {
  71. src++;
  72. continue;
  73. }
  74. if (src + 4 > end)
  75. return -EINVAL;
  76. a = decode_bits(src[0]);
  77. b = decode_bits(src[1]);
  78. c = decode_bits(src[2]);
  79. d = decode_bits(src[3]);
  80. if (a < 0 || b < 0 || c < 0 || d < 0)
  81. return -EINVAL;
  82. *dst++ = (a << 2) | (b >> 4);
  83. if (src[2] == '=')
  84. return olen + 1;
  85. *dst++ = ((b & 15) << 4) | (c >> 2);
  86. if (src[3] == '=')
  87. return olen + 2;
  88. *dst++ = ((c & 3) << 6) | d;
  89. olen += 3;
  90. src += 4;
  91. }
  92. return olen;
  93. }