Debian Codegraph @

rsa/rsapss.go

raw
1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package rsapss contains the RSASSA-PSS padding function extracted from the crypto/rsa package.
6 package rsapss
7
8 import (
9 "errors"
10 "hash"
11 )
12
13 // EMSAPSSEncode applies Probabilistic Signature Scheme (PSS) padding to RSA signature.
14 //
15 // Copied without modification from the crypto/rsa package.
16 // https://github.com/golang/go/blob/86fca3dcb63157b8e45e565e821e7fb098fcf368/src/crypto/internal/fips140/rsa/pkcs1v22.go#L71
17 func EMSAPSSEncode(mHash []byte, emBits int, salt []byte, hash hash.Hash) ([]byte, error) {
18 // See RFC 8017, Section 9.1.1.
19
20 hLen := hash.Size()
21 sLen := len(salt)
22 emLen := (emBits + 7) / 8
23
24 // 1. If the length of M is greater than the input limitation for the
25 // hash function (2^61 - 1 octets for SHA-1), output "message too
26 // long" and stop.
27 //
28 // 2. Let mHash = Hash(M), an octet string of length hLen.
29
30 if len(mHash) != hLen {
31 return nil, errors.New("crypto/rsa: input must be hashed with given hash")
32 }
33
34 // 3. If emLen < hLen + sLen + 2, output "encoding error" and stop.
35
36 if emLen < hLen+sLen+2 {
37 return nil, errors.New("crypto/rsa: key size too small for PSS signature")
38 }
39
40 em := make([]byte, emLen)
41 psLen := emLen - sLen - hLen - 2
42 db := em[:psLen+1+sLen]
43 h := em[psLen+1+sLen : emLen-1]
44
45 // 4. Generate a random octet string salt of length sLen; if sLen = 0,
46 // then salt is the empty string.
47 //
48 // 5. Let
49 // M' = (0x)00 00 00 00 00 00 00 00 || mHash || salt;
50 //
51 // M' is an octet string of length 8 + hLen + sLen with eight
52 // initial zero octets.
53 //
54 // 6. Let H = Hash(M'), an octet string of length hLen.
55
56 var prefix [8]byte
57
58 hash.Write(prefix[:])
59 hash.Write(mHash)
60 hash.Write(salt)
61
62 h = hash.Sum(h[:0])
63 hash.Reset()
64
65 // 7. Generate an octet string PS consisting of emLen - sLen - hLen - 2
66 // zero octets. The length of PS may be 0.
67 //
68 // 8. Let DB = PS || 0x01 || salt; DB is an octet string of length
69 // emLen - hLen - 1.
70
71 db[psLen] = 0x01
72 copy(db[psLen+1:], salt)
73
74 // 9. Let dbMask = MGF(H, emLen - hLen - 1).
75 //
76 // 10. Let maskedDB = DB \xor dbMask.
77
78 mgf1XOR(db, hash, h)
79
80 // 11. Set the leftmost 8 * emLen - emBits bits of the leftmost octet in
81 // maskedDB to zero.
82
83 db[0] &= 0xff >> (8*emLen - emBits)
84
85 // 12. Let EM = maskedDB || H || 0xbc.
86 em[emLen-1] = 0xbc
87
88 // 13. Output EM.
89 return em, nil
90 }
91
92 // Copied without modification from the crypto/rsa package.
93 // https://github.com/golang/go/blob/86fca3dcb63157b8e45e565e821e7fb098fcf368/src/crypto/internal/fips140/rsa/pkcs1v22.go#L37
94 //
95 // incCounter increments a four byte, big-endian counter.
96 func incCounter(c *[4]byte) {
97 if c[3]++; c[3] != 0 {
98 return
99 }
100 if c[2]++; c[2] != 0 {
101 return
102 }
103 if c[1]++; c[1] != 0 {
104 return
105 }
106 c[0]++
107 }
108
109 // Copied without modification from the crypto/rsa package.
110 // https://github.com/golang/go/blob/86fca3dcb63157b8e45e565e821e7fb098fcf368/src/crypto/internal/fips140/rsa/pkcs1v22.go#L52
111 //
112 // mgf1XOR XORs the bytes in out with a mask generated using the MGF1 function
113 // specified in PKCS #1 v2.1.
114 func mgf1XOR(out []byte, hash hash.Hash, seed []byte) {
115 var counter [4]byte
116 var digest []byte
117
118 done := 0
119 for done < len(out) {
120 hash.Write(seed)
121 hash.Write(counter[0:4])
122 digest = hash.Sum(digest[:0])
123 hash.Reset()
124
125 for i := 0; i < len(digest) && done < len(out); i++ {
126 out[done] ^= digest[i]
127 done++
128 }
129 incCounter(&counter)
130 }
131 }