Skip to content

Commit 76acbab

Browse files
committed
encoding/base64: add constant-time behavior, enabled by default
1 parent ed08d2a commit 76acbab

File tree

2 files changed

+137
-22
lines changed

2 files changed

+137
-22
lines changed

src/encoding/base64/base64.go

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
* Encodings
1717
*/
1818

19+
type MappingFunc func (in uint) byte
1920
// An Encoding is a radix 64 encoding/decoding scheme, defined by a
2021
// 64-character alphabet. The most common encoding is the "base64"
2122
// encoding defined in RFC 4648 and used in MIME (RFC 2045) and PEM
@@ -26,6 +27,8 @@ type Encoding struct {
2627
decodeMap [256]uint8 // mapping of symbol byte value to symbol index
2728
padChar rune
2829
strict bool
30+
encodeMapFunc MappingFunc // optional mapping function to replace table look-ups when encoding
31+
decodeMapFunc MappingFunc // optional mapping function to replace table look-ups when decoding
2932
}
3033

3134
const (
@@ -83,6 +86,10 @@ func NewEncoding(encoder string) *Encoding {
8386
}
8487
e.decodeMap[encoder[i]] = uint8(i)
8588
}
89+
var encodeFunc = e.encodeMapDefault
90+
e.encodeMapFunc = encodeFunc
91+
var decodeFunc = e.decodeMapDefault
92+
e.decodeMapFunc = decodeFunc
8693
return e
8794
}
8895

@@ -104,6 +111,17 @@ func (enc Encoding) WithPadding(padding rune) *Encoding {
104111
return &enc
105112
}
106113

114+
// WithDecodeMappingFunc sets the value fo encodeMapFunc
115+
func (enc Encoding) WithDecodeMappingFunc(f MappingFunc) *Encoding {
116+
enc.decodeMapFunc = f
117+
return &enc
118+
}
119+
// WithEncodeMappingFunc sets the value fo encodeMapFunc
120+
func (enc Encoding) WithEncodeMappingFunc(f MappingFunc) *Encoding {
121+
enc.encodeMapFunc = f
122+
return &enc
123+
}
124+
107125
// Strict creates a new encoding identical to enc except with
108126
// strict decoding enabled. In this mode, the decoder requires that
109127
// trailing padding bits are zero, as described in RFC 4648 section 3.5.
@@ -116,11 +134,15 @@ func (enc Encoding) Strict() *Encoding {
116134
}
117135

118136
// StdEncoding is the standard base64 encoding, as defined in RFC 4648.
119-
var StdEncoding = NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
137+
var StdEncoding = NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/").
138+
WithEncodeMappingFunc(StandardBase64Encode).
139+
WithDecodeMappingFunc(StandardBase64Decode)
120140

121141
// URLEncoding is the alternate base64 encoding defined in RFC 4648.
122142
// It is typically used in URLs and file names.
123-
var URLEncoding = NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_")
143+
var URLEncoding = NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_").
144+
WithEncodeMappingFunc(UrlSafeBase64Encode).
145+
WithDecodeMappingFunc(UrlSafeBase64Decode)
124146

