Archive 07/10/2020.

Question 5.2.1 et TP14

stostain

Je ne comprends pas où peut se trouver mon erreur, j’ai suivi les instructions du TP mais j’obtiens un résultat très étrange en enchaînant les calculs des hash (qui m’ont conduit notamment à échouer à la question 5.2.1). Les chaînes renvoyées par la fonction sont de longueur variable et les réponses sont irrégulières passée le premier appel. Voici un extrait de quelques sorties obtenues :

sha256 “Exemple !”
sha256 “Exemple !”
SHA256: DF635D31DAE41917C429EF1CAD90DC08714BFAC12C4EB5D3583262031B49
sha256 “Exemple !”
sha256 “Exemple !”
SHA256: DF635D342A43E9B0C48F76E7CD90DC08714BFAC12C4EB5D3583262031B49
sha256 “Exemple !”
sha256 “Exemple !”
SHA256: DF635D342A43E9B0C48F76E7CB9663AB6F3B1632E73D70A2ACF972031B49
sha256 “Exemple !”
sha256 “Exemple !”
SHA256: DF635D342A43E9B0C48F76E7CB966308714BFAC12C4EB5D3583262031B49
sha256 “Exemple !”
sha256 “Exemple !”
SHA256: DF635D342A43E9B0C48F76E7CB966308714BFAC12C4EB5D3583262031B49
sha256 “Exemple !”
sha256 “Exemple !”
SHA256: DF635D342A43E9B0C48F76E7CB9663AB6F3B1632E73D70A2ACF97F675CDAE41917C429EF1CAD90DC08714BFAC12C4EB5D3583262031B49
sha256 “Exemple !”
sha256 “Exemple !”
SHA256: DF635D342A43E9B0C48F76E7CB9663AB6F3B1632E73D7IB4A39232E5A1DAE41917C429EF1CAD90DC08714BFAC12C4EB5D3583262031B49
sha256 “Exemple !”
sha256 “Exemple !”
SHA256: DF635D342A43E9B0C48F76E7CB9663AB6F3B1632E73D70A2ACF97F675CD0D5D917C429EF1CAD90DC08714BFAC12C4EB5D3583262031B49

aabadie2

Bonjour,

Je viens de tester et je n’arrive pas à reproduire votre erreur.

Est-ce que vous pouvez poster le fichier main.c que vous compilez ?

stostain

Bonjour,

Merci pour votre réponse.
Voici le contenu du fichier main.c :

#include <inttypes.h>
#include <stdio.h>
#include <string.h>

/* Add required includes here */
#include "fmt.h"
#include "shell.h"

#include "hashes/sha256.h"
#include "hashes/sha3.h"


/* Add here the buffers to store the SHA256 hash and its hexadecimal representation */
static uint8_t sha256_hash[SHA256_DIGEST_LENGTH];
static char sha256_hash_hex[SHA256_DIGEST_LENGTH * 2];

/* Add here the buffers to store the SHA3 hash and its hexadecimal representation */
static uint8_t sha3_hash[SHA3_256_DIGEST_LENGTH];
static char sha3_hash_hex[SHA3_256_DIGEST_LENGTH * 2];

/* Implement the SHA256 command handler here */
static int _sha256_handler(int argc, char **argv)
{
    if (argc != 2) {
        printf("usage: %s <input to hash>\n", argv[0]);
        return 1;
    }

    /* Compute the hash here */
    sha256_context_t sha256;
    sha256_init(&sha256);
    sha256_update(&sha256, (uint8_t*)argv[1], strlen(argv[1]));
    sha256_final(&sha256, sha256_hash);


    /* Convert the hash array to a string of hex characters */
    fmt_bytes_hex(sha256_hash_hex, sha256_hash, SHA256_DIGEST_LENGTH);

    /* Print the hash */
    printf("SHA256: %s\n", sha256_hash_hex);

    return 0;
}


/* Implement the SHA3 command handler here */
static int _sha3_handler(int argc, char **argv)
{
    if (argc != 2) {
        printf("usage: %s <input to hash>\n", argv[0]);
        return 1;
    }

    /* Compute the hash here */
    keccak_state_t state;
    sha3_256_init(&state);
    sha3_update(&state, argv[1], strlen(argv[1]));
    sha3_256_final(&state, sha3_hash);

    /* Convert the hash array to a string in hex format */
    fmt_bytes_hex(sha3_hash_hex, sha3_hash, SHA3_256_DIGEST_LENGTH);

    /* Print the hash */
    printf("SHA3: %s\n", sha3_hash_hex);

    return 0;
}

