Skip to content

Commit 33e8ad5

Browse files
committed
core/pip: rmv gostr
1 parent d116015 commit 33e8ad5

1 file changed

Lines changed: 24 additions & 41 deletions

File tree

intra/backend/ipn_pipkeygen.go

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -64,26 +64,24 @@ type PipKeyProvider interface {
6464
Finalize(blingSig string) (*PipKey, error)
6565
}
6666

67-
// nb: PipToken inherits fields from Gostr but not its methods.
68-
6967
// PipToken is a 32 byte random token for bespoke auth.
70-
type PipToken Gostr
68+
type PipToken string
7169

7270
// PipMsg is a 64 byte hex encoded string that contains:
7371
// - first 32 bytes as message (random)
7472
// - next 32 bytes as client identifier (random)
75-
type PipMsg Gostr
73+
type PipMsg string
7674

77-
// AsPipMsg typecast Gostr m to PipMsg.
75+
// AsPipMsg typecast m to PipMsg.
7876
// m must be a 64 bytes hex string
7977
// (32b for msg + 32b for opaque-id).
8078
// Returns nil if the string m is nil or not a valid PipMsg.
8179
func AsPipMsg(m string) *PipMsg {
82-
p := &PipMsg{S: m}
80+
p := (PipMsg)(m)
8381
if !p.ok() {
8482
return nil
8583
}
86-
return p
84+
return &p
8785
}
8886

8987
func NewPipMsgWith(tok *PipToken) *PipMsg {
@@ -95,9 +93,10 @@ func NewPipMsgWith(tok *PipToken) *PipMsg {
9593
log.E("pipkey: new: invalid msg size; want %d, got %d", 2*msgsize, len(msg))
9694
return nil
9795
}
98-
return pipmsgof(msg + tok.S)
96+
return pipmsgof(msg + (string)(*tok))
9997
}
10098

99+
// go.dev/play/p/hPFgE9s9tMP
101100
// go.dev/play/p/OTMIv7FLtVs
102101
func pipmsgof(m string) *PipMsg {
103102
// 2 chars per byte in hex
@@ -106,54 +105,45 @@ func pipmsgof(m string) *PipMsg {
106105
return nil
107106
}
108107
// m is a 64 byte hex encoded string + tok is a 64 byte
109-
return &PipMsg{S: m}
110-
}
111-
112-
// Returns empty Gostr if p is nil or invalid PipMsg.
113-
func (p *PipMsg) AsGostr() string {
114-
if !p.ok() {
115-
return ""
116-
}
117-
// go.dev/play/p/hPFgE9s9tMP
118-
return p.S
108+
return AsPipMsg(m)
119109
}
120110

121111
func (p *PipMsg) v() string {
122112
if p == nil {
123113
return ""
124114
}
125-
return p.S
115+
return string(*p)
126116
}
127117

128118
func (p *PipMsg) ok() bool {
129-
return p != nil && len(p.S) >= 2*(msgsize+cidsize)
119+
return p != nil && len(*p) >= 2*(msgsize+cidsize)
130120
}
131121

132122
func (p *PipMsg) msg() []byte {
133123
if p == nil || !p.ok() {
134-
log.E("pipkey: msg: invalid; got %d", len(p.S))
124+
log.E("pipkey: msg: invalid; got %d", len(*p))
135125
return nil
136126
}
137127
// first 32 bytes are the message
138-
return hex2byte(p.S[:2*msgsize])
128+
return hex2byte(string(*p)[:2*msgsize])
139129
}
140130

141131
func (p *PipMsg) cid() []byte {
142132
if p == nil || !p.ok() {
143-
log.E("pipkey: cid: invalid; got %d", len(p.S))
133+
log.E("pipkey: cid: invalid; got %d", len(*p))
144134
return nil
145135
}
146136
// next 32 bytes are the client identifier
147-
return hex2byte(p.S[2*msgsize : 2*(msgsize+cidsize)])
137+
return hex2byte(string(*p)[2*msgsize : 2*(msgsize+cidsize)])
148138
}
149139

