| 1 |
|
| #ifndef PTOTP_B32_H |
| 2 |
|
| #define PTOTP_B32_H |
| 3 |
|
| |
| 4 |
|
| #include <stddef.h> |
| 5 |
|
| |
| 6 |
|
| /* ptotp_b32.h - RFC 4648 base32, written for what it decodes: a |
| 7 |
|
| * shared secret typed in by hand when a QR cannot be scanned. |
| 8 |
|
| * |
| 9 |
|
| * The requirements that rule out every implementation on the shelf: |
| 10 |
|
| * arbitrary length (secrets are 20, 32 or 64 bytes by algorithm); |
| 11 |
|
| * padding emitted and both padded and bare input accepted; case |
| 12 |
|
| * folded, spaces and hyphens skipped, because authenticator apps |
| 13 |
|
| * display secrets in grouped-four chunks and people type what they |
| 14 |
|
| * see; and REJECTION of anything else - a decoder that maps a typo to |
| 15 |
|
| * some byte produces a different secret that verifies nothing and |
| 16 |
|
| * reports nothing, which is the worst failure this file could have. |
| 17 |
|
| */ |
| 18 |
|
| |
| 19 |
|
| static const char ptotp_b32_alpha[32] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; |
| 20 |
|
| |
| 21 |
|
| /* Encoded length including padding, excluding the NUL. */ |
| 22 |
86 |
| static size_t ptotp_b32_elen(size_t n) |
| 23 |
|
| { |
| 24 |
86 |
| return ((n + 4) / 5) * 8; |
| 25 |
|
| } |
| 26 |
|
| |
| 27 |
|
| /* Encode n bytes into dst (at least ptotp_b32_elen(n) + 1). */ |
| 28 |
92 |
| static void ptotp_b32_encode(char *dst, const unsigned char *src, size_t n) |
| 29 |
|
| { |
| 30 |
|
| size_t i; |
| 31 |
|
| |
| 32 |
727 |
▶ | for (i = 0; i < n; i += 5) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i += 5) { |
635 |
92 |
727 |
|
| 33 |
635 |
| unsigned char b[5] = { 0, 0, 0, 0, 0 }; |
| 34 |
635 |
| size_t have = n - i < 5 ? n - i : 5, j; |
| 35 |
|
| /* how many output symbols carry data for 1..5 input bytes */ |
| 36 |
|
| static const int syms[6] = { 0, 2, 4, 5, 7, 8 }; |
| 37 |
|
| |
| 38 |
3640 |
▶ | for (j = 0; j < have; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < have; j++) |
3005 |
635 |
3640 |
|
| 39 |
3005 |
| b[j] = src[i + j]; |
| 40 |
|
| |
| 41 |
|
| { |
| 42 |
|
| char out[8]; |
| 43 |
635 |
| out[0] = ptotp_b32_alpha[b[0] >> 3]; |
| 44 |
635 |
| out[1] = ptotp_b32_alpha[((b[0] & 0x07) << 2) | (b[1] >> 6)]; |
| 45 |
635 |
| out[2] = ptotp_b32_alpha[(b[1] >> 1) & 0x1f]; |
| 46 |
635 |
| out[3] = ptotp_b32_alpha[((b[1] & 0x01) << 4) | (b[2] >> 4)]; |
| 47 |
635 |
| out[4] = ptotp_b32_alpha[((b[2] & 0x0f) << 1) | (b[3] >> 7)]; |
| 48 |
635 |
| out[5] = ptotp_b32_alpha[(b[3] >> 2) & 0x1f]; |
| 49 |
635 |
| out[6] = ptotp_b32_alpha[((b[3] & 0x03) << 3) | (b[4] >> 5)]; |
| 50 |
635 |
| out[7] = ptotp_b32_alpha[b[4] & 0x1f]; |
| 51 |
5715 |
▶ | for (j = 0; j < 8; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < 8; j++) |
5080 |
635 |
5715 |
|
| 52 |
5080 |
▶ | *dst++ = j < (size_t)syms[have] ? out[j] : '='; |
Branch: 1 branch
| Branch | True | False | Total |
| *dst++ = j < (size_t)syms[have] ? out[j] : '='; |
4843 |
237 |
5080 |
|
| 53 |
|
| } |
| 54 |
|
| } |
| 55 |
92 |
| *dst = '\0'; |
| 56 |
92 |
| } |
| 57 |
|
| |
| 58 |
|
| /* Decode into dst (at least (len * 5) / 8 bytes). Returns the decoded |
| 59 |
|
| * length, or -1 on any byte outside the alphabet after the skip set |
| 60 |
|
| * (space, tab, hyphen), on padding that is not terminal, or on a |
| 61 |
|
| * symbol count no encoding produces. Case-insensitive. |
| 62 |
|
| * |
| 63 |
|
| * Rejection is the point: -1 and nothing else for '0', '1', '8', '9', |
| 64 |
|
| * '=' in the middle, and anything punctuation-shaped besides the |
| 65 |
|
| * grouping characters people actually type. */ |
| 66 |
165 |
| static long ptotp_b32_decode(unsigned char *dst, const char *src, size_t len) |
| 67 |
|
| { |
| 68 |
165 |
| unsigned long acc = 0; |
| 69 |
165 |
| int bits = 0, done = 0; |
| 70 |
165 |
| size_t i, out = 0, symbols = 0; |
| 71 |
|
| |
| 72 |
7484 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
7333 |
151 |
7484 |
|
| 73 |
7333 |
| unsigned char c = (unsigned char)src[i]; |
| 74 |
|
| int v; |
| 75 |
|
| |
| 76 |
7333 |
▶ | if (c == ' ' || c == '\t' || c == '-') |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == ' ' || c == '\t' || c == '-') |
7332 |
1 |
7333 |
| if (c == ' ' || c == '\t' || c == '-') |
7331 |
1 |
7332 |
| if (c == ' ' || c == '\t' || c == '-') |
1 |
7330 |
7331 |
|
| 77 |
3 |
| continue; |
| 78 |
7330 |
▶ | if (c == '=') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '=') { |
264 |
7066 |
7330 |
|
| 79 |
264 |
| done = 1; /* padding: only more padding/skips may follow */ |
| 80 |
264 |
| continue; |
| 81 |
|
| } |
| 82 |
7066 |
▶ | if (done) |
Branch: 1 branch
| Branch | True | False | Total |
| if (done) |
1 |
7065 |
7066 |
|
| 83 |
1 |
| return -1; |
| 84 |
|
| |
| 85 |
7065 |
▶ | if (c >= 'A' && c <= 'Z') v = c - 'A'; |
Branch: 2 branches
| Branch | True | False | Total |
| if (c >= 'A' && c <= 'Z') v = c - 'A'; |
6084 |
981 |
7065 |
| if (c >= 'A' && c <= 'Z') v = c - 'A'; |
6074 |
10 |
6084 |
|
| 86 |
991 |
▶ | else if (c >= 'a' && c <= 'z') v = c - 'a'; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (c >= 'a' && c <= 'z') v = c - 'a'; |
10 |
981 |
991 |
| else if (c >= 'a' && c <= 'z') v = c - 'a'; |
10 |
0 |
10 |
|
| 87 |
981 |
▶ | else if (c >= '2' && c <= '7') v = c - '2' + 26; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (c >= '2' && c <= '7') v = c - '2' + 26; |
971 |
10 |
981 |
| else if (c >= '2' && c <= '7') v = c - '2' + 26; |
968 |
3 |
971 |
|
| 88 |
13 |
| else return -1; |
| 89 |
|
| |
| 90 |
7052 |
| acc = (acc << 5) | (unsigned long)v; |
| 91 |
7052 |
| bits += 5; |
| 92 |
7052 |
| symbols++; |
| 93 |
7052 |
▶ | if (bits >= 8) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (bits >= 8) { |
4368 |
2684 |
7052 |
|
| 94 |
4368 |
| bits -= 8; |
| 95 |
4368 |
| dst[out++] = (unsigned char)((acc >> bits) & 0xff); |
| 96 |
|
| } |
| 97 |
|
| } |
| 98 |
|
| |
| 99 |
|
| /* A valid encoding has 0,2,4,5,7 symbols mod 8 - never 1,3,6 - |
| 100 |
|
| * and whatever bits remain must be the zero padding the encoder |
| 101 |
|
| * wrote, or the input was truncated mid-byte. */ |
| 102 |
151 |
▶ | switch (symbols % 8) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch (symbols % 8) { |
149 |
2 |
151 |
|
| 103 |
149 |
| case 0: case 2: case 4: case 5: case 7: |
| 104 |
149 |
| break; |
| 105 |
2 |
| default: |
| 106 |
2 |
| return -1; |
| 107 |
|
| } |
| 108 |
149 |
▶ | if (bits && (acc & ((1ul << bits) - 1))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (bits && (acc & ((1ul << bits) - 1))) |
79 |
70 |
149 |
| if (bits && (acc & ((1ul << bits) - 1))) |
0 |
79 |
79 |
|
| 109 |
0 |
| return -1; |
| 110 |
|
| |
| 111 |
149 |
| return (long)out; |
| 112 |
|
| } |
| 113 |
|
| |
| 114 |
|
| #endif /* PTOTP_B32_H */ |