this post was submitted on 21 Mar 2025
18 points (100.0% liked)

Programming

19061 readers
259 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 2 years ago
MODERATORS
 

Hi, [email protected]. I'm curious about zero-knowledge encryption, and I would like to use it in my CS50x final project. My goal is to authenticate users and store their encrypted data on the server so that only the users can decrypt it.

I understand the general concepts of public and private keys, as well as symmetric keys, and how to use them to protect data. However, I don't understand how to authenticate users. I have searched online for information on implementing the zero proof knowledge authentication flow, but I found either vague high-level descriptions or research papers that require a strong background in mathematics and cryptography to understand and implement.

Could you maybe suggest some resources on this topic? When your search for "how to implement jwt authentication", you can find many articles that describe the flow with code examples. I'm looking for something similar.

Or should I choose a simpler project?

top 11 comments
sorted by: hot top controversial new old
[–] [email protected] 14 points 8 hours ago (1 children)

So... to store encrypted data that only the user can decrypt you don't need any fancy zero knowledge algorithms. Just have the user keep the encryption key.

For authentication you could use one of these algorithms. OPAQUE seems to be popular. I'm not an expert but it seems like it has several neat zero-knowledge style properties.

But probably forget about implementing it without a strong background in cryptography.

[–] [email protected] 2 points 1 hour ago

Thanks, I will take a look! Implementing the encryption algorithm itself wasn't my goal, I was hoping to find and reuse an existing library. You know, like we don't implement our own algorithms to hash passwords or generate keys.

[–] [email protected] 1 points 5 hours ago (1 children)

It seems to me that you're asking about two different things: zero-knowledge authentication, and public key authentication. I think you'd have a much easier time using public key auth. And tbh I don't know anything about the zero-knowledge stuff. I don't know what reading resources to point to, so I'll try to provide a little clarifying background instead.

The simplest way to a authenticate a user if you have their public key is probably to require every request to be signed with that key. The server gets the request, verifies the signature, and that's it, that's an authenticated request. Although adding a nonce to the signed content would be a good idea if replay attacks might be a problem.

If you want to be properly standards-compliant you want a standard "envelope" for signed requests. Personally I would use the multipart/signed MIME type since that is a ready-made, standardized format that is about as simple as it gets.

You mentioned JSON Web Tokens (JWTs) which are a similar idea. That's a format that you might think you could use for signing requests - it's sort of another quasi-standardized envelope format for signed data. But the wrinkle is that JWTs aren't used to sign arbitrary data. The data is expected to be a set of "claims". A JWT is a JSON header, JSON claims, and a signature all of three which are serialized with base64 and concatenated. Usually you would put a JWT in the Authorization header of an HTTP request like this:

Authorization: Bearer $jwt

Then the server verifies the JWT signature, inspects the "claims", and decides whether the request is authorized based on whether it has the right claims. JWTs make sense if you want an authentication token that is separate from the request body. They are more complicated than multipart/signed content since the purpose is to standardize a narrow use case, but to also support all of the features that the stakeholders wanted.

Another commenter suggested Diffie-Hellman key exchange which I think is not a bad idea as a third alternative if you want to establish sessions. Diffie-Hellman used in every https connection to establish a session key. In https the session key is used for symmetric encryption of all subsequent traffic over that connection. But the session key doesn't have to be an encryption key - you could use the key exchange to establish a session password. You could use that temporary password to authenticate all requests in that session. I do know of an intro video for Diffie-Hellman: https://youtu.be/Ex_ObHVftDg

The first two options I suggested require the server to have user public keys for each account. The Diffie-Hellman option also requires users to have the server's public key available. An advantage is that Diffie-Hellman authenticates both parties to each other so users know they can trust the server. But if your server uses https you'll get server authentication anyway during the connection key exchange. And the Diffie-Hellman session password needs an encrypted connection to be secure. The JWT option would probably also need an encrypted connection.

[–] [email protected] 1 points 1 hour ago

Thanks for your reply. The idea of zero-knowledge authentication is that the password never touches the server. Instead, the user can prove that they know the password when logging in by solving a challenge. This enables the user to log in from any new device without the need to transfer keys between them. I'll take a closer look at your suggestions though. Thanks again!

[–] [email protected] 9 points 9 hours ago (1 children)

I believe I understand what you want. "Zero" login. So when a user comes to your site or first boots up your app a private key gets generated locally. It will then do a handshake with the server, where that the server understands that these encrypted messages are from this user, this uniquely identifies the user, and also can be used for e2e.

Reference https://dev.to/spalladino/a-beginners-intro-to-coding-zero-knowledge-proofs-c56

[–] [email protected] 3 points 9 hours ago (1 children)

I think he means something like challenge-response type of auth flow that while using user/pass, the password waa never sent to the server?

[–] [email protected] 1 points 8 hours ago (1 children)

Similar to a diffie-hellman key exchange maybe? https://en.m.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange

I believe this has been broken but that is the general gist.

[–] [email protected] 2 points 8 hours ago* (last edited 1 hour ago) (1 children)

DLP broken? Didn't heard of that.

[–] [email protected] 2 points 7 hours ago* (last edited 7 hours ago)

Probably saw this in passing. It doesn't seem to indicate fully broken just this instance.

https://www.reddit.com/r/math/comments/wc4gkx/supersingular_isogeny_diffiehellman_broken/

[–] [email protected] 1 points 7 hours ago (1 children)

research papers that require a strong background in mathematics and cryptography to understand and implement.

Lol. I guess that makes sense. Outside of school, we hope that all authentication will be implemented only cryptography experts anyway.

Could you maybe suggest some resources on this topic?

Not really, sorry. I'm not aware of anyone creating resources for your situation.

Or should I choose a simpler project?

For some context, cryptography isn't even usually implemented "completely correctly" by experts. That's part of why we have constant software security patches.

If I were in your shoes, I guess it would depend on my instructor and advisors.

If I felt like they have the skills to catch mistakes and no time to help correct mistakes, then I would just choose a simpler project. If they're cool with awarding a good grade for a functional demo, I might just go for it.

I guess I would take this one to an advisor and get some feedback on practicality.

[–] [email protected] 2 points 1 hour ago

For some context, cryptography isn’t even usually implemented “completely correctly” by experts. That’s part of why we have constant software security patches.

Yeah, I totally agree, and I don't expect to implement it properly or go public with this. I just got this idea for the final project. When it comes to password hashing, we have libraries in all popular languages that handle this, and we have open-source tools to generate keys. So, I was hoping to find something ready to use for my project. Unfortunately, it seems this area isn't very popular.