Appendix A - Reference Implementation (Appendice A - Implementazione di riferimento)
Questa appendice contiene i seguenti file tratti da RSAREF: A Cryptographic Toolkit for Privacy-Enhanced Mail:
global.h-- file di intestazione globalemd5.h-- file di intestazione per MD5md5c.c-- codice sorgente per MD5
Per ulteriori informazioni su RSAREF, inviare un'e-mail a <[email protected]>.
L'appendice include anche il seguente file:
mddriver.c-- driver di test per MD2, MD4 e MD5
Il driver compila per MD5 per impostazione predefinita, ma può compilare per MD2 o MD4 se il simbolo MD è definito sulla riga di comando del compilatore C come 2 o 4.
L'implementazione è portabile e dovrebbe funzionare su molte piattaforme diverse. Tuttavia, non è difficile ottimizzare l'implementazione su piattaforme particolari, un esercizio lasciato al lettore. Ad esempio, su piattaforme « little-endian » in cui il byte con indirizzo più basso in una parola a 32 bit è il meno significativo e dove non ci sono restrizioni di allineamento, la chiamata a Decode in MD5Transform può essere sostituita da un typecast.
A.1 global.h
/* GLOBAL.H - RSAREF types and constants */
/* PROTOTYPES should be set to one if and only if the compiler supports
function argument prototyping. The following makes PROTOTYPES default
to 0 if it has not already been defined with C compiler flags. */
#ifndef PROTOTYPES
#define PROTOTYPES 0
#endif
/* POINTER defines a generic pointer type */
typedef unsigned char *POINTER;
/* UINT2 defines a two byte word */
typedef unsigned short int UINT2;
/* UINT4 defines a four byte word */
typedef unsigned long int UINT4;
/* PROTO_LIST is defined depending on how PROTOTYPES is defined above.
If using PROTOTYPES, then PROTO_LIST returns the list, otherwise it
returns an empty list. */
#if PROTOTYPES
#define PROTO_LIST(list) list
#else
#define PROTO_LIST(list) ()
#endif
A.2 md5.h
/* MD5.H - header file for MD5C.C */
/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
rights reserved. [Notice di copyright identica a md5.h] */
/* MD5 context. */
typedef struct {
UINT4 state[4]; /* state (ABCD) */
UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */
unsigned char buffer[64]; /* input buffer */
} MD5_CTX;
void MD5Init PROTO_LIST ((MD5_CTX *));
void MD5Update PROTO_LIST
((MD5_CTX *, unsigned char *, unsigned int));
void MD5Final PROTO_LIST ((unsigned char [16], MD5_CTX *));
A.3 md5c.c
/* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm */
/* [Codice di implementazione C completo omesso per motivi di spazio.
Per l'implementazione completa, consultare il testo ufficiale RFC 1321.] */
A.4 mddriver.c
Il programma driver di test mddriver.c fornisce utility per testare MD5:
- String digest
- File digest
- Time trial
- Esecuzione della suite di test
A.5 Test suite (Suite di test)
La suite di test MD5 (opzione driver "-x") dovrebbe produrre i seguenti risultati:
MD5 test suite:
MD5 ("") = d41d8cd98f00b204e9800998ecf8427e
MD5 ("a") = 0cc175b9c0f1b6a831c399e269772661
MD5 ("abc") = 900150983cd24fb0d6963f7d28e17f72
MD5 ("message digest") = f96b697d7cb7938d525a2f31aaf161d0
MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b
MD5 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") =
d174ab98d277d9f5a5611c2c9f419d9f
MD5 ("12345678901234567890123456789012345678901234567890123456789012345678901234567890") = 57edf4a22be3c955ac49da2e2107b67a