Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wrap crypto_sign_detached, w/ tests #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions sodium/crypto_sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,29 @@ func Sign(sealedMessageOut []byte, message []byte, sk []byte) int {
(*C.uchar)(&sk[0])))
}

func SignDetached(detachedSealOut []byte, message []byte, sk []byte) int {
checkSize(detachedSealOut, SignBytes(), "signature output")
checkSize(sk, SignSecretKeyBytes(), "secret key")

return int(C.crypto_sign_detached(
(*C.uchar)(&detachedSealOut[0]),
nil,
(*C.uchar)(&message[0]),
(C.ulonglong)(len(message)),
(*C.uchar)(&sk[0])))
}

func SignVerifyDetached(sig []byte, message []byte, pk []byte) int {
checkSize(sig, SignBytes(), "signature input")
checkSize(pk, SignPublicKeyBytes(), "public key")

return int(C.crypto_sign_verify_detached(
(*C.uchar)(&sig[0]),
(*C.uchar)(&message[0]),
(C.ulonglong)(len(message)),
(*C.uchar)(&pk[0])))
}

func SignOpen(messageOut []byte, sealedMessage []byte, pk []byte) int {
checkSize(messageOut, len(sealedMessage)-SignBytes(), "message output")
checkSize(pk, SignPublicKeyBytes(), "public key")
Expand Down
13 changes: 13 additions & 0 deletions sodium/crypto_sign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ func TestSignAndVerify(t *testing.T) {
t.Fatalf("bad result signing: %v", ret)
}

// detached sign the message
signed_detached := make([]byte, SignBytes())
ret = SignDetached(signed_detached, msg, Sk)
if ret != 0 {
t.Fatalf("bad result signing detached: %v", ret)
}

// verify message
msg2 := make([]byte, len(msg))
ret = SignOpen(msg2, signed, Pk)
Expand All @@ -46,6 +53,12 @@ func TestSignAndVerify(t *testing.T) {
t.Fatalf("different unsealed message")
}

// verify detached
ret = SignVerifyDetached(signed_detached, msg, Pk)
if ret != 0 {
t.Fatalf("bad result verifying detached: %v", ret)
}

// try corrupting the message at various points
corruptedCopy := make([]byte, len(signed))
for c := 0; c < len(signed); c += 100 {
Expand Down