Debian Codegraph @

crypto/rfc3961/encryption.go

raw symbols SCIP
1 // Package rfc3961 provides encryption and checksum methods as specified in RFC 3961
2 package rfc3961
3
4 import (
5 "crypto/cipher"
6 "crypto/des"
7 "crypto/hmac"
8 "crypto/rand"
9 "errors"
10 "fmt"
11
12 "gopkg.in/jcmturner/gokrb5.v5/crypto/common"
13 "gopkg.in/jcmturner/gokrb5.v5/crypto/etype"
14 )
15
16 // DES3EncryptData encrypts the data provided using DES3 and methods specific to the etype provided.
17 func DES3EncryptData(key, data []byte, e etype.EType) ([]byte, []byte, error) {
18 if len(key) != e.GetKeyByteSize() {
19 return nil, nil, fmt.Errorf("incorrect keysize: expected: %v actual: %v", e.GetKeyByteSize(), len(key))
20 }
21 data, _ = common.ZeroPad(data, e.GetMessageBlockByteSize())
22
23 block, err := des.NewTripleDESCipher(key)
24 if err != nil {
25 return nil, nil, fmt.Errorf("error creating cipher: %v", err)
26 }
27
28 ivz := make([]byte, des.BlockSize)
29
30 ct := make([]byte, len(data))
31 mode := cipher.NewCBCEncrypter(block, ivz)
32 mode.CryptBlocks(ct, data)
33 return ct[len(ct)-e.GetMessageBlockByteSize():], ct, nil
34 }
35
36 // DES3EncryptMessage encrypts the message provided using DES3 and methods specific to the etype provided.
37 // The encrypted data is concatenated with its integrity hash to create an encrypted message.
38 func DES3EncryptMessage(key, message []byte, usage uint32, e etype.EType) ([]byte, []byte, error) {
39 //confounder
40 c := make([]byte, e.GetConfounderByteSize())
41 _, err := rand.Read(c)
42 if err != nil {
43 return []byte{}, []byte{}, fmt.Errorf("could not generate random confounder: %v", err)
44 }
45 plainBytes := append(c, message...)
46 plainBytes, _ = common.ZeroPad(plainBytes, e.GetMessageBlockByteSize())
47
48 // Derive key for encryption from usage
49 var k []byte
50 if usage != 0 {
51 k, err = e.DeriveKey(key, common.GetUsageKe(usage))
52 if err != nil {
53 return []byte{}, []byte{}, fmt.Errorf("error deriving key for encryption: %v", err)
54 }
55 }
56
57 iv, b, err := e.EncryptData(k, plainBytes)
58 if err != nil {
59 return iv, b, fmt.Errorf("error encrypting data: %v", err)
60 }
61
62 // Generate and append integrity hash
63 ih, err := common.GetIntegrityHash(plainBytes, key, usage, e)
64 if err != nil {
65 return iv, b, fmt.Errorf("error encrypting data: %v", err)
66 }
67 b = append(b, ih...)
68 return iv, b, nil
69 }
70
71 // DES3DecryptData decrypts the data provided using DES3 and methods specific to the etype provided.
72 func DES3DecryptData(key, data []byte, e etype.EType) ([]byte, error) {
73 if len(key) != e.GetKeyByteSize() {
74 return []byte{}, fmt.Errorf("incorrect keysize: expected: %v actual: %v", e.GetKeyByteSize(), len(key))
75 }
76
77 if len(data) < des.BlockSize || len(data)%des.BlockSize != 0 {
78 return []byte{}, errors.New("ciphertext is not a multiple of the block size")
79 }
80 block, err := des.NewTripleDESCipher(key)
81 if err != nil {
82 return []byte{}, fmt.Errorf("error creating cipher: %v", err)
83 }
84 pt := make([]byte, len(data))
85 ivz := make([]byte, des.BlockSize)
86 mode := cipher.NewCBCDecrypter(block, ivz)
87 mode.CryptBlocks(pt, data)
88 return pt, nil
89 }
90
91 // DES3DecryptMessage decrypts the message provided using DES3 and methods specific to the etype provided.
92 // The integrity of the message is also verified.
93 func DES3DecryptMessage(key, ciphertext []byte, usage uint32, e etype.EType) ([]byte, error) {
94 //Derive the key
95 k, err := e.DeriveKey(key, common.GetUsageKe(usage))
96 if err != nil {
97 return nil, fmt.Errorf("error deriving key: %v", err)
98 }
99 // Strip off the checksum from the end
100 b, err := e.DecryptData(k, ciphertext[:len(ciphertext)-e.GetHMACBitLength()/8])
101 if err != nil {
102 return nil, fmt.Errorf("error decrypting: %v", err)
103 }
104 //Verify checksum
105 if !e.VerifyIntegrity(key, ciphertext, b, usage) {
106 return nil, errors.New("error decrypting: integrity verification failed")
107 }
108 //Remove the confounder bytes
109 return b[e.GetConfounderByteSize():], nil
110 }
111
112 // VerifyIntegrity verifies the integrity of cipertext bytes ct.
113 func VerifyIntegrity(key, ct, pt []byte, usage uint32, etype etype.EType) bool {
114 h := make([]byte, etype.GetHMACBitLength()/8)
115 copy(h, ct[len(ct)-etype.GetHMACBitLength()/8:])
116 expectedMAC, _ := common.GetIntegrityHash(pt, key, usage, etype)
117 return hmac.Equal(h, expectedMAC)
118 }