125147
// RawStdEncoding is the standard raw, unpadded base64 encoding,
126148
// as defined in RFC 4648 section 3.2.
@@ -157,10 +179,10 @@ func (enc *Encoding) Encode(dst, src []byte) {
157179
// Convert 3x 8bit source bytes into 4 bytes
158180
val := uint(src[si+0])<<16 | uint(src[si+1])<<8 | uint(src[si+2])
159181

160-
dst[di+0] = enc.encode[val>>18&0x3F]
161-
dst[di+1] = enc.encode[val>>12&0x3F]
162-
dst[di+2] = enc.encode[val>>6&0x3F]
163-
dst[di+3] = enc.encode[val&0x3F]
182+
dst[di+0] = enc.encodeMapFunc(val>>18&0x3F)
183+
dst[di+1] = enc.encodeMapFunc(val>>12&0x3F)
184+
dst[di+2] = enc.encodeMapFunc(val>>6&0x3F)
185+
dst[di+3] = enc.encodeMapFunc(val&0x3F)
164186

165187
si += 3
166188
di += 4
@@ -176,8 +198,8 @@ func (enc *Encoding) Encode(dst, src []byte) {
176198
val |= uint(src[si+1]) << 8
177199
}
178200

179-
dst[di+0] = enc.encode[val>>18&0x3F]
180-
dst[di+1] = enc.encode[val>>12&0x3F]
201+
dst[di+0] = enc.encodeMapFunc(val>>18&0x3F)
202+
dst[di+1] = enc.encodeMapFunc(val>>12&0x3F)
181203

182204
switch remain {
183205
case 2:
@@ -330,8 +352,7 @@ func (enc *Encoding) decodeQuantum(dst, src []byte, si int) (nsi, n int, err err
330352
}
331353
in := src[si]
332354
si++
333-
334-
out := enc.decodeMap[in]
355+
out := enc.decodeMapFunc(uint(in))
335356
if out != 0xff {
336357
dbuf[j] = out
337358
continue
@@ -529,14 +550,14 @@ func (enc *Encoding) Decode(dst, src []byte) (n int, err error) {
529550
for strconv.IntSize >= 64 && len(src)-si >= 8 && len(dst)-n >= 8 {
530551
src2 := src[si : si+8]
531552
if dn, ok := assemble64(
532-
enc.decodeMap[src2[0]],
533-
enc.decodeMap[src2[1]],
534-
enc.decodeMap[src2[2]],
535-
enc.decodeMap[src2[3]],
536-
enc.decodeMap[src2[4]],
537-
enc.decodeMap[src2[5]],
538-
enc.decodeMap[src2[6]],
539-
enc.decodeMap[src2[7]],
553+
enc.decodeMapFunc(uint(src2[0])),
554+
enc.decodeMapFunc(uint(src2[1])),
555+
enc.decodeMapFunc(uint(src2[2])),
556+
enc.decodeMapFunc(uint(src2[3])),
557+
enc.decodeMapFunc(uint(src2[4])),
558+
enc.decodeMapFunc(uint(src2[5])),
559+
enc.decodeMapFunc(uint(src2[6])),
560+
enc.decodeMapFunc(uint(src2[7])),
540561
); ok {
541562
byteorder.BEPutUint64(dst[n:], dn)
542563
n += 6
@@ -554,10 +575,10 @@ func (enc *Encoding) Decode(dst, src []byte) (n int, err error) {
554575
for len(src)-si >= 4 && len(dst)-n >= 4 {
555576
src2 := src[si : si+4]
556577
if dn, ok := assemble32(
557-
enc.decodeMap[src2[0]],
558-
enc.decodeMap[src2[1]],
559-
enc.decodeMap[src2[2]],
560-
enc.decodeMap[src2[3]],
578+
enc.decodeMapFunc(uint(src2[0])),
579+
enc.decodeMapFunc(uint(src2[1])),
580+
enc.decodeMapFunc(uint(src2[2])),
581+
enc.decodeMapFunc(uint(src2[3])),
561582
); ok {
562583
byteorder.BEPutUint32(dst[n:], dn)
563584
n += 3

src/encoding/base64/mapping.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright 2025 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Package base64 implements base64 encoding as specified by RFC 4648.
6+
package base64
7+
8+
func (enc Encoding) encodeMapDefault(in uint) byte {
9+
return enc.encode[in]
10+
}
11+
12+
func (enc Encoding) decodeMapDefault(in uint) byte {
13+
return enc.decodeMap[in]
14+
}
15+
16+
func StandardBase64Decode(in uint) byte {
17+
ch := int(in)
18+
ret := -1
19+
20+
// if (ch > 0x40 && ch < 0x5b) ret += ch - 0x41 + 1; // -64
21+
ret += (((0x40 - ch) & (ch - 0x5b)) >> 8) & (ch - 64)
22+
23+
// if (ch > 0x60 && ch < 0x7b) ret += ch - 0x61 + 26 + 1; // -70
24+
ret += (((0x60 - ch) & (ch - 0x7b)) >> 8) & (ch - 70)
25+
26+
// if (ch > 0x2f && ch < 0x3a) ret += ch - 0x30 + 52 + 1; // 5
27+
ret += (((0x2f - ch) & (ch - 0x3a)) >> 8) & (ch + 5)
28+
29+
// if (ch == 0x2b) ret += 62 + 1
30+
ret += (((0x2a - ch) & (ch - 0x2c)) >> 8) & 63
31+
32+
// if (ch == 0x2f) ret += 63 + 1;
33+
ret += (((0x2e - ch) & (ch - 0x30)) >> 8) & 64
34+
35+
return byte(ret)
36+
}
37+
38+
func StandardBase64Encode(in uint) byte {
39+
src := int(in)
40+
diff := int(0x41)
41+
42+
// if (in > 25) diff += 0x61 - 0x41 - 26; // 6
43+
diff += ((25 - src) >> 8) & 6;
44+
45+
// if (in > 51) diff += 0x30 - 0x61 - 26; // -75
46+
diff -= ((51 - src) >> 8) & 75;
47+
48+
// if (in > 61) diff += 0x2b - 0x30 - 10; // -15
49+
diff -= ((61 - src) >> 8) & 15;
50+
51+
// if (in > 62) diff += 0x2f - 0x2b - 1; // 3
52+
diff += ((62 - src) >> 8) & 3
53+
return byte(src + diff)
54+
}
55+
56+
func UrlSafeBase64Decode(in uint) byte {
57+
ch := int(in)
58+
ret := -1
59+
60+
// if (ch > 0x40 && ch < 0x5b) ret += ch - 0x41 + 1; // -64
61+
ret += (((0x40 - ch) & (ch - 0x5b)) >> 8) & (ch - 64)
62+
63+
// if (ch > 0x60 && ch < 0x7b) ret += ch - 0x61 + 26 + 1; // -70
64+
ret += (((0x60 - ch) & (ch - 0x7b)) >> 8) & (ch - 70)
65+
66+
// if (ch > 0x2f && ch < 0x3a) ret += ch - 0x30 + 52 + 1; // 5
67+
ret += (((0x2f - ch) & (ch - 0x3a)) >> 8) & (ch + 5)
68+
69+
// if (ch == 0x2c) ret += 62 + 1;
70+
ret += (((0x2c - ch) & (ch - 0x2e)) >> 8) & 63
71+
72+
// if (ch == 0x5f) ret += 63 + 1;
73+
ret += (((0x5e - ch) & (ch - 0x60)) >> 8) & 64
74+
75+
return byte(ret)
76+
}
77+
78+
79+
func UrlSafeBase64Encode(in uint) byte {
80+
src := int(in)
81+
diff := int(0x41)
82+
// if (src > 25) diff += 0x61 - 0x41 - 26; // 6
83+
diff += ((25 - src) >> 8) & 6
84+
85+
// if (src > 51) diff += 0x30 - 0x61 - 26; // -75
86+
diff -= ((51 - src) >> 8) & 75
87+
88+
// if (src > 61) diff += 0x2d - 0x30 - 10; // -13
89+
diff -= ((61 - src) >> 8) & 13
90+
91+
// if (src > 62) diff += 0x5f - 0x2b - 1; // 3
92+
diff += ((62 - src) >> 8) & 49
93+
return byte(src + diff)
94+
}

0 commit comments

Comments
 (0)