Skip to content

Instantly share code, notes, and snippets.

@jvehent
Created January 11, 2019 21:21
Show Gist options
  • Save jvehent/8d62d817895730b0725d47dacd9c5cfd to your computer and use it in GitHub Desktop.
Save jvehent/8d62d817895730b0725d47dacd9c5cfd to your computer and use it in GitHub Desktop.
2019/01/11 16:19:00 routine 2 make ECDSA Key named "testdup1547241500": &{PKCS11PrivateKey:{PKCS11Object:{Handle:8 Slot:1623786617} PubKey:0xc00011c600}}
&{P:+39402006196394479212279040100143613805079739270465446667948293404245721771496870329047266088258938001861606973112319
N:+39402006196394479212279040100143613805079739270465446667946905279627659399113263569398956308152294913554433653942643
B:+27580193559959705877849011840389048093056905856361568521428707301988689241309860865136260764883745107765439761230575
Gx:+26247035095799689268623156744566981891852923491109213387815615900925518854738050089022388053975719786650872476732087
Gy:+8325710961489029985546751289520108179287853048861315594709205902480503199884419224438643760392947333078086511627871
BitSize:384 Name:P-384}
2019/01/11 16:19:00 routine 0 make ECDSA Key named "testdup1547241500": &{PKCS11PrivateKey:{PKCS11Object:{Handle:9 Slot:1623786617} PubKey:0xc00011c7a0}}
&{P:+39402006196394479212279040100143613805079739270465446667948293404245721771496870329047266088258938001861606973112319
N:+39402006196394479212279040100143613805079739270465446667946905279627659399113263569398956308152294913554433653942643
B:+27580193559959705877849011840389048093056905856361568521428707301988689241309860865136260764883745107765439761230575
Gx:+26247035095799689268623156744566981891852923491109213387815615900925518854738050089022388053975719786650872476732087
Gy:+8325710961489029985546751289520108179287853048861315594709205902480503199884419224438643760392947333078086511627871
BitSize:384 Name:P-384}
2019/01/11 16:19:00 routine 1 make ECDSA Key named "testdup1547241500": &{PKCS11PrivateKey:{PKCS11Object:{Handle:10 Slot:1623786617} PubKey:0xc000104600}}
&{P:+39402006196394479212279040100143613805079739270465446667948293404245721771496870329047266088258938001861606973112319
N:+39402006196394479212279040100143613805079739270465446667946905279627659399113263569398956308152294913554433653942643
B:+27580193559959705877849011840389048093056905856361568521428707301988689241309860865136260764883745107765439761230575
Gx:+26247035095799689268623156744566981891852923491109213387815615900925518854738050089022388053975719786650872476732087
Gy:+8325710961489029985546751289520108179287853048861315594709205902480503199884419224438643760392947333078086511627871
BitSize:384 Name:P-384}
@jvehent
Copy link
Author

jvehent commented Jan 11, 2019

func main() {
        p11Ctx, err := crypto11.Configure(&crypto11.PKCS11Config{
              Path:       "/usr/lib/softhsm/libsofthsm2.so",
              TokenLabel: "test",
              Pin:        "0000",
        })
        if err != nil {
                log.Fatal(err)
        }
        slots, err := p11Ctx.GetSlotList(true)
        if err != nil {
                log.Fatalf("Failed to list PKCS#11 Slots: %s", err.Error())
        }
        if len(slots) < 1 {
                log.Fatal("No slot found")
        }
        keyName := fmt.Sprintf("testdup%d", time.Now().Unix())
        i := 0
        go waitAndMakeKey(slots, i, keyName)
        i++
        go waitAndMakeKey(slots, i, keyName)
        i++
        go waitAndMakeKey(slots, i, keyName)
        time.Sleep(time.Minute)
}

func waitAndMakeKey(slots []uint, i int, keyName string) {
        log.Printf("starting routine %d", i)
        nextTime := time.Now().Truncate(time.Minute)
        nextTime = nextTime.Add(time.Minute)
        time.Sleep(time.Until(nextTime))
        ecdsakey, err := crypto11.GenerateECDSAKeyPairOnSlot(slots[0], []byte(keyName), []byte(keyName), elliptic.P384())
        if err != nil {
                log.Fatal(err)
        }
        log.Printf("routine %d make ECDSA Key named %q: %+v\n%+v", i, keyName, ecdsakey, ecdsakey.Public().(*ecdsa.PublicKey).Params())
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment