| 1 |
// Copyright 2014 Matthew Endsley |
| 2 |
// All rights reserved |
| 3 |
// |
| 4 |
// Redistribution and use in source and binary forms, with or without |
| 5 |
// modification, are permitted providing that the following conditions |
| 6 |
// are met: |
| 7 |
// 1. Redistributions of source code must retain the above copyright |
| 8 |
// notice, this list of conditions and the following disclaimer. |
| 9 |
// 2. Redistributions in binary form must reproduce the above copyright |
| 10 |
// notice, this list of conditions and the following disclaimer in the |
| 11 |
// documentation and/or other materials provided with the distribution. |
| 12 |
// |
| 13 |
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 14 |
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 15 |
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 16 |
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 17 |
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 18 |
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 19 |
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 20 |
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 21 |
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 22 |
// IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 23 |
// POSSIBILITY OF SUCH DAMAGE. |
| 24 |
|
| 25 |
package keywrap |
| 26 |
|
| 27 |
import ( |
| 28 |
"bytes" |
| 29 |
"testing" |
| 30 |
) |
| 31 |
|
| 32 |
// A.1.8 - JSON Web Encryption |
| 33 |
func TestWrap(t *testing.T) { |
| 34 |
key := []byte{64, 154, 239, 170, 64, 40, 195, 99, 19, 84, 192, 142, 192, 238, 207, 217} |
| 35 |
sharedKey := []byte{25, 172, 32, 130, 225, 114, 26, 181, 138, 106, 254, 192, 95, 133, 74, 82} |
| 36 |
expectedWrappedKey := []byte{164, 255, 251, 1, 64, 200, 65, 200, 34, 197, 81, 143, 43, 211, 240, 38, 191, 161, 181, 117, 119, 68, 44, 80} |
| 37 |
|
| 38 |
wrappedKey, err := Wrap(sharedKey, key) |
| 39 |
if err != nil { |
| 40 |
t.Fatal("keywrap: failed to Wrap key: ", err) |
| 41 |
} |
| 42 |
|
| 43 |
if !bytes.Equal(expectedWrappedKey, wrappedKey) { |
| 44 |
t.Fatalf("unwrap: unexpected wrapped key:\n\t%v\n\t%v", expectedWrappedKey, wrappedKey) |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
// A.1.8 - JSON Web Encryption |
| 49 |
func TestUnwrap(t *testing.T) { |
| 50 |
sharedKey := []byte{25, 172, 32, 130, 225, 114, 26, 181, 138, 106, 254, 192, 95, 133, 74, 82} |
| 51 |
wrappedKey := []byte{164, 255, 251, 1, 64, 200, 65, 200, 34, 197, 81, 143, 43, 211, 240, 38, 191, 161, 181, 117, 119, 68, 44, 80} |
| 52 |
expectedKey := []byte{64, 154, 239, 170, 64, 40, 195, 99, 19, 84, 192, 142, 192, 238, 207, 217} |
| 53 |
|
| 54 |
key, err := Unwrap(sharedKey, wrappedKey) |
| 55 |
if err != nil { |
| 56 |
t.Fatal("keywrap: failed to unwrap key: ", err) |
| 57 |
} |
| 58 |
|
| 59 |
if !bytes.Equal(expectedKey, key) { |
| 60 |
t.Fatalf("keywrap: unexpected wrapped key:\n\t%v\n\t%v", expectedKey, key) |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
// Test wrap error cases. |
| 65 |
func TestWrapError(t *testing.T) { |
| 66 |
plaintext := make([]byte, 7) |
| 67 |
key := make([]byte, 32) |
| 68 |
_, err := Wrap(key, plaintext) |
| 69 |
if err != ErrWrapPlaintext { |
| 70 |
t.Fatalf("keywrap: expected Wrap to fail with %v, but have err=%v", ErrWrapPlaintext, err) |
| 71 |
} |
| 72 |
|
| 73 |
plaintext = append(plaintext, byte(0)) |
| 74 |
_, err = Wrap(key[:31], plaintext) |
| 75 |
if err != ErrInvalidKey { |
| 76 |
t.Fatalf("keywrap: expected Wrap to fail with %v, but have err=%v", ErrInvalidKey, err) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
// Test unwrap error cases. |
| 81 |
func TestUnwrapError(t *testing.T) { |
| 82 |
key := []byte{64, 154, 239, 170, 64, 40, 195, 99, 19, 84, 192, 142, 192, 238, 207, 217} |
| 83 |
sharedKey := []byte{25, 172, 32, 130, 225, 114, 26, 181, 138, 106, 254, 192, 95, 133, 74, 82} |
| 84 |
wrapped, err := Wrap(key, sharedKey) |
| 85 |
if err != nil { |
| 86 |
t.Fatalf("%v", err) |
| 87 |
} |
| 88 |
|
| 89 |
l := len(wrapped) |
| 90 |
_, err = Unwrap(key, wrapped[:l-2]) |
| 91 |
if err != ErrUnwrapCiphertext { |
| 92 |
t.Fatalf("keywrap: expected Unwrap to fail with %v, but have err=%v", ErrUnwrapCiphertext, err) |
| 93 |
} |
| 94 |
|
| 95 |
l = len(key) |
| 96 |
_, err = Unwrap(key[:l-2], wrapped) |
| 97 |
if err != ErrInvalidKey { |
| 98 |
t.Fatalf("keywrap: expected Unwrap to fail with %v, but have err=%v", ErrInvalidKey, err) |
| 99 |
} |
| 100 |
|
| 101 |
wrapped[0]-- |
| 102 |
_, err = Unwrap(key, wrapped) |
| 103 |
if err != ErrUnwrapFailed { |
| 104 |
t.Fatalf("keywrap: expected Unwrap to fail with %v, but have err=%v", ErrUnwrapFailed, err) |
| 105 |
} |
| 106 |
|
| 107 |
} |