| 1 | package ecc |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | . "gopkg.in/check.v1" |
| 6 | "io/ioutil" |
| 7 | "math/big" |
| 8 | "crypto/elliptic" |
| 9 | ) |
| 10 | |
| 11 | func Test(t *testing.T) { TestingT(t) } |
| 12 | type TestSuite struct{} |
| 13 | var _ = Suite(&TestSuite{}) |
| 14 | |
| 15 | func (s *TestSuite) TestNewPublic(c *C) { |
| 16 | //given |
| 17 | x:=[]byte {4, 114, 29, 223, 58, 3, 191, 170, 67, 128, 229, 33, 242, 178, 157, 150, 133, 25, 209, 139, 166, 69, 55, 26, 84, 48, 169, 165, 67, 232, 98, 9} |
| 18 | y:=[]byte {131, 116, 8, 14, 22, 150, 18, 75, 24, 181, 159, 78, 90, 51, 71, 159, 214, 186, 250, 47, 207, 246, 142, 127, 54, 183, 72, 72, 253, 21, 88, 53} |
| 19 | |
| 20 | //when |
| 21 | test:=NewPublic(x,y) |
| 22 | |
| 23 | //then |
| 24 | c.Assert(test.X, DeepEquals, bigInt("2010878128539620107131539503221291822343443356718189500659356750794038206985")) |
| 25 | c.Assert(test.Y, DeepEquals, bigInt("59457993017710823357637488495120101390437944162821778556218889662829000218677")) |
| 26 | c.Assert(test.Curve, Equals, elliptic.P256()) |
| 27 | } |
| 28 | |
| 29 | func (s *TestSuite) TestNewPrivate(c *C) { |
| 30 | //given |
| 31 | x:=[]byte {4, 114, 29, 223, 58, 3, 191, 170, 67, 128, 229, 33, 242, 178, 157, 150, 133, 25, 209, 139, 166, 69, 55, 26, 84, 48, 169, 165, 67, 232, 98, 9} |
| 32 | y:=[]byte {131, 116, 8, 14, 22, 150, 18, 75, 24, 181, 159, 78, 90, 51, 71, 159, 214, 186, 250, 47, 207, 246, 142, 127, 54, 183, 72, 72, 253, 21, 88, 53} |
| 33 | d:=[]byte{ 42, 148, 231, 48, 225, 196, 166, 201, 23, 190, 229, 199, 20, 39, 226, 70, 209, 148, 29, 70, 125, 14, 174, 66, 9, 198, 80, 251, 95, 107, 98, 206 } |
| 34 | |
| 35 | //when |
| 36 | test:=NewPrivate(x,y,d) |
| 37 | |
| 38 | //then |
| 39 | c.Assert(test.X, DeepEquals, bigInt("2010878128539620107131539503221291822343443356718189500659356750794038206985")) |
| 40 | c.Assert(test.Y, DeepEquals, bigInt("59457993017710823357637488495120101390437944162821778556218889662829000218677")) |
| 41 | c.Assert(test.D, DeepEquals, bigInt("19260228627344101198652694952536756709538941185117188878548538012226554651342")) |
| 42 | c.Assert(test.Curve, Equals, elliptic.P256()) |
| 43 | } |
| 44 | |
| 45 | func (s *TestSuite) TestReadPublicPKIX(c *C) { |
| 46 | //given |
| 47 | keyBytes, _ := ioutil.ReadFile("./ec_public.key") |
| 48 | |
| 49 | //when |
| 50 | test,e := ReadPublic(keyBytes) |
| 51 | |
| 52 | //then |
| 53 | c.Assert(e, IsNil) |
| 54 | |
| 55 | c.Assert(test.X, DeepEquals, bigInt("76939435694210362824363841832595476784225842365248086547769733757874741672069")) |
| 56 | c.Assert(test.Y, DeepEquals, bigInt("80047042001812490693675653292813886154388201612539715595028491948003157744818")) |
| 57 | c.Assert(test.Curve, Equals, elliptic.P256()) |
| 58 | } |
| 59 | |
| 60 | func (s *TestSuite) TestReadPublicPKCS1(c *C) { |
| 61 | //given |
| 62 | keyBytes, _ := ioutil.ReadFile("./ec_cert.pem") |
| 63 | |
| 64 | //when |
| 65 | test,e := ReadPublic(keyBytes) |
| 66 | |
| 67 | //then |
| 68 | c.Assert(e, IsNil) |
| 69 | |
| 70 | c.Assert(test.X, DeepEquals, bigInt("76939435694210362824363841832595476784225842365248086547769733757874741672069")) |
| 71 | c.Assert(test.Y, DeepEquals, bigInt("80047042001812490693675653292813886154388201612539715595028491948003157744818")) |
| 72 | c.Assert(test.Curve, Equals, elliptic.P256()) |
| 73 | } |
| 74 | |
| 75 | func (s *TestSuite) TestReadPrivatePKCS1(c *C) { |
| 76 | //given |
| 77 | keyBytes, _ := ioutil.ReadFile("./ec_private.key") |
| 78 | |
| 79 | //when |
| 80 | test,e := ReadPrivate(keyBytes) |
| 81 | |
| 82 | //then |
| 83 | c.Assert(e, IsNil) |
| 84 | |
| 85 | c.Assert(test.X, DeepEquals, bigInt("76939435694210362824363841832595476784225842365248086547769733757874741672069")) |
| 86 | c.Assert(test.Y, DeepEquals, bigInt("80047042001812490693675653292813886154388201612539715595028491948003157744818")) |
| 87 | c.Assert(test.D, DeepEquals, bigInt("7222604869653061109880849859470152714201198955914263913554931724612175399644")) |
| 88 | c.Assert(test.Curve, Equals, elliptic.P256()) |
| 89 | } |
| 90 | |
| 91 | func (s *TestSuite) TestReadPrivatePKCS8(c *C) { |
| 92 | //given |
| 93 | keyBytes, _ := ioutil.ReadFile("./ec_private.pem") |
| 94 | |
| 95 | //when |
| 96 | test,e := ReadPrivate(keyBytes) |
| 97 | |
| 98 | //then |
| 99 | c.Assert(e, IsNil) |
| 100 | |
| 101 | c.Assert(test.X, DeepEquals, bigInt("76939435694210362824363841832595476784225842365248086547769733757874741672069")) |
| 102 | c.Assert(test.Y, DeepEquals, bigInt("80047042001812490693675653292813886154388201612539715595028491948003157744818")) |
| 103 | c.Assert(test.D, DeepEquals, bigInt("7222604869653061109880849859470152714201198955914263913554931724612175399644")) |
| 104 | c.Assert(test.Curve, Equals, elliptic.P256()) |
| 105 | } |
| 106 | |
| 107 | //utils |
| 108 | func bigInt(value string) *big.Int { |
| 109 | i:=new (big.Int) |
| 110 | i.SetString(value,10) |
| 111 | |
| 112 | return i |
| 113 | } |