<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jared Stanbrough</title>
    <description>The latest articles on DEV Community by Jared Stanbrough (@jareds).</description>
    <link>https://dev.to/jareds</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F703018%2F6e6188a3-d392-4ecb-907e-8a68e2720a88.jpeg</url>
      <title>DEV Community: Jared Stanbrough</title>
      <link>https://dev.to/jareds</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jareds"/>
    <language>en</language>
    <item>
      <title>How to get started with Rust for RISC-V Linux</title>
      <dc:creator>Jared Stanbrough</dc:creator>
      <pubDate>Wed, 17 Nov 2021 20:14:21 +0000</pubDate>
      <link>https://dev.to/jareds/how-to-get-started-with-rust-for-risc-v-linux-2fop</link>
      <guid>https://dev.to/jareds/how-to-get-started-with-rust-for-risc-v-linux-2fop</guid>
      <description>&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/build-trust" rel="noopener noreferrer"&gt;
        build-trust
      &lt;/a&gt; / &lt;a href="https://github.com/build-trust/ockam" rel="noopener noreferrer"&gt;
        ockam
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Orchestrate end-to-end encryption, cryptographic identities, mutual authentication, and authorization policies between distributed applications – at massive scale.
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;
 

&lt;p&gt;In this hands-on guide, we'll introduce RISC-V, and show how to cross compile a Rust example of Ockam for RISC-V Linux systems. &lt;/p&gt;

&lt;p&gt;We'll also see how to test RISC-V Linux programs using qemu (a hardware emulator).&lt;/p&gt;

&lt;p&gt;Before we dive in, let's start with a ~2 minute demo of how to run Ockam's end-to-end encryption example on Microchip's PolarFire SoC Icicle kit (which includes 4 RISC-V cores).&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/RdIEyVZyPMY"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  What is RISC-V?
&lt;/h2&gt;

&lt;p&gt;RISC-V is a free, open CPU instruction set architecture (ISA) that is revolutionizing hardware. The open source nature of the ISA has allowed global innovation and creativity in much the same way Open Source revolutionized software. Since its first publication from UC Berkeley in 2011, adoption of RISC-V has seen incredible growth. Organizations like RISC-V International collaborate with dozens of companies, academic institutions, and passionate individuals to propel the ecosystem forward.&lt;/p&gt;

&lt;p&gt;Open architectures such as RISC-V provide transparency down to the deepest levels of a system. For systems that require strong security and safety guarantees, an open ISA provides the foundation of a completely understood supply chain.&lt;/p&gt;

&lt;p&gt;RISC-V is also extensible: additional instructions can be added to perform new operations. This ability is being used to add many features to the ISA, including cryptographic extensions. These extensions enable the development of specialized processors such as Secure Enclaves and Trusted Execution Environments.&lt;/p&gt;

&lt;h2&gt;
  
  
  About the RISC-V ISA
&lt;/h2&gt;

&lt;p&gt;RISC-V cores come in many shapes and sizes, because the ISA is so flexible and extensible.&lt;/p&gt;

&lt;p&gt;There are 32-bit, 64-bit and even 128-bit versions of the core integer instructions.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;rv32 is the 32-bit configuration suitable for microcontrollers and other small, low power systems.&lt;/li&gt;
&lt;li&gt;rv64 is the 64-bit general purpose, server class configuration. RISC-V Linux projects target the RV64.&lt;/li&gt;
&lt;li&gt;rv128 is a mostly theoretical 128-bit configuration.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;RISC-V cores are also configurable using what are called instruction set variants. Variants add additional instructions to the core ISA. Several variants have been officially standardized, and more are on the way. Variants are referenced by a single letter code such as I, M and C.&lt;/p&gt;

&lt;p&gt;The most important RISC-V variants are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;I&lt;/strong&gt; - The base integer instructions. All RISC-V cores implement this.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;M&lt;/strong&gt; - Multiplication instructions. Most cores implement this, but some very small microcontrollers do not.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A&lt;/strong&gt; - Atomic instructions. These instructions enable multi-core systems to have consistent views of memory.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;F&lt;/strong&gt; - Floating point instructions. Often omitted on microcontroller configurations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;D&lt;/strong&gt; - Double floating point instructions. Often omitted on microcontroller configurations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;C&lt;/strong&gt; - Compressed instructions. Similar to ARM's Thumb instructions. Reduces code size. Sometimes omitted.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;G&lt;/strong&gt; - Short hand for the set of &lt;strong&gt;IMAFD&lt;/strong&gt; variants.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;RISC-V cores are referred to by their bit-width and set of supported variants. For example, a common class of microcontroller configurations is &lt;code&gt;rv32imc&lt;/code&gt; which is a 32-bit processor with a multiplier and compressed instructions.&lt;/p&gt;

&lt;p&gt;The RISC-V configuration targeted by Linux distributions is typically &lt;code&gt;rv64gc&lt;/code&gt;, although it is possible to run Linux on other configurations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rust on RISC-V
&lt;/h2&gt;

&lt;p&gt;Rust supports several RISC-V targets. Rust targets are split into &lt;a href="https://doc.rust-lang.org/nightly/rustc/platform-support.html" rel="noopener noreferrer"&gt;several tiers&lt;/a&gt;,&lt;br&gt;
which have different guarantees with respect to support and stability.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;riscv64-unknown-linux-gnu&lt;/code&gt; is a "Tier 2 With Host Tools" target that has support for &lt;code&gt;std&lt;/code&gt; running on Linux.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;riscv64gc-unknown-none-elf&lt;/code&gt; and &lt;code&gt;riscv64imac-unknown-none-elf&lt;/code&gt; are Tier 2 targets that can be used in bare metal projects.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;riscv32i&lt;/code&gt;, &lt;code&gt;riscv32imc&lt;/code&gt; and &lt;code&gt;riscv32imac&lt;/code&gt; &lt;code&gt;-unknown-none-elf&lt;/code&gt; are the 32-bit Tier 2 targets that can be used in bare metal projects.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;riscv32imc-esp-espidf&lt;/code&gt; is a Tier 3 target that supports ESP RISC-V chips like the esp32-c3.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Rust programs targeting RISC-V can be built with or without the &lt;code&gt;std&lt;/code&gt; library. Building &lt;code&gt;no_std&lt;/code&gt; applications is a complex topic, and requires details about the underlying hardware being targeted. Additionally, an allocator is often needed if the application requires a heap.&lt;/p&gt;

&lt;p&gt;This guide focuses on &lt;code&gt;std&lt;/code&gt; projects for RISC-V running Linux. Ockam support for &lt;code&gt;no_std&lt;/code&gt; is an ongoing effort with initial support for several ARM boards. In the future, Ockam &lt;code&gt;no_std&lt;/code&gt; support will be extended to the RISC-V ecosystem.&lt;/p&gt;

&lt;p&gt;Cross compiling for a different processor requires the presence of a toolchain and libraries for that target. There are a variety of ways to cross compile to RISC-V:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;a href="https://github.com/rust-embedded/cross" rel="noopener noreferrer"&gt;cargo cross&lt;/a&gt;. This is the fastest and easiest way to get started.&lt;/li&gt;
&lt;li&gt;Install cross compiler packages from your OS distribution. Debian for example, has many riscv64 tools.&lt;/li&gt;
&lt;li&gt;Build and install the official &lt;a href="https://github.com/riscv-collab/riscv-gnu-toolchain" rel="noopener noreferrer"&gt;riscv-gnu-toolchain&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Rust on RISC-V with cargo cross
&lt;/h3&gt;

&lt;p&gt;First, install cross:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cargo &lt;span class="nb"&gt;install &lt;/span&gt;cross
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Create a new Rust project:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cargo new &lt;span class="nt"&gt;--bin&lt;/span&gt; ockam_rv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Add the Ockam dependency to the project's &lt;code&gt;Cargo.toml&lt;/code&gt; dependencies:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="nn"&gt;[dependencies]&lt;/span&gt;
&lt;span class="py"&gt;ockam&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now let's turn &lt;code&gt;main.rs&lt;/code&gt; into an Ockam Node:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;ockam&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;#[ockam::node]&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, Ockam!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="nf"&gt;.stop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;code&gt;cross&lt;/code&gt; takes the same arguments as &lt;code&gt;cargo&lt;/code&gt;. To build the project, run:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cross build &lt;span class="nt"&gt;--target&lt;/span&gt; riscv64gc-unknown-linux-gnu
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This will generate a RISC-V binary in the &lt;code&gt;target/riscv64gc-unknown-linux-gnu/debug/&lt;/code&gt; directory!&lt;/p&gt;

