...

Source file src/github.com/lestrrat-go/jwx/jwe/lowlevel_test.go

Documentation: github.com/lestrrat-go/jwx/jwe

     1  package jwe
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/lestrrat-go/jwx/jwa"
     7  	"github.com/lestrrat-go/jwx/jwe/internal/cipher"
     8  	"github.com/lestrrat-go/jwx/jwe/internal/keyenc"
     9  	"github.com/lestrrat-go/jwx/jwe/internal/keygen"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  // This test uses Appendix 3 to verify some low level tools for
    14  // KeyWrap and CBC HMAC encryption.
    15  // This test uses a static cek so that we can validate the results
    16  // against the contents in the above Appendix
    17  func TestLowLevelParts_A128KW_A128CBCHS256(t *testing.T) {
    18  	var plaintext = []byte{
    19  		76, 105, 118, 101, 32, 108, 111, 110, 103, 32, 97, 110, 100, 32,
    20  		112, 114, 111, 115, 112, 101, 114, 46,
    21  	}
    22  	var cek = []byte{
    23  		4, 211, 31, 197, 84, 157, 252, 254, 11, 100, 157, 250, 63, 170, 106,
    24  		206, 107, 124, 212, 45, 111, 107, 9, 219, 200, 177, 0, 240, 143, 156,
    25  		44, 207,
    26  	}
    27  	var iv = []byte{
    28  		3, 22, 60, 12, 43, 67, 104, 105, 108, 108, 105, 99, 111, 116, 104,
    29  		101,
    30  	}
    31  	var sharedkey = []byte{
    32  		25, 172, 32, 130, 225, 114, 26, 181, 138, 106, 254, 192, 95, 133, 74, 82,
    33  	}
    34  	var encsharedkey = []byte{
    35  		232, 160, 123, 211, 183, 76, 245, 132, 200, 128, 123, 75, 190, 216,
    36  		22, 67, 201, 138, 193, 186, 9, 91, 122, 31, 246, 90, 28, 139, 57, 3,
    37  		76, 124, 193, 11, 98, 37, 173, 61, 104, 57,
    38  	}
    39  	var aad = []byte{
    40  		101, 121, 74, 104, 98, 71, 99, 105, 79, 105, 74, 66, 77, 84, 73, 52,
    41  		83, 49, 99, 105, 76, 67, 74, 108, 98, 109, 77, 105, 79, 105, 74, 66,
    42  		77, 84, 73, 52, 81, 48, 74, 68, 76, 85, 104, 84, 77, 106, 85, 50, 73,
    43  		110, 48,
    44  	}
    45  	var ciphertext = []byte{
    46  		40, 57, 83, 181, 119, 33, 133, 148, 198, 185, 243, 24, 152, 230, 6,
    47  		75, 129, 223, 127, 19, 210, 82, 183, 230, 168, 33, 215, 104, 143,
    48  		112, 56, 102,
    49  	}
    50  	var authtag = []byte{
    51  		83, 73, 191, 98, 104, 205, 211, 128, 201, 189, 199, 133, 32, 38,
    52  		194, 85,
    53  	}
    54  
    55  	const compactExpected = `eyJhbGciOiJBMTI4S1ciLCJlbmMiOiJBMTI4Q0JDLUhTMjU2In0.6KB707dM9YTIgHtLvtgWQ8mKwboJW3of9locizkDTHzBC2IlrT1oOQ.AxY8DCtDaGlsbGljb3RoZQ.KDlTtXchhZTGufMYmOYGS4HffxPSUrfmqCHXaI9wOGY.U0m_YmjN04DJvceFICbCVQ`
    56  
    57  	k, err := keyenc.NewAES(jwa.A128KW, sharedkey)
    58  	if !assert.NoError(t, err, "Create key wrap") {
    59  		return
    60  	}
    61  
    62  	enckey, err := k.Encrypt(cek)
    63  	if !assert.NoError(t, err, "Failed to encrypt key") {
    64  		return
    65  	}
    66  	if !assert.Equal(t, encsharedkey, enckey.Bytes(), "encrypted keys match") {
    67  		return
    68  	}
    69  
    70  	cipher, err := cipher.NewAES(jwa.A128CBC_HS256)
    71  	if !assert.NoError(t, err, "NewAesContentCipher is successful") {
    72  		return
    73  	}
    74  	cipher.NonceGenerator = keygen.Static(iv)
    75  
    76  	iv, encrypted, tag, err := cipher.Encrypt(cek, plaintext, aad)
    77  	if !assert.NoError(t, err, "encrypt() successful") {
    78  		return
    79  	}
    80  
    81  	if !assert.Equal(t, ciphertext, encrypted, "Generated cipher text does not match") {
    82  		return
    83  	}
    84  
    85  	if !assert.Equal(t, tag, authtag, "Generated tag text does not match") {
    86  		return
    87  	}
    88  
    89  	data, err := cipher.Decrypt(cek, iv, encrypted, tag, aad)
    90  	if !assert.NoError(t, err, "decrypt successful") {
    91  		return
    92  	}
    93  
    94  	if !assert.Equal(t, plaintext, data, "decrypt works") {
    95  		return
    96  	}
    97  
    98  	r := NewRecipient()
    99  	r.Headers().Set(AlgorithmKey, jwa.A128KW)
   100  	r.SetEncryptedKey(enckey.Bytes())
   101  
   102  	protected := NewHeaders()
   103  	protected.Set(ContentEncryptionKey, jwa.A128CBC_HS256)
   104  
   105  	msg := NewMessage()
   106  	msg.Set(ProtectedHeadersKey, protected)
   107  	msg.Set(AuthenticatedDataKey, aad)
   108  	msg.Set(CipherTextKey, ciphertext)
   109  	msg.Set(InitializationVectorKey, iv)
   110  	msg.Set(TagKey, tag)
   111  	msg.Set(RecipientsKey, []Recipient{r})
   112  
   113  	serialized, err := Compact(msg)
   114  	if !assert.NoError(t, err, "compact serialization is successful") {
   115  		return
   116  	}
   117  
   118  	if !assert.Equal(t, compactExpected, string(serialized), "compact serialization matches") {
   119  		serialized, err = JSON(msg, WithPrettyFormat(true))
   120  		if !assert.NoError(t, err, "JSON serialization is successful") {
   121  			return
   122  		}
   123  		t.Logf("%s", serialized)
   124  	}
   125  }
   126  

View as plain text