2007-11-09 21:40:13 +00:00
|
|
|
from pycryptopp.hash.sha256 import SHA256
|
2007-06-25 20:23:51 +00:00
|
|
|
import os
|
2008-09-26 04:38:24 +00:00
|
|
|
from allmydata.util.netstring import netstring
|
2007-03-30 01:11:30 +00:00
|
|
|
|
2009-12-15 00:01:47 +00:00
|
|
|
try:
|
|
|
|
import hashlib
|
|
|
|
sha1 = hashlib.sha1
|
|
|
|
except ImportError:
|
|
|
|
# hashlib was added in Python 2.5
|
|
|
|
import sha
|
|
|
|
sha1 = sha.new
|
|
|
|
|
2008-02-15 02:58:01 +00:00
|
|
|
# Be very very cautious when modifying this file. Almost any change will
|
|
|
|
# cause a compatibility break, invalidating all outstanding URIs and making
|
|
|
|
# any previously uploaded files become inaccessible. BE CONSERVATIVE AND TEST
|
|
|
|
# AGAINST OLD DATA!
|
|
|
|
|
|
|
|
# Various crypto values are this size: hash outputs (from SHA-256d),
|
2007-12-18 01:34:11 +00:00
|
|
|
# randomly-generated secrets such as the lease secret, and symmetric encryption
|
|
|
|
# keys. In the near future we will add DSA private keys, and salts of various
|
|
|
|
# kinds.
|
|
|
|
CRYPTO_VAL_SIZE=32
|
|
|
|
|
2008-02-15 02:58:01 +00:00
|
|
|
class _SHA256d_Hasher:
|
2008-02-07 02:36:43 +00:00
|
|
|
# use SHA-256d, as defined by Ferguson and Schneier: hash the output
|
|
|
|
# again to prevent length-extension attacks
|
|
|
|
def __init__(self, truncate_to=None):
|
|
|
|
self.h = SHA256()
|
|
|
|
self.truncate_to = truncate_to
|
2008-02-15 02:58:01 +00:00
|
|
|
self._digest = None
|
2008-02-07 02:36:43 +00:00
|
|
|
def update(self, data):
|
2008-02-15 02:58:01 +00:00
|
|
|
assert isinstance(data, str) # no unicode
|
2008-02-07 02:36:43 +00:00
|
|
|
self.h.update(data)
|
|
|
|
def digest(self):
|
2008-02-15 02:58:01 +00:00
|
|
|
if self._digest is None:
|
|
|
|
h1 = self.h.digest()
|
|
|
|
del self.h
|
|
|
|
h2 = SHA256(h1).digest()
|
|
|
|
if self.truncate_to:
|
|
|
|
h2 = h2[:self.truncate_to]
|
|
|
|
self._digest = h2
|
|
|
|
return self._digest
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def tagged_hasher(tag, truncate_to=None):
|
|
|
|
hasher = _SHA256d_Hasher(truncate_to)
|
2008-02-07 02:36:43 +00:00
|
|
|
hasher.update(netstring(tag))
|
|
|
|
return hasher
|
|
|
|
|
2008-02-15 02:58:01 +00:00
|
|
|
def tagged_hash(tag, val, truncate_to=None):
|
|
|
|
hasher = tagged_hasher(tag, truncate_to)
|
|
|
|
hasher.update(val)
|
|
|
|
return hasher.digest()
|
|
|
|
|
|
|
|
def tagged_pair_hash(tag, val1, val2, truncate_to=None):
|
|
|
|
s = _SHA256d_Hasher(truncate_to)
|
2007-03-30 01:11:30 +00:00
|
|
|
s.update(netstring(tag))
|
|
|
|
s.update(netstring(val1))
|
|
|
|
s.update(netstring(val2))
|
|
|
|
return s.digest()
|
|
|
|
|
2008-02-15 02:58:01 +00:00
|
|
|
## specific hash tags that we use
|
|
|
|
|
|
|
|
# immutable
|
|
|
|
STORAGE_INDEX_TAG = "allmydata_immutable_key_to_storage_index_v1"
|
|
|
|
BLOCK_TAG = "allmydata_encoded_subshare_v1"
|
|
|
|
UEB_TAG = "allmydata_uri_extension_v1"
|
|
|
|
PLAINTEXT_TAG = "allmydata_plaintext_v1"
|
|
|
|
CIPHERTEXT_TAG = "allmydata_crypttext_v1"
|
|
|
|
CIPHERTEXT_SEGMENT_TAG = "allmydata_crypttext_segment_v1"
|
|
|
|
PLAINTEXT_SEGMENT_TAG = "allmydata_plaintext_segment_v1"
|
2008-03-24 16:46:06 +00:00
|
|
|
CONVERGENT_ENCRYPTION_TAG = "allmydata_immutable_content_to_key_with_added_secret_v1+"
|
2008-02-15 02:58:01 +00:00
|
|
|
|
|
|
|
CLIENT_RENEWAL_TAG = "allmydata_client_renewal_secret_v1"
|
|
|
|
CLIENT_CANCEL_TAG = "allmydata_client_cancel_secret_v1"
|
|
|
|
FILE_RENEWAL_TAG = "allmydata_file_renewal_secret_v1"
|
|
|
|
FILE_CANCEL_TAG = "allmydata_file_cancel_secret_v1"
|
|
|
|
BUCKET_RENEWAL_TAG = "allmydata_bucket_renewal_secret_v1"
|
|
|
|
BUCKET_CANCEL_TAG = "allmydata_bucket_cancel_secret_v1"
|
|
|
|
|
|
|
|
# mutable
|
|
|
|
MUTABLE_WRITEKEY_TAG = "allmydata_mutable_privkey_to_writekey_v1"
|
|
|
|
MUTABLE_WRITE_ENABLER_MASTER_TAG = "allmydata_mutable_writekey_to_write_enabler_master_v1"
|
|
|
|
MUTABLE_WRITE_ENABLER_TAG = "allmydata_mutable_write_enabler_master_and_nodeid_to_write_enabler_v1"
|
|
|
|
MUTABLE_PUBKEY_TAG = "allmydata_mutable_pubkey_to_fingerprint_v1"
|
|
|
|
MUTABLE_READKEY_TAG = "allmydata_mutable_writekey_to_readkey_v1"
|
|
|
|
MUTABLE_DATAKEY_TAG = "allmydata_mutable_readkey_to_datakey_v1"
|
|
|
|
MUTABLE_STORAGEINDEX_TAG = "allmydata_mutable_readkey_to_storage_index_v1"
|
|
|
|
|
|
|
|
# dirnodes
|
|
|
|
DIRNODE_CHILD_WRITECAP_TAG = "allmydata_mutable_writekey_and_salt_to_dirnode_child_capkey_v1"
|
2009-07-13 00:13:20 +00:00
|
|
|
DIRNODE_CHILD_SALT_TAG = "allmydata_dirnode_child_rwcap_to_salt_v1"
|
2007-06-08 04:47:21 +00:00
|
|
|
|
2008-02-01 19:27:37 +00:00
|
|
|
def storage_index_hash(key):
|
2007-07-23 02:48:44 +00:00
|
|
|
# storage index is truncated to 128 bits (16 bytes). We're only hashing a
|
2008-03-24 16:46:06 +00:00
|
|
|
# 16-byte value to get it, so there's no point in using a larger value. We
|
|
|
|
# use this same tagged hash to go from encryption key to storage index for
|
|
|
|
# random-keyed immutable files and convergent-encryption immutabie
|
2008-02-07 02:50:47 +00:00
|
|
|
# files. Mutable files use ssk_storage_index_hash().
|
2008-02-15 02:58:01 +00:00
|
|
|
return tagged_hash(STORAGE_INDEX_TAG, key, 16)
|
2007-07-20 01:21:44 +00:00
|
|
|
|
2007-06-08 04:47:21 +00:00
|
|
|
def block_hash(data):
|
2008-02-15 02:58:01 +00:00
|
|
|
return tagged_hash(BLOCK_TAG, data)
|
2007-06-08 04:47:21 +00:00
|
|
|
def block_hasher():
|
2008-02-15 02:58:01 +00:00
|
|
|
return tagged_hasher(BLOCK_TAG)
|
2007-06-08 04:47:21 +00:00
|
|
|
|
2007-06-08 22:59:16 +00:00
|
|
|
def uri_extension_hash(data):
|
2008-02-15 02:58:01 +00:00
|
|
|
return tagged_hash(UEB_TAG, data)
|
2007-06-08 22:59:16 +00:00
|
|
|
def uri_extension_hasher():
|
2008-02-15 02:58:01 +00:00
|
|
|
return tagged_hasher(UEB_TAG)
|
2007-06-08 04:47:21 +00:00
|
|
|
|
2007-06-10 03:46:04 +00:00
|
|
|
def plaintext_hash(data):
|
2008-02-15 02:58:01 +00:00
|
|
|
return tagged_hash(PLAINTEXT_TAG, data)
|
2007-06-10 03:46:04 +00:00
|
|
|
def plaintext_hasher():
|
2008-02-15 02:58:01 +00:00
|
|
|
return tagged_hasher(PLAINTEXT_TAG)
|
2007-06-10 03:46:04 +00:00
|
|
|
|
|
|
|
def crypttext_hash(data):
|
2008-02-15 02:58:01 +00:00
|
|
|
return tagged_hash(CIPHERTEXT_TAG, data)
|
2007-06-10 03:46:04 +00:00
|
|
|
def crypttext_hasher():
|
2008-02-15 02:58:01 +00:00
|
|
|
return tagged_hasher(CIPHERTEXT_TAG)
|
2007-06-08 04:47:21 +00:00
|
|
|
|
|
|
|
def crypttext_segment_hash(data):
|
2008-02-15 02:58:01 +00:00
|
|
|
return tagged_hash(CIPHERTEXT_SEGMENT_TAG, data)
|
2007-06-08 04:47:21 +00:00
|
|
|
def crypttext_segment_hasher():
|
2008-02-15 02:58:01 +00:00
|
|
|
return tagged_hasher(CIPHERTEXT_SEGMENT_TAG)
|
2007-06-08 04:47:21 +00:00
|
|
|
|
|
|
|
def plaintext_segment_hash(data):
|
2008-02-15 02:58:01 +00:00
|
|
|
return tagged_hash(PLAINTEXT_SEGMENT_TAG, data)
|
2007-06-08 04:47:21 +00:00
|
|
|
def plaintext_segment_hasher():
|
2008-02-15 02:58:01 +00:00
|
|
|
return tagged_hasher(PLAINTEXT_SEGMENT_TAG)
|
|
|
|
|
|
|
|
KEYLEN = 16
|
2009-07-05 02:48:15 +00:00
|
|
|
IVLEN = 16
|
2007-06-08 04:47:21 +00:00
|
|
|
|
2008-03-24 16:46:06 +00:00
|
|
|
def convergence_hash(k, n, segsize, data, convergence):
|
|
|
|
h = convergence_hasher(k, n, segsize, convergence)
|
|
|
|
h.update(data)
|
|
|
|
return h.digest()
|
|
|
|
def convergence_hasher(k, n, segsize, convergence):
|
|
|
|
assert isinstance(convergence, str)
|
2008-02-07 02:50:47 +00:00
|
|
|
param_tag = netstring("%d,%d,%d" % (k, n, segsize))
|
2008-03-24 16:46:06 +00:00
|
|
|
tag = CONVERGENT_ENCRYPTION_TAG + netstring(convergence) + param_tag
|
2008-02-15 02:58:01 +00:00
|
|
|
return tagged_hasher(tag, KEYLEN)
|
2007-06-08 04:47:21 +00:00
|
|
|
|
2007-06-25 20:23:51 +00:00
|
|
|
def random_key():
|
|
|
|
return os.urandom(KEYLEN)
|
|
|
|
|
2007-08-28 02:00:18 +00:00
|
|
|
def my_renewal_secret_hash(my_secret):
|
2008-02-15 02:58:01 +00:00
|
|
|
return tagged_hash(my_secret, CLIENT_RENEWAL_TAG)
|
2007-08-28 02:00:18 +00:00
|
|
|
def my_cancel_secret_hash(my_secret):
|
2008-02-15 02:58:01 +00:00
|
|
|
return tagged_hash(my_secret, CLIENT_CANCEL_TAG)
|
2007-08-28 02:00:18 +00:00
|
|
|
|
|
|
|
def file_renewal_secret_hash(client_renewal_secret, storage_index):
|
2008-02-15 02:58:01 +00:00
|
|
|
return tagged_pair_hash(FILE_RENEWAL_TAG,
|
2007-08-28 02:00:18 +00:00
|
|
|
client_renewal_secret, storage_index)
|
|
|
|
|
|
|
|
def file_cancel_secret_hash(client_cancel_secret, storage_index):
|
2008-02-15 02:58:01 +00:00
|
|
|
return tagged_pair_hash(FILE_CANCEL_TAG,
|
2007-08-28 02:00:18 +00:00
|
|
|
client_cancel_secret, storage_index)
|
|
|
|
|
|
|
|
def bucket_renewal_secret_hash(file_renewal_secret, peerid):
|
2007-11-07 01:49:59 +00:00
|
|
|
assert len(peerid) == 20, "%s: %r" % (len(peerid), peerid) # binary!
|
2008-02-15 02:58:01 +00:00
|
|
|
return tagged_pair_hash(BUCKET_RENEWAL_TAG, file_renewal_secret, peerid)
|
2007-08-28 02:00:18 +00:00
|
|
|
|
|
|
|
def bucket_cancel_secret_hash(file_cancel_secret, peerid):
|
2007-11-07 01:49:59 +00:00
|
|
|
assert len(peerid) == 20, "%s: %r" % (len(peerid), peerid) # binary!
|
2008-02-15 02:58:01 +00:00
|
|
|
return tagged_pair_hash(BUCKET_CANCEL_TAG, file_cancel_secret, peerid)
|
|
|
|
|
2007-06-26 19:36:21 +00:00
|
|
|
|
|
|
|
def _xor(a, b):
|
|
|
|
return "".join([chr(ord(c) ^ ord(b)) for c in a])
|
|
|
|
|
|
|
|
def hmac(tag, data):
|
|
|
|
ikey = _xor(tag, "\x36")
|
|
|
|
okey = _xor(tag, "\x5c")
|
2007-11-09 21:40:13 +00:00
|
|
|
h1 = SHA256(ikey + data).digest()
|
|
|
|
h2 = SHA256(okey + h1).digest()
|
2007-06-26 19:36:21 +00:00
|
|
|
return h2
|
2007-11-01 22:15:29 +00:00
|
|
|
|
|
|
|
def mutable_rwcap_key_hash(iv, writekey):
|
2008-02-15 02:58:01 +00:00
|
|
|
return tagged_pair_hash(DIRNODE_CHILD_WRITECAP_TAG, iv, writekey, KEYLEN)
|
2009-07-13 00:13:20 +00:00
|
|
|
def mutable_rwcap_salt_hash(writekey):
|
|
|
|
return tagged_hash(DIRNODE_CHILD_SALT_TAG, writekey, IVLEN)
|
2008-02-15 02:58:01 +00:00
|
|
|
|
2007-11-01 22:15:29 +00:00
|
|
|
def ssk_writekey_hash(privkey):
|
2008-02-15 02:58:01 +00:00
|
|
|
return tagged_hash(MUTABLE_WRITEKEY_TAG, privkey, KEYLEN)
|
2007-11-06 04:51:08 +00:00
|
|
|
def ssk_write_enabler_master_hash(writekey):
|
2008-02-15 02:58:01 +00:00
|
|
|
return tagged_hash(MUTABLE_WRITE_ENABLER_MASTER_TAG, writekey)
|
2007-11-07 01:49:59 +00:00
|
|
|
def ssk_write_enabler_hash(writekey, peerid):
|
|
|
|
assert len(peerid) == 20, "%s: %r" % (len(peerid), peerid) # binary!
|
2007-11-06 04:51:08 +00:00
|
|
|
wem = ssk_write_enabler_master_hash(writekey)
|
2008-02-15 02:58:01 +00:00
|
|
|
return tagged_pair_hash(MUTABLE_WRITE_ENABLER_TAG, wem, peerid)
|
2007-11-06 04:51:08 +00:00
|
|
|
|
2007-11-01 22:15:29 +00:00
|
|
|
def ssk_pubkey_fingerprint_hash(pubkey):
|
2008-02-15 02:58:01 +00:00
|
|
|
return tagged_hash(MUTABLE_PUBKEY_TAG, pubkey)
|
2007-11-01 22:15:29 +00:00
|
|
|
|
|
|
|
def ssk_readkey_hash(writekey):
|
2008-02-15 02:58:01 +00:00
|
|
|
return tagged_hash(MUTABLE_READKEY_TAG, writekey, KEYLEN)
|
2007-11-03 03:51:39 +00:00
|
|
|
def ssk_readkey_data_hash(IV, readkey):
|
2008-02-15 02:58:01 +00:00
|
|
|
return tagged_pair_hash(MUTABLE_DATAKEY_TAG, IV, readkey, KEYLEN)
|
2007-11-01 22:15:29 +00:00
|
|
|
def ssk_storage_index_hash(readkey):
|
2008-02-15 02:58:01 +00:00
|
|
|
return tagged_hash(MUTABLE_STORAGEINDEX_TAG, readkey, KEYLEN)
|
2009-03-23 03:20:55 +00:00
|
|
|
|
|
|
|
def constant_time_compare(a, b):
|
|
|
|
n = os.urandom(8)
|
|
|
|
return bool(tagged_hash(n, a) == tagged_hash(n, b))
|
2009-11-26 23:42:57 +00:00
|
|
|
|
|
|
|
BACKUPDB_DIRHASH_TAG = "allmydata_backupdb_dirhash_v1"
|
|
|
|
def backupdb_dirhash(contents):
|
|
|
|
return tagged_hash(BACKUPDB_DIRHASH_TAG, contents)
|