OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [crypto/] [des/] [cipher.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2011 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
 
5
package des
6
 
7
import "strconv"
8
 
9
// The DES block size in bytes.
10
const BlockSize = 8
11
 
12
type KeySizeError int
13
 
14
func (k KeySizeError) Error() string {
15
        return "crypto/des: invalid key size " + strconv.Itoa(int(k))
16
}
17
 
18
// Cipher is an instance of DES encryption.
19
type Cipher struct {
20
        subkeys [16]uint64
21
}
22
 
23
// NewCipher creates and returns a new Cipher.
24
func NewCipher(key []byte) (*Cipher, error) {
25
        if len(key) != 8 {
26
                return nil, KeySizeError(len(key))
27
        }
28
 
29
        c := new(Cipher)
30
        c.generateSubkeys(key)
31
        return c, nil
32
}
33
 
34
// BlockSize returns the DES block size, 8 bytes.
35
func (c *Cipher) BlockSize() int { return BlockSize }
36
 
37
// Encrypt encrypts the 8-byte buffer src and stores the result in dst.
38
// Note that for amounts of data larger than a block,
39
// it is not safe to just call Encrypt on successive blocks;
40
// instead, use an encryption mode like CBC (see crypto/cipher/cbc.go).
41
func (c *Cipher) Encrypt(dst, src []byte) { encryptBlock(c.subkeys[:], dst, src) }
42
 
43
// Decrypt decrypts the 8-byte buffer src and stores the result in dst.
44
func (c *Cipher) Decrypt(dst, src []byte) { decryptBlock(c.subkeys[:], dst, src) }
45
 
46
// Reset zeros the key data, so that it will no longer
47
// appear in the process's memory.
48
func (c *Cipher) Reset() {
49
        for i := 0; i < len(c.subkeys); i++ {
50
                c.subkeys[i] = 0
51
        }
52
}
53
 
54
// A TripleDESCipher is an instance of TripleDES encryption.
55
type TripleDESCipher struct {
56
        cipher1, cipher2, cipher3 Cipher
57
}
58
 
59
// NewCipher creates and returns a new Cipher.
60
func NewTripleDESCipher(key []byte) (*TripleDESCipher, error) {
61
        if len(key) != 24 {
62
                return nil, KeySizeError(len(key))
63
        }
64
 
65
        c := new(TripleDESCipher)
66
        c.cipher1.generateSubkeys(key[:8])
67
        c.cipher2.generateSubkeys(key[8:16])
68
        c.cipher3.generateSubkeys(key[16:])
69
        return c, nil
70
}
71
 
72
// BlockSize returns the TripleDES block size, 8 bytes.
73
// It is necessary to satisfy the Block interface in the
74
// package "crypto/cipher".
75
func (c *TripleDESCipher) BlockSize() int { return BlockSize }
76
 
77
// Encrypts the 8-byte buffer src and stores the result in dst.
78
// Note that for amounts of data larger than a block,
79
// it is not safe to just call Encrypt on successive blocks;
80
// instead, use an encryption mode like CBC (see crypto/cipher/cbc.go).
81
func (c *TripleDESCipher) Encrypt(dst, src []byte) {
82
        c.cipher1.Encrypt(dst, src)
83
        c.cipher2.Decrypt(dst, dst)
84
        c.cipher3.Encrypt(dst, dst)
85
}
86
 
87
// Decrypts the 8-byte buffer src and stores the result in dst.
88
func (c *TripleDESCipher) Decrypt(dst, src []byte) {
89
        c.cipher3.Decrypt(dst, src)
90
        c.cipher2.Encrypt(dst, dst)
91
        c.cipher1.Decrypt(dst, dst)
92
}
93
 
94
// Reset zeros the key data, so that it will no longer
95
// appear in the process's memory.
96
func (c *TripleDESCipher) Reset() {
97
        c.cipher1.Reset()
98
        c.cipher2.Reset()
99
        c.cipher3.Reset()
100
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.