chore: not expose private dh key.

This commit is contained in:
kaichaosun 2025-12-01 14:39:49 +08:00
parent 523dca43e1
commit 622fe0a78b
No known key found for this signature in database
GPG Key ID: 223E0F992F4F03BF
3 changed files with 8 additions and 7 deletions

View File

@ -94,10 +94,6 @@ proc decrypt*(convo: PrivateV1, enc: EncryptedPayload): Result[seq[byte], ChatEr
)
copyMem(addr header.dhPublic[0], unsafeAddr dr.dh[0], dr.dh.len) # TODO: Avoid this copy
if convo.doubleratchet.dhSelf.public == header.dhPublic:
info "outgoing message, no need to decrypt"
return err(ChatError(code: errDecryptOutgoing, context: "Attempted to decrypt outgoing message"))
convo.doubleratchet.decrypt(header, dr.ciphertext, @[]).mapErr(proc(e: NaxolotlError): ChatError = ChatError(code: errWrapped, context: repr(e) ))
@ -194,6 +190,10 @@ proc handleFrame*[T: ConversationStore](convo: PrivateV1, client: T,
let enc = decode(bytes, EncryptedPayload).valueOr:
raise newException(ValueError, fmt"Failed to decode EncryptedPayload: {repr(error)}")
if convo.doubleratchet.dhSelfPublic() == enc.doubleratchet.dh:
info "outgoing message, no need to handle", convo = convo.id()
return
let plaintext = convo.decrypt(enc).valueOr:
error "decryption failed", error = error
return

View File

@ -8,8 +8,6 @@ type
ErrorCode* = enum
errTypeError
errWrapped
errDecryptOutgoing
proc `$`*(x: ChatError): string =
fmt"ChatError(code={$x.code}, context: {x.context})"

View File

@ -16,7 +16,7 @@ const maxSkip = 10
type Doubleratchet* = object
dhSelf*: PrivateKey
dhSelf: PrivateKey
dhRemote: PublicKey
rootKey: RootKey
@ -187,3 +187,6 @@ func initDoubleratchet*(sharedSecret: array[32, byte], dhSelf: PrivateKey, dhRem
if isSending:
result.dhRatchetSend()
func dhSelfPublic*(self: Doubleratchet): PublicKey =
self.dhSelf.public