mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-23 05:06:43 +00:00
Zero XOR CTR IV in ECIES and remove it from ciphertext
This commit is contained in:
parent
3fef601903
commit
d1f39f841b
1 changed files with 15 additions and 12 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package ecies
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/cipher"
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
|
|
@ -185,16 +186,17 @@ func symEncrypt(rand io.Reader, params *ECIESParams, key, m []byte) (ct []byte,
|
|||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
iv, err := generateIV(params, rand)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
/*
|
||||
In SEC 1 Version 2.0 the IV value is not specified in for XOR CTR.
|
||||
It is specified, however, that it should not be transmitted as part of the ciphertext.
|
||||
This means it cannot be random, as the other party would not know the value.
|
||||
Therefore we set it to the zeroed value defined for AES in CTR mode.
|
||||
*/
|
||||
iv := bytes.Repeat([]byte{0}, 16)
|
||||
ctr := cipher.NewCTR(c, iv)
|
||||
|
||||
ct = make([]byte, len(m)+params.BlockSize)
|
||||
copy(ct, iv)
|
||||
ctr.XORKeyStream(ct[params.BlockSize:], m)
|
||||
ct = make([]byte, len(m))
|
||||
ctr.XORKeyStream(ct, m)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -206,10 +208,11 @@ func symDecrypt(rand io.Reader, params *ECIESParams, key, ct []byte) (m []byte,
|
|||
return
|
||||
}
|
||||
|
||||
ctr := cipher.NewCTR(c, ct[:params.BlockSize])
|
||||
iv := bytes.Repeat([]byte{0}, 16)
|
||||
ctr := cipher.NewCTR(c, iv)
|
||||
|
||||
m = make([]byte, len(ct)-params.BlockSize)
|
||||
ctr.XORKeyStream(m, ct[params.BlockSize:])
|
||||
m = make([]byte, len(ct))
|
||||
ctr.XORKeyStream(m, ct)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -245,7 +248,7 @@ func Encrypt(rand io.Reader, pub *PublicKey, m, s1, s2 []byte) (ct []byte, err e
|
|||
hash.Reset()
|
||||
|
||||
em, err := symEncrypt(rand, params, Ke, m)
|
||||
if err != nil || len(em) <= params.BlockSize {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue