diff --git a/sodium/crypto_sign.go b/sodium/crypto_sign.go index 899e33b..344a885 100644 --- a/sodium/crypto_sign.go +++ b/sodium/crypto_sign.go @@ -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") diff --git a/sodium/crypto_sign_test.go b/sodium/crypto_sign_test.go index b90960f..57c423f 100644 --- a/sodium/crypto_sign_test.go +++ b/sodium/crypto_sign_test.go @@ -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) @@ -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 {