Arduino Uno as HID keyboard

Turin is the hometown of Arduino. I have been at the fablab multiple times but I had to come all the way to America to get my hands on a simple Arduino Uno.

For 60$ I bought a cheap (but still good!) mechanical keyboard by Qisan, a clone of the Arduino Uno and a USB host shield.

Given that is 3 years since I have been using a dvorak layout and it's a pain to change layout on every machine that you have to use. You can imagine that given this three pieces of hardware together I put together an hardware key mapper for the keyboard.

I have never had experience with Arduino before but it was not that difficult to make it do simple things like blinking the led or send signal through to a serial monitor.

It took me half an hour to wear down all my excitement: the USB Host Shield library broke all the compatibility with the similar project I found wandering online.

In particular this blog has the most precious information and the guy wrote a HID driver that allows the Uno to be seen as a HID device.

It was a noob error but I didn't checked the various arduino alternatives and I discovered late that just a few have the HID capabilities that would make this work easier. I should have bought and Arduino Due or Leonardo maybe.

Also, the various guides about flashing with a dfu tool are specific to older models of the Uno and it took me some time to figure the name of the new components so that I could flash a new firmware.

A small journey in the Arduino world

It feels pretentious to write a little guide for this kind of work, given also the fact that I have roughly 10 hours of experience with the Arduino. But the other resources are really outdated so I hope this piece can be useful to someone out there.

All the files I have used today are on my repos and I included also an outdated version of the USB Host Shield library that I used.

The original code from this blog post works like a charm but just as a simple passthrough.

It was not difficult at all to examine the code: during each loop of the iteration a char array gets read from the shield and if it is contains information Arduino with the Serial.Write method send the data to the host.

The buffer array is a simple array of length 8 and the first two positions are reserved. In particular the first one represent the various modifier keys.

The dvorak layout has the same pairs as the US layout but eventually I got used to having the '@' where at the same place of 'Q' (qwerty) and '"' over the '2'. Also, I am an avid vim user (I should thank Simone Basso for that) and I swapped some keys on the new 65 keys keyboard. The modifier bit at the beginning of the array came in handy for my code.

An hardware key remapper is a simple but long switch C statement but I decided to consider also the modifier bit: in this way certain keys like the Window (UGH!) key is mapped to a different layer of keys. I got all the codes for the HID events here.

The process of flashing the code on the Uno goes like this:

  • write the looping code;
  • push it to the Arduino using the IDE;
  • shortcircuit the board so that it goes in DFU mode;
  • flash the .hex HID firmware;
  • try your code;
  • repeat until it's right.

Everything fits in one picture

Flashing the firmware

The firmware is in my repo but I got it from (here)[]. The tool I used to flash it is dfu-programmer (version 0.62). Every time you want to flash a new firmware the Arduino must be put in DFU mode (you can see the difference with lsusb). To do that simply create a shortcircuit using a small metal wire on the two pins near the reset button and a led will blink. This video shows the method briefly (no real need for a jumper). The commands are the following and there is no risk to brick the Uno:

dfu-programmer atmega16u2 erase
dfu-programmer atmega16u2 flash Arduino-keyboard-0.2.hex
dfu-programmer atmega16u2 reset

After each flashing the device needs to be disconnected once. Of course you can flash the original firmware back. It is included in my repo or on the official ones.

Arduino and the shield

That's it, as you can see is not difficult at all. The worst part is gathering the various info that are left dormant in blogs or forums.

Lifehacks (2)

  • If you're at a party and you don't know anyone, make it a point to meet the host and introduce yourself. The host can introduce you to other guys/girls and it scores you points.

  • Never buy high-end cables, and never buy cables at retail. Cables have higher profit margins than almost everything except extended warranties. Despite what the marketing and sales people will tell you, there is no difference. Need a computer cable? Order it from a wholesaler online. That USB cable that your printer requires will cost you $25 at Staples and $1.50 at Newegg.

  • Never quote an entire post unless it's shorter than the one you write in response.

  • Don't eat food after 6pm.

  • In college, always check the library to see if the teacher is using a test bank.

Interpolation using a genetic algorithm

This weekend I was in Milan to get a visa and I had the opportunity to work with a friend, Michele, on genetic algorithms. It was the first time I dig up in such field and it was very exciting. In this post I want to explain some bits of our work.

A brief introduction to GA

A genetic algorithm is a search/optimization algorithm that uses an heuristic approach to reduce the search space and evolve gradually to a solution.


