github.com/zntrio/harp/v2@v2.0.9/pkg/sdk/security/crypto/rfc6979/ecdsa_test.go (about)

     1  // Licensed to Elasticsearch B.V. under one or more contributor
     2  // license agreements. See the NOTICE file distributed with
     3  // this work for additional information regarding copyright
     4  // ownership. Elasticsearch B.V. licenses this file to you under
     5  // the Apache License, Version 2.0 (the "License"); you may
     6  // not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package rfc6979_test
    19  
    20  // From https://github.com/codahale/rfc6979
    21  
    22  import (
    23  	"crypto/ecdsa"
    24  	"crypto/elliptic"
    25  	"crypto/sha1"
    26  	"crypto/sha256"
    27  	"crypto/sha512"
    28  	"hash"
    29  	"math/big"
    30  	"testing"
    31  
    32  	"github.com/zntrio/harp/v2/pkg/sdk/security/crypto/rfc6979"
    33  )
    34  
    35  type ecdsaFixture struct {
    36  	name    string
    37  	key     *ecdsaKey
    38  	alg     func() hash.Hash
    39  	message string
    40  	r, s    string
    41  }
    42  
    43  type ecdsaKey struct {
    44  	key      *ecdsa.PrivateKey
    45  	subgroup int
    46  }
    47  
    48  var p224 = &ecdsaKey{
    49  	key: &ecdsa.PrivateKey{
    50  		PublicKey: ecdsa.PublicKey{
    51  			Curve: elliptic.P224(),
    52  			X:     ecdsaLoadInt("00CF08DA5AD719E42707FA431292DEA11244D64FC51610D94B130D6C"),
    53  			Y:     ecdsaLoadInt("EEAB6F3DEBE455E3DBF85416F7030CBD94F34F2D6F232C69F3C1385A"),
    54  		},
    55  		D: ecdsaLoadInt("F220266E1105BFE3083E03EC7A3A654651F45E37167E88600BF257C1"),
    56  	},
    57  	subgroup: 224,
    58  }
    59  
    60  var p256 = &ecdsaKey{
    61  	key: &ecdsa.PrivateKey{
    62  		PublicKey: ecdsa.PublicKey{
    63  			Curve: elliptic.P256(),
    64  			X:     ecdsaLoadInt("60FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6"),
    65  			Y:     ecdsaLoadInt("7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299"),
    66  		},
    67  		D: ecdsaLoadInt("C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721"),
    68  	},
    69  	subgroup: 256,
    70  }
    71  
    72  var p384 = &ecdsaKey{
    73  	key: &ecdsa.PrivateKey{
    74  		PublicKey: ecdsa.PublicKey{
    75  			Curve: elliptic.P384(),
    76  			X:     ecdsaLoadInt("EC3A4E415B4E19A4568618029F427FA5DA9A8BC4AE92E02E06AAE5286B300C64DEF8F0EA9055866064A254515480BC13"),
    77  			Y:     ecdsaLoadInt("8015D9B72D7D57244EA8EF9AC0C621896708A59367F9DFB9F54CA84B3F1C9DB1288B231C3AE0D4FE7344FD2533264720"),
    78  		},
    79  		D: ecdsaLoadInt("6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D896D5724E4C70A825F872C9EA60D2EDF5"),
    80  	},
    81  	subgroup: 384,
    82  }
    83  
    84  var p521 = &ecdsaKey{
    85  	key: &ecdsa.PrivateKey{
    86  		PublicKey: ecdsa.PublicKey{
    87  			Curve: elliptic.P521(),
    88  			X:     ecdsaLoadInt("1894550D0785932E00EAA23B694F213F8C3121F86DC97A04E5A7167DB4E5BCD371123D46E45DB6B5D5370A7F20FB633155D38FFA16D2BD761DCAC474B9A2F5023A4"),
    89  			Y:     ecdsaLoadInt("0493101C962CD4D2FDDF782285E64584139C2F91B47F87FF82354D6630F746A28A0DB25741B5B34A828008B22ACC23F924FAAFBD4D33F81EA66956DFEAA2BFDFCF5"),
    90  		},
    91  		D: ecdsaLoadInt("0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75CAA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83538"),
    92  	},
    93  	subgroup: 521,
    94  }
    95  
    96  var fixtures = []ecdsaFixture{
    97  	// ECDSA, 224 Bits (Prime Field)
    98  	// https://tools.ietf.org/html/rfc6979#appendix-A.2.4
    99  	{
   100  		name:    "P224/SHA-1 #1",
   101  		key:     p224,
   102  		alg:     sha1.New,
   103  		message: "sample",
   104  		r:       "22226F9D40A96E19C4A301CE5B74B115303C0F3A4FD30FC257FB57AC",
   105  		s:       "66D1CDD83E3AF75605DD6E2FEFF196D30AA7ED7A2EDF7AF475403D69",
   106  	},
   107  	{
   108  		name:    "P224/SHA-224 #1",
   109  		key:     p224,
   110  		alg:     sha256.New224,
   111  		message: "sample",
   112  		r:       "1CDFE6662DDE1E4A1EC4CDEDF6A1F5A2FB7FBD9145C12113E6ABFD3E",
   113  		s:       "A6694FD7718A21053F225D3F46197CA699D45006C06F871808F43EBC",
   114  	},
   115  	{
   116  		name:    "P224/SHA-256 #1",
   117  		key:     p224,
   118  		alg:     sha256.New,
   119  		message: "sample",
   120  		r:       "61AA3DA010E8E8406C656BC477A7A7189895E7E840CDFE8FF42307BA",
   121  		s:       "BC814050DAB5D23770879494F9E0A680DC1AF7161991BDE692B10101",
   122  	},
   123  	{
   124  		name:    "P224/SHA-384 #1",
   125  		key:     p224,
   126  		alg:     sha512.New384,
   127  		message: "sample",
   128  		r:       "0B115E5E36F0F9EC81F1325A5952878D745E19D7BB3EABFABA77E953",
   129  		s:       "830F34CCDFE826CCFDC81EB4129772E20E122348A2BBD889A1B1AF1D",
   130  	},
   131  	{
   132  		name:    "P224/SHA-512 #1",
   133  		key:     p224,
   134  		alg:     sha512.New,
   135  		message: "sample",
   136  		r:       "074BD1D979D5F32BF958DDC61E4FB4872ADCAFEB2256497CDAC30397",
   137  		s:       "A4CECA196C3D5A1FF31027B33185DC8EE43F288B21AB342E5D8EB084",
   138  	},
   139  	{
   140  		name:    "P224/SHA-1 #2",
   141  		key:     p224,
   142  		alg:     sha1.New,
   143  		message: "test",
   144  		r:       "DEAA646EC2AF2EA8AD53ED66B2E2DDAA49A12EFD8356561451F3E21C",
   145  		s:       "95987796F6CF2062AB8135271DE56AE55366C045F6D9593F53787BD2",
   146  	},
   147  	{
   148  		name:    "P224/SHA-224 #2",
   149  		key:     p224,
   150  		alg:     sha256.New224,
   151  		message: "test",
   152  		r:       "C441CE8E261DED634E4CF84910E4C5D1D22C5CF3B732BB204DBEF019",
   153  		s:       "902F42847A63BDC5F6046ADA114953120F99442D76510150F372A3F4",
   154  	},
   155  	{
   156  		name:    "P224/SHA-256 #2",
   157  		key:     p224,
   158  		alg:     sha256.New,
   159  		message: "test",
   160  		r:       "AD04DDE87B84747A243A631EA47A1BA6D1FAA059149AD2440DE6FBA6",
   161  		s:       "178D49B1AE90E3D8B629BE3DB5683915F4E8C99FDF6E666CF37ADCFD",
   162  	},
   163  	{
   164  		name:    "P224/SHA-384 #2",
   165  		key:     p224,
   166  		alg:     sha512.New384,
   167  		message: "test",
   168  		r:       "389B92682E399B26518A95506B52C03BC9379A9DADF3391A21FB0EA4",
   169  		s:       "414A718ED3249FF6DBC5B50C27F71F01F070944DA22AB1F78F559AAB",
   170  	},
   171  	{
   172  		name:    "P224/SHA-512 #2",
   173  		key:     p224,
   174  		alg:     sha512.New,
   175  		message: "test",
   176  		r:       "049F050477C5ADD858CAC56208394B5A55BAEBBE887FDF765047C17C",
   177  		s:       "077EB13E7005929CEFA3CD0403C7CDCC077ADF4E44F3C41B2F60ECFF",
   178  	},
   179  	// ECDSA, 256 Bits (Prime Field)
   180  	// https://tools.ietf.org/html/rfc6979#appendix-A.2.5
   181  	{
   182  		name:    "P256/SHA-1 #1",
   183  		key:     p256,
   184  		alg:     sha1.New,
   185  		message: "sample",
   186  		r:       "61340C88C3AAEBEB4F6D667F672CA9759A6CCAA9FA8811313039EE4A35471D32",
   187  		s:       "6D7F147DAC089441BB2E2FE8F7A3FA264B9C475098FDCF6E00D7C996E1B8B7EB",
   188  	},
   189  	{
   190  		name:    "P256/SHA-224 #1",
   191  		key:     p256,
   192  		alg:     sha256.New224,
   193  		message: "sample",
   194  		r:       "53B2FFF5D1752B2C689DF257C04C40A587FABABB3F6FC2702F1343AF7CA9AA3F",
   195  		s:       "B9AFB64FDC03DC1A131C7D2386D11E349F070AA432A4ACC918BEA988BF75C74C",
   196  	},
   197  	{
   198  		name:    "P256/SHA-256 #1",
   199  		key:     p256,
   200  		alg:     sha256.New,
   201  		message: "sample",
   202  		r:       "EFD48B2AACB6A8FD1140DD9CD45E81D69D2C877B56AAF991C34D0EA84EAF3716",
   203  		s:       "F7CB1C942D657C41D436C7A1B6E29F65F3E900DBB9AFF4064DC4AB2F843ACDA8",
   204  	},
   205  	{
   206  		name:    "P256/SHA-384 #1",
   207  		key:     p256,
   208  		alg:     sha512.New384,
   209  		message: "sample",
   210  		r:       "0EAFEA039B20E9B42309FB1D89E213057CBF973DC0CFC8F129EDDDC800EF7719",
   211  		s:       "4861F0491E6998B9455193E34E7B0D284DDD7149A74B95B9261F13ABDE940954",
   212  	},
   213  	{
   214  		name:    "P256/SHA-512 #1",
   215  		key:     p256,
   216  		alg:     sha512.New,
   217  		message: "sample",
   218  		r:       "8496A60B5E9B47C825488827E0495B0E3FA109EC4568FD3F8D1097678EB97F00",
   219  		s:       "2362AB1ADBE2B8ADF9CB9EDAB740EA6049C028114F2460F96554F61FAE3302FE",
   220  	},
   221  	{
   222  		name:    "P256/SHA-1 #2",
   223  		key:     p256,
   224  		alg:     sha1.New,
   225  		message: "test",
   226  		r:       "0CBCC86FD6ABD1D99E703E1EC50069EE5C0B4BA4B9AC60E409E8EC5910D81A89",
   227  		s:       "01B9D7B73DFAA60D5651EC4591A0136F87653E0FD780C3B1BC872FFDEAE479B1",
   228  	},
   229  	{
   230  		name:    "P256/SHA-224 #2",
   231  		key:     p256,
   232  		alg:     sha256.New224,
   233  		message: "test",
   234  		r:       "C37EDB6F0AE79D47C3C27E962FA269BB4F441770357E114EE511F662EC34A692",
   235  		s:       "C820053A05791E521FCAAD6042D40AEA1D6B1A540138558F47D0719800E18F2D",
   236  	},
   237  	{
   238  		name:    "P256/SHA-256 #2",
   239  		key:     p256,
   240  		alg:     sha256.New,
   241  		message: "test",
   242  		r:       "F1ABB023518351CD71D881567B1EA663ED3EFCF6C5132B354F28D3B0B7D38367",
   243  		s:       "019F4113742A2B14BD25926B49C649155F267E60D3814B4C0CC84250E46F0083",
   244  	},
   245  	{
   246  		name:    "P256/SHA-384 #2",
   247  		key:     p256,
   248  		alg:     sha512.New384,
   249  		message: "test",
   250  		r:       "83910E8B48BB0C74244EBDF7F07A1C5413D61472BD941EF3920E623FBCCEBEB6",
   251  		s:       "8DDBEC54CF8CD5874883841D712142A56A8D0F218F5003CB0296B6B509619F2C",
   252  	},
   253  	{
   254  		name:    "P256/SHA-512 #2",
   255  		key:     p256,
   256  		alg:     sha512.New,
   257  		message: "test",
   258  		r:       "461D93F31B6540894788FD206C07CFA0CC35F46FA3C91816FFF1040AD1581A04",
   259  		s:       "39AF9F15DE0DB8D97E72719C74820D304CE5226E32DEDAE67519E840D1194E55",
   260  	},
   261  	// ECDSA, 384 Bits (Prime Field)
   262  	// https://tools.ietf.org/html/rfc6979#appendix-A.2.6
   263  	{
   264  		name:    "P384/SHA-1 #1",
   265  		key:     p384,
   266  		alg:     sha1.New,
   267  		message: "sample",
   268  		r:       "EC748D839243D6FBEF4FC5C4859A7DFFD7F3ABDDF72014540C16D73309834FA37B9BA002899F6FDA3A4A9386790D4EB2",
   269  		s:       "A3BCFA947BEEF4732BF247AC17F71676CB31A847B9FF0CBC9C9ED4C1A5B3FACF26F49CA031D4857570CCB5CA4424A443",
   270  	},
   271  	{
   272  		name:    "P384/SHA-224 #1",
   273  		key:     p384,
   274  		alg:     sha256.New224,
   275  		message: "sample",
   276  		r:       "42356E76B55A6D9B4631C865445DBE54E056D3B3431766D0509244793C3F9366450F76EE3DE43F5A125333A6BE060122",
   277  		s:       "9DA0C81787064021E78DF658F2FBB0B042BF304665DB721F077A4298B095E4834C082C03D83028EFBF93A3C23940CA8D",
   278  	},
   279  	{
   280  		name:    "P384/SHA-256 #1",
   281  		key:     p384,
   282  		alg:     sha256.New,
   283  		message: "sample",
   284  		r:       "21B13D1E013C7FA1392D03C5F99AF8B30C570C6F98D4EA8E354B63A21D3DAA33BDE1E888E63355D92FA2B3C36D8FB2CD",
   285  		s:       "F3AA443FB107745BF4BD77CB3891674632068A10CA67E3D45DB2266FA7D1FEEBEFDC63ECCD1AC42EC0CB8668A4FA0AB0",
   286  	},
   287  	{
   288  		name:    "P384/SHA-384 #1",
   289  		key:     p384,
   290  		alg:     sha512.New384,
   291  		message: "sample",
   292  		r:       "94EDBB92A5ECB8AAD4736E56C691916B3F88140666CE9FA73D64C4EA95AD133C81A648152E44ACF96E36DD1E80FABE46",
   293  		s:       "99EF4AEB15F178CEA1FE40DB2603138F130E740A19624526203B6351D0A3A94FA329C145786E679E7B82C71A38628AC8",
   294  	},
   295  	{
   296  		name:    "P384/SHA-512 #1",
   297  		key:     p384,
   298  		alg:     sha512.New,
   299  		message: "sample",
   300  		r:       "ED0959D5880AB2D869AE7F6C2915C6D60F96507F9CB3E047C0046861DA4A799CFE30F35CC900056D7C99CD7882433709",
   301  		s:       "512C8CCEEE3890A84058CE1E22DBC2198F42323CE8ACA9135329F03C068E5112DC7CC3EF3446DEFCEB01A45C2667FDD5",
   302  	},
   303  	{
   304  		name:    "P384/SHA-1 #2",
   305  		key:     p384,
   306  		alg:     sha1.New,
   307  		message: "test",
   308  		r:       "4BC35D3A50EF4E30576F58CD96CE6BF638025EE624004A1F7789A8B8E43D0678ACD9D29876DAF46638645F7F404B11C7",
   309  		s:       "D5A6326C494ED3FF614703878961C0FDE7B2C278F9A65FD8C4B7186201A2991695BA1C84541327E966FA7B50F7382282",
   310  	},
   311  	{
   312  		name:    "P384/SHA-224 #2",
   313  		key:     p384,
   314  		alg:     sha256.New224,
   315  		message: "test",
   316  		r:       "E8C9D0B6EA72A0E7837FEA1D14A1A9557F29FAA45D3E7EE888FC5BF954B5E62464A9A817C47FF78B8C11066B24080E72",
   317  		s:       "07041D4A7A0379AC7232FF72E6F77B6DDB8F09B16CCE0EC3286B2BD43FA8C6141C53EA5ABEF0D8231077A04540A96B66",
   318  	},
   319  	{
   320  		name:    "P384/SHA-256 #2",
   321  		key:     p384,
   322  		alg:     sha256.New,
   323  		message: "test",
   324  		r:       "6D6DEFAC9AB64DABAFE36C6BF510352A4CC27001263638E5B16D9BB51D451559F918EEDAF2293BE5B475CC8F0188636B",
   325  		s:       "2D46F3BECBCC523D5F1A1256BF0C9B024D879BA9E838144C8BA6BAEB4B53B47D51AB373F9845C0514EEFB14024787265",
   326  	},
   327  	{
   328  		name:    "P384/SHA-384 #2",
   329  		key:     p384,
   330  		alg:     sha512.New384,
   331  		message: "test",
   332  		r:       "8203B63D3C853E8D77227FB377BCF7B7B772E97892A80F36AB775D509D7A5FEB0542A7F0812998DA8F1DD3CA3CF023DB",
   333  		s:       "DDD0760448D42D8A43AF45AF836FCE4DE8BE06B485E9B61B827C2F13173923E06A739F040649A667BF3B828246BAA5A5",
   334  	},
   335  	{
   336  		name:    "P384/SHA-512 #2",
   337  		key:     p384,
   338  		alg:     sha512.New,
   339  		message: "test",
   340  		r:       "A0D5D090C9980FAF3C2CE57B7AE951D31977DD11C775D314AF55F76C676447D06FB6495CD21B4B6E340FC236584FB277",
   341  		s:       "976984E59B4C77B0E8E4460DCA3D9F20E07B9BB1F63BEEFAF576F6B2E8B224634A2092CD3792E0159AD9CEE37659C736",
   342  	},
   343  	// ECDSA, 521 Bits (Prime Field)
   344  	// https://tools.ietf.org/html/rfc6979#appendix-A.2.7
   345  	{
   346  		name:    "P521/SHA-1 #1",
   347  		key:     p521,
   348  		alg:     sha1.New,
   349  		message: "sample",
   350  		r:       "0343B6EC45728975EA5CBA6659BBB6062A5FF89EEA58BE3C80B619F322C87910FE092F7D45BB0F8EEE01ED3F20BABEC079D202AE677B243AB40B5431D497C55D75D",
   351  		s:       "0E7B0E675A9B24413D448B8CC119D2BF7B2D2DF032741C096634D6D65D0DBE3D5694625FB9E8104D3B842C1B0E2D0B98BEA19341E8676AEF66AE4EBA3D5475D5D16",
   352  	},
   353  	{
   354  		name:    "P521/SHA-224 #1",
   355  		key:     p521,
   356  		alg:     sha256.New224,
   357  		message: "sample",
   358  		r:       "1776331CFCDF927D666E032E00CF776187BC9FDD8E69D0DABB4109FFE1B5E2A30715F4CC923A4A5E94D2503E9ACFED92857B7F31D7152E0F8C00C15FF3D87E2ED2E",
   359  		s:       "050CB5265417FE2320BBB5A122B8E1A32BD699089851128E360E620A30C7E17BA41A666AF126CE100E5799B153B60528D5300D08489CA9178FB610A2006C254B41F",
   360  	},
   361  	{
   362  		name:    "P521/SHA-256 #1",
   363  		key:     p521,
   364  		alg:     sha256.New,
   365  		message: "sample",
   366  		r:       "1511BB4D675114FE266FC4372B87682BAECC01D3CC62CF2303C92B3526012659D16876E25C7C1E57648F23B73564D67F61C6F14D527D54972810421E7D87589E1A7",
   367  		s:       "04A171143A83163D6DF460AAF61522695F207A58B95C0644D87E52AA1A347916E4F7A72930B1BC06DBE22CE3F58264AFD23704CBB63B29B931F7DE6C9D949A7ECFC",
   368  	},
   369  	{
   370  		name:    "P521/SHA-384 #1",
   371  		key:     p521,
   372  		alg:     sha512.New384,
   373  		message: "sample",
   374  		r:       "1EA842A0E17D2DE4F92C15315C63DDF72685C18195C2BB95E572B9C5136CA4B4B576AD712A52BE9730627D16054BA40CC0B8D3FF035B12AE75168397F5D50C67451",
   375  		s:       "1F21A3CEE066E1961025FB048BD5FE2B7924D0CD797BABE0A83B66F1E35EEAF5FDE143FA85DC394A7DEE766523393784484BDF3E00114A1C857CDE1AA203DB65D61",
   376  	},
   377  	{
   378  		name:    "P521/SHA-512 #1",
   379  		key:     p521,
   380  		alg:     sha512.New,
   381  		message: "sample",
   382  		r:       "0C328FAFCBD79DD77850370C46325D987CB525569FB63C5D3BC53950E6D4C5F174E25A1EE9017B5D450606ADD152B534931D7D4E8455CC91F9B15BF05EC36E377FA",
   383  		s:       "0617CCE7CF5064806C467F678D3B4080D6F1CC50AF26CA209417308281B68AF282623EAA63E5B5C0723D8B8C37FF0777B1A20F8CCB1DCCC43997F1EE0E44DA4A67A",
   384  	},
   385  	{
   386  		name:    "P521/SHA-1 #2",
   387  		key:     p521,
   388  		alg:     sha1.New,
   389  		message: "test",
   390  		r:       "13BAD9F29ABE20DE37EBEB823C252CA0F63361284015A3BF430A46AAA80B87B0693F0694BD88AFE4E661FC33B094CD3B7963BED5A727ED8BD6A3A202ABE009D0367",
   391  		s:       "1E9BB81FF7944CA409AD138DBBEE228E1AFCC0C890FC78EC8604639CB0DBDC90F717A99EAD9D272855D00162EE9527567DD6A92CBD629805C0445282BBC916797FF",
   392  	},
   393  	{
   394  		name:    "P521/SHA-224 #2",
   395  		key:     p521,
   396  		alg:     sha256.New224,
   397  		message: "test",
   398  		r:       "1C7ED902E123E6815546065A2C4AF977B22AA8EADDB68B2C1110E7EA44D42086BFE4A34B67DDC0E17E96536E358219B23A706C6A6E16BA77B65E1C595D43CAE17FB",
   399  		s:       "177336676304FCB343CE028B38E7B4FBA76C1C1B277DA18CAD2A8478B2A9A9F5BEC0F3BA04F35DB3E4263569EC6AADE8C92746E4C82F8299AE1B8F1739F8FD519A4",
   400  	},
   401  	{
   402  		name:    "P521/SHA-256 #2",
   403  		key:     p521,
   404  		alg:     sha256.New,
   405  		message: "test",
   406  		r:       "00E871C4A14F993C6C7369501900C4BC1E9C7B0B4BA44E04868B30B41D8071042EB28C4C250411D0CE08CD197E4188EA4876F279F90B3D8D74A3C76E6F1E4656AA8",
   407  		s:       "0CD52DBAA33B063C3A6CD8058A1FB0A46A4754B034FCC644766CA14DA8CA5CA9FDE00E88C1AD60CCBA759025299079D7A427EC3CC5B619BFBC828E7769BCD694E86",
   408  	},
   409  	{
   410  		name:    "P521/SHA-384 #2",
   411  		key:     p521,
   412  		alg:     sha512.New384,
   413  		message: "test",
   414  		r:       "14BEE21A18B6D8B3C93FAB08D43E739707953244FDBE924FA926D76669E7AC8C89DF62ED8975C2D8397A65A49DCC09F6B0AC62272741924D479354D74FF6075578C",
   415  		s:       "133330865C067A0EAF72362A65E2D7BC4E461E8C8995C3B6226A21BD1AA78F0ED94FE536A0DCA35534F0CD1510C41525D163FE9D74D134881E35141ED5E8E95B979",
   416  	},
   417  	{
   418  		name:    "P521/SHA-512 #2",
   419  		key:     p521,
   420  		alg:     sha512.New,
   421  		message: "test",
   422  		r:       "13E99020ABF5CEE7525D16B69B229652AB6BDF2AFFCAEF38773B4B7D08725F10CDB93482FDCC54EDCEE91ECA4166B2A7C6265EF0CE2BD7051B7CEF945BABD47EE6D",
   423  		s:       "1FBD0013C674AA79CB39849527916CE301C66EA7CE8B80682786AD60F98F7E78A19CA69EFF5C57400E3B3A0AD66CE0978214D13BAF4E9AC60752F7B155E2DE4DCE3",
   424  	},
   425  }
   426  
   427  func TestECDSA(t *testing.T) {
   428  	for _, f := range fixtures {
   429  		testEcsaFixture(&f, t)
   430  	}
   431  }
   432  
   433  func ecdsaLoadInt(s string) (n *big.Int) {
   434  	n, _ = new(big.Int).SetString(s, 16)
   435  	return
   436  }
   437  
   438  func testEcsaFixture(f *ecdsaFixture, t *testing.T) {
   439  	t.Logf("Testing %s", f.name)
   440  
   441  	h := f.alg()
   442  	h.Write([]byte(f.message))
   443  	digest := h.Sum(nil)
   444  
   445  	g := f.key.subgroup / 8
   446  	if len(digest) > g {
   447  		digest = digest[0:g]
   448  	}
   449  
   450  	r, s := rfc6979.SignECDSA(f.key.key, digest, f.alg)
   451  	expectedR := ecdsaLoadInt(f.r)
   452  	expectedS := ecdsaLoadInt(f.s)
   453  
   454  	if r.Cmp(expectedR) != 0 {
   455  		t.Errorf("%s: Expected R of %X, got %X", f.name, expectedR, r)
   456  	}
   457  
   458  	if s.Cmp(expectedS) != 0 {
   459  		t.Errorf("%s: Expected S of %X, got %X", f.name, expectedS, s)
   460  	}
   461  }