| 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, !noasm |
| 5 |
|
| 6 |
package aes |
| 7 |
|
| 8 |
import ( |
| 9 |
"github.com/henrydcase/nobs/utils" |
| 10 |
) |
| 11 |
|
| 12 |
// defined in asm_*.s |
| 13 |
|
| 14 |
//go:noescape |
| 15 |
func encryptBlockAsm(nr int, xk *uint32, dst, src *byte) |
| 16 |
|
| 17 |
//go:noescape |
| 18 |
func decryptBlockAsm(nr int, xk *uint32, dst, src *byte) |
| 19 |
|
| 20 |
//go:noescape |
| 21 |
func expandKeyAsm(nr int, key *byte, enc *uint32, dec *uint32) |
| 22 |
|
| 23 |
type AESAsm struct { |
| 24 |
enc [32 + 28]uint32 |
| 25 |
dec [32 + 28]uint32 |
| 26 |
} |
| 27 |
|
| 28 |
func (c *AESAsm) SetKey(key []byte) error { |
| 29 |
var rounds int |
| 30 |
switch len(key) { |
| 31 |
case 128 / 8: |
| 32 |
rounds = 10 |
| 33 |
case 192 / 8: |
| 34 |
rounds = 12 |
| 35 |
case 256 / 8: |
| 36 |
rounds = 14 |
| 37 |
} |
| 38 |
|
| 39 |
expandKeyAsm(rounds, &key[0], &c.enc[0], &c.dec[0]) |
| 40 |
return nil |
| 41 |
} |
| 42 |
|
| 43 |
func (c *AESAsm) BlockSize() int { return BlockSize } |
| 44 |
|
| 45 |
func (c *AESAsm) Encrypt(dst, src []byte) { |
| 46 |
if len(src) < BlockSize { |
| 47 |
panic("crypto/aes: input not full block") |
| 48 |
} |
| 49 |
if len(dst) < BlockSize { |
| 50 |
panic("crypto/aes: output not full block") |
| 51 |
} |
| 52 |
if InexactOverlap(dst[:BlockSize], src[:BlockSize]) { |
| 53 |
panic("crypto/aes: invalid buffer overlap") |
| 54 |
} |
| 55 |
encryptBlockAsm(len(c.enc)/4-1, &c.enc[0], &dst[0], &src[0]) |
| 56 |
} |
| 57 |
|
| 58 |
func (c *AESAsm) Decrypt(dst, src []byte) { |
| 59 |
if len(src) < BlockSize { |
| 60 |
panic("crypto/aes: input not full block") |
| 61 |
} |
| 62 |
if len(dst) < BlockSize { |
| 63 |
panic("crypto/aes: output not full block") |
| 64 |
} |
| 65 |
if InexactOverlap(dst[:BlockSize], src[:BlockSize]) { |
| 66 |
panic("crypto/aes: invalid buffer overlap") |
| 67 |
} |
| 68 |
decryptBlockAsm(len(c.dec)/4-1, &c.dec[0], &dst[0], &src[0]) |
| 69 |
} |
| 70 |
|
| 71 |
// expandKey is used by BenchmarkExpand to ensure that the asm implementation |
| 72 |
// of key expansion is used for the benchmark when it is available. |
| 73 |
func expandKey(key []byte, enc, dec []uint32) { |
| 74 |
if utils.X86.HasAES { |
| 75 |
rounds := 10 // rounds needed for AES128 |
| 76 |
switch len(key) { |
| 77 |
case 192 / 8: |
| 78 |
rounds = 12 |
| 79 |
case 256 / 8: |
| 80 |
rounds = 14 |
| 81 |
} |
| 82 |
expandKeyAsm(rounds, &key[0], &enc[0], &dec[0]) |
| 83 |
} else { |
| 84 |
expandKeyGo(key, enc, dec) |
| 85 |
} |
| 86 |
} |