It is an algorithm that has its root in the theory of natural selectioni by Charles Darwin. The main components of a GA are:

  • the population, that concentrate all the available solutions at a given time;
  • the fitness function, that gives an approximation of the quality of the solution codified by a given member of the population.

In a GA the first thing to do is to generate a population.

A population is a group of objects with given attributes, usually a string, and they contains in some form the solution (usually inside a string); the first population is randomly generated and contains a big number of solutions, but not every solution (this is not a bruteforce approach).

After this step the fitness functions evaluates the quality of every solution that a given member carries: the evaluation should be considered from a bottom up point of view.


Now, as in Darwin's theory of evolution, the member of the population are going to "reproduce": two members are going to be coupled to generate a new member of the second generation and every child member will contain a solution that is the product of the original genes of their parent members.

This time the reproduction of the population into a second one is not entirely random. The fitness function gives us an approximation of the quality of every gene that a member carries and by the rule of the "survival by the fittest" the probability that a member is going to reproduce with another one is proportional to the quality of its genes.

When we have a second generation of members we can recur on our GA and generate a third generation. From this point we can recur until we converge to a solution that is common to every member, or at least that is suited to our needs.


Actually, in some cases, a mutation function can be added, so that, like in real world, some times the genes are "scrambled" indipendently from the fitness function.

There is more to a GA, for example we could talk about possible ways of storing the genes inside a member or when to use mutation, anyway I want to stop here and continue with an analysis of my problem.

Interpolating a function using a GA

Me and Michele decided to spend some time developing a little python script to explore GA capabilities and we decided to interpolate some points on a cartesian plane.

Our program, that is available here uses a class to define the various members of the population and a string for the genes, a class as well for the points on the plane.

The fitness function is not as precise as it should be because this is only a proof of concept:

mutationProbability = 0.1
rangeLimit = 5
def fitness(item, pointList, n):
    value = 0
    for p in pointList:
        y = 0
        for i in range(n):
           y += item.gene[i] * pow(p.x, i)
        result = 1 - (abs (p.y - y) / rangeLimit)
        if result < 0:
            result = 0
        value += result
    return value / n

item is just a member of the population, poinList is the list of points and n is the number of points (n - 1 is the grade of the function).

for i in range(n):
    y += item.gene[i] * pow(p.x, i)

this piece of code gives us the value of the function encoded in the genes in the points of pointList;

result = 1 - (abs (p.y - y) / rangeLimit)
    if result < 0:
        result = 0

while here the script stores 1 - the previous result because if the GA has yield a good result there should be distance = 0 from the function evaluated and the points; If this is the case, the fitness function should attribute the highest possible reproduction probability for that member. At the end the fitness function returns the total value over the number of points evaluated.

As you can see this fitness function is by no means an optimal one. The reproduction probability is higher for functions that crosses some points and are really distant from others rather than for functions that are closer to every point but crosses none. Anyway for simple cases the GA yields good results, as an example for points (0 0), (1 4), (2 9) one of the member with the highest reproduction probability has this function in its genes:

-0.0487839869993989 * x^0 + 4.600339125358671 * x^1 + -0.2780958075230644 * x^2

that crosses this points: (0 -0.0488), (1 4.2735), (2 8.0395) given 80 iterations, initial population of 600 members and a two digit approximation.

For a more precise computation a higher population size and a really high number of iterations should be used.

Why Wright's proof is a fake

I explained in my previous post (in italian) that the signature that Wright provided as a public proof is in fact invalid. I want to explain briefly how you could check this claim. The key in Wright's post is this:

------------------------- Signature File -------------------------
------------------------- End Signature --------------------------

Now we can use some bash utilities:

  • base64, that translates encoded ASCII text;
  • hexdump, that displays hexadecimal contents from the input;
  • cut, used to remove the binary part of the input;
  • tr, used to delete spaces and carriage return from the input;
base64 -d <<<'MEUCIQDBKn1Uly8m0UyzETObUSL4wYdBfd4ejvtoQfVcNCIK4AIgZmMsXNQWHvo6KDd2Tu6euEl13VTC3ihl6XUlhcU+fM4=' | hexdump -C| cut -b 11-60| tr -d ' \n'


Let's analyze the command one by one:

  • base64 -d decodes the redirected string, the output is some gibberish characters so I won't display them here;
  • hexdump -C is used with a pipe to convert to hexadecimal:
