Skip to content

Commit 98c2a51

Browse files
authored
Merge pull request #102 from voyager1708/feature/auth/certificates-port
Feature/auth/certificates port
2 parents c5d8ee1 + 06dc96a commit 98c2a51

File tree

108 files changed

+13890
-166
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+13890
-166
lines changed

bsv/aes_gcm.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from Cryptodome.Cipher import AES
2+
from Cryptodome.Util import Padding
3+
4+
class AESGCMError(Exception):
5+
pass
6+
7+
def aes_gcm_encrypt(plaintext: bytes, key: bytes, iv: bytes, aad: bytes = b""):
8+
cipher = AES.new(key, AES.MODE_GCM, nonce=iv)
9+
cipher.update(aad)
10+
ciphertext, tag = cipher.encrypt_and_digest(plaintext)
11+
return ciphertext, tag
12+
13+
def aes_gcm_decrypt(ciphertext: bytes, key: bytes, iv: bytes, tag: bytes, aad: bytes = b""):
14+
cipher = AES.new(key, AES.MODE_GCM, nonce=iv)
15+
cipher.update(aad)
16+
try:
17+
plaintext = cipher.decrypt_and_verify(ciphertext, tag)
18+
return plaintext
19+
except ValueError as e:
20+
raise AESGCMError(f"decryption failed: {e}")
21+
22+
# --- GHASH utilities (for test vector compatibility, optional) ---
23+
def xor_bytes(a: bytes, b: bytes) -> bytes:
24+
return bytes(x ^ y for x, y in zip(a, b))
25+
26+
def right_shift(block: bytes) -> bytes:
27+
b = bytearray(block)
28+
carry = 0
29+
for i in range(len(b)):
30+
old_carry = carry
31+
carry = b[i] & 0x01
32+
b[i] >>= 1
33+
if old_carry:
34+
b[i] |= 0x80
35+
return bytes(b)
36+
37+
def check_bit(block: bytes, index: int, bit: int) -> bool:
38+
return ((block[index] >> bit) & 1) == 1
39+
40+
def multiply(block0: bytes, block1: bytes) -> bytes:
41+
v = bytearray(block1)
42+
z = bytearray(16)
43+
r = bytearray([0xe1] + [0x00]*15)
44+
for i in range(16):
45+
for j in range(7, -1, -1):
46+
if check_bit(block0, i, j):
47+
z = bytearray(x ^ y for x, y in zip(z, v))
48+
if check_bit(v, 15, 0):
49+
v = bytearray(x ^ y for x, y in zip(right_shift(v), r))
50+
else:
51+
v = bytearray(right_shift(v))
52+
return bytes(z)
53+
54+
def ghash(input_bytes: bytes, hash_subkey: bytes) -> bytes:
55+
result = bytes(16)
56+
for i in range(0, len(input_bytes), 16):
57+
block = input_bytes[i:i+16]
58+
if len(block) < 16:
59+
block = block + b"\x00" * (16 - len(block))
60+
result = multiply(xor_bytes(result, block), hash_subkey)
61+
return result

bsv/auth/cert_encryption.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
def get_certificate_encryption_details(field_name: str, serial_number: Optional[str]) -> Tuple[dict, str]:
55
"""
6-
TS/Go準拠の証明書フィールド暗号化メタデータを返す。
6+
Returns certificate field encryption metadata compatible with TS/Go.
77
- protocol_id: {'protocol': 'certificate field encryption', 'security_level': 1}
8-
- key_id: serial_numberがあれば "{serial_number} {field_name}", なければ field_name
8+
- key_id: If serial_number is present, "{serial_number} {field_name}", otherwise field_name
99
"""
1010
protocol_id = {
1111
"protocol": "certificate field encryption",

bsv/auth/certificate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from bsv.keys import PublicKey, PrivateKey
44
from bsv.utils import unsigned_to_varint, Reader, Writer, serialize_ecdsa_der, deserialize_ecdsa_der, hash256
55

6-
# Outpointの簡易表現
6+
# Simple representation of Outpoint
77
class Outpoint(NamedTuple):
88
txid: str # 32byte hex string
99
index: int

bsv/auth/clients/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# __init__.py for bsv.auth.clients

0 commit comments

Comments
 (0)