&lt;p&gt;You can also run the project in an emulated environment with &lt;code&gt;cross&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cross run &lt;span class="nt"&gt;--target&lt;/span&gt; riscv64gc-unknown-linux-gnu
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;You should see output similar to:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Finished dev [unoptimized + debuginfo] target(s) in 0.04s
Running `/linux-runner riscv64 /target/riscv64gc-unknown-linux-gnu/debug/ockam_rv`
2021-11-03T19:52:01.495439Z  INFO ockam_node::node: Initializing ockam node
Hello, Ockam!
2021-11-03T19:52:01.586869Z  INFO ockam_node::context: Shutting down all workers
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In most cases, &lt;code&gt;cross&lt;/code&gt; is sufficient for cross compiling to RISC-V Linux. However, it does require Docker or Podman.&lt;/p&gt;
&lt;h3&gt;
  
  
  Hello Ockam
&lt;/h3&gt;

&lt;p&gt;Let's create an encrypted secure channel between Alice and Bob. When a message is sent through this channel it will be encrypted when it enters the channel and decrypted just before it exits the channel.&lt;/p&gt;

&lt;p&gt;For the purpose of our example, we'll create a local channel within one program. In our &lt;a href="https://github.com/ockam-network/ockam/tree/develop/documentation/guides/rust#readme" rel="noopener noreferrer"&gt;other examples&lt;/a&gt;, you'll see that it's just as easy to create end-to-end protected channels over multi-hop, multi-protocol transport routes:&lt;/p&gt;

&lt;p&gt;Replace the contents of &lt;code&gt;src/main.rs&lt;/code&gt; with the following code:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;ockam&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="n"&gt;route&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Entity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;TrustEveryonePolicy&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Vault&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nd"&gt;#[ockam::node]&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Create a Vault to safely store secret keys for Alice and Bob.&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;vault&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Vault&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Create an Entity to represent Bob.&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;bob&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Entity&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;vault&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Create a secure channel listener for Bob that will wait for requests to&lt;/span&gt;
    &lt;span class="c1"&gt;// initiate an Authenticated Key Exchange.&lt;/span&gt;
    &lt;span class="n"&gt;bob&lt;/span&gt;&lt;span class="nf"&gt;.create_secure_channel_listener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"bob"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;TrustEveryonePolicy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Create an entity to represent Alice.&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;alice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Entity&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;vault&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// As Alice, connect to Bob's secure channel listener and perform an&lt;/span&gt;
    &lt;span class="c1"&gt;// Authenticated Key Exchange to establish an encrypted secure channel with Bob.&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;channel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;alice&lt;/span&gt;&lt;span class="nf"&gt;.create_secure_channel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"bob"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;TrustEveryonePolicy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Send a message, ** THROUGH ** the secure channel,&lt;/span&gt;
    &lt;span class="c1"&gt;// to the "app" worker on the other side.&lt;/span&gt;
    &lt;span class="c1"&gt;//&lt;/span&gt;
    &lt;span class="c1"&gt;// This message will automatically get encrypted when it enters the channel&lt;/span&gt;
    &lt;span class="c1"&gt;// and decrypted just before it exits the channel.&lt;/span&gt;
    &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="nf"&gt;.send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;route!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"app"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="s"&gt;"Hello Ockam!"&lt;/span&gt;&lt;span class="nf"&gt;.to_string&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Wait to receive a message for the "app" worker and print it.&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="py"&gt;.receive&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"App Received: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// should print "Hello Ockam!"&lt;/span&gt;

    &lt;span class="c1"&gt;// Stop all workers, stop the node, cleanup and return.&lt;/span&gt;
    &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="nf"&gt;.stop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;A lot happened when you ran this small example. It created a secure vault, spawned workers to represent entities, established a mutually authenticated channel and then routed a message through that channel. This involved running cryptographic protocols for generating keys, authenticating as an entity, performing an authenticated key exchange and exchanging messages with authenticated encryption.&lt;/p&gt;
&lt;h3&gt;
  
  
  Rust on RISC-V with OS toolchains
&lt;/h3&gt;

&lt;p&gt;The process for installing cross-compiling tools differs between OSes and distributions. The primary requirement for building Rust executables for RISC-V is the presence of a RISC-V linker, such as GCC.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For Debian: &lt;code&gt;apt-get install gcc-riscv64-linux-gnu&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;For MacOS: &lt;code&gt;brew tap riscv/riscv &amp;amp;&amp;amp; brew install --cc=gcc-10 riscv-tools&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In your &lt;code&gt;ockam_rv&lt;/code&gt; project, create a &lt;code&gt;.cargo/config.toml&lt;/code&gt; file. In this file, we specify that we're builing for RISC-V&lt;br&gt;
and also configure the location of the linker.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="nn"&gt;[build]&lt;/span&gt;
&lt;span class="py"&gt;target&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"riscv64gc-unknown-linux-gnu"&lt;/span&gt;