150140
// Opaque returns the client id part of the PipMsg as hex string.
151141
func (p *PipMsg) Opaque() *PipToken {
152142
if p == nil || !p.ok() {
153-
log.E("pipkey: opaque: invalid; got %d", len(p.S))
143+
log.E("pipkey: opaque: invalid; got %d", len(*p))
154144
return nil
155145
}
156-
tok, err := asPipToken(p.S[2*(msgsize) : 2*(msgsize+cidsize)])
146+
tok, err := asPipToken(string(*p)[2*(msgsize) : 2*(msgsize+cidsize)])
157147
if err != nil {
158148
log.E("pipkey: opaque conv: %v", err)
159149
return nil
@@ -192,11 +182,7 @@ func (p *PipKey) V() string {
192182
}, delim)
193183
}
194184

195-
func PipKeyFrom(v string) (*PipKey, error) {
196-
if v == "" {
197-
return nil, errEmptyPipKeyState
198-
}
199-
state := v
185+
func PipKeyFrom(state string) (*PipKey, error) {
200186
if len(state) <= 0 {
201187
return nil, errEmptyPipKeyState
202188
}
@@ -244,11 +230,7 @@ func newPipKeyState(id, blindMsg, r, salt, msg string) *PipKeyState {
244230
}
245231
}
246232

247-
func NewPipKeyStateFrom(v string) (*PipKeyState, error) {
248-
if v == "" {
249-
return nil, errEmptyPipKeyState
250-
}
251-
state := v
233+
func NewPipKeyStateFrom(state string) (*PipKeyState, error) {
252234
if len(state) <= 0 {
253235
return nil, errEmptyPipKeyState
254236
}
@@ -343,16 +325,16 @@ var _ PipKeyProvider = (*pkgen)(nil)
343325
// NewPipKeyProvider creates a new PipKeyProvider instance.
344326
// pubjwk: JWK string of the public key of the RSA-PSS signer (for which modulus must be 2048 bits, and hash-fn must be SHA384).
345327
// msgOrExistingState: if empty, a new PipKeyProvider is created with a random message, if not empty, it's the state of an existing PipKey.
346-
// Typically, msgOrExistingState is got from PipKeyState
328+
// Typically, msgOrExistingState is got from PipKeyState.V()
347329
func NewPipKeyProvider(pubjwk []byte, msgOrExistingState string) (PipKeyProvider, error) {
348330
return newPipKey(pubjwk, msgOrExistingState, false)
349331
}
350332

351333
// NewPipKeyProviderFromMsg creates a new PipKeyProvider instance from a JWK and a msg hex string.
352334
// Generating Blind() for the same msg with the same JWK will NOT result in the same PipKeyState.
353-
// To restore a previous state, use NewPipKeyProvider() with the PipKeyState string.
354-
func NewPipKeyProviderFromMsg(pubjwk []byte, msg *PipMsg) (PipKeyProvider, error) {
355-
return newPipKey(pubjwk, msg.v(), true)
335+
// To restore a previous state, use NewPipKeyProvider() with the PipKeyState.V() string.
336+
func NewPipKeyProviderFromMsg(pubjwk []byte, msg string) (PipKeyProvider, error) {
337+
return newPipKey(pubjwk, msg, true)
356338
}
357339

358340
func newPipKey(bjwk []byte, msgOrExistingState string, msgOnly bool) (PipKeyProvider, error) {
@@ -573,7 +555,8 @@ func asPipToken(tok string) (*PipToken, error) {
573555
if len(tok) != 2*tokensize {
574556
return nil, errTokenCreat
575557
}
576-
return &PipToken{S: tok}, nil
558+
// StrOf interns the string
559+
return (*PipToken)(&tok), nil
577560
}
578561

579562
func token() string {

0 commit comments

Comments
 (0)