-
Notifications
You must be signed in to change notification settings - Fork 0
/
hibeutils.go
174 lines (144 loc) · 4.47 KB
/
hibeutils.go
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
Copyright (C) BABEC. All rights reserved.
Copyright (C) THL A29 Limited, a Tencent company. All rights reserved.
SPDX-License-Identifier: Apache-2.0
*/
package hibe_amd64
import (
"crypto/sha256"
"errors"
"fmt"
"math/big"
"reflect"
"strings"
"chainmaker.org/chainmaker/common/v2/crypto"
"chainmaker.org/chainmaker/common/v2/crypto/hash"
"chainmaker.org/chainmaker/common/v2/crypto/hibe/hibe_amd64/hibe"
"chainmaker.org/chainmaker/common/v2/crypto/sym"
)
// ValidateId is used to validate id format
func ValidateId(id string) error {
if id == "" {
return errors.New("invalid parameters, id is nil")
}
idStrList := strings.Split(id, "/")
for _, s := range idStrList {
if s == "" {
return fmt.Errorf("invalid parameters, id: %s, format error, only like : \"A/B/C\" can be used", id)
}
if strings.Contains(s, " ") {
return fmt.Errorf("invalid parameters, id: %s, format error, only like : \"A/B/C\" can be used", id)
}
}
return nil
}
// refineIdsAndParams Remove redundant data (Id)
// Only the lowest level ID is reserved for the ID with superior subordinate relationship
func refineIdsAndParams(ids []string, paramsList []*hibe.Params) ([]string, []*hibe.Params, error) {
refinedReceiverIds := make([]string, 1)
refinedReceiverIds[0] = ids[0]
refinedParamsList := make([]*hibe.Params, 1)
refinedParamsList[0] = paramsList[0]
for i := 1; i < len(ids); i++ {
matched := false
for j, rId := range refinedReceiverIds {
iIdSlice := strings.Split(ids[i], "/")
iLen := len(iIdSlice)
rIdSlice := strings.Split(rId, "/")
rLen := len(rIdSlice)
// 1.They do not have a subordinate relationship
if iLen == rLen {
continue
}
if rLen > iLen { // 2. refinedReceiverIds[j] has prefix ids[i]
matched = true
for k := 0; k < iLen; k++ {
if iIdSlice[k] != rIdSlice[k] {
matched = false
break
}
}
// they have different parameters, return error
if matched && !reflect.DeepEqual(paramsList[i].Marshal(), refinedParamsList[j].Marshal()) {
return nil, nil, fmt.Errorf("ID [%s] is matched, but Params are different, please check it",
ids[i])
}
} else { // 3. ids[i] has prefix refinedReceiverIds[j]
matched = true
for k := 0; k < rLen; k++ {
if iIdSlice[k] != rIdSlice[k] {
matched = false
break
}
}
// they have different parameters, return error
if matched && !reflect.DeepEqual(paramsList[i].Marshal(), refinedParamsList[j].Marshal()) {
return nil, nil, fmt.Errorf("ID [%s] is matched, but Params are different, please check it",
ids[i])
}
if matched {
refinedReceiverIds[j] = ids[i]
refinedParamsList[j] = paramsList[i]
break
}
}
}
if !matched {
refinedReceiverIds = append(refinedReceiverIds, ids[i])
refinedParamsList = append(refinedParamsList, paramsList[i])
}
}
return refinedReceiverIds, refinedParamsList, nil
}
// idStrList2HibeIds construct HibeId list according to id list
func idStrList2HibeIds(idList []string) [][]*big.Int {
// construct ids []*big.Int
hibeIds := make([][]*big.Int, len(idList))
for i, idStr := range idList {
_, hibeIds[i] = IdStr2HibeId(idStr)
}
return hibeIds
}
// IdStr2HibeId construct HibeId according to id
func IdStr2HibeId(id string) ([]string, []*big.Int) {
// idStr eg: "org1/ou1" -> ["org1", "ou1"]
strId := strings.Split(id, "/")
// idsStr -> hibeId
hibeIds := make([]*big.Int, len(strId))
for i, value := range strId {
hashedStrId := sha256.Sum256([]byte(value))
bigIdBytes := hashedStrId[:]
bigId := new(big.Int)
bigId.SetBytes(bigIdBytes)
hibeIds[i] = bigId
}
return strId, hibeIds
}
// generateSymKeyFromGtBytes is used to generate symmetric key according to gtBytes and symKeyType
func generateSymKeyFromGtBytes(gtBytes []byte, symKeyType crypto.KeyType) (crypto.SymmetricKey, error) {
var symKey crypto.SymmetricKey
if symKeyType == crypto.AES {
// not gm
gtBytesHash, err := hash.Get(crypto.HASH_TYPE_SHA256, gtBytes)
if err != nil {
return nil, err
}
symKey, err = sym.GenerateSymKey(crypto.AES, gtBytesHash)
if err != nil {
return nil, err
}
} else if symKeyType == crypto.SM4 {
// gm
gtBytesHash, err := hash.Get(crypto.HASH_TYPE_SM3, gtBytes)
if err != nil {
return nil, err
}
symKey, err = sym.GenerateSymKey(crypto.SM4, gtBytesHash[:16])
if err != nil {
return nil, err
}
} else {
return nil, fmt.Errorf("invalid parameters, unsupported symmetric encryption algorithm type : %d", symKeyType)
}
return symKey, nil
}