mirror of
https://github.com/henrydcase/nobs.git
synced 2024-11-26 09:01:20 +00:00
CTR-DRBG: Use hardware acceleration on X86 (#18)
benchmark old ns/op new ns/op delta BenchmarkInit-4 3403 397 -88.33% BenchmarkRead-4 14535 1560 -89.27%
This commit is contained in:
parent
71624cdc4c
commit
6f9706df01
@ -14,6 +14,7 @@ package drbg
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/henrydcase/nobs/drbg/internal/aes"
|
"github.com/henrydcase/nobs/drbg/internal/aes"
|
||||||
|
"github.com/henrydcase/nobs/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Constants below correspond to AES-256, which is currently
|
// Constants below correspond to AES-256, which is currently
|
||||||
@ -30,12 +31,15 @@ type CtrDrbg struct {
|
|||||||
counter uint
|
counter uint
|
||||||
strength uint
|
strength uint
|
||||||
resistance bool
|
resistance bool
|
||||||
blockEnc aes.AES
|
blockEnc aes.IAES
|
||||||
tmpBlk [3 * BlockLen]byte
|
tmpBlk [3 * BlockLen]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCtrDrbg() *CtrDrbg {
|
func NewCtrDrbg() *CtrDrbg {
|
||||||
return new(CtrDrbg)
|
if utils.X86.HasAES {
|
||||||
|
return &CtrDrbg{blockEnc: &aes.AESAsm{}}
|
||||||
|
}
|
||||||
|
return &CtrDrbg{blockEnc: &aes.AES{}}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CtrDrbg) inc() {
|
func (c *CtrDrbg) inc() {
|
||||||
|
@ -18,6 +18,13 @@ type AES struct {
|
|||||||
keyLen int
|
keyLen int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AES interface
|
||||||
|
type IAES interface {
|
||||||
|
SetKey(key []byte) error
|
||||||
|
Encrypt(dst, src []byte)
|
||||||
|
Decrypt(dst, src []byte)
|
||||||
|
}
|
||||||
|
|
||||||
type KeySizeError int
|
type KeySizeError int
|
||||||
|
|
||||||
func (k KeySizeError) Error() string {
|
func (k KeySizeError) Error() string {
|
||||||
|
@ -21,8 +21,8 @@ func decryptBlockAsm(nr int, xk *uint32, dst, src *byte)
|
|||||||
func expandKeyAsm(nr int, key *byte, enc *uint32, dec *uint32)
|
func expandKeyAsm(nr int, key *byte, enc *uint32, dec *uint32)
|
||||||
|
|
||||||
type AESAsm struct {
|
type AESAsm struct {
|
||||||
enc []uint32
|
enc [32 + 28]uint32
|
||||||
dec []uint32
|
dec [32 + 28]uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *AESAsm) SetKey(key []byte) error {
|
func (c *AESAsm) SetKey(key []byte) error {
|
||||||
|
Loading…
Reference in New Issue
Block a user