|
| 1 | +""" |
| 2 | +This module contains Version4 implementation of the Paseto protocol. |
| 3 | +
|
| 4 | +https://github.com/paseto-standard/paseto-spec/blob/master/docs/01-Protocol-Versions/Version4.md |
| 5 | +""" |
| 6 | + |
| 7 | +import hashlib |
| 8 | +import hmac |
| 9 | +import os |
| 10 | +from typing import Tuple |
| 11 | + |
| 12 | +from paseto.crypto import libsodium_wrapper, primitives |
| 13 | +from paseto.exceptions import InvalidKey, InvalidMac |
| 14 | +from paseto.paserk.keys import ( |
| 15 | + _TYPE_LOCAL, |
| 16 | + _TYPE_PUBLIC, |
| 17 | + _TYPE_SECRET, |
| 18 | + _create_asymmetric_key, |
| 19 | + _create_symmetric_key, |
| 20 | + _deserialize_key, |
| 21 | +) |
| 22 | +from paseto.paserk.keys import _verify_key as _generic_verify_key |
| 23 | +from paseto.protocol.common import check_footer, check_header, decode_message |
| 24 | +from paseto.protocol.util import b64, pae |
| 25 | + |
| 26 | +HEADER_LOCAL = b"v4.local." |
| 27 | +HEADER_PUBLIC = b"v4.public." |
| 28 | + |
| 29 | +NONCE_SIZE = 32 |
| 30 | +ENCRYPTION_KEY_LENGTH = 32 |
| 31 | +AUTHENTICATION_KEY_LENGTH = 32 |
| 32 | +MAC_SIZE = 32 |
| 33 | + |
| 34 | +INFO_ENCRYPTION = b"paseto-encryption-key" |
| 35 | +INFO_AUTHENTICATION = b"paseto-auth-key-for-aead" |
| 36 | + |
| 37 | + |
| 38 | +def encrypt( |
| 39 | + message: bytes, key: bytes, footer: bytes = b"", implicit_assertion: bytes = b"" |
| 40 | +) -> bytes: |
| 41 | + """PASETO Version4 encrypt function.""" |
| 42 | + |
| 43 | + # verify that key is intended for use with this function |
| 44 | + _verify_key(key, _TYPE_LOCAL) |
| 45 | + raw_key: bytes = _deserialize_key(key) |
| 46 | + |
| 47 | + # Step 1 |
| 48 | + header: bytes = HEADER_LOCAL |
| 49 | + |
| 50 | + # Step 2 |
| 51 | + nonce: bytes = os.urandom(NONCE_SIZE) |
| 52 | + |
| 53 | + # Step 3 |
| 54 | + encryption_key: bytes |
| 55 | + authentication_key: bytes |
| 56 | + nonce2: bytes |
| 57 | + encryption_key, authentication_key, nonce2 = _split_key(raw_key, nonce) |
| 58 | + |
| 59 | + # Step 4 |
| 60 | + ciphertext: bytes = libsodium_wrapper.crypto_stream_xchacha20_xor( |
| 61 | + message=message, nonce=nonce2, key=encryption_key |
| 62 | + ) |
| 63 | + |
| 64 | + # Step 5 |
| 65 | + pre_auth: bytes = pae([header, nonce, ciphertext, footer, implicit_assertion]) |
| 66 | + |
| 67 | + # Step 6 |
| 68 | + message_authentication_code: bytes = hashlib.blake2b( |
| 69 | + pre_auth, key=authentication_key, digest_size=32 |
| 70 | + ).digest() |
| 71 | + |
| 72 | + # Step 7 |
| 73 | + ret: bytes = header + b64(nonce + ciphertext + message_authentication_code) |
| 74 | + if footer: |
| 75 | + ret += b"." + b64(footer) |
| 76 | + return ret |
| 77 | + |
| 78 | + |
| 79 | +def decrypt( |
| 80 | + message: bytes, key: bytes, footer: bytes = b"", implicit_assertion: bytes = b"" |
| 81 | +) -> bytes: |
| 82 | + """PASETO Version4 decrypt function.""" |
| 83 | + |
| 84 | + # verify that key is intended for use with this function |
| 85 | + _verify_key(key, _TYPE_LOCAL) |
| 86 | + raw_key: bytes = _deserialize_key(key) |
| 87 | + |
| 88 | + # Step 1 |
| 89 | + check_footer(message, footer) |
| 90 | + |
| 91 | + # Step 2 |
| 92 | + header: bytes = HEADER_LOCAL |
| 93 | + check_header(message, header) |
| 94 | + |
| 95 | + # Step 3 |
| 96 | + decoded: bytes = decode_message(message, len(header)) |
| 97 | + nonce: bytes = decoded[:NONCE_SIZE] |
| 98 | + ciphertext: bytes = decoded[NONCE_SIZE:-MAC_SIZE] |
| 99 | + |
| 100 | + # Step 4 |
| 101 | + encryption_key, authentication_key, nonce2 = _split_key(raw_key, nonce) |
| 102 | + |
| 103 | + # Step 5 |
| 104 | + pre_auth: bytes = pae([header, nonce, ciphertext, footer, implicit_assertion]) |
| 105 | + |
| 106 | + # Step 6 |
| 107 | + computed_mac: bytes = hashlib.blake2b( |
| 108 | + pre_auth, key=authentication_key, digest_size=MAC_SIZE |
| 109 | + ).digest() |
| 110 | + |
| 111 | + # Step 7 |
| 112 | + mac_in_message: bytes = decoded[-MAC_SIZE:] |
| 113 | + if not hmac.compare_digest(mac_in_message, computed_mac): |
| 114 | + raise InvalidMac("Invalid MAC for given ciphertext") |
| 115 | + |
| 116 | + # Steps 8 and 9 |
| 117 | + return libsodium_wrapper.crypto_stream_xchacha20_xor( |
| 118 | + message=ciphertext, nonce=nonce2, key=encryption_key |
| 119 | + ) |
| 120 | + |
| 121 | + |
| 122 | +def sign( |
| 123 | + message: bytes, |
| 124 | + secret_key: bytes, |
| 125 | + footer: bytes = b"", |
| 126 | + implicit_assertion: bytes = b"", |
| 127 | +) -> bytes: |
| 128 | + """Sign message and return token which can then be used with verify().""" |
| 129 | + |
| 130 | + # verify that key is intended for use with this function |
| 131 | + _verify_key(secret_key, _TYPE_SECRET) |
| 132 | + raw_secret_key: bytes = _deserialize_key(secret_key) |
| 133 | + |
| 134 | + # Step 1 |
| 135 | + header = HEADER_PUBLIC |
| 136 | + |
| 137 | + # Step 2 |
| 138 | + message2 = pae([header, message, footer, implicit_assertion]) |
| 139 | + |
| 140 | + # Step 3 |
| 141 | + signature = primitives.sign(message2, raw_secret_key) |
| 142 | + |
| 143 | + # Step 4 |
| 144 | + ret = header + b64(message + signature) |
| 145 | + if footer: |
| 146 | + ret += b"." + b64(footer) |
| 147 | + |
| 148 | + return ret |
| 149 | + |
| 150 | + |
| 151 | +def verify( |
| 152 | + signed_message: bytes, |
| 153 | + public_key: bytes, |
| 154 | + footer: bytes = b"", |
| 155 | + implicit_assertion: bytes = b"", |
| 156 | +) -> bytes: |
| 157 | + """Verify signature and return message. Raises exception if signature is invalid.""" |
| 158 | + |
| 159 | + # verify that key is intended for use with this function |
| 160 | + _verify_key(public_key, _TYPE_PUBLIC) |
| 161 | + raw_public_key: bytes = _deserialize_key(public_key) |
| 162 | + |
| 163 | + # Step 1 |
| 164 | + check_footer(signed_message, footer) |
| 165 | + |
| 166 | + # Step 2 |
| 167 | + header = HEADER_PUBLIC |
| 168 | + check_header(signed_message, header) |
| 169 | + |
| 170 | + # Step 3 |
| 171 | + raw_inner_message: bytes = decode_message(signed_message, len(header)) |
| 172 | + signature = raw_inner_message[-64:] |
| 173 | + message = raw_inner_message[:-64] |
| 174 | + |
| 175 | + # Step 4 |
| 176 | + message2 = pae([header, message, footer, implicit_assertion]) |
| 177 | + |
| 178 | + # Steps 5 and 6 |
| 179 | + primitives.verify(signature, message2, raw_public_key) |
| 180 | + return message |
| 181 | + |
| 182 | + |
| 183 | +def _split_key(key: bytes, nonce: bytes) -> Tuple[bytes, bytes, bytes]: |
| 184 | + |
| 185 | + hashed: bytes = hashlib.blake2b( |
| 186 | + INFO_ENCRYPTION + nonce, key=key, digest_size=56 |
| 187 | + ).digest() |
| 188 | + encryption_key: bytes = hashed[:ENCRYPTION_KEY_LENGTH] |
| 189 | + nonce2: bytes = hashed[ENCRYPTION_KEY_LENGTH:] |
| 190 | + authentication_key: bytes = hashlib.blake2b( |
| 191 | + INFO_AUTHENTICATION + nonce, key=key, digest_size=AUTHENTICATION_KEY_LENGTH |
| 192 | + ).digest() |
| 193 | + |
| 194 | + return encryption_key, authentication_key, nonce2 |
| 195 | + |
| 196 | + |
| 197 | +def _verify_key(key: bytes, key_type: bytes) -> None: |
| 198 | + if not _generic_verify_key(key, 4, key_type): |
| 199 | + raise InvalidKey |
| 200 | + |
| 201 | + |
| 202 | +def create_symmetric_key() -> bytes: |
| 203 | + """Return key for use with encrypt() and decrypt().""" |
| 204 | + return _create_symmetric_key(4) |
| 205 | + |
| 206 | + |
| 207 | +def create_asymmetric_key() -> Tuple[bytes, bytes]: |
| 208 | + """Return key pair for use with sign() and verify().""" |
| 209 | + return _create_asymmetric_key(4) |
0 commit comments