-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
338 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
//go:build windows | ||
// +build windows | ||
|
||
package cng | ||
|
||
import ( | ||
"errors" | ||
"runtime" | ||
"unsafe" | ||
|
||
"github.com/microsoft/go-crypto-winnative/internal/bcrypt" | ||
) | ||
|
||
type dsaAlgorithm struct { | ||
handle bcrypt.ALG_HANDLE | ||
} | ||
|
||
func loadDSA() (h dsaAlgorithm, err error) { | ||
v, err := loadOrStoreAlg(bcrypt.DSA_ALGORITHM, bcrypt.ALG_NONE_FLAG, "", func(h bcrypt.ALG_HANDLE) (interface{}, error) { | ||
return dsaAlgorithm{h}, nil | ||
}) | ||
if err != nil { | ||
return dsaAlgorithm{}, err | ||
} | ||
return v.(dsaAlgorithm), nil | ||
} | ||
|
||
type DSAParameters struct { | ||
P, Q, G, S BigInt | ||
} | ||
|
||
func GenerateDSAParameters(l int) (DSAParameters, error) { | ||
h, err := loadDSA() | ||
if err != nil { | ||
return DSAParameters{}, err | ||
} | ||
var hkey bcrypt.KEY_HANDLE | ||
if err := bcrypt.GenerateKeyPair(h.handle, &hkey, uint32(l), 0); err != nil { | ||
return DSAParameters{}, err | ||
} | ||
defer bcrypt.DestroyKey(hkey) | ||
|
||
if err := bcrypt.FinalizeKeyPair(hkey, 0); err != nil { | ||
return DSAParameters{}, err | ||
} | ||
if l <= 1024 { | ||
hdr, data, err := exporDSAKey(hkey, false) | ||
if err != nil { | ||
return DSAParameters{}, err | ||
} | ||
if hdr.Magic != bcrypt.DSA_PUBLIC_MAGIC || hdr.KeySize*8 != uint32(l) { | ||
return DSAParameters{}, errors.New("crypto/dsa: exported key is corrupted") | ||
} | ||
consumeBigInt := func(size uint32) BigInt { | ||
b := data[:size] | ||
data = data[size:] | ||
return b | ||
} | ||
return DSAParameters{ | ||
S: hdr.Seed[:], | ||
Q: hdr.Q[:], | ||
P: consumeBigInt(hdr.KeySize), | ||
G: consumeBigInt(hdr.KeySize), | ||
}, nil | ||
} | ||
hdr, data, err := exporDSAV2Key(hkey, false) | ||
if err != nil { | ||
return DSAParameters{}, err | ||
} | ||
if hdr.Magic != bcrypt.DSA_PUBLIC_MAGIC_V2 || hdr.KeySize*8 != uint32(l) { | ||
return DSAParameters{}, errors.New("crypto/dsa: exported key is corrupted") | ||
} | ||
consumeBigInt := func(size uint32) BigInt { | ||
b := data[:size] | ||
data = data[size:] | ||
return b | ||
} | ||
return DSAParameters{ | ||
S: consumeBigInt(hdr.SeedLength), | ||
Q: consumeBigInt(hdr.GroupSize), | ||
P: consumeBigInt(hdr.KeySize), | ||
G: consumeBigInt(hdr.KeySize), | ||
}, nil | ||
} | ||
|
||
type DSAPrivateKey struct { | ||
hkey bcrypt.KEY_HANDLE | ||
} | ||
|
||
func (k *DSAPrivateKey) finalize() { | ||
bcrypt.DestroyKey(k.hkey) | ||
} | ||
|
||
func GenerateKey(params DSAParameters) (*DSAPrivateKey, error) { | ||
h, err := loadDSA() | ||
if err != nil { | ||
return nil, err | ||
} | ||
keySize := uint32(len(params.P)) | ||
groupSize := uint32(len(params.Q)) | ||
seedSize := uint32(len(params.S)) | ||
var hkey bcrypt.KEY_HANDLE | ||
if err := bcrypt.GenerateKeyPair(h.handle, &hkey, keySize*8, 0); err != nil { | ||
return nil, err | ||
} | ||
var blob []byte | ||
if keySize*8 <= 1024 { | ||
blob = make([]byte, sizeOfDSAParamsHeader+keySize*2) | ||
hdr := bcrypt.DSA_PARAMETER_HEADER{ | ||
Length: uint32(len(blob)), | ||
Magic: bcrypt.DSA_PARAMETERS_MAGIC_V2, | ||
KeySize: keySize, | ||
//Count: , | ||
} | ||
copy(hdr.Seed[:], params.S[:]) | ||
copy(hdr.Q[:], params.Q[:]) | ||
copy(blob, (*(*[sizeOfDSAParamsHeader]byte)(unsafe.Pointer(&hdr)))[:]) | ||
i := int(sizeOfDSAParamsHeader) | ||
copy(blob[i:], params.P[:]) | ||
i += len(params.P) | ||
copy(blob[i:], params.G[:]) | ||
} else { | ||
blob = make([]byte, sizeOfDSAParamsV2Header+keySize*2+groupSize+seedSize) | ||
hdr := bcrypt.DSA_PARAMETER_HEADER_V2{ | ||
Length: uint32(len(blob)), | ||
Magic: bcrypt.DSA_PARAMETERS_MAGIC_V2, | ||
KeySize: keySize, | ||
GroupSize: groupSize, | ||
HashAlgorithm: bcrypt.DSA_HASH_ALGORITHM_SHA256, | ||
StandardVersion: bcrypt.DSA_FIPS186_3, | ||
SeedLength: seedSize, | ||
//Count: , | ||
} | ||
copy(blob, (*(*[sizeOfDSAParamsV2Header]byte)(unsafe.Pointer(&hdr)))[:]) | ||
i := int(sizeOfDSAParamsV2Header) | ||
copy(blob[i:], params.S[:]) | ||
i += len(params.S) | ||
copy(blob[i:], params.Q[:]) | ||
i += len(params.Q) | ||
copy(blob[i:], params.P[:]) | ||
i += len(params.P) | ||
copy(blob[i:], params.G[:]) | ||
|
||
} | ||
// TODO: This still not works... | ||
if err := bcrypt.SetProperty(bcrypt.HANDLE(hkey), utf16PtrFromString(bcrypt.DSA_PARAMETERS), blob, 0); err != nil { | ||
bcrypt.DestroyKey(hkey) | ||
return nil, err | ||
} | ||
if err := bcrypt.FinalizeKeyPair(hkey, 0); err != nil { | ||
bcrypt.DestroyKey(hkey) | ||
return nil, err | ||
} | ||
k := &DSAPrivateKey{hkey} | ||
runtime.SetFinalizer(k, (*DSAPrivateKey).finalize) | ||
return k, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
//go:build windows | ||
// +build windows | ||
|
||
package cng_test | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/microsoft/go-crypto-winnative/cng" | ||
"github.com/microsoft/go-crypto-winnative/cng/bbig" | ||
) | ||
|
||
func TestGenerateDSAParameters(t *testing.T) { | ||
testParameterGeneration(t, 1024, 160) | ||
testParameterGeneration(t, 2048, 256) | ||
testParameterGeneration(t, 3072, 256) | ||
} | ||
|
||
func testParameterGeneration(t *testing.T, L, N int) { | ||
params, err := cng.GenerateDSAParameters(L) | ||
if err != nil { | ||
t.Errorf("%d: %d-%d", L, N, err) | ||
return | ||
} | ||
|
||
P := bbig.Dec(params.P) | ||
Q := bbig.Dec(params.Q) | ||
G := bbig.Dec(params.G) | ||
|
||
if P.BitLen() != L { | ||
t.Errorf("%d-%d: params.BitLen got:%d want:%d", L, N, P.BitLen(), L) | ||
} | ||
|
||
if Q.BitLen() != N { | ||
t.Errorf("%d-%d: q.BitLen got:%d want:%d", L, N, Q.BitLen(), L) | ||
} | ||
|
||
one := new(big.Int) | ||
one.SetInt64(1) | ||
pm1 := new(big.Int).Sub(P, one) | ||
quo, rem := new(big.Int).DivMod(pm1, Q, new(big.Int)) | ||
if rem.Sign() != 0 { | ||
t.Errorf("%d-%d: p-1 mod q != 0", L, N) | ||
} | ||
x := new(big.Int).Exp(G, quo, P) | ||
if x.Cmp(one) == 0 { | ||
t.Errorf("%d-%d: invalid generator", L, N) | ||
} | ||
|
||
_, err = cng.GenerateKey(params) | ||
if err != nil { | ||
t.Errorf("error generating key: %s", err) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters