|
1 | 1 | from fastapi import APIRouter, Request, HTTPException, Response, Depends |
2 | 2 | from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials |
3 | 3 | from pydantic import BaseModel, validator |
4 | | -from base64 import b64encode, b64decode |
5 | | -from app.core.crypto import verify_signature |
6 | | -from app.logic.message import otp_batch_processor, otp_message_processor |
| 4 | +from app.logic.message import message_processor |
7 | 5 | from app.utils.helper_utils import valid_b64 |
8 | 6 | from app.utils.jwt import verify_jwt_token |
9 | 7 | from app.core.constants import ( |
|
17 | 15 |
|
18 | 16 | router = APIRouter() |
19 | 17 |
|
20 | | -class PadsPayload(BaseModel): |
21 | | - otp_hashchain_ciphertext: str |
22 | | - otp_hashchain_signature : str |
23 | | - recipient : str |
| 18 | +class SendPayload(BaseModel): |
| 19 | + ciphertext_blob: str |
| 20 | + recipient : str |
24 | 21 |
|
25 | | -class SendMessagePayload(BaseModel): |
26 | | - message_encrypted: str |
27 | | - recipient : str |
28 | | - |
29 | | -@router.post("/messages/send_pads") |
30 | | -async def message_send_pads(payload: PadsPayload, response: Response, user=Depends(verify_jwt_token)): |
31 | | - otp_hashchain_ciphertext = payload.otp_hashchain_ciphertext |
32 | | - otp_hashchain_signature = payload.otp_hashchain_signature |
33 | | - recipient = payload.recipient |
| 22 | +@router.post("/messages/send") |
| 23 | +async def message_send(payload: SendPayload, response: Response, user=Depends(verify_jwt_token)): |
| 24 | + ciphertext_blob = payload.ciphertext_blob |
| 25 | + recipient = payload.recipient |
34 | 26 |
|
35 | 27 | user_id = user["id"] |
36 | | - |
37 | | - |
38 | | - # ML-KEM-1024 ciphertext is always 1568 bytes, and Classic McEliece8192128 is always 208 bytes, |
39 | | - # and since our default One-Time-Pad size is around 11 kilobytes (11264) |
40 | | - # We can be confident that the decoded ciphertext_blob size must match 551936 bytes |
41 | | - # |
42 | | - # 11264 / 32 = 352 |
43 | | - # 352 x 1568 = 551936 |
44 | | - # 352 x 208 = 73216 |
45 | | - # size to match is 551936 + 73216 = 625152 |
46 | | - |
47 | | - print(len(b64decode(otp_hashchain_ciphertext))) |
48 | | - if (not valid_b64(otp_hashchain_ciphertext)) or len(b64decode(otp_hashchain_ciphertext)) != (OTP_PAD_SIZE // 32) * (ML_KEM_1024_CT_LEN + CLASSIC_MCELIECE_8_F_CT_LEN): |
49 | | - raise HTTPException(status_code=400, detail="Malformed otp_hashchain_ciphertext") |
50 | | - |
51 | | - # Dilithium5 signature is always 4595 |
52 | | - if (not valid_b64(otp_hashchain_signature)) or len(b64decode(otp_hashchain_signature)) != ML_DSA_87_SIGN_LEN: |
53 | | - raise HTTPException(status_code=400, detail="Malformed otp_hashchain_signature") |
54 | 28 |
|
55 | 29 | if (not recipient.isdigit()) or len(recipient) != 16: |
56 | 30 | raise HTTPException(status_code=400, detail="Invalid recipient") |
57 | 31 |
|
58 | | - try: |
59 | | - await asyncio.to_thread(otp_batch_processor, user_id, recipient, otp_hashchain_ciphertext, otp_hashchain_signature) |
60 | | - except ValueError as e: |
61 | | - raise HTTPException(status_code=400, detail=e) |
62 | | - |
63 | | - return {"status": "success"} |
64 | | - |
65 | | - |
66 | | -@router.post("/messages/send_message") |
67 | | -async def message_send_message(payload: SendMessagePayload, response: Response, user=Depends(verify_jwt_token)): |
68 | | - message_encrypted = payload.message_encrypted |
69 | | - recipient = payload.recipient |
70 | | - |
71 | | - user_id = user["id"] |
| 32 | + |
| 33 | + if (not valid_b64(ciphertext_blob)): |
| 34 | + raise HTTPException(status_code=400, detail="Malformed ciphertext_blob") |
72 | 35 |
|
73 | | - if (not recipient.isdigit()) or len(recipient) != 16: |
74 | | - raise HTTPException(status_code=400, detail="Invalid recipient") |
75 | 36 |
|
76 | | - # 64 is the hash chain output calculated using sha3_512, and 2 is for the padding length field and 1 character is bare minimum for a message |
77 | | - if len(message_encrypted) < (64 + 2 + 1): |
78 | | - raise HTTPException(status_code=400, detail="Your message is malformed") |
79 | 37 |
|
80 | 38 | try: |
81 | | - await asyncio.to_thread(otp_message_processor, user_id, recipient, message_encrypted) |
| 39 | + await asyncio.to_thread(message_processor, user_id, recipient, ciphertext_blob) |
82 | 40 | except ValueError as e: |
83 | 41 | raise HTTPException(status_code=400, detail=e) |
84 | 42 |
|
85 | 43 | return {"status": "success"} |
86 | 44 |
|
87 | | - |
0 commit comments