package aes
import (
"crypto/cipher"
"crypto/internal/alias"
"crypto/internal/boring"
"internal/cpu"
"internal/goarch"
)
func encryptBlockAsm(nr int , xk *uint32 , dst , src *byte )
func decryptBlockAsm(nr int , xk *uint32 , dst , src *byte )
func expandKeyAsm(nr int , key *byte , enc *uint32 , dec *uint32 )
type aesCipherAsm struct {
aesCipher
}
type aesCipherGCM struct {
aesCipherAsm
}
var supportsAES = cpu .X86 .HasAES || cpu .ARM64 .HasAES || goarch .IsPpc64 == 1 || goarch .IsPpc64le == 1
var supportsGFMUL = cpu .X86 .HasPCLMULQDQ || cpu .ARM64 .HasPMULL
func newCipher(key []byte ) (cipher .Block , error ) {
if !supportsAES {
return newCipherGeneric (key )
}
c := aesCipherGCM {aesCipherAsm {aesCipher {l : uint8 (len (key ) + 28 )}}}
var rounds int
switch len (key ) {
case 128 / 8 :
rounds = 10
case 192 / 8 :
rounds = 12
case 256 / 8 :
rounds = 14
default :
return nil , KeySizeError (len (key ))
}
expandKeyAsm (rounds , &key [0 ], &c .enc [0 ], &c .dec [0 ])
if supportsAES && supportsGFMUL {
return &c , nil
}
return &c .aesCipherAsm , nil
}
func (c *aesCipherAsm ) BlockSize () int { return BlockSize }
func (c *aesCipherAsm ) Encrypt (dst , src []byte ) {
boring .Unreachable ()
if len (src ) < BlockSize {
panic ("crypto/aes: input not full block" )
}
if len (dst ) < BlockSize {
panic ("crypto/aes: output not full block" )
}
if alias .InexactOverlap (dst [:BlockSize ], src [:BlockSize ]) {
panic ("crypto/aes: invalid buffer overlap" )
}
encryptBlockAsm (int (c .l )/4 -1 , &c .enc [0 ], &dst [0 ], &src [0 ])
}
func (c *aesCipherAsm ) Decrypt (dst , src []byte ) {
boring .Unreachable ()
if len (src ) < BlockSize {
panic ("crypto/aes: input not full block" )
}
if len (dst ) < BlockSize {
panic ("crypto/aes: output not full block" )
}
if alias .InexactOverlap (dst [:BlockSize ], src [:BlockSize ]) {
panic ("crypto/aes: invalid buffer overlap" )
}
decryptBlockAsm (int (c .l )/4 -1 , &c .dec [0 ], &dst [0 ], &src [0 ])
}
func expandKey(key []byte , enc , dec []uint32 ) {
if supportsAES {
rounds := 10
switch len (key ) {
case 192 / 8 :
rounds = 12
case 256 / 8 :
rounds = 14
}
expandKeyAsm (rounds , &key [0 ], &enc [0 ], &dec [0 ])
} else {
expandKeyGo (key , enc , dec )
}
}
The pages are generated with Golds v0.7.0-preview . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .