cipher_asm.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build amd64
  5. package aes
  6. // defined in asm_$GOARCH.s
  7. func hasAsm() bool
  8. func encryptBlockAsm(nr int, xk *uint32, dst, src *byte)
  9. func decryptBlockAsm(nr int, xk *uint32, dst, src *byte)
  10. func expandKeyAsm(nr int, key *byte, enc *uint32, dec *uint32)
  11. var useAsm = hasAsm()
  12. func encryptBlock(xk []uint32, dst, src []byte) {
  13. if useAsm {
  14. encryptBlockAsm(len(xk)/4-1, &xk[0], &dst[0], &src[0])
  15. } else {
  16. encryptBlockGo(xk, dst, src)
  17. }
  18. }
  19. func decryptBlock(xk []uint32, dst, src []byte) {
  20. if useAsm {
  21. decryptBlockAsm(len(xk)/4-1, &xk[0], &dst[0], &src[0])
  22. } else {
  23. decryptBlockGo(xk, dst, src)
  24. }
  25. }
  26. func expandKey(key []byte, enc, dec []uint32) {
  27. if useAsm {
  28. rounds := 10
  29. switch len(key) {
  30. case 128 / 8:
  31. rounds = 10
  32. case 192 / 8:
  33. rounds = 12
  34. case 256 / 8:
  35. rounds = 14
  36. }
  37. expandKeyAsm(rounds, &key[0], &enc[0], &dec[0])
  38. } else {
  39. expandKeyGo(key, enc, dec)
  40. }
  41. }