...

Source file src/gopkg.in/go-jose/go-jose.v2/cipher/ecdh_es_test.go

Documentation: gopkg.in/go-jose/go-jose.v2/cipher

     1  /*-
     2   * Copyright 2014 Square Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package josecipher
    18  
    19  import (
    20  	"bytes"
    21  	"crypto/ecdsa"
    22  	"crypto/elliptic"
    23  	"encoding/base64"
    24  	"math/big"
    25  	"testing"
    26  )
    27  
    28  // Example keys from JWA, Appendix C
    29  var aliceKey = &ecdsa.PrivateKey{
    30  	PublicKey: ecdsa.PublicKey{
    31  		Curve: elliptic.P256(),
    32  		X:     fromBase64Int("gI0GAILBdu7T53akrFmMyGcsF3n5dO7MmwNBHKW5SV0="),
    33  		Y:     fromBase64Int("SLW_xSffzlPWrHEVI30DHM_4egVwt3NQqeUD7nMFpps="),
    34  	},
    35  	D: fromBase64Int("0_NxaRPUMQoAJt50Gz8YiTr8gRTwyEaCumd-MToTmIo="),
    36  }
    37  
    38  var bobKey = &ecdsa.PrivateKey{
    39  	PublicKey: ecdsa.PublicKey{
    40  		Curve: elliptic.P256(),
    41  		X:     fromBase64Int("weNJy2HscCSM6AEDTDg04biOvhFhyyWvOHQfeF_PxMQ="),
    42  		Y:     fromBase64Int("e8lnCO-AlStT-NJVX-crhB7QRYhiix03illJOVAOyck="),
    43  	},
    44  	D: fromBase64Int("VEmDZpDXXK8p8N0Cndsxs924q6nS1RXFASRl6BfUqdw="),
    45  }
    46  
    47  // Build big int from base64-encoded string. Strips whitespace (for testing).
    48  func fromBase64Int(data string) *big.Int {
    49  	val, err := base64.URLEncoding.DecodeString(data)
    50  	if err != nil {
    51  		panic("Invalid test data: " + err.Error())
    52  	}
    53  	return new(big.Int).SetBytes(val)
    54  }
    55  
    56  func TestVectorECDHES(t *testing.T) {
    57  	apuData := []byte("Alice")
    58  	apvData := []byte("Bob")
    59  
    60  	expected := []byte{
    61  		86, 170, 141, 234, 248, 35, 109, 32, 92, 34, 40, 205, 113, 167, 16, 26}
    62  
    63  	output := DeriveECDHES("A128GCM", apuData, apvData, bobKey, &aliceKey.PublicKey, 16)
    64  
    65  	if bytes.Compare(output, expected) != 0 {
    66  		t.Error("output did not match what we expect, got", output, "wanted", expected)
    67  	}
    68  }
    69  
    70  func TestInvalidECPublicKey(t *testing.T) {
    71  	defer func() { recover() }()
    72  
    73  	// Invalid key
    74  	invalid := &ecdsa.PrivateKey{
    75  		PublicKey: ecdsa.PublicKey{
    76  			Curve: elliptic.P256(),
    77  			X:     fromBase64Int("MTEx"),
    78  			Y:     fromBase64Int("MTEx"),
    79  		},
    80  		D: fromBase64Int("0_NxaRPUMQoAJt50Gz8YiTr8gRTwyEaCumd-MToTmIo="),
    81  	}
    82  
    83  	DeriveECDHES("A128GCM", []byte{}, []byte{}, bobKey, &invalid.PublicKey, 16)
    84  	t.Fatal("should panic if public key was invalid")
    85  }
    86  
    87  func BenchmarkECDHES_128(b *testing.B) {
    88  	apuData := []byte("APU")
    89  	apvData := []byte("APV")
    90  
    91  	b.ResetTimer()
    92  	for i := 0; i < b.N; i++ {
    93  		DeriveECDHES("ID", apuData, apvData, bobKey, &aliceKey.PublicKey, 16)
    94  	}
    95  }
    96  
    97  func BenchmarkECDHES_192(b *testing.B) {
    98  	apuData := []byte("APU")
    99  	apvData := []byte("APV")
   100  
   101  	b.ResetTimer()
   102  	for i := 0; i < b.N; i++ {
   103  		DeriveECDHES("ID", apuData, apvData, bobKey, &aliceKey.PublicKey, 24)
   104  	}
   105  }
   106  
   107  func BenchmarkECDHES_256(b *testing.B) {
   108  	apuData := []byte("APU")
   109  	apvData := []byte("APV")
   110  
   111  	b.ResetTimer()
   112  	for i := 0; i < b.N; i++ {
   113  		DeriveECDHES("ID", apuData, apvData, bobKey, &aliceKey.PublicKey, 32)
   114  	}
   115  }
   116  

View as plain text