Merge pull request #2 from safing/feature/increase-poly1305-misuse-resistance
Add misuse resistance checks
This commit is contained in:
commit
4ef354f865
1 changed files with 16 additions and 1 deletions
|
@ -25,7 +25,9 @@ func init() {
|
||||||
// Poly1305 implements the cryptographic interface for Poly1305 message authentication codes.
|
// Poly1305 implements the cryptographic interface for Poly1305 message authentication codes.
|
||||||
type Poly1305 struct {
|
type Poly1305 struct {
|
||||||
tools.ToolLogicBase
|
tools.ToolLogicBase
|
||||||
key [32]byte
|
key [32]byte
|
||||||
|
keyIsSetUp bool
|
||||||
|
keyUsed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup implements the ToolLogic interface.
|
// Setup implements the ToolLogic interface.
|
||||||
|
@ -35,6 +37,7 @@ func (poly *Poly1305) Setup() (err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
poly.keyIsSetUp = true
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -43,14 +46,26 @@ func (poly *Poly1305) Setup() (err error) {
|
||||||
func (poly *Poly1305) Reset() error {
|
func (poly *Poly1305) Reset() error {
|
||||||
// clean up key
|
// clean up key
|
||||||
poly.Helper().Burn(poly.key[:])
|
poly.Helper().Burn(poly.key[:])
|
||||||
|
poly.keyUsed = false
|
||||||
|
poly.keyIsSetUp = false
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// MAC implements the ToolLogic interface.
|
// MAC implements the ToolLogic interface.
|
||||||
func (poly *Poly1305) MAC(data, associatedData []byte) ([]byte, error) {
|
func (poly *Poly1305) MAC(data, associatedData []byte) ([]byte, error) {
|
||||||
|
// check for key initialization
|
||||||
|
if !poly.keyIsSetUp {
|
||||||
|
return nil, errors.New("key not initialized")
|
||||||
|
}
|
||||||
|
// check for key reuse
|
||||||
|
if poly.keyUsed {
|
||||||
|
return nil, errors.New("key reuse detected")
|
||||||
|
}
|
||||||
|
|
||||||
// create MAC
|
// create MAC
|
||||||
mac := poly1305.New(&poly.key)
|
mac := poly1305.New(&poly.key)
|
||||||
|
poly.keyUsed = true
|
||||||
// write data
|
// write data
|
||||||
n, err := mac.Write(data)
|
n, err := mac.Write(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Add table
Reference in a new issue