| 1 |
# Copyright 2016-2019 Donald Stufft and individual contributors |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
|
| 16 |
import binascii |
| 17 |
from typing import NoReturn |
| 18 |
|
| 19 |
import nacl.bindings |
| 20 |
from nacl.utils import bytes_as_string |
| 21 |
|
| 22 |
BYTES = nacl.bindings.crypto_generichash_BYTES |
| 23 |
BYTES_MIN = nacl.bindings.crypto_generichash_BYTES_MIN |
| 24 |
BYTES_MAX = nacl.bindings.crypto_generichash_BYTES_MAX |
| 25 |
KEYBYTES = nacl.bindings.crypto_generichash_KEYBYTES |
| 26 |
KEYBYTES_MIN = nacl.bindings.crypto_generichash_KEYBYTES_MIN |
| 27 |
KEYBYTES_MAX = nacl.bindings.crypto_generichash_KEYBYTES_MAX |
| 28 |
SALTBYTES = nacl.bindings.crypto_generichash_SALTBYTES |
| 29 |
PERSONALBYTES = nacl.bindings.crypto_generichash_PERSONALBYTES |
| 30 |
|
| 31 |
SCRYPT_AVAILABLE = nacl.bindings.has_crypto_pwhash_scryptsalsa208sha256 |
| 32 |
|
| 33 |
_b2b_init = nacl.bindings.crypto_generichash_blake2b_init |
| 34 |
_b2b_final = nacl.bindings.crypto_generichash_blake2b_final |
| 35 |
_b2b_update = nacl.bindings.crypto_generichash_blake2b_update |
| 36 |
|
| 37 |
|
| 38 |
class blake2b: |
| 39 |
""" |
| 40 |
:py:mod:`hashlib` API compatible blake2b algorithm implementation |
| 41 |
""" |
| 42 |
|
| 43 |
MAX_DIGEST_SIZE = BYTES |
| 44 |
MAX_KEY_SIZE = KEYBYTES_MAX |
| 45 |
PERSON_SIZE = PERSONALBYTES |
| 46 |
SALT_SIZE = SALTBYTES |
| 47 |
|
| 48 |
def __init__( |
| 49 |
self, |
| 50 |
data: bytes = b"", |
| 51 |
digest_size: int = BYTES, |
| 52 |
key: bytes = b"", |
| 53 |
salt: bytes = b"", |
| 54 |
person: bytes = b"", |
| 55 |
): |
| 56 |
""" |
| 57 |
:py:class:`.blake2b` algorithm initializer |
| 58 |
|
| 59 |
:param data: |
| 60 |
:type data: bytes |
| 61 |
:param int digest_size: the requested digest size; must be |
| 62 |
at most :py:attr:`.MAX_DIGEST_SIZE`; |
| 63 |
the default digest size is :py:data:`.BYTES` |
| 64 |
:param key: the key to be set for keyed MAC/PRF usage; if set, |
| 65 |
the key must be at most :py:data:`.KEYBYTES_MAX` long |
| 66 |
:type key: bytes |
| 67 |
:param salt: a initialization salt at most |
| 68 |
:py:attr:`.SALT_SIZE` long; it will be zero-padded |
| 69 |
if needed |
| 70 |
:type salt: bytes |
| 71 |
:param person: a personalization string at most |
| 72 |
:py:attr:`.PERSONAL_SIZE` long; it will be zero-padded |
| 73 |
if needed |
| 74 |
:type person: bytes |
| 75 |
""" |
| 76 |
|
| 77 |
self._state = _b2b_init( |
| 78 |
key=key, salt=salt, person=person, digest_size=digest_size |
| 79 |
) |
| 80 |
self._digest_size = digest_size |
| 81 |
|
| 82 |
if data: |
| 83 |
self.update(data) |
| 84 |
|
| 85 |
@property |
| 86 |
def digest_size(self) -> int: |
| 87 |
return self._digest_size |
| 88 |
|
| 89 |
@property |
| 90 |
def block_size(self) -> int: |
| 91 |
return 128 |
| 92 |
|
| 93 |
@property |
| 94 |
def name(self) -> str: |
| 95 |
return "blake2b" |
| 96 |
|
| 97 |
def update(self, data: bytes) -> None: |
| 98 |
_b2b_update(self._state, data) |
| 99 |
|
| 100 |
def digest(self) -> bytes: |
| 101 |
_st = self._state.copy() |
| 102 |
return _b2b_final(_st) |
| 103 |
|
| 104 |
def hexdigest(self) -> str: |
| 105 |
return bytes_as_string(binascii.hexlify(self.digest())) |
| 106 |
|
| 107 |
def copy(self) -> "blake2b": |
| 108 |
_cp = type(self)(digest_size=self.digest_size) |
| 109 |
_st = self._state.copy() |
| 110 |
_cp._state = _st |
| 111 |
return _cp |
| 112 |
|
| 113 |
def __reduce__(self) -> NoReturn: |
| 114 |
""" |
| 115 |
Raise the same exception as hashlib's blake implementation |
| 116 |
on copy.copy() |
| 117 |
""" |
| 118 |
raise TypeError( |
| 119 |
"can't pickle {} objects".format(self.__class__.__name__) |
| 120 |
) |
| 121 |
|
| 122 |
|
| 123 |
def scrypt( |
| 124 |
password: bytes, |
| 125 |
salt: bytes = b"", |
| 126 |
n: int = 2**20, |
| 127 |
r: int = 8, |
| 128 |
p: int = 1, |
| 129 |
maxmem: int = 2**25, |
| 130 |
dklen: int = 64, |
| 131 |
) -> bytes: |
| 132 |
""" |
| 133 |
Derive a cryptographic key using the scrypt KDF. |
| 134 |
|
| 135 |
:raises nacl.exceptions.UnavailableError: If called when using a |
| 136 |
minimal build of libsodium. |
| 137 |
|
| 138 |
Implements the same signature as the ``hashlib.scrypt`` implemented |
| 139 |
in cpython version 3.6 |
| 140 |
""" |
| 141 |
return nacl.bindings.crypto_pwhash_scryptsalsa208sha256_ll( |
| 142 |
password, salt, n, r, p, maxmem=maxmem, dklen=dklen |
| 143 |
) |