Beispiel #1
0
 def test_checksumsmall(self):
     s = xbxdb.scoped_session()
     primitive = s.query(
         xbxc.Primitive).filter(xbxc.Primitive.name == "keccakc512").one()
     self.assertEqual(
         "995ec6efa3a2e23e34c2b0fba46e55fd634c414173019d6437858741a70a82f2",
         primitive.checksumsmall)
Beispiel #2
0
 def _calculate_power(self):
     s = xbxdb.scoped_session()
     power, current, voltage, avgpwr, maxpwr = self.xbh.get_power()
     this.power_samples = PowerSample(power=power,
                                      current=current,
                                      voltage=voltage,
                                      run=self)
     this.max_power = maxpwr
     this.avg_power = avgpwr
Beispiel #3
0
def main():
    logging.config.fileConfig("logging.ini", disable_existing_loggers=False)
    xbxdb.init(xbxu.get_db_path(CONFIG_PATH))
    s = xbxdb.scoped_session()

    config = xbxc.Config(CONFIG_PATH)

    bs = xbxb.BuildSession(config)
    bs.buildall()

    s.add(bs)
    s.commit()
Beispiel #4
0
    def runall(self):
        for b in (i for i in self.build_session.builds if i.build_ok):
            s = xbxdb.scoped_session()
            be = BuildExec(self, b)

            # If not already run w/ a pass/fail, or if rerunning results is
            # enabled, then do run
            if be.test_ok == None or self.config.rerun:
                be.load_build()
                be.execute()
                s.add(be)
                s.commit()
Beispiel #5
0
def _get_build_session(config, build_session_id=None):
    try:
        s = xbxdb.scoped_session()
        if build_session_id == None:
            return (s.query(xbxb.BuildSession).join(
                "config", "operation").filter(
                    xbxc.Operation.name == config.operation.name).order_by(
                        xbxb.BuildSession.timestamp.desc())).first()
        else:
            return s.query(xbxb.BuildSession).filter(
                BuildSession.id == build_session_id).one()
    except NoResultFound as e:
        raise NoBuildSessionError("Build must be run first") from e
Beispiel #6
0
def main():
    logging.config.fileConfig("logging.ini", disable_existing_loggers=False)
    xbxdb.init(xbxu.get_db_path(CONFIG_PATH))
    s = xbxdb.scoped_session()

    config = xbxc.Config(CONFIG_PATH)

    logging.config.fileConfig("logging.ini", disable_existing_loggers=False)
    config = xbxc.Config("config.ini")

    rs = xbxr.RunSession(config)
    rs.init_xbh()
    rs.runall()
    s.add(rs)
    s.commit()
Beispiel #7
0
    def test_validate_implementation(self):
        s = xbxdb.scoped_session()
        # Assume only one instance, since we nuked db earlier
        impls = s.query(xbxc.Implementation).filter(
            xbxc.Implementation.name == "simple",
            xbxc.Implementation.primitive_name == "keccakc512").one()

        impl = impls

        testname = os.path.join(impl.path, "test")

        self.assertTrue(impl.valid_hash)
        with open(testname, "w") as f:
            f.write("test")
        self.assertFalse(impl.valid_hash)
        os.remove(testname)
        self.assertTrue(impl.valid_hash)
Beispiel #8
0
    def setUpClass(cls):
        config = xbxc.Config(CONFIG_PATH)

        bs = xbxb.BuildSession(config)
        bs.buildall()

        s = xbxdb.scoped_session()
        s.add(bs)

        XbdCryptoHashTest.build = (s.query(xbxb.Build).join(
            xbxc.Implementation).filter(
                xbxb.Build.platform == config.platform,
                xbxb.Build.operation_name == "crypto_hash",
                xbxb.Build.primitive_name == "keccakc512",
                xbxc.Implementation.name == "simple")).one()

        XbdCryptoHashTest.xbh = xbhlib.Xbh(config.xbh_addr, config.xbh_port,
                                           config.platform.pagesize,
                                           config.platform.clock_hz)

        if UPLOAD:
            XbdCryptoHashTest.xbh.upload_prog(XbdCryptoHashTest.build.hex_path)
Beispiel #9
0
    def init_xbh(self):
        c = self.config
        s = xbxdb.scoped_session()
        try:
            self.xbh = xbhlib.Xbh(c.xbh_addr, c.xbh_port, c.platform.pagesize,
                                  c.platform.clock_hz, c.xbh_timeout)

        except xbhlib.Error as e:
            logger.critical(str(e))
            sys.exit(1)

        self.xbh_rev, self.xbh_mac = self.xbh.get_xbh_rev()

        if not self.drift_measurements:
            self.do_drift_measurement()

        s.add(self)
        s.commit()
        # Initialize xbh attribute on all build_execs and build_exec.runs if items loaded
        # from db
        for be in self.build_execs:
            be.xbh = self.xbh
