File: cipher.go

package info (click to toggle)
golang-github-henrydcase-nobs 0.1%2Bgit20200305.7d891c7-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,928 kB
  • sloc: asm: 6,587; makefile: 53; python: 38
file content (88 lines) | stat: -rw-r--r-- 1,912 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package aes

import (
	"strconv"
)

// The AES block size in bytes.
const BlockSize = 16

// A cipher is an instance of AES encryption using a particular key.
type AES struct {
	enc    [32 + 28]uint32
	dec    [32 + 28]uint32
	keyLen int
}

// AES interface
type IAES interface {
	SetKey(key []byte) error
	Encrypt(dst, src []byte)
	Decrypt(dst, src []byte)
}

type KeySizeError int

func (k KeySizeError) Error() string {
	return "crypto/aes: invalid key size " + strconv.Itoa(int(k))
}

func NewCipher() *AES {
	return new(AES)
}

// NewCipher creates and returns a new cipher.Block.
// The key argument should be the AES key,
// either 16, 24, or 32 bytes to select
// AES-128, AES-192, or AES-256.
func (c *AES) SetKey(key []byte) error {
	k := len(key)

	switch k {
	default:
		return KeySizeError(k)
	case 16, 24, 32:
		break
	}
	for i, _ := range c.enc {
		c.enc[i] = 0
	}
	for i, _ := range c.dec {
		c.dec[i] = 0
	}
	c.keyLen = k
	expandKeyGo(key, c.enc[:c.keyLen+28], c.dec[:c.keyLen+28])
	return nil
}

func (c *AES) BlockSize() int { return BlockSize }

func (c *AES) Encrypt(dst, src []byte) {
	if len(src) < BlockSize {
		panic("crypto/aes: input not full block")
	}
	if len(dst) < BlockSize {
		panic("crypto/aes: output not full block")
	}
	if InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
		panic("crypto/aes: invalid buffer overlap")
	}
	encryptBlockGo(c.enc[:c.keyLen+28], dst, src)
}

func (c *AES) Decrypt(dst, src []byte) {
	if len(src) < BlockSize {
		panic("crypto/aes: input not full block")
	}
	if len(dst) < BlockSize {
		panic("crypto/aes: output not full block")
	}
	if InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
		panic("crypto/aes: invalid buffer overlap")
	}
	decryptBlockGo(c.dec[:c.keyLen+28], dst, src)
}