/* Declare the list of shell commands */
static const shell_command_t shell_commands[] = {
    { "sha256", "Compute SHA256 hasg", _sha256_handler },
    { "sha3", "Compute SHA3 hash", _sha3_handler },
    { NULL, NULL, NULL }
};

int main(void)
{
    /* Configure and start the shell */
    char line_buf[SHELL_DEFAULT_BUFSIZE];
    shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);

    return 0;
}

Voici les sorties du shell pour l’exécution des commandes suivantes, ce qui devrait vous permettre de reproduire ces sorties :

jovyan@bf6c278a4e03:~/work/iot-lab-training$ export SITE=grenoble
jovyan@bf6c278a4e03:~/work/iot-lab-training$ iotlab-experiment submit -n "riot-hash" -d 20 -l 1,archi=m3:at86rf231+site=$SITE
{
    "id": 200498
}
jovyan@bf6c278a4e03:~/work/iot-lab-training$ iotlab-experiment wait --timeout 30 --cancel-on-timeout
Waiting that experiment 200498 gets in state Running
"Running"
jovyan@bf6c278a4e03:~/work/iot-lab-training$ iotlab-experiment get --nodes
{
    "items": [
        {
            "archi": "m3:at86rf231",
            "camera": null,
            "mobile": "0",
            "mobility_type": " ",
            "network_address": "m3-97.grenoble.iot-lab.info",
            "production": "YES",
            "site": "grenoble",
            "state": "Alive",
            "uid": "b179",
            "x": "0.40",
            "y": "25.92",
            "z": "-0.04"
        }
    ]
}
jovyan@bf6c278a4e03:~/work/iot-lab-training$ make -C riot/security/hash BOARD=iotlab-m3 IOTLAB_NODE=auto-ssh flash
make: Entering directory '/home/jovyan/work/iot-lab-training/riot/security/hash'
Building application "hash" for "iotlab-m3" with MCU "stm32f1".

"make" -C /home/jovyan/work/iot-lab-training/riot/RIOT/boards/iotlab-m3
"make" -C /home/jovyan/work/iot-lab-training/riot/RIOT/boards/common/iotlab
"make" -C /home/jovyan/work/iot-lab-training/riot/RIOT/core
"make" -C /home/jovyan/work/iot-lab-training/riot/RIOT/cpu/stm32f1
"make" -C /home/jovyan/work/iot-lab-training/riot/RIOT/cpu/cortexm_common
"make" -C /home/jovyan/work/iot-lab-training/riot/RIOT/cpu/cortexm_common/periph
"make" -C /home/jovyan/work/iot-lab-training/riot/RIOT/cpu/stm32_common
"make" -C /home/jovyan/work/iot-lab-training/riot/RIOT/cpu/stm32_common/periph
"make" -C /home/jovyan/work/iot-lab-training/riot/RIOT/cpu/stm32f1/periph
"make" -C /home/jovyan/work/iot-lab-training/riot/RIOT/drivers
"make" -C /home/jovyan/work/iot-lab-training/riot/RIOT/drivers/periph_common
"make" -C /home/jovyan/work/iot-lab-training/riot/RIOT/sys
"make" -C /home/jovyan/work/iot-lab-training/riot/RIOT/sys/auto_init
"make" -C /home/jovyan/work/iot-lab-training/riot/RIOT/sys/fmt
"make" -C /home/jovyan/work/iot-lab-training/riot/RIOT/sys/hashes
"make" -C /home/jovyan/work/iot-lab-training/riot/RIOT/sys/isrpipe
"make" -C /home/jovyan/work/iot-lab-training/riot/RIOT/sys/newlib_syscalls_default
"make" -C /home/jovyan/work/iot-lab-training/riot/RIOT/sys/pm_layered
"make" -C /home/jovyan/work/iot-lab-training/riot/RIOT/sys/shell
"make" -C /home/jovyan/work/iot-lab-training/riot/RIOT/sys/stdio_uart
"make" -C /home/jovyan/work/iot-lab-training/riot/RIOT/sys/tsrb
   text    data     bss     dec     hex filename
  11336     204    2812   14352    3810 /home/jovyan/work/iot-lab-training/riot/security/hash/bin/iotlab-m3/hash.elf
