Debian Codegraph @

element/encryption.go

raw
1 package element
2
3 import "fmt"
4
5 // NewPinTanEncryptionAlgorithm returns an EncryptionAlgorithmDataElement ready
6 // to use in pin/tan flow
7 func NewPinTanEncryptionAlgorithm() *EncryptionAlgorithmDataElement {
8 e := &EncryptionAlgorithmDataElement{
9 Usage: NewAlphaNumeric("2", 3),
10 OperationMode: NewAlphaNumeric("2", 3),
11 Algorithm: NewAlphaNumeric("13", 3),
12 Key: NewBinary([]byte(defaultPinTan), 512),
13 KeyParamID: NewAlphaNumeric("5", 3),
14 InitializationValueParamID: NewAlphaNumeric("1", 3),
15 }
16 e.DataElement = NewDataElementGroup(encryptionAlgorithmDEG, 7, e)
17 return e
18 }
19
20 // NewRDHEncryptionAlgorithm returns an EncryptionAlgorithmDataElement ready to
21 // use in CardReader flow
22 func NewRDHEncryptionAlgorithm(pubKey []byte) *EncryptionAlgorithmDataElement {
23 e := &EncryptionAlgorithmDataElement{
24 Usage: NewAlphaNumeric("2", 3),
25 OperationMode: NewAlphaNumeric("2", 3),
26 Algorithm: NewAlphaNumeric("13", 3),
27 Key: NewBinary(pubKey, 512),
28 KeyParamID: NewAlphaNumeric("6", 3),
29 InitializationValueParamID: NewAlphaNumeric("1", 3),
30 }
31 e.DataElement = NewDataElementGroup(encryptionAlgorithmDEG, 7, e)
32 return e
33 }
34
35 // EncryptionAlgorithmDataElement represents an encryption algorithm
36 type EncryptionAlgorithmDataElement struct {
37 DataElement
38 // "2" for OSY, Owner Symmetric
39 Usage *AlphaNumericDataElement
40 // "2" for CBC, Cipher Block Chaining.
41 OperationMode *AlphaNumericDataElement
42 // "13" for 2-Key-Triple-DES
43 Algorithm *AlphaNumericDataElement
44 Key *BinaryDataElement
45 // "5" for KYE, Symmetric key, en-/decryption with a symmetric key (DDV)
46 // "6" for KYP, Symmetric key, encryption with a public key (RDH).
47 KeyParamID *AlphaNumericDataElement
48 InitializationValueParamID *AlphaNumericDataElement
49 InitializationValue *BinaryDataElement
50 }
51
52 // GroupDataElements returns the grouped DataElements
53 func (e *EncryptionAlgorithmDataElement) GroupDataElements() []DataElement {
54 return []DataElement{
55 e.Usage,
56 e.OperationMode,
57 e.Algorithm,
58 e.Key,
59 e.KeyParamID,
60 e.InitializationValueParamID,
61 e.InitializationValue,
62 }
63 }
64
65 // UnmarshalHBCI unmarshals value into the DataElement
66 func (e *EncryptionAlgorithmDataElement) UnmarshalHBCI(value []byte) error {
67 e = &EncryptionAlgorithmDataElement{}
68 elements, err := ExtractElements(value)
69 if err != nil {
70 return err
71 }
72 if len(elements) < 7 {
73 return fmt.Errorf("Malformed marshaled value")
74 }
75 e.DataElement = NewDataElementGroup(encryptionAlgorithmDEG, 5, e)
76 e.Usage = &AlphaNumericDataElement{}
77 err = e.Usage.UnmarshalHBCI(elements[0])
78 if err != nil {
79 return err
80 }
81 e.OperationMode = &AlphaNumericDataElement{}
82 err = e.OperationMode.UnmarshalHBCI(elements[1])
83 if err != nil {
84 return err
85 }
86 if len(elements) > 2 && len(elements[2]) > 0 {
87 e.Algorithm = &AlphaNumericDataElement{}
88 err = e.Algorithm.UnmarshalHBCI(elements[2])
89 if err != nil {
90 return err
91 }
92 }
93 if len(elements) > 3 && len(elements[3]) > 0 {
94 e.Key = &BinaryDataElement{}
95 err = e.Key.UnmarshalHBCI(elements[3])
96 if err != nil {
97 return err
98 }
99 }
100 if len(elements) > 4 && len(elements[4]) > 0 {
101 e.KeyParamID = &AlphaNumericDataElement{}
102 err = e.KeyParamID.UnmarshalHBCI(elements[4])
103 if err != nil {
104 return err
105 }
106 }
107 if len(elements) > 5 && len(elements[5]) > 0 {
108 e.InitializationValueParamID = &AlphaNumericDataElement{}
109 err = e.InitializationValueParamID.UnmarshalHBCI(elements[5])
110 if err != nil {
111 return err
112 }
113 }
114 if len(elements) > 6 && len(elements[6]) > 0 {
115 e.InitializationValue = &BinaryDataElement{}
116 err = e.InitializationValue.UnmarshalHBCI(elements[6])
117 if err != nil {
118 return err
119 }
120 }
121 return nil
122 }