diff options
| author | Jakob L. Kreuze | 2022-11-18 12:29:58 -0500 |
|---|---|---|
| committer | Jakob L. Kreuze | 2022-11-19 12:27:24 -0500 |
| commit | bd20d5071da3de0a9c15ce0a5bc3f40e7b6c2879 (patch) | |
| tree | f8d0a5df380965b4a1d4e471b4da0ed045c89f16 /haunt/jakob/dynamic/captcha.scm | |
| parent | 2c4ea5ffb92de49b485db8687290f3e53dea855a (diff) | |
[dynamic] Initial comment/captcha system that doesn't work
Diffstat (limited to 'haunt/jakob/dynamic/captcha.scm')
| -rw-r--r-- | haunt/jakob/dynamic/captcha.scm | 74 |
1 files changed, 61 insertions, 13 deletions
diff --git a/haunt/jakob/dynamic/captcha.scm b/haunt/jakob/dynamic/captcha.scm index b095ebf..3b192cb 100644 --- a/haunt/jakob/dynamic/captcha.scm +++ b/haunt/jakob/dynamic/captcha.scm @@ -14,8 +14,8 @@ ;;; along with this program. If not, see ;;; <http://www.gnu.org/licenses/>. -(define-module (captcha) - #:use-module (base64) +(define-module (jakob dynamic captcha) + #:use-module (gcrypt base64) #:use-module (gcrypt hash) #:use-module (gcrypt mac) #:use-module (gcrypt random) @@ -26,10 +26,13 @@ #:use-module (ice-9 popen) #:use-module (ice-9 rdelim) #:use-module (ice-9 threads) + #:use-module (jakob dynamic aes) #:use-module (rnrs bytevectors) #:use-module (srfi srfi-1) #:use-module (srfi srfi-19) - #:export (new-captcha)) + #:use-module (system foreign) + #:export (new-captcha + validate-captcha)) (define proc-mutex (make-mutex)) @@ -109,26 +112,71 @@ (error "Cannot generate PNG")) data))) -(define (new-uuid) - (with-mutex proc-mutex - (let* ((port (open-input-pipe "uuidgen")) - (str (read-line port))) - (close-pipe port) - str))) +;; These are ephemeral keys. At this point, it doesn't make sense to store keys +;; locally, since the server process is singular and long-running. +(define %tex-aes-key (gen-random-bv %aes-key-size)) +(define %tex-mac-key (gen-random-bv 64)) + +(define (encode-solution solution) + (define nonce (gen-random-bv 8)) + ;; This is horrible :D + (define encoded-solution + (pointer->bytevector (make-c-struct (list double) (list solution)) 8)) + (let ((result (make-bytevector 16))) + (bytevector-copy! nonce 0 result 0 8) + (bytevector-copy! encoded-solution 0 result 8 8) + result)) + +(define (decode-solution encoded) + (let ((result (make-bytevector 8))) + (bytevector-copy! encoded 8 result 0 8) + (first (parse-c-struct (bytevector->pointer result) (list double))))) (define (new-captcha) (let* ((lower-bound (random 10)) (upper-bound (+ lower-bound 1 (random 9))) (expression (random-expression)) - (latex-src (sexp->latex (simplify-sexp (differentiate-sexp expression))))) - (values (new-uuid) - (- (local-eval expression (let ((x upper-bound)) (the-environment))) - (local-eval expression (let ((x lower-bound)) (the-environment)))) + (latex-src (sexp->latex (simplify-sexp (differentiate-sexp expression)))) + (solution (- (local-eval expression (let ((x upper-bound)) (the-environment))) + (local-eval expression (let ((x lower-bound)) (the-environment))))) + (ciphertext (base64-encode (aes-256-encrypt %tex-aes-key (encode-solution solution))))) + (display solution) + (newline) + (display (decode-solution (aes-256-decrypt %tex-aes-key (base64-decode ciphertext)))) + (newline) + (values (sign-data-base64 %tex-mac-key ciphertext) + ciphertext (latex->image (format #f "\\int_{~a}^{~a} ~a \\, dx" lower-bound upper-bound latex-src))))) +(define (validate-captcha answer solution mac) + (define epsilon 0.1) + (display "Here!") + (newline) + (display "Valid signature? ") + (display (valid-base64-signature? %tex-mac-key solution mac)) + (newline) + (display "Signature was ") + (display mac) + (newline) + (display "Valid answer? ") + (display (<= (abs (- (decode-solution (aes-256-decrypt %tex-aes-key (base64-decode solution))) + (string->number answer))) + epsilon)) + (newline) + (display "Answer was ") + (display (decode-solution (aes-256-decrypt %tex-aes-key (base64-decode solution)))) + (newline) + (display "Solution given was ") + (display answer) + (newline) + (and (valid-base64-signature? %tex-mac-key solution mac) + (<= (abs (- (decode-solution (aes-256-decrypt %tex-aes-key (base64-decode solution))) + (string->number answer))) + epsilon))) + ;; How many zeroes the SHA-256 hash has to be prefixed by to be a valid proof of work. |