Skip to content

Commit 4fe5427

Browse files
v1.5.11
1 parent 0fddb3d commit 4fe5427

1 file changed

Lines changed: 132 additions & 130 deletions

File tree

cmd/edgetk/main.go

Lines changed: 132 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ func main() {
330330
flag.Parse()
331331

332332
if *version {
333-
fmt.Println("EDGE Toolkit v1.5.11 06 Sep 2025")
333+
fmt.Println("EDGE Toolkit v1.5.11 10 Sep 2025")
334334
}
335335

336336
if len(os.Args) < 2 {
@@ -10963,53 +10963,52 @@ Subcommands:
1096310963
return
1096410964
}
1096510965

10966-
var privASN1 PrivateKeyASN1
10967-
if _, err := asn1.Unmarshal(block.Bytes, &privASN1); err == nil {
10968-
priv := &PrivateKey{
10969-
PublicKey: PublicKey{
10970-
P: privASN1.P,
10971-
G: privASN1.G,
10972-
Y: new(big.Int).Exp(privASN1.G, privASN1.X, privASN1.P),
10973-
},
10974-
X: privASN1.X,
10975-
}
10966+
var rawComponents []asn1.RawValue
10967+
if _, err := asn1.Unmarshal(block.Bytes, &rawComponents); err == nil {
10968+
if len(rawComponents) == 3 {
10969+
var privASN1 PrivateKeyASN1
10970+
if _, err := asn1.Unmarshal(block.Bytes, &privASN1); err == nil {
10971+
priv := &PrivateKey{
10972+
PublicKey: PublicKey{
10973+
P: privASN1.P,
10974+
G: privASN1.G,
10975+
Y: new(big.Int).Exp(privASN1.G, privASN1.X, privASN1.P),
10976+
},
10977+
X: privASN1.X,
10978+
}
1097610979

10977-
publicKey := new(big.Int).Exp(privASN1.G, privASN1.X, privASN1.P)
10980+
publicKey := priv.PublicKey.Y
1097810981

10979-
fmt.Print(string(data))
10982+
fmt.Print(string(data))
1098010983

10981-
xval := new(big.Int).Set(priv.X)
10982-
fmt.Println("Private Key (x):")
10983-
x := fmt.Sprintf("%x", xval)
10984-
splitz := SplitSubN(x, 2)
10985-
for _, chunk := range split(strings.Trim(fmt.Sprint(splitz), "[]"), 45) {
10986-
fmt.Printf(" %-10s\n", strings.ReplaceAll(chunk, " ", ":"))
10987-
}
10988-
fmt.Println("Prime (p):")
10989-
p := fmt.Sprintf("%x", priv.P)
10990-
splitz = SplitSubN(p, 2)
10991-
for _, chunk := range split(strings.Trim(fmt.Sprint(splitz), "[]"), 45) {
10992-
fmt.Printf(" %-10s\n", strings.ReplaceAll(chunk, " ", ":"))
10993-
}
10994-
fmt.Println("Subgroup Order (q):")
10995-
q := fmt.Sprintf("%x", priv.Q)
10996-
splitz = SplitSubN(q, 2)
10997-
for _, chunk := range split(strings.Trim(fmt.Sprint(splitz), "[]"), 45) {
10998-
fmt.Printf(" %-10s\n", strings.ReplaceAll(chunk, " ", ":"))
10999-
}
11000-
fmt.Println("Generator (g in the range [2, p-2]):")
11001-
g := fmt.Sprintf("%x", priv.G)
11002-
splitz = SplitSubN(g, 2)
11003-
for _, chunk := range split(strings.Trim(fmt.Sprint(splitz), "[]"), 45) {
11004-
fmt.Printf(" %-10s\n", strings.ReplaceAll(chunk, " ", ":"))
11005-
}
11006-
fmt.Println("Public Key (Y = g^x mod p):")
11007-
pub := fmt.Sprintf("%x", publicKey)
11008-
splitz = SplitSubN(pub, 2)
11009-
for _, chunk := range split(strings.Trim(fmt.Sprint(splitz), "[]"), 45) {
11010-
fmt.Printf(" %-10s\n", strings.ReplaceAll(chunk, " ", ":"))
10984+
xval := new(big.Int).Set(priv.X)
10985+
fmt.Println("Private Key (x):")
10986+
x := fmt.Sprintf("%x", xval)
10987+
splitz := SplitSubN(x, 2)
10988+
for _, chunk := range split(strings.Trim(fmt.Sprint(splitz), "[]"), 45) {
10989+
fmt.Printf(" %-10s\n", strings.ReplaceAll(chunk, " ", ":"))
10990+
}
10991+
fmt.Println("Prime (p):")
10992+
p := fmt.Sprintf("%x", priv.P)
10993+
splitz = SplitSubN(p, 2)
10994+
for _, chunk := range split(strings.Trim(fmt.Sprint(splitz), "[]"), 45) {
10995+
fmt.Printf(" %-10s\n", strings.ReplaceAll(chunk, " ", ":"))
10996+
}
10997+
fmt.Println("Generator (g in the range [2, p-2]):")
10998+
g := fmt.Sprintf("%x", priv.G)
10999+
splitz = SplitSubN(g, 2)
11000+
for _, chunk := range split(strings.Trim(fmt.Sprint(splitz), "[]"), 45) {
11001+
fmt.Printf(" %-10s\n", strings.ReplaceAll(chunk, " ", ":"))
11002+
}
11003+
fmt.Println("Public Key (Y = g^x mod p):")
11004+
pub := fmt.Sprintf("%x", publicKey)
11005+
splitz = SplitSubN(pub, 2)
11006+
for _, chunk := range split(strings.Trim(fmt.Sprint(splitz), "[]"), 45) {
11007+
fmt.Printf(" %-10s\n", strings.ReplaceAll(chunk, " ", ":"))
11008+
}
11009+
os.Exit(0)
11010+
}
1101111011
}
11012-
os.Exit(0)
1101311012
}
1101411013

1101511014
priv, err := readPrivateKeyFromPEM(*key)
@@ -11073,6 +11072,7 @@ Subcommands:
1107311072
}
1107411073
os.Exit(0)
1107511074
}
11075+
1107611076
if *pkey == "text" && *key != "" && blockType == "ELGAMAL PUBLIC KEY" {
1107711077
pemData, err := os.ReadFile(*key)
1107811078
if err != nil {
@@ -11086,42 +11086,41 @@ Subcommands:
1108611086
os.Exit(1)
1108711087
}
1108811088

11089-
var pubASN1 PublicKeyASN1
11090-
if _, err := asn1.Unmarshal(block.Bytes, &pubASN1); err == nil {
11091-
publicKeyVal := &PublicKey{
11092-
P: pubASN1.P,
11093-
G: pubASN1.G,
11094-
Y: pubASN1.Y,
11095-
}
11089+
var rawComponents []asn1.RawValue
11090+
if _, err := asn1.Unmarshal(block.Bytes, &rawComponents); err == nil {
11091+
if len(rawComponents) == 3 {
11092+
var pubASN1 PublicKeyASN1
11093+
if _, err := asn1.Unmarshal(block.Bytes, &pubASN1); err == nil {
11094+
publicKeyVal := &PublicKey{
11095+
P: pubASN1.P,
11096+
G: pubASN1.G,
11097+
Y: pubASN1.Y,
11098+
}
1109611099

11097-
fmt.Print(string(pemData))
11100+
fmt.Print(string(pemData))
1109811101

11099-
fmt.Println("Public Key Parameters:")
11100-
fmt.Println("Prime (p):")
11101-
p := fmt.Sprintf("%x", publicKeyVal.P)
11102-
splitz := SplitSubN(p, 2)
11103-
for _, chunk := range split(strings.Trim(fmt.Sprint(splitz), "[]"), 45) {
11104-
fmt.Printf(" %-10s\n", strings.ReplaceAll(chunk, " ", ":"))
11105-
}
11106-
fmt.Println("Subgroup Order (q):")
11107-
q := fmt.Sprintf("%x", publicKeyVal.Q)
11108-
splitz = SplitSubN(q, 2)
11109-
for _, chunk := range split(strings.Trim(fmt.Sprint(splitz), "[]"), 45) {
11110-
fmt.Printf(" %-10s\n", strings.ReplaceAll(chunk, " ", ":"))
11111-
}
11112-
fmt.Println("Generator (g):")
11113-
g := fmt.Sprintf("%x", publicKeyVal.G)
11114-
splitz = SplitSubN(g, 2)
11115-
for _, chunk := range split(strings.Trim(fmt.Sprint(splitz), "[]"), 45) {
11116-
fmt.Printf(" %-10s\n", strings.ReplaceAll(chunk, " ", ":"))
11117-
}
11118-
fmt.Println("Public Key (Y):")
11119-
y := fmt.Sprintf("%x", publicKeyVal.Y)
11120-
splitz = SplitSubN(y, 2)
11121-
for _, chunk := range split(strings.Trim(fmt.Sprint(splitz), "[]"), 45) {
11122-
fmt.Printf(" %-10s\n", strings.ReplaceAll(chunk, " ", ":"))
11102+
fmt.Println("Public Key Parameters:")
11103+
fmt.Println("Prime (p):")
11104+
p := fmt.Sprintf("%x", publicKeyVal.P)
11105+
splitz := SplitSubN(p, 2)
11106+
for _, chunk := range split(strings.Trim(fmt.Sprint(splitz), "[]"), 45) {
11107+
fmt.Printf(" %-10s\n", strings.ReplaceAll(chunk, " ", ":"))
11108+
}
11109+
fmt.Println("Generator (g):")
11110+
g := fmt.Sprintf("%x", publicKeyVal.G)
11111+
splitz = SplitSubN(g, 2)
11112+
for _, chunk := range split(strings.Trim(fmt.Sprint(splitz), "[]"), 45) {
11113+
fmt.Printf(" %-10s\n", strings.ReplaceAll(chunk, " ", ":"))
11114+
}
11115+
fmt.Println("Public Key (Y):")
11116+
y := fmt.Sprintf("%x", publicKeyVal.Y)
11117+
splitz = SplitSubN(y, 2)
11118+
for _, chunk := range split(strings.Trim(fmt.Sprint(splitz), "[]"), 45) {
11119+
fmt.Printf(" %-10s\n", strings.ReplaceAll(chunk, " ", ":"))
11120+
}
11121+
return
11122+
}
1112311123
}
11124-
return
1112511124
}
1112611125

1112711126
publicKeyVal, err := readPublicKeyFromPEM(*key)
@@ -27895,93 +27894,96 @@ func NewPKCS8Key() PKCS8Key {
2789527894
}
2789627895

2789727896
func (this PKCS8Key) MarshalPublicKey(key *PublicKey) ([]byte, error) {
27898-
var publicKeyBytes []byte
27899-
var err error
27900-
27901-
paramBytes, err := asn1.Marshal(ElGamalParams{
27902-
G: key.G,
27903-
Q: key.Q,
27897+
pubASN1 := struct {
27898+
P *big.Int
27899+
Q *big.Int
27900+
G *big.Int
27901+
Y *big.Int
27902+
}{
2790427903
P: key.P,
27905-
})
27906-
if err != nil {
27907-
return nil, errors.New("elgamal: failed to marshal algo param: " + err.Error())
27904+
Q: key.Q,
27905+
G: key.G,
27906+
Y: key.Y,
2790827907
}
27909-
27910-
publicKeyBytes = append(publicKeyBytes, paramBytes...)
27911-
27912-
yBytes := key.Y.Bytes()
27913-
publicKeyBytes = append(publicKeyBytes, yBytes...)
27914-
27915-
return publicKeyBytes, nil
27908+
return asn1.Marshal(pubASN1)
2791627909
}
2791727910

2791827911
func MarshalPKCS8PublicKey(pub *PublicKey) ([]byte, error) {
2791927912
return NewPKCS8Key().MarshalPublicKey(pub)
2792027913
}
2792127914

2792227915
func (this PKCS8Key) ParsePublicKey(der []byte) (*PublicKey, error) {
27923-
var pubKey PublicKey
27924-
var algoParams ElGamalParams
27916+
var pubASN1 struct {
27917+
P *big.Int
27918+
Q *big.Int
27919+
G *big.Int
27920+
Y *big.Int
27921+
}
2792527922

27926-
rest, err := asn1.Unmarshal(der, &algoParams)
27923+
_, err := asn1.Unmarshal(der, &pubASN1)
2792727924
if err != nil {
2792827925
return nil, err
2792927926
}
2793027927

27931-
pubKey.G = algoParams.G
27932-
pubKey.Q = algoParams.Q
27933-
pubKey.P = algoParams.P
27934-
27935-
pubKey.Y = new(big.Int).SetBytes(rest)
27936-
27937-
return &pubKey, nil
27928+
return &PublicKey{
27929+
P: pubASN1.P,
27930+
Q: pubASN1.Q,
27931+
G: pubASN1.G,
27932+
Y: pubASN1.Y,
27933+
}, nil
2793827934
}
2793927935

2794027936
func ParsePKCS8PublicKey(derBytes []byte) (*PublicKey, error) {
2794127937
return NewPKCS8Key().ParsePublicKey(derBytes)
2794227938
}
2794327939

2794427940
func (this PKCS8Key) MarshalPrivateKey(key *PrivateKey) ([]byte, error) {
27945-
var privateKeyBytes []byte
27946-
var err error
27947-
27948-
paramBytes, err := asn1.Marshal(ElGamalParams{
27949-
G: key.G,
27950-
Q: key.Q,
27951-
P: key.P,
27952-
})
27953-
if err != nil {
27954-
return nil, errors.New("elgamal: failed to marshal algo param: " + err.Error())
27941+
privASN1 := struct {
27942+
Version int
27943+
P *big.Int
27944+
Q *big.Int
27945+
G *big.Int
27946+
Y *big.Int
27947+
X *big.Int
27948+
}{
27949+
Version: 0,
27950+
P: key.P,
27951+
Q: key.Q,
27952+
G: key.G,
27953+
Y: key.Y,
27954+
X: key.X,
2795527955
}
27956-
27957-
privateKeyBytes = append(privateKeyBytes, paramBytes...)
27958-
27959-
xBytes := key.X.Bytes()
27960-
privateKeyBytes = append(privateKeyBytes, xBytes...)
27961-
27962-
return privateKeyBytes, nil
27956+
return asn1.Marshal(privASN1)
2796327957
}
2796427958

2796527959
func MarshalPKCS8PrivateKey(key *PrivateKey) ([]byte, error) {
2796627960
return NewPKCS8Key().MarshalPrivateKey(key)
2796727961
}
2796827962

2796927963
func (this PKCS8Key) ParsePrivateKey(der []byte) (key *PrivateKey, err error) {
27970-
var privKey PrivateKey
27971-
var algoParams ElGamalParams
27964+
var privASN1 struct {
27965+
Version int
27966+
P *big.Int
27967+
Q *big.Int
27968+
G *big.Int
27969+
Y *big.Int
27970+
X *big.Int
27971+
}
2797227972

27973-
rest, err := asn1.Unmarshal(der, &algoParams)
27973+
_, err = asn1.Unmarshal(der, &privASN1)
2797427974
if err != nil {
2797527975
return nil, err
2797627976
}
2797727977

27978-
privKey.G = algoParams.G
27979-
privKey.Q = algoParams.Q
27980-
privKey.P = algoParams.P
27981-
27982-
privKey.X = new(big.Int).SetBytes(rest)
27983-
27984-
return &privKey, nil
27978+
return &PrivateKey{
27979+
PublicKey: PublicKey{
27980+
P: privASN1.P,
27981+
Q: privASN1.Q,
27982+
G: privASN1.G,
27983+
Y: privASN1.Y,
27984+
},
27985+
X: privASN1.X,
27986+
}, nil
2798527987
}
2798627988

2798727989
func ParsePKCS8PrivateKey(derBytes []byte) (key *PrivateKey, err error) {

0 commit comments

Comments
 (0)