iotlab-node --jmespath='keys(@)[0]' --format='int'  --list grenoble,m3,97 --flash /home/jovyan/work/iot-lab-training/riot/security/hash/bin/iotlab-m3/hash.bin | grep 0
0
make: Leaving directory '/home/jovyan/work/iot-lab-training/riot/security/hash'
jovyan@bf6c278a4e03:~/work/iot-lab-training$ make -C riot/security/hash BOARD=iotlab-m3 IOTLAB_NODE=auto-ssh term
make: Entering directory '/home/jovyan/work/iot-lab-training/riot/security/hash'
ssh -t fun1c9eb3351e@grenoble.iot-lab.info 'socat - tcp:m3-97.grenoble.iot-lab.info:20000'
help
help
Command              Description
---------------------------------------
sha256               Compute SHA256 hasg
sha3                 Compute SHA3 hash
> sha256 "Exemple !"
sha256 "Exemple !"
SHA256: DF635D342A43E9B0C48F76E7CB9663AB6F3B1632E73D70A2ACF97F675CD0D5DF
> sha256 "Exemple !"
sha256 "Exemple !"
SHA256: DF635D342A43E9B0C48F76E7CB9663AB6F3B1632E73D70A2ACF97F675CD0D5DF
> sha256 "Exemple !"
sha256 "Exemple !"
SHA256: DF635D342A43E9B0C48F76E7CB9663AB6F3B1632E73D70A2ACF97F675CD0D5DF
> sha256 "Exemple !" 
sha256 "Exemple !"
SHA256: DF635D342A43E9B0C48F76E7CB9663AB6F3B1632E73D70A2ACF97F675CD0D5DF
> sha256 "Exemple !"
sha256 "Exemple !"
SHA256: DF635D342A43E9B0C48F76E7CB9663AB6F3B1632E73D70A2ACF97F675CD0D5DF
> sha256 "Exemple !"
sha256 "Exemple !"
SHA256: DF635D342A43E9B0C48F76E7CB9663AB6F3B1632E73D70A2ACF97F675CD0D5DF
> sha3 "Exemple !"
sha3 "Exemple !"
SHA3: 21F1AB4E9EFAE9B80208768AF13C6675516ED768B6E531611512AB34E5A34F01
> sha3 "Exemple !"
sha3 "Exemple !"
SHA3: 21F1AB4E9EFAE9B80208768AF13C6675516ED768B6E531611512AB34E5A34F01
> sha256 "Exemple !"
sha256 "Exemple !"

> sha256 "Exemple !"
sha256 "Exe0208768AF13C6675516ED768B6E531611512AB34E5A34F01
> sha256 "Exemple !"
sha256 "Exemple !"
SHA256: DF635D342A43E9B0C48F76E7CB9663AB6F3B1632E73D70A2ACF97E9EFAE9B80208768AF13C6675516ED768B6E531611512AB34E5A34F01
> sha256 "Exemple !"
sha256 "Exemple !"
SHA256: DF635D342A43E9B0C48F76E7CB9663AB6F3B1632E73D70A2ACF97F675CD0D50208768AF13C6675516ED768B6E531611512AB34E5A34F01
> sha3 "Exemple !"
sha3 "Exemple !"
SHA3: 21F1AB4E9EFAE9B80208768AF13C6675516ED768B6E531611512AB34E5A34F01
> sha256 "Exemple !"
sha256 "Exemple !"
5516ED768B6E531611512AB34E5A34F01
> sha256 "Exemple !"
sha256 "Exemple !"
5516ED768B6E531611512AB34E5A34F01
> sha256 "Exemple !"
sha256 "Exemple !"
SHA256: DF635D342A43E9B0C48F76E7CB9663AB6F3B1632E73D70A2ACF97F67FAE9B80208768AF13C6675516ED768B6E531611512AB34E5A34F01
stostain

La situation semble se produire lorsque l’on appelle la commande sha256 après avoir appelé sha3 dans la même session du shell riot.

aabadie2

En effet, je peux reproduire le problème. Je proposerai un correctif dès que je trouve d’où vient le problème (probablement une explication dans le code de RIOT).

frucot

Même genre de problème, si je fais une demande avec sha3 après avoir fait sha256, je n’ai pas de réponse pour > sha3 riot

lguilloteau

Bonjour,
Même problème mais uniquement lorsque l’on se connecte au iotlab-m3.
La commande sha256 “This is RIOT!” donne des hashis différents à chaque fois (et pas les bons).
Par contre la cde sha3 donne le bon résultat.

Exmple de hashis bizarre :

