This repository has been archived by the owner on Aug 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjwe.cbc.go
132 lines (124 loc) · 4.2 KB
/
jwe.cbc.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
package jwc
import (
"bytes"
"crypto"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"hash"
)
func plaintextCBC(K, iv, ciphertext, authTag, additionalAuthenticatedData []byte, hashFactory func() hash.Hash) (plaintext []byte, err error) {
hmacKey := K[:16]
authTagVerifier := renderCBCAuthTag(additionalAuthenticatedData, iv, ciphertext, hmacKey, hashFactory)
if !bytes.EqualFold(authTagVerifier, authTag) {
return nil, fmt.Errorf("unable to authenticate data")
}
cek := K[16:]
block, err := aes.NewCipher(cek)
if err != nil {
return nil, err
}
mode := cipher.NewCBCDecrypter(block, iv)
plaintext = make([]byte, len(ciphertext))
mode.CryptBlocks(plaintext, ciphertext)
plaintext, err = UnpadPKCS7(plaintext)
if err != nil {
return nil, err
}
return plaintext, nil
}
func newJWEA128CBCHS256(protectedHeaders *JOSEHeaders, pubKey crypto.PublicKey, plaintext []byte) (*JWE, error) {
cek, cipherCEK, cipherCEKB64, err := generateCEK(32, protectedHeaders.Algorithm, pubKey)
if err != nil {
return nil, err
}
hashFactory := sha256.New
return newCBC(protectedHeaders, cek, cipherCEK, cipherCEKB64, hashFactory, pubKey, plaintext)
}
func newJWEA192CBCHS384(protectedHeaders *JOSEHeaders, pubKey crypto.PublicKey, plaintext []byte) (*JWE, error) {
cek, cipherCEK, cipherCEKB64, err := generateCEK(40, protectedHeaders.Algorithm, pubKey)
if err != nil {
return nil, err
}
hashFactory := sha512.New384
return newCBC(protectedHeaders, cek, cipherCEK, cipherCEKB64, hashFactory, pubKey, plaintext)
}
func newJWEA256CBCHS512(protectedHeaders *JOSEHeaders, pubKey crypto.PublicKey, plaintext []byte) (*JWE, error) {
cek, cipherCEK, cipherCEKB64, err := generateCEK(48, protectedHeaders.Algorithm, pubKey)
if err != nil {
return nil, err
}
hashFactory := sha512.New
return newCBC(protectedHeaders, cek, cipherCEK, cipherCEKB64, hashFactory, pubKey, plaintext)
}
func newCBC(
protectedHeaders *JOSEHeaders,
K []byte,
cipherCEK []byte,
cipherCEKB64 string,
hashFactory func() hash.Hash,
pubKey crypto.PublicKey,
plaintext []byte,
) (*JWE, error) {
headersBytes, err := json.Marshal(protectedHeaders)
if err != nil {
return nil, err
}
headersB64 := base64.RawURLEncoding.EncodeToString(headersBytes)
iv, ivB64, err := generateInitVector(aes.BlockSize)
if err != nil {
return nil, err
}
hmacKey := K[:16]
cek := K[16:]
block, err := aes.NewCipher(cek)
if err != nil {
return nil, err
}
mode := cipher.NewCBCEncrypter(block, iv)
plaintext = PadPKCS7(plaintext, aes.BlockSize)
ciphertext := make([]byte, len(plaintext))
mode.CryptBlocks(ciphertext, plaintext)
ciphertextB64 := base64.RawURLEncoding.EncodeToString(ciphertext)
authenticatedData := []byte(string2ASCII(headersB64))
authTag := renderCBCAuthTag(authenticatedData, iv, ciphertext, hmacKey, hashFactory)
authTagB64 := base64.RawURLEncoding.EncodeToString(authTag)
jwe := JWE{
ProtectedB64: headersB64,
AdditionalAuthenticatedData: string(authenticatedData),
CipherCEKB64: cipherCEKB64,
InitVectorB64: ivB64,
CiphertextB64: ciphertextB64,
TagB64: authTagB64,
}
return &jwe, nil
}
func renderCBCAuthTag(additionalAuthenticatedData, iv, ciphertext, hmacKey []byte, hashFactory func() hash.Hash) (authTag []byte) {
additionalAuthenticatedDataLen := len(additionalAuthenticatedData)
additionalAuthenticatedDataLenBigEndian := make([]byte, 32)
binary.BigEndian.PutUint32(additionalAuthenticatedDataLenBigEndian, uint32(additionalAuthenticatedDataLen))
authTag = hmac.New(hashFactory, hmacKey).Sum(append(append(append(additionalAuthenticatedData, iv...), ciphertext...), additionalAuthenticatedDataLenBigEndian...))[:16]
return authTag
}
// PadPKCS7 -
func PadPKCS7(src []byte, blockSize int) []byte {
padding := blockSize - len(src)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(src, padtext...)
}
// UnpadPKCS7 -
func UnpadPKCS7(src []byte) ([]byte, error) {
length := len(src)
unpadding := int(src[length-1])
if unpadding > length {
return nil, errors.New("PKCS#7: error while unpadding")
}
return src[:(length - unpadding)], nil
}