aboutsummaryrefslogtreecommitdiffstats
path: root/techlibs/xilinx/tests/bram2.v
blob: 0a6013ca66f5adb3643d98cf73f745443ec8b92d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module myram(
	input		  rd_clk,
	input	   [ 7:0] rd_addr,
	output reg [17:0] rd_data,
	input		  wr_clk,
	input		  wr_enable,
	input	   [ 7:0] wr_addr,
	input	   [17:0] wr_data
);
	reg [17:0] memory [0:255];
	integer i;

	function [17:0] hash(input [7:0] k);
		reg [31:0] x;
		begin
			x = {k, ~k, k, ~k};
			x = x ^ (x << 13);
			x = x ^ (x >> 17);
			x = x ^ (x << 5);
			hash = x;
		end
	endfunction

	initial begin
		for (i = 0; i < 256; i = i+1)
			memory[i] = hash(i);
	end

	always @(posedge rd_clk)
		rd_data <= memory[rd_addr];

	always @(posedge wr_clk)
		if (wr_enable)
			memory[wr_addr] <= wr_data;
endmodule
from cryptography.hazmat.primitives.kdf import KeyDerivationFunction def _int_to_u32be(n): return struct.pack('>I', n) def _common_args_checks(algorithm, length, otherinfo): max_length = algorithm.digest_size * (2 ** 32 - 1) if length > max_length: raise ValueError( "Can not derive keys larger than {0} bits.".format( max_length )) if not (otherinfo is None or isinstance(otherinfo, bytes)): raise TypeError("otherinfo must be bytes.") def _concatkdf_derive(key_material, length, auxfn, otherinfo): if not isinstance(key_material, bytes): raise TypeError("key_material must be bytes.") output = [b""] outlen = 0 counter = 1 while (length > outlen): h = auxfn() h.update(_int_to_u32be(counter)) h.update(key_material) h.update(otherinfo) output.append(h.finalize()) outlen += len(output[-1]) counter += 1 return b"".join(output)[:length] @utils.register_interface(KeyDerivationFunction) class ConcatKDFHash(object): def __init__(self, algorithm, length, otherinfo, backend): _common_args_checks(algorithm, length, otherinfo) self._algorithm = algorithm self._length = length self._otherinfo = otherinfo if self._otherinfo is None: self._otherinfo = b"" if not isinstance(backend, HashBackend): raise UnsupportedAlgorithm( "Backend object does not implement HashBackend.", _Reasons.BACKEND_MISSING_INTERFACE ) self._backend = backend self._used = False def _hash(self): return hashes.Hash(self._algorithm, self._backend) def derive(self, key_material): if self._used: raise AlreadyFinalized self._used = True return _concatkdf_derive(key_material, self._length, self._hash, self._otherinfo) def verify(self, key_material, expected_key): if not constant_time.bytes_eq(self.derive(key_material), expected_key): raise InvalidKey @utils.register_interface(KeyDerivationFunction) class ConcatKDFHMAC(object): def __init__(self, algorithm, length, salt, otherinfo, backend): _common_args_checks(algorithm, length, otherinfo) self._algorithm = algorithm self._length = length self._otherinfo = otherinfo if self._otherinfo is None: self._otherinfo = b"" if not (salt is None or isinstance(salt, bytes)): raise TypeError("salt must be bytes.") if salt is None: salt = b"\x00" * algorithm.block_size self._salt = salt if not isinstance(backend, HMACBackend): raise UnsupportedAlgorithm( "Backend object does not implement HMACBackend.", _Reasons.BACKEND_MISSING_INTERFACE ) self._backend = backend self._used = False def _hmac(self): return hmac.HMAC(self._salt, self._algorithm, self._backend) def derive(self, key_material): if self._used: raise AlreadyFinalized self._used = True return _concatkdf_derive(key_material, self._length, self._hmac, self._otherinfo) def verify(self, key_material, expected_key): if not constant_time.bytes_eq(self.derive(key_material), expected_key): raise InvalidKey