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 package internal
36
37 import (
38 "crypto"
39 "crypto/rsa"
40 "errors"
41 "fmt"
42 )
43
44 var hashPrefixes = map[crypto.Hash][]byte{
45 crypto.MD5: {
46 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05,
47 0x00, 0x04, 0x10,
48 },
49 crypto.SHA1: {0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14},
50 crypto.SHA224: {
51 0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04,
52 0x05, 0x00, 0x04, 0x1c,
53 },
54 crypto.SHA256: {
55 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
56 0x05, 0x00, 0x04, 0x20,
57 },
58 crypto.SHA384: {
59 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02,
60 0x05, 0x00, 0x04, 0x30,
61 },
62 crypto.SHA512: {
63 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03,
64 0x05, 0x00, 0x04, 0x40,
65 },
66 crypto.MD5SHA1: {},
67 crypto.RIPEMD160: {0x30, 0x20, 0x30, 0x08, 0x06, 0x06, 0x28, 0xcf, 0x06, 0x03, 0x00, 0x31, 0x04, 0x14},
68 }
69
70 func PadPKCS1v15(pub *rsa.PublicKey, hash crypto.Hash, hashed []byte) ([]byte, error) {
71 hashLen, prefix, err := pkcs1v15HashInfo(hash, len(hashed))
72 if err != nil {
73 return nil, err
74 }
75
76 tLen := len(prefix) + hashLen
77 k := pub.Size()
78 if k < tLen+11 {
79 return nil, fmt.Errorf("message too long")
80 }
81
82
83 em := make([]byte, k)
84 em[1] = 1
85 for i := 2; i < k-tLen-1; i++ {
86 em[i] = 0xff
87 }
88 copy(em[k-tLen:k-hashLen], prefix)
89 copy(em[k-hashLen:k], hashed)
90
91 return em, nil
92 }
93
94 func pkcs1v15HashInfo(hash crypto.Hash, inLen int) (hashLen int, prefix []byte, err error) {
95
96
97 if hash == 0 {
98 return inLen, nil, nil
99 }
100
101 hashLen = hash.Size()
102 if inLen != hashLen {
103 return 0, nil, errors.New("threshold_internal: crypto/rsa: input must be hashed message")
104 }
105 prefix, ok := hashPrefixes[hash]
106 if !ok {
107 return 0, nil, errors.New("threshold_internal: crypto/rsa: unsupported hash function")
108 }
109 return
110 }
111
View as plain text