&lt;span class="nn"&gt;[target.riscv64gc-unknown-linux-gnu]&lt;/span&gt;
&lt;span class="py"&gt;linker&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"/path/to/your/riscv64-unknown-linux-gnu-gcc"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now when you run &lt;code&gt;cargo build&lt;/code&gt;, your local cross-compiling toolchain will be used to link the program, which is available in &lt;code&gt;target/riscv64gc-unknown-linux-gnu/debug&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can now test this binary in an emulated RISC-V environment, or hardware. It is important to remember that glibc versions can differ between Linux distribution versions. Ensure that your emulated environment has a glibc version equal to or greater than used by your build tools.&lt;/p&gt;
&lt;h3&gt;
  
  
  Rust on RISC-V with the official toolchain
&lt;/h3&gt;

&lt;p&gt;If packages are not available for RISC-V development on your OS, you may need to clone and build the tools yourself.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Clone the git repo &lt;code&gt;https://github.com/riscv-collab/riscv-gnu-toolchain&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Follow the instructions to install pre-requisites for your system.&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;./configure --prefix=/opt/riscv&lt;/code&gt; - Change the prefix path as appropriate.&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;make linux&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The tools default to building for RV64GC, which is perfect for Rust on RISC-V Linux. When the build is done, you will have &lt;code&gt;/opt/riscv/bin/riscv64-unknown-linux-gnu-gcc&lt;/code&gt; along with other tools and libraries.&lt;/p&gt;

&lt;p&gt;Configure your Cargo project to use this as the linker.&lt;/p&gt;
&lt;h3&gt;
  
  
  Testing with qemu and Debian
&lt;/h3&gt;

&lt;p&gt;A great way to test RISC-V programs is by using qemu and Debian Quick Image Baker pre-baked images.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go to &lt;a href="https://people.debian.org/%7Egio/dqib/" rel="noopener noreferrer"&gt;https://people.debian.org/~gio/dqib/&lt;/a&gt; and download &lt;code&gt;Images for riscv64-virt&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Uncompress the downloaded &lt;code&gt;artifacts.zip&lt;/code&gt; file.&lt;/li&gt;
&lt;li&gt;Execute &lt;code&gt;./run.sh&lt;/code&gt; to boot your RISC-V Linux system!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;From here, you can either copy binaries into your vm over the local network or mounted filesystem.&lt;/p&gt;
&lt;h3&gt;
  
  
  Testing with qemu and buildroot
&lt;/h3&gt;

&lt;p&gt;Buildroot is also a great way to test RISC-V programs, especially in custom or constrained environments.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Download buildroot or clone from &lt;code&gt;https://github.com/buildroot/buildroot&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;make qemu_riscv64_virt_defconfig&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Either run &lt;code&gt;make&lt;/code&gt; to build the system or &lt;code&gt;make menuconfig&lt;/code&gt; to tweak any settings.&lt;/li&gt;
&lt;li&gt;The resulting system will be built in &lt;code&gt;output/images/&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;./start-qemu.sh&lt;/code&gt; in the &lt;code&gt;output/images&lt;/code&gt; directory.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Like with Debian on qemu, you can copy your test binary over the network, or use any other facility that qemu provides.&lt;/p&gt;

&lt;p&gt;In this example we walked through how to build and run Ockam in Rust for RISC-V Linux. We then looked at how to test these Rust programs with qemu and run them on RISC-V hardware like Microchip Polarfire Icicle kit.&lt;/p&gt;

&lt;p&gt;To learn more, check out our guides on &lt;a href="https://github.com/ockam-network/ockam/tree/develop/documentation/use-cases/end-to-end-encryption-with-rust#readme" rel="noopener noreferrer"&gt;End-to-End Encryption with Rust&lt;/a&gt;.&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/build-trust" rel="noopener noreferrer"&gt;
        build-trust
      &lt;/a&gt; / &lt;a href="https://github.com/build-trust/ockam" rel="noopener noreferrer"&gt;
        ockam
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Orchestrate end-to-end encryption, cryptographic identities, mutual authentication, and authorization policies between distributed applications – at massive scale.
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;



</description>
      <category>rust</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
