File: encryption.go

package info (click to toggle)
golang-github-mitch000001-go-hbci 0.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,468 kB
  • sloc: java: 1,092; makefile: 5
file content (122 lines) | stat: -rw-r--r-- 3,826 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package element

import "fmt"

// NewPinTanEncryptionAlgorithm returns an EncryptionAlgorithmDataElement ready
// to use in pin/tan flow
func NewPinTanEncryptionAlgorithm() *EncryptionAlgorithmDataElement {
	e := &EncryptionAlgorithmDataElement{
		Usage:                      NewAlphaNumeric("2", 3),
		OperationMode:              NewAlphaNumeric("2", 3),
		Algorithm:                  NewAlphaNumeric("13", 3),
		Key:                        NewBinary([]byte(defaultPinTan), 512),
		KeyParamID:                 NewAlphaNumeric("5", 3),
		InitializationValueParamID: NewAlphaNumeric("1", 3),
	}
	e.DataElement = NewDataElementGroup(encryptionAlgorithmDEG, 7, e)
	return e
}

// NewRDHEncryptionAlgorithm returns an EncryptionAlgorithmDataElement ready to
// use in CardReader flow
func NewRDHEncryptionAlgorithm(pubKey []byte) *EncryptionAlgorithmDataElement {
	e := &EncryptionAlgorithmDataElement{
		Usage:                      NewAlphaNumeric("2", 3),
		OperationMode:              NewAlphaNumeric("2", 3),
		Algorithm:                  NewAlphaNumeric("13", 3),
		Key:                        NewBinary(pubKey, 512),
		KeyParamID:                 NewAlphaNumeric("6", 3),
		InitializationValueParamID: NewAlphaNumeric("1", 3),
	}
	e.DataElement = NewDataElementGroup(encryptionAlgorithmDEG, 7, e)
	return e
}

// EncryptionAlgorithmDataElement represents an encryption algorithm
type EncryptionAlgorithmDataElement struct {
	DataElement
	// "2" for OSY, Owner Symmetric
	Usage *AlphaNumericDataElement
	// "2" for CBC, Cipher Block Chaining.
	OperationMode *AlphaNumericDataElement
	// "13" for 2-Key-Triple-DES
	Algorithm *AlphaNumericDataElement
	Key       *BinaryDataElement
	// "5" for KYE, Symmetric key, en-/decryption with a symmetric key (DDV)
	// "6" for KYP, Symmetric key, encryption with a public key (RDH).
	KeyParamID                 *AlphaNumericDataElement
	InitializationValueParamID *AlphaNumericDataElement
	InitializationValue        *BinaryDataElement
}

// GroupDataElements returns the grouped DataElements
func (e *EncryptionAlgorithmDataElement) GroupDataElements() []DataElement {
	return []DataElement{
		e.Usage,
		e.OperationMode,
		e.Algorithm,
		e.Key,
		e.KeyParamID,
		e.InitializationValueParamID,
		e.InitializationValue,
	}
}

// UnmarshalHBCI unmarshals value into the DataElement
func (e *EncryptionAlgorithmDataElement) UnmarshalHBCI(value []byte) error {
	e = &EncryptionAlgorithmDataElement{}
	elements, err := ExtractElements(value)
	if err != nil {
		return err
	}
	if len(elements) < 7 {
		return fmt.Errorf("Malformed marshaled value")
	}
	e.DataElement = NewDataElementGroup(encryptionAlgorithmDEG, 5, e)
	e.Usage = &AlphaNumericDataElement{}
	err = e.Usage.UnmarshalHBCI(elements[0])
	if err != nil {
		return err
	}
	e.OperationMode = &AlphaNumericDataElement{}
	err = e.OperationMode.UnmarshalHBCI(elements[1])
	if err != nil {
		return err
	}
	if len(elements) > 2 && len(elements[2]) > 0 {
		e.Algorithm = &AlphaNumericDataElement{}
		err = e.Algorithm.UnmarshalHBCI(elements[2])
		if err != nil {
			return err
		}
	}
	if len(elements) > 3 && len(elements[3]) > 0 {
		e.Key = &BinaryDataElement{}
		err = e.Key.UnmarshalHBCI(elements[3])
		if err != nil {
			return err
		}
	}
	if len(elements) > 4 && len(elements[4]) > 0 {
		e.KeyParamID = &AlphaNumericDataElement{}
		err = e.KeyParamID.UnmarshalHBCI(elements[4])
		if err != nil {
			return err
		}
	}
	if len(elements) > 5 && len(elements[5]) > 0 {
		e.InitializationValueParamID = &AlphaNumericDataElement{}
		err = e.InitializationValueParamID.UnmarshalHBCI(elements[5])
		if err != nil {
			return err
		}
	}
	if len(elements) > 6 && len(elements[6]) > 0 {
		e.InitializationValue = &BinaryDataElement{}
		err = e.InitializationValue.UnmarshalHBCI(elements[6])
		if err != nil {
			return err
		}
	}
	return nil
}