github.com/yoctocloud/packer@v0.6.2-0.20160520224004-e11a0a18423f/builder/azure/pkcs12/mac_test.go (about)

     1  package pkcs12
     2  
     3  import (
     4  	"crypto/hmac"
     5  	"encoding/asn1"
     6  	"testing"
     7  )
     8  
     9  func verifyMac(macData *macData, message, password []byte) error {
    10  	if !macData.Mac.Algorithm.Algorithm.Equal(oidSha1Algorithm) {
    11  		return NotImplementedError("unknown digest algorithm: " + macData.Mac.Algorithm.Algorithm.String())
    12  	}
    13  
    14  	expectedMAC := computeMac(message, macData.Iterations, macData.MacSalt, password)
    15  
    16  	if !hmac.Equal(macData.Mac.Digest, expectedMAC) {
    17  		return ErrIncorrectPassword
    18  	}
    19  	return nil
    20  }
    21  
    22  func TestVerifyMac(t *testing.T) {
    23  	td := macData{
    24  		Mac: digestInfo{
    25  			Digest: []byte{0x18, 0x20, 0x3d, 0xff, 0x1e, 0x16, 0xf4, 0x92, 0xf2, 0xaf, 0xc8, 0x91, 0xa9, 0xba, 0xd6, 0xca, 0x9d, 0xee, 0x51, 0x93},
    26  		},
    27  		MacSalt:    []byte{1, 2, 3, 4, 5, 6, 7, 8},
    28  		Iterations: 2048,
    29  	}
    30  
    31  	message := []byte{11, 12, 13, 14, 15}
    32  	password, _ := bmpString("")
    33  
    34  	td.Mac.Algorithm.Algorithm = asn1.ObjectIdentifier([]int{1, 2, 3})
    35  	err := verifyMac(&td, message, password)
    36  	if _, ok := err.(NotImplementedError); !ok {
    37  		t.Errorf("err: %v", err)
    38  	}
    39  
    40  	td.Mac.Algorithm.Algorithm = asn1.ObjectIdentifier([]int{1, 3, 14, 3, 2, 26})
    41  	err = verifyMac(&td, message, password)
    42  	if err != ErrIncorrectPassword {
    43  		t.Errorf("Expected incorrect password, got err: %v", err)
    44  	}
    45  
    46  	password, _ = bmpString("Sesame open")
    47  	err = verifyMac(&td, message, password)
    48  	if err != nil {
    49  		t.Errorf("err: %v", err)
    50  	}
    51  
    52  }