sha256 “This is RIOT!”
sha256 “This is RIOT!”
SHA256: 7808CE5C27FBA9BDDCD5CB076B423E22961D6BCC5FF(Bk759B2B5474663D26B675350B09E2C2A41B6090B0F2BBFFBFF5C1802F7628426B
sha256 “This is RIOT!”
sha256 “This is RIOT!”
SHA256: 7808CE5C27FBA9BDDCD5CB076B423E22961D6BCC5FF2CA62722C0D6DF63D26B675350B09E2C2A41B6090B0F2BBFFBFF5C1802F7628426B
sha256 “This is RIOT!”
sha256 “This is RIOT!”
SHA256: 7808CE5C27FBA9BDDCD5C9E2C2A41B6090B0F2BBFFBFF5C1802F7628426B

solognot

Pour moi aussi sha256 fonctionne correctement uniquement sur la macine default, sur iotlab-m3 les valeurs sont incorrectes.

mathiae

Bonjour,

Je ne suis pas arrivé non plus à trouver le hash pour la question 5.2.1

Voir ci-dessous.

En revanche, je calcule correctement le hash pour la question 5.2.2. Soit, une mauvaise valeur a été entrée pour la question 5.2.1, soit le code du sha256 n’est pas correct.

sha256 “This is RIOT!”
sha256 “This is RIOT!”
SHA256: 7808CE…etc…031B49

aabadie2

Bonjour,

Je viens de regarder et il y a un bug dans le code de l’application.

Dans l’énoncé du notebook, il faut remplacer les lignes:

static char sha256_hash_hex[SHA256_DIGEST_LENGTH * 2];

et

static char sha3_hash_hex[SHA3_256_DIGEST_LENGTH * 2];

par respectivement

static char sha256_hash_hex[SHA256_DIGEST_LENGTH * 2 + 1];

et

static char sha3_hash_hex[SHA3_256_DIGEST_LENGTH * 2 + 1];

Pour résumer, il manque le caractère null de fin de chaine et ça le printf ne marche pas très bien.

Pour info, ça règle aussi le problème d’appel de sha3 avant sha256 !

mathiae

Bonjour,

Merci pour cette réponse. J’ai recompilé et exécuter à nouveau le code. Nickel !

Merci pour cette réponse détaillée.

ojt

Il aurait ete bien de corriger le notebook du coup meme problème :wink:

jcw67

Bonjour @aabadie2,

J’arrive seulement à ce quiz (19/05/2020, solution datée du 17 avril).

Je suis évidemment aussi tombé dans le piège : le notebook n’est pas corrigé.

Et pourriez-vous réinitaliser les quiz 5.2.1 et 5.2.2 , qui sont impossibles à faire sans avoir lu cette discussion … qui est plus loin dans le cours !

jc

MarieCollin

Bonjour @jcw67,

le notebook a été corrigé. Pour que ce soit effectif dans votre environnement sur FUN, il est nécessaire de réinitialiser votre copie du notebook. Voir les instructions par exemple données ici pour le TP1 : Problème 1.4

Nous avons aussi réinitialisé le nombre d’essais pour les questions 5.2.1 et 5.2.2 dans votre parcours. Vous pouvez donc les refaire si vous le souhaitez.

Bonne fin de cours.

jcw67

Bonjour @MarieCollin,

Merci d’avoir réinitialisé le nombre d’essais !
Je viens de gagner 2 points en relançant sha256 "This is RIOT!" (j’étais déjà allé voir les discussions à la fin du chapitre avant de lancer sha3)

Par contre, pas de changement dans le notebook les “+ 1” manquent encore :

static char sha256_hash_hex[SHA256_DIGEST_LENGTH * 2];
...
static char sha3_hash_hex[SHA3_256_DIGEST_LENGTH * 2];

Manips terminal (heures UTC) :

jovyan@87fc10095432:~/work/iot-lab-training$   (prompt initial)
cd riot/security//hash/
-> jovyan@87fc10095432:~/work/iot-lab-training/riot/security/hash$
ls -l
total 28
drwxr-sr-x 4 jovyan users  4096 May 19 16:14 bin
-rw-r--r-- 1 jovyan users 13060 May 19 17:00 hash.ipynb
-rw-r--r-- 1 jovyan users  2204 May 19 16:52 main.c
-rw-r--r-- 1 jovyan users   192 May 19 15:46 Makefile

git checkout hash.ipynb
ls -l
total 24
drwxr-sr-x 4 jovyan users 4096 May 19 16:14 bin
-rw-r--r-- 1 jovyan users 9148 May 20 12:35 hash.ipynb <===
-rw-r--r-- 1 jovyan users 2204 May 19 16:52 main.c
-rw-r--r-- 1 jovyan users  192 May 19 15:46 Makefile

-> double-click sur hash.ipynb dans la colonne de gauche
Les valeurs des cellules sont réinitalisées … mais pas de +1.

A voir par un spécialiste C : même erreur dans le TP 15 sur la déclaration de static char buf_str[BUF_SIZE * 2] = { 0 }; ?
Avec #define BUF_SIZE (64U) , on ne risque rien pour les tests demandés.
Pour des essais plus longs, buf_str[len] = 0; dans decrypt pourrait écraser quelque chose en mémoire ?

jc

MarieCollin

Pour la correction des TP14 et TP15, je laisse @aabadie2 vous répondre.