00000000  30 45 02 21 00 c1 2a 7d  54 97 2f 26 d1 4c b3 11  |0E.!..*}T./&.L..|
00000010  33 9b 51 22 f8 c1 87 41  7d de 1e 8e fb 68 41 f5  |3.Q"...A}....hA.|
00000020  5c 34 22 0a e0 02 20 66  63 2c 5c d4 16 1e fa 3a  |\4"... fc,\....:|
00000030  28 37 76 4e ee 9e b8 49  75 dd 54 c2 de 28 65 e9  |(7vN...Iu.T..(e.|
00000040  75 25 85 c5 3e 7c ce                              |u%..>|.|
  • cut -b 11-60 displays only the characters from column 11 to 60:
30 45 02 21 00 c1 2a 7d  54 97 2f 26 d1 4c b3 11  
33 9b 51 22 f8 c1 87 41  7d de 1e 8e fb 68 41 f5  
5c 34 22 0a e0 02 20 66  63 2c 5c d4 16 1e fa 3a  
28 37 76 4e ee 9e b8 49  75 dd 54 c2 de 28 65 e9  
75 25 85 c5 3e 7c ce                            
  • tr -d ' \n' is used to delete spaces and carriage returns from the output so that is shown in one line and it gives us the final result:

If you noticed, there is also another cleartext string at the beginning of Wright's post:

$ base64 -d <<<'IFdyaWdodCwgaXQgaXMgbm90IHRoZSBzYW1lIGFzIGlmIEkgc2lnbiBDcmFpZyBXcmlnaHQsIFNhdG9zaGkuCgo='
Wright, it is not the same as if I sign Craig Wright, Satoshi.

Now let's head to has a little utility to get hexadecimal informations out of a transaction on the blockchain, so let's use it to get the related info about this transaction:

tx/828ef3b079f9c23829c56fe86e85b4a69d9e06e5b54ea597eef5fb3ffef509fe tx/828ef3b079f9... in hexadecimal

As you can see the entire output of the first bash command, that is 3045022100c12a7d54972f26d14cb311339b5122f8c187417dde1e8efb6841f55c34220ae0022066632c5cd4161efa3a2837764eee9eb84975dd54c2de2865e9752585c53e7cce is contained in: "script":"483045022100c12a7d54972f26d14cb311339b5122f8c187417dde1e8efb6841f55c34220ae0022066632c5cd4161efa3a2837764eee9eb84975dd54c2de2865e9752585c53e7cce01" except for the 48 at the beginning and the 01 at the end.

That is a signature hash: this page explains that the 48 is just a decimal prefix given to uncompressed transactions, and the 01 at the end is just a SIGHASH_ALL code that flags the end of the signature.

So, is it a fake?

Yes, indeed. At the end, I ask, why would you choose anything else than the easiest and most conclusive way to prove something?

Wright "signs" the blockchain


Ieri mattina appena sveglio mi sono imbattuto in questo post di Gavin Andresen, uno dei più importanti membri della Bitcoin Foundation. In quelle righe Gavin attribuisce l'identità di Satoshi Nakamoto, il padre dei Bitcoin, a Craig Wright, un imprenditore australiano. Sono stato subito scosso dall'articolo, dove si spiega:

Part of that time was spent on a careful cryptographic verification of messages signed with keys that only Satoshi should possess.

Continuo la mia navigazione imbattendomi nei seguenti titoli di alcune testate italiane:

Gli articoli da clickbait e la completa mancanza di spiegazioni tecniche hanno fatto squillare immediatamente il mio campanello antibufala. Voglio approfondire la questione in questo post.

Prova numero 1: il post ufficiale di Wright

Craig Wright in data 2 maggio ha pubblicato sul suo sito personale questo articolo dove spiega che utilizzando 10 chiavi private associate agli indirizzi utilizzati da Satoshi ha firmato dei messaggi inviati da alcuni giornalisti e mostra gli script utilizzati per la firma sotto forma di screenshots. Prima di tutto, ad un'analisi più attenta si vede che lo script di Wright ha un typo e quindi non è eseguibile. Il motivo per cui ho dubitato maggiormente però è il fatto che nonostante Wright abbia speso molto tempo nel suo blog post ha evitato di darci una prova riproducibile del fatto che la chiave privata di uno dei genesis block sia in suo possesso. Quindi il post di Wright non basta a verificare che egli sia in effetti Nakamoto.

Prova numero 2: Jon Matonis e Gavin Andresen

Il primo post che ho letto e che inizialmente mi aveva convinto della sincerità di Wright è stato il quello di Andersen. A poche ore da quella lettura ho appreso grazie ad un tweet che è stato revocato a Gavin l'accesso ai commit relativi allo sviluppo di Bitcoin in quanto si sospetta che sia stato hackerato. Così il post di Gavin ha perso ogni valore nel tentativo di chiarire la vicenda. Invece ho ancora molti dubbi relativamente al post di Matonis (uno dei fondatori della Bitcoin Foundation) dove afferma che a Marzo Wright si è rivelato a lui firmando in sua presenza un messaggio con le chiavi crittografiche del blocco #1 e del blocco #9. Queste sarebbero le prove decisive per dimostrare l'autenticità delle affermazioni di Wright, ma non ne abbiamo prove pubbliche e riproducibili. Anzi, dal post si deduce che Wright abbia utilizzato il suo laptop personale piuttosto che un computer sicuramente non manomesso in precedenza.

Prova numero 3: Gavin su Reddit

Alcuni redditor in risposta al post di Gavin hanno chiesto a lui stesso maggiori dettagli. Questa è stata la risposta di Gavin:

Craig signed a message that I chose ("Gavin's favorite number is eleven. CSW" if I recall correctly) using the private key from block number 1. That signature was copied on to a clean usb stick I brought with me to London, and then validated on a brand-new laptop with a freshly downloaded copy of electrum. I was not allowed to keep the message or laptop (fear it would leak before Official Announcement). I don't have an explanation for the funky OpenSSL procedure in his blog post.

Dall'articolo di Wired:

Andresen says an administrative assistant working with Wright left to buy a computer from a nearby store, and returned with what Andresen describes as a Windows laptop in a “factory-sealed” box.

Naturalmente queste non sono prove sufficenti poichè non abbiamo la certezza che il portatile sia stato manomesso in anticipo.

Prova numero 4: gli articoli di BBC ed Economist

Wright ha avuto un colloquio privato con la BBC e un giornalista dell'Economist ed ha fornito delle prove che loro ritengono sufficenti a verificare le sue affermazioni. Vorrei poter andare più in profondità con le affermazioni delle due testate, ma esse stesse non forniscono alcuna dimostrazione concreta delle procedure che Wright ha eseguito. Si legge:

At the meeting with the BBC, Mr Wright digitally signed messages using cryptographic keys created during the early days of Bitcoin's development. The keys are inextricably linked to blocks of bitcoins known to have been created or "mined" by Satoshi Nakamoto.

In mancanza di una spiegazione più approfondita e prettamente tecnica non vedo perchè dovrei prendere queste parole per certe.

Prova numero 5: Gran Finale

L'unica prova concreta che tutta la community ha a disposizione è la firma che Wright ha utilizzato per autenticarsi firmando il discorso in cui Sartre spiega perchè rinuncia al premio Nobel. Questa firma: MEUCIQDBKn1Uly8m0UyzETObUSL4wYdBfd4ejvtoQfVcNCIK4AIgZmMsXNQWHvo6KDd2Tu6euEl13VTC3ihl6XUlhcU+fM4= non è altro che la versione esadecimale della transazione che Satoshi ha fatto nel 2009. Quella stringa (in base64) non è affatto una firma, bensì è un messaggio in chiaro: "Wright, it is not the same as if I sign Craig Wright, Satoshi.\n\n".

wikiHow: how to claim you're Satoshi

Charlie Lee, il creatore dei Litecoin su medium ci ha dato una dimostrazione semplice e concisa di come il vero Satoshi si sarebbe dovuto autenticare. Non abbiamo bisogno di colloqui privati, laptop nuovi di fabbrica e screenshot di script. Nei primi blocchi, i genesis blocks, rimangono registatrate un numero sufficiente di chiavi pubbliche appartenenti al creatore della blockchain. Chiunque dichiari di essere Satoshi deve poter firmare un messaggio utilizzando una di quelle chiavi. Questo è quello che il creatore dei Litecoin mostra in 4 righe. Ogni altra prova è discutibile e non necessaria.

EDIT 22:12

Su Twitter il profilo ufficiale di Electrum scrive:

Note: There was no download of a signature file of electrum (.asc file) from a UK IP on Apr 7th.

Questo significa che il 7 Aprile, quando Wright ha mostrato a Gavin le sue chiavi utilizzando Electrum, nessuna delle due parti si è preoccupata di verificare che il client fosse autentico. Questo invalida ulteriormente tutte le affermazioni di Gavin.