Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
whqwjb
go-ethereum
提交
d344054e
G
go-ethereum
项目概览
whqwjb
/
go-ethereum
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
go-ethereum
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
d344054e
编写于
2月 27, 2015
作者:
F
Felix Lange
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
p2p: make RLPx frame MAC 16 bytes as defined in the spec
上级
d084aed5
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
19 addition
and
13 deletion
+19
-13
p2p/rlpx.go
p2p/rlpx.go
+19
-12
p2p/rlpx_test.go
p2p/rlpx_test.go
+0
-1
未找到文件。
p2p/rlpx.go
浏览文件 @
d344054e
...
...
@@ -62,12 +62,15 @@ func (rw *rlpxFrameRW) WriteMsg(msg Msg) error {
putInt24
(
fsize
,
headbuf
)
// TODO: check overflow
copy
(
headbuf
[
3
:
],
zeroHeader
)
rw
.
enc
.
XORKeyStream
(
headbuf
[
:
16
],
headbuf
[
:
16
])
// first half is now encrypted
copy
(
headbuf
[
16
:
],
updateHeaderMAC
(
rw
.
egressMAC
,
rw
.
macCipher
,
headbuf
[
:
16
]))
// write header MAC
copy
(
headbuf
[
16
:
],
updateMAC
(
rw
.
egressMAC
,
rw
.
macCipher
,
headbuf
[
:
16
]))
if
_
,
err
:=
rw
.
conn
.
Write
(
headbuf
);
err
!=
nil
{
return
err
}
// write encrypted frame, updating the egress MAC while writing to conn.
// write encrypted frame, updating the egress MAC hash with
// the data written to conn.
tee
:=
cipher
.
StreamWriter
{
S
:
rw
.
enc
,
W
:
io
.
MultiWriter
(
rw
.
conn
,
rw
.
egressMAC
)}
if
_
,
err
:=
tee
.
Write
(
ptype
);
err
!=
nil
{
return
err
...
...
@@ -81,9 +84,10 @@ func (rw *rlpxFrameRW) WriteMsg(msg Msg) error {
}
}
// write
packet-mac. egress MAC
is up to date because
// write
frame MAC. egress MAC hash
is up to date because
// frame content was written to it as well.
mac
:=
updateHeaderMAC
(
rw
.
egressMAC
,
rw
.
macCipher
,
rw
.
egressMAC
.
Sum
(
nil
))
fmacseed
:=
rw
.
egressMAC
.
Sum
(
nil
)
mac
:=
updateMAC
(
rw
.
egressMAC
,
rw
.
macCipher
,
fmacseed
)
_
,
err
:=
rw
.
conn
.
Write
(
mac
)
return
err
}
...
...
@@ -95,8 +99,8 @@ func (rw *rlpxFrameRW) ReadMsg() (msg Msg, err error) {
return
msg
,
err
}
// verify header mac
shouldMAC
:=
update
Header
MAC
(
rw
.
ingressMAC
,
rw
.
macCipher
,
headbuf
[
:
16
])
if
!
hmac
.
Equal
(
shouldMAC
[
:
16
]
,
headbuf
[
16
:
])
{
shouldMAC
:=
updateMAC
(
rw
.
ingressMAC
,
rw
.
macCipher
,
headbuf
[
:
16
])
if
!
hmac
.
Equal
(
shouldMAC
,
headbuf
[
16
:
])
{
return
msg
,
errors
.
New
(
"bad header MAC"
)
}
rw
.
dec
.
XORKeyStream
(
headbuf
[
:
16
],
headbuf
[
:
16
])
// first half is now decrypted
...
...
@@ -115,11 +119,12 @@ func (rw *rlpxFrameRW) ReadMsg() (msg Msg, err error) {
// read and validate frame MAC. we can re-use headbuf for that.
rw
.
ingressMAC
.
Write
(
framebuf
)
if
_
,
err
:=
io
.
ReadFull
(
rw
.
conn
,
headbuf
);
err
!=
nil
{
fmacseed
:=
rw
.
ingressMAC
.
Sum
(
nil
)
if
_
,
err
:=
io
.
ReadFull
(
rw
.
conn
,
headbuf
[
:
16
]);
err
!=
nil
{
return
msg
,
err
}
shouldMAC
=
update
HeaderMAC
(
rw
.
ingressMAC
,
rw
.
macCipher
,
rw
.
ingressMAC
.
Sum
(
nil
)
)
if
!
hmac
.
Equal
(
shouldMAC
,
headbuf
)
{
shouldMAC
=
update
MAC
(
rw
.
ingressMAC
,
rw
.
macCipher
,
fmacseed
)
if
!
hmac
.
Equal
(
shouldMAC
,
headbuf
[
:
16
]
)
{
return
msg
,
errors
.
New
(
"bad frame MAC"
)
}
...
...
@@ -136,14 +141,16 @@ func (rw *rlpxFrameRW) ReadMsg() (msg Msg, err error) {
return
msg
,
nil
}
func
updateHeaderMAC
(
mac
hash
.
Hash
,
block
cipher
.
Block
,
header
[]
byte
)
[]
byte
{
// updateMAC reseeds the given hash with encrypted seed.
// it returns the first 16 bytes of the hash sum after seeding.
func
updateMAC
(
mac
hash
.
Hash
,
block
cipher
.
Block
,
seed
[]
byte
)
[]
byte
{
aesbuf
:=
make
([]
byte
,
aes
.
BlockSize
)
block
.
Encrypt
(
aesbuf
,
mac
.
Sum
(
nil
))
for
i
:=
range
aesbuf
{
aesbuf
[
i
]
^=
header
[
i
]
aesbuf
[
i
]
^=
seed
[
i
]
}
mac
.
Write
(
aesbuf
)
return
mac
.
Sum
(
nil
)
return
mac
.
Sum
(
nil
)
[
:
16
]
}
func
readInt24
(
b
[]
byte
)
uint32
{
...
...
p2p/rlpx_test.go
浏览文件 @
d344054e
...
...
@@ -29,7 +29,6 @@ func TestRlpxFrameFake(t *testing.T) {
01010101010101010101010101010101
ba628a4ba590cb43f7848f41c4382885
01010101010101010101010101010101
01010101010101010101010101010101
`
)
// Check WriteMsg. This puts a message into the buffer.
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录