github.com/trustbloc/kms-go@v1.1.2/util/cryptoutil/utils_test.go (about)

     1  /*
     2  Copyright SecureKey Technologies Inc. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package cryptoutil
     8  
     9  import (
    10  	"encoding/base64"
    11  	"testing"
    12  
    13  	"github.com/btcsuite/btcutil/base58"
    14  	"github.com/stretchr/testify/require"
    15  	chacha "golang.org/x/crypto/chacha20poly1305"
    16  )
    17  
    18  func TestDeriveKEK_Util(t *testing.T) {
    19  	z, err := DeriveECDHX25519(nil, nil)
    20  	require.EqualError(t, err, "deriveECDHX25519: invalid key")
    21  	require.Empty(t, z)
    22  
    23  	validChachaKey, err := base64.RawURLEncoding.DecodeString("c8CSJr_27PN9xWCpzXNmepRndD6neQcnO9DS0YWjhNs")
    24  	require.NoError(t, err)
    25  
    26  	chachaKey := new([chacha.KeySize]byte)
    27  	copy(chachaKey[:], validChachaKey)
    28  	z, err = DeriveECDHX25519(chachaKey, nil)
    29  	require.EqualError(t, err, "deriveECDHX25519: invalid key")
    30  	require.Empty(t, z)
    31  
    32  	validChachaKey2, err := base64.RawURLEncoding.DecodeString("AAjrHjiFLw6kf6CZ5zqH1ooG3y2aQhuqxmUvqJnIvDI")
    33  	require.NoError(t, err)
    34  
    35  	chachaKey2 := new([chacha.KeySize]byte)
    36  	copy(chachaKey2[:], validChachaKey2)
    37  	z, err = DeriveECDHX25519(chachaKey, chachaKey2)
    38  	require.NoError(t, err)
    39  	require.NotEmpty(t, z)
    40  
    41  	// lowOrderPoint from golang.org/x/crypto/curve25519.
    42  	// https://github.com/golang/crypto/blob/f4817d981/curve25519/vectors_test.go#L10
    43  	lowOrderPoint := []byte{
    44  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    45  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    46  	}
    47  	chachaKey2 = new([chacha.KeySize]byte)
    48  	copy(chachaKey2[:], lowOrderPoint)
    49  	// test error from curve25519.X25519() call in DeriveECDHX25519()
    50  	_, err = DeriveECDHX25519(chachaKey, chachaKey2)
    51  	require.EqualError(t, err, "deriveECDHX25519: crypto/ecdh: bad X25519 remote ECDH input: low order point")
    52  }
    53  
    54  func TestNonceGeneration(t *testing.T) {
    55  	t.Run("Verify nonce against libsodium generated data", func(t *testing.T) {
    56  		data := [][]string{
    57  			{
    58  				"6Gy2UWZCvYcTnpNvQX6ZNhz8FEofrhVxLCEPrjNTTZui", "9mGybrrDfGPdnGXA4BXbzJXnbg2w27bZ1ok6whbJrhF9",
    59  				"EWqT43jjhcy4wJHamH2RFthdLAQhits8F",
    60  			},
    61  			{
    62  				"GJBA64X9GReJrUttG4xQ1dLm726Sn3XQE5hAQeiCZtBV", "kRU8Ef7NTmhijeqKyWzZaZmVAq5UhnpfMfzsBYgBGrV",
    63  				"Kr7Wi5EGyTVNQy44oeFcBJtJJ7dVvXEAL",
    64  			},
    65  			{
    66  				"CYx2Jtgti3Rc45ZCgHMWxSCVYgivwcy2PKcXDpadJz5M", "2gWhujzcfRtpeZhiXeXoARqzCzBESdKUG5DoAzLCzhSY",
    67  				"MZ53sJMYDDtA9JUTFSqmXmD7s7m6hVW2m",
    68  			},
    69  			{
    70  				"At8qPKFRTPzTBjvEUxWzQ3Sc7B3Ywk5G2tEmrzcMWo8C", "2GBiu2FEtSpxDJ4C8bdCUfTfsqW8eb39M985uB85NbC8",
    71  				"6UU5xChzZFsBHgzFMDga8jnLwSqcS46Ln",
    72  			},
    73  			{
    74  				"7rRY74rDVcjghFP9y8fR15xxmZaHBuZnFrYTXwnmwrnE", "CCs3kZHuXSM7mcH5yrXp5bCzMqDsBztqcHsRvgmJBN7D",
    75  				"MPEnkeoVe8X67yBK9k2AZdXHw4e2udx2X",
    76  			},
    77  		}
    78  
    79  		for _, datum := range data {
    80  			pub1 := base58.Decode(datum[0])
    81  			pub2 := base58.Decode(datum[1])
    82  
    83  			correctNonce := base58.Decode(datum[2])
    84  			testNonce, err := Nonce(pub1, pub2)
    85  
    86  			require.NoError(t, err)
    87  			require.ElementsMatch(t, correctNonce, testNonce[:])
    88  		}
    89  	})
    90  }
    91  
    92  func TestKeyConversion(t *testing.T) {
    93  	t.Run("Test public key conversion", func(t *testing.T) {
    94  		// Test data generated using GoKillers/libsodium
    95  		edPubs := []string{
    96  			"GV28sQUKYSWdkYtu7h46ACGvjbpL7BUv8TZJr5Lukxra",
    97  			"6a58vqYauxsAU2J1dGXNxTDcC6nPyntxM2bh8YWJBwTW",
    98  			"9UKEhZgwcpbvrfxAyy7hzFzYJvtf48EvmjAfcFUZYLNk",
    99  			"5aa4euy5AGiS9JqDeCTgWqUmFmd64ADKergicwoG2jFU",
   100  			"BF3niopmPgYV6xRmTJMMR88ZMnHeJXoCYiQ4Q9qCMpHU",
   101  			"AdHgkKSDMD3YLYzua8yczqFTeLgYdD6W3LR5wjogEUk9",
   102  			"EFnybhqg65JqfankLNKeQb228dkNcVF1c7vzdtvNaz6J",
   103  			"2HbU7ZiZ398b4SvU6b9GVGE4W3UEjYcTStgvNpPb2oUc",
   104  			"2r4M2aL4YDE2Qy6MzLWb3it93Mt84oSGrPNJ9V6VaAF4",
   105  			"CTsYpNjdhK68mjkE4wNrnTVW2qERFNoPXWBnUW9E9bhz",
   106  			"B7PGAJGqfei7cKFvaaF53uPDVmWBkVyLe5UqmrL3GVmF",
   107  			"CJZRRZyhpz29qf8uBfmUWrHa8G3XwEmtYdwiNyT42XeK",
   108  			"3q99EhPvy4ma62BGztcyabGHtX7sZjBtnfkJyE7JJmF8",
   109  			"8HTpnbCjbFxX8Nwx131eHwJU7dsVaupuPKDtsaeF9phe",
   110  			"89JQa4hvQU3Pk7oY8eBYrZp49ZkpVaZWLHSsxjmq9W1b",
   111  			"EVQM6epVv9ZGkAnPJBX7ns8zS3Nf5EKd9iSpbv5aqCRn",
   112  			"CiNPtf1mfdkEYRGCXiCurCNMjydZjuedbPG9kVgy4UyN",
   113  			"DKS6yB7oGMExmAFKiEAYufDevQuAHCq9UtaKJFmSzJ3k",
   114  			"8UwSjbTo3FwMC4CxqqLJBWik67ubBtE8RRC7FvxjA7xU",
   115  			"Ati3h5YVzWUjdrT956dqYh5NXZaDYqRXoHpp5Htcjxoj",
   116  		}
   117  
   118  		curvePubs := []string{
   119  			"8sKzfbhmTnCURJYwTsBvNbXyjNMzKy7kyZNMJe8PspLD",
   120  			"BMKZw7RcyieQHihYMvSGVZ8UFogHjoorWndJnz9L25Hm",
   121  			"D8X75d5pGTiHFYR7m2iqMjQehjd3v8MFejAGP2Mcirzu",
   122  			"3JMxHyQEwnrHhKgemRvCtjp3Z2T9UGVTMXfBKWpNQe3B",
   123  			"3gYHgo7UKQg2CV7RT67yYSRkN7X5UZhL8CTJZefU5i5x",
   124  			"BA1dA5CZuSWPjyGsW94mT2kgUVRBUppiGPkd4M2RdD69",
   125  			"7S8cYe1gd1jWy4DUp3nBVW5TmYXKnGcTMLk3xnhiEp1b",
   126  			"G215pJNTMDWafrDGHenjbAkpWoM3w9dfY5tAbWEx8pyR",
   127  			"37rPhyJfRcZk6v3Lq9kmZJMBUjyvfroniBXMiZdh7PVq",
   128  			"5FmcvFtPrdxD5GJc5P9SpVzvhSNyJDdQGPnrLnzCV64z",
   129  			"73GaEVd6oXEgqPKYmvbFg2jccMG5VLmq65Bn7gS56BRQ",
   130  			"A6W67iKzaohB4LDNa4XsAaHQbbgzdLtmaNPq6TfciRaS",
   131  			"AuANoekPVzVrNn3dbkmvMhRA926Z4rtH96jnksJ26MyK",
   132  			"FD3XcmwNmoZQ1bKfLDyk151kqsh7xVXH5W7GHt9ZJRLJ",
   133  			"5CpahvkR9BTzCtry63UeXgdWJBFdNBXd42trefE6K4jf",
   134  			"3u8zsCK3uipjMrXjpXTWj9me74wLR45YxvN7qmE3aRFb",
   135  			"5u3af9JJQQGSo42wgzeBBfhctoPUYTRXUGCMU64DpqpH",
   136  			"6b2Hc6mxnZTBW9fkNEiDyGNKnt7XuQcbDVzMGoqJfR5Y",
   137  			"DScPyc8TY6pEYWFZxQyhUvUoZEHbEJKxeE23wUS9A6hj",
   138  			"CWY4n7zi6KysyvsVHXtBAW9cQ9esFerxLZfTedfom3mF",
   139  		}
   140  
   141  		for i, edKeyString := range edPubs {
   142  			edKey := base58.Decode(edKeyString)
   143  
   144  			curveKey := base58.Decode(curvePubs[i])
   145  
   146  			convert, err := PublicEd25519toCurve25519(edKey)
   147  			require.NoError(t, err)
   148  			require.ElementsMatch(t, curveKey, convert)
   149  		}
   150  	})
   151  
   152  	t.Run("Test private key conversion", func(t *testing.T) {
   153  		// Test data generated using GoKillers/libsodium
   154  		edPrivs := []string{
   155  			"DH43p5VzoVzDkgkQzgzybE9Z2hK8KevuS9x56A6AEKscHBS3ZXaf6hU4kP6VPM421REUoxCssVeif9XCVaGhURS",
   156  			"42LHpaJQCLs1JNANNqvbD5of5MDgsYEtZ4DXRJCouADyc6BGryHSv5BzHvvtFQu21XtMNP3nz5XRWVk2t8BBPS2b",
   157  			"3KsjZqADnZhKy7gb6FmsaWGYAyyawHvoW4RccUfYYTNjG9y1RN8z6FjTQMEhW3rh93Xs4aEGdbbzs9zi63ovzHQm",
   158  			"37uWqrXwpWK19P7EMNw7kXjMucvPSDuJbwjdZrFynebuFJL165w6hH4ergNWVZyB39atHhBjyK1U6WLMPrUL5FB8",
   159  			"2bXak57U5YKF8j2Ked6JoTPJr1SBs7Fap99vL5EjPS69fqsE1gSmG1KLY3tE58CWaRRDf422dtTaJjpYw1tc3xoj",
   160  			"4kR1WSRU3jzDU4EZgZGaf22a9SkoQcWWdYLRAyVDXMBhDGNc4wnREVtKb4rzZ9q6jP7ANCcsqVS7C4DkQ6AKkbAH",
   161  			"3PRatPTTQ5d7Dj5eaLaRvnbE5J28M5s1PSaZ2THmZuvXRo9Piq6dnuXcdxr39ezceCHoHmGRpPwBfKK8dWb7BVi9",
   162  			"5WTFGmXkxJc2p9XTXMwjrqCkTn1SJMCBAGQ3yDKEm9TVQF3nWwLWRneFrXzDzgBYcUo1Mxj3Ym46BYgqQWgLGSTW",
   163  			"2x4qYudE5J2rA9JQFHTHevn4nYaVJtT3f7RCzqWjZ3726tCUHbgzg74QBSzhGbWVpfQhtaRZ8RsMkcnMjGqNzrj5",
   164  			"2QAb1BPVidRV9dxb1BsZ137vXP1fQb2BbCBWo5fb5Tpj8vV1fU2vVake8JSiMBmU8XfcR8yxDmBezzbGQCKeEUds",
   165  			"5e5yq8BzzZ9MYJxkrFX4DDR6RxvwiFP1TqtimpjeQdRF16Cfq1aqVEnKbh6eRAJd7PZXuq9hMYtwLyqCeMSaaTT4",
   166  			"2tHVLALEobsefPsLprDJb7FPZ5Cj9F6zMU6hiVp9LSu2XP5AqCXxvKB18uwo4PHK8DNaAdkQ78diMtb5NBw3p9km",
   167  			"5YCxw8E9kkt46DKRv5SkJ28ZH3DoMJhSHxpPoRQhPNaP3Ve4pJyaBoKotFvpLwuwi2MwjfDMZbkfBFvQKqudrp8Q",
   168  			"277X3QJjmJVXgULzUJ87bYg76tTfnVbufhRCBJJNqBUP1hqPqueXLCKNfDrdp5atdKp18tLkSKT54yt4ef7ZL393",
   169  			"4k7nC7YKkhmTArmKYXBqQWYXAX4wzhNkaWT5RUTbczaj54iAXdUvcoXUjBgK5J5cWbLp7q55sCnv3SwNSKBbPraP",
   170  			"KKP9gymE9GvMn83LGqth7QuD7Vd4vbzsyLqCYgnnLPJdY8VuzciC4cdCvRpHvmGXXFUxyzNe7V97QBD3AxMXyPY",
   171  			"5oST3U3ffd8iamT7QgadQvyPGh58nr9r4CCiMW45ucV8qupDcmMAUFTLiPCKKYX9tPV69qfe2wQLC9x53tzxBrzd",
   172  			"38LdKpmjmed3AwDbKhCfjopoKTibn1WLVBsrGgnbRaw9mcXqRyoemmQgcnqR2au3DhB9hQFVVdbtqBeRJxyqQgwU",
   173  			"2e3votxseb2bhdKHAx2uzgQWVExkz7eAHLF1KTQYiSpVzv6s9PkSu6nUBa1EqJFoJvPKiE1PxUMPvLm7Wc4mFWQ",
   174  			"2Dt3qPTHyVPE4EpN5c62zTX1DQnJtQgbwXrkAHo6fKJYYgjnEA7Ggqie4ZfCukWG8sTvBLCoWbhQSJtV5PzFqLp9",
   175  		}
   176  
   177  		curvePrivs := []string{
   178  			"3im72Q16HHqfgqziCKR7dKkZF59ZHEGxqc5nfhFqF223",
   179  			"5sa8TDuj2Ha41Khou9yQ8kAVz6bxjwW1idv5QCbWMEcT",
   180  			"6yCAYLdEcyVadtqCbG3ffMx2PqhE1gXfH3enPMwjbLYD",
   181  			"HB3tAasj3hJQVa12EN9a4Ph7GrRyNPDQH1qMKBgZ8s2s",
   182  			"ZyB9Vd461HHHBKzbXFERBMeauPN7smc8MvBRxjvBLyR",
   183  			"9eSnTQusUh1aYdVhfzHUy1F4QLyHh4UpM3yV37vqoE4J",
   184  			"9fhmCLQhwFSffe7J7iD8QUBtJ15szDGnaynD9A91EAby",
   185  			"5KNPvL9JF7fpAEuuvGYRPRFa1KG68MFU5EWyW4wN5twb",
   186  			"81SrqWepV72qmWrCDtyay6y2bb9SLXYDat7oCYBAb5s8",
   187  			"G5besqGYbhA78K5hjV7umf1PyyhXV8HJhUhk6PypeWZw",
   188  			"84QJE1CBi4h8xBhQMqwFwUuCYGLXFB31NBirfD3ENduJ",
   189  			"3ic7MKSCj6YnfsGXwr2VACXC3CKhK5HxX81p17mWH4t6",
   190  			"9gF459xtS64bFcDpaCZc5Utqm5SkUUEauKQxekRJZHbs",
   191  			"4qF59e48Hq6nq7tKkJQcUHia2hD3iXVpjX91bW2sp1qs",
   192  			"DSDoiSRmL41WQ4Q8RNWoPfeXSQGn4vwQB4ii9zt7VTpq",
   193  			"BpaTEPTgUMr1XSjVAqMj2j8Q6ZvSEsmbruGfypu9XPDh",
   194  			"Ajds6dh1M1MTetjLHzrYRXYZmepVUoJnTz9Q8VhhDBPg",
   195  			"25ctY6ghvZV5RqTCdZySZVEfE6MLu4jEmFoTURDsQxDT",
   196  			"1akW9fT6rNkbVPCrP4SpLXUhYYrKK69gnjZHnFk6adf",
   197  			"18BWLZh74cem5mKbPZ7nqbBQ8zgqigg1BppVDEhMV3d",
   198  		}
   199  
   200  		for i, edKeyString := range edPrivs {
   201  			edKey := base58.Decode(edKeyString)
   202  			curveKey := base58.Decode(curvePrivs[i])
   203  
   204  			convert, err := SecretEd25519toCurve25519(edKey)
   205  			require.NoError(t, err)
   206  
   207  			require.ElementsMatch(t, curveKey, convert)
   208  		}
   209  	})
   210  
   211  	t.Run("Fail on converting nil Pub key", func(t *testing.T) {
   212  		_, err := PublicEd25519toCurve25519(nil)
   213  		require.EqualError(t, err, "public key is nil")
   214  	})
   215  
   216  	t.Run("Fail on converting Pub key of incorrect length", func(t *testing.T) {
   217  		_, err := PublicEd25519toCurve25519([]byte{1, 2, 3, 4, 5})
   218  		require.EqualError(t, err, "5-byte key size is invalid")
   219  	})
   220  
   221  	t.Run("Fail on converting nil Priv key", func(t *testing.T) {
   222  		_, err := SecretEd25519toCurve25519(nil)
   223  		require.EqualError(t, err, "private key is nil")
   224  	})
   225  
   226  	t.Run("Fail: invalid pubkey, cannot convert to curve25519", func(t *testing.T) {
   227  		edKeyBytes := base58.Decode("6ZAQ7QpmR9EqhJdwx1jQsjq6nnpehwVqUbhVxiEiYEV7")
   228  		_, err := PublicEd25519toCurve25519(edKeyBytes)
   229  		require.EqualError(t, err, "error converting public key")
   230  	})
   231  }