Beispiel #10
0
    def __init__(self, config_path, **kwargs):
        super().__init__(**kwargs)
        _logger.debug("Loading configuration")
        config = configparser.ConfigParser()
        config.read_file(open(DEFAULT_CONF))
        config.read(config_path)
        self.config_path = config_path

        self.hash = hash_config(config_path)

        ## Basic path configuration
        self.platforms_path = config.get('paths', 'platforms')
        self.algopack_path = config.get('paths', 'algopacks')
        self.embedded_path = config.get('paths', 'embedded')
        self.work_path = config.get('paths', 'work')
        self.data_path = config.get('paths', 'data')
        self.impl_conf_path = config.get('paths', 'impl_conf')

        self.one_compiler = config.getboolean('build', 'one_compiler')
        self.parallel_build = config.getboolean('build', 'parallel_build')

        # XBH address
        self.xbh_addr = config.get('xbh', 'address')
        self.xbh_port = config.get('xbh', 'port')

        # Platform
        self.platform = Platform.from_path(config.get('hardware', 'platform'),
                                           self.platforms_path)

        # Operation
        name = config.get('algorithm', 'operation')
        op_filename = config.get('paths', 'operations')
        self.operation = _enum_operation(name, op_filename)

        # Runtime parameters
        self.rerun = config.getboolean('run', 'rerun')
        self.drift_measurements = config.getint('run', 'drift_measurements')
        self.checksum_tests = config.getint('run', 'checksum_tests')
        self.xbh_timeout = config.getint('run', 'xbh_timeout')
        self.exec_runs = config.getint('run', 'exec_runs')

        # Parameters
        self.operation_params = []
        op_params = config.get('run',
                               self.operation.name + "_parameters").split("\n")
        for line in op_params:
            if line:
                row = line.split(',')
                row = list(map(lambda val: int(val), row))
                self.operation_params += [row]

        primitives = config.get('algorithm', 'primitives').strip().split("\n")

        impl_conf = configparser.ConfigParser()
        impl_conf.read(self.impl_conf_path)

        self.enforce_bwlist_checksums = config.getboolean(
            'implementation', 'enforce_bwlist_checksums')

        self.implementations = _enum_prim_impls(self.operation, primitives,
                                                self.algopack_path)

        self._process_black_white_lists(config, impl_conf)

        # Need to flush in order to populate associations
        s = xbxdb.scoped_session()
        s.add(self)
        s.flush()

        self._enum_supercop_impls(config, impl_conf)
        self._enum_dependencies(config, impl_conf)

        # Update after implementations updated for black/whitelists and
        # dependencies
        s.flush()
Beispiel #11
0
 def get_config_assoc(self, config):
     s = xbxdb.scoped_session()
     a = s.query(ConfigImplAssociation).filter(
         ConfigImplAssociation.config_hash == config.hash,
         ConfigImplAssociation.implementation_hash == self.hash).one()
     return a
Beispiel #12
0
    def run(cls, build_exec, params=None):
        """Factory method to create and execute run instances.

        Call this instead of constructor
        """
        _logger.info("Running benchmark on {} with plaintext length {} and "
                     "associated data length {}".
                     format(build_exec.build.buildid, params[0], params[1]))

        primitive = build_exec.build.primitive;


        enc_run = cls(build_exec, plaintext_len=params[0],
                      assoc_data_len=params[1], mode=CryptoAeadRun.ENCRYPT)
        dec_run = cls(build_exec, plaintext_len=params[0],
                      assoc_data_len=params[1], mode=CryptoAeadRun.DECRYPT)
        dec_forged_run = cls(build_exec, plaintext_len=params[0],
                      assoc_data_len=params[1], mode=CryptoAeadRun.FORGED_DEC)

        # Set identical pair ids so we know encryption/decryption pairs
        s = xbxdb.scoped_session()
        s.add(enc_run)
        s.add(dec_run)
        s.add(dec_forged_run)
        s.flush()  # Need to add and flush to get id
        enc_run.data_id = enc_run.id
        dec_run.data_id = enc_run.id
        dec_forged_run.data_id = enc_run.id

        # Generate data
        (enc_data, plaintext, assoc_data,
         key, secret_num, public_num) = enc_run._gen_enc_params()
        
        retval, ciphertext = enc_run._execute(enc_data)
        enc_run._calculate_power()

        if retval != 0:
            raise xbxr.XbdResultFailError(
                "XBD execute returned {}".format(retval))

        dec_data = CryptoAeadRun._assemble_dec_params(ciphertext, assoc_data,
                                                  public_num, key)

        retval, dec_output = dec_run._execute(dec_data)
        dec_run._calculate_power()

        # Unpack lengths
        # We put secret num first since it is fixed length
        fmt = "!II"
        secnum_len, plaintext_len = struct.unpack_from(fmt, dec_output)

        # Unpack data
        (decrypted_secret_num,
         decrypted_plaintext) = struct.unpack(
             "{}s{}s".format(secnum_len, plaintext_len),
             dec_output[struct.calcsize(fmt):]
         )

        if ((decrypted_plaintext != plaintext or
             decrypted_secret_num != secret_num) and
                primitive.name != '0cipher'):
            raise xbxr.XbdResultFailError(
                "Decrypted plaintext does not match CipherText")

        forged_ciphertext = bytearray(ciphertext)

        #Tamper with last byte of ciphertext
        if len(ciphertext):
            forged_ciphertext[-1] = (forged_ciphertext[-1] + 1) % 256

        forged_data = CryptoAeadRun._assemble_dec_params(forged_ciphertext,
                                                         assoc_data, public_num,
                                                         key)

        retval,_ = dec_forged_run._execute(forged_data)
        dec_forged_run = _calculate_power()
        
        if retval != -1 and primitive.name != '0cipher':
            raise xbxr.XbdResultFailError(
                "Message forgery not detected")

        return enc_run, dec_run, dec_forged_run
Beispiel #13
0
 def setUp(self):
     self.config = xbxc.Config(CONFIG_PATH)
     s = xbxdb.scoped_session()
     s.add(self.config)