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