Ejemplo n.º 1
0
Archivo: md2.c Proyecto: avokhmin/RPM5
/**
  Self-test the hash
  @return 0 if successful, CRYPT_NOP if self-tests have been disabled
*/  
int md2_test(void)
{
   static const struct {
        char *msg;
        byte md[16];
   } tests[] = {
      { "",
        {0x83,0x50,0xe5,0xa3,0xe2,0x4c,0x15,0x3d,
         0xf2,0x27,0x5c,0x9f,0x80,0x69,0x27,0x73
        }
      },
      { "a",
        {0x32,0xec,0x01,0xec,0x4a,0x6d,0xac,0x72,
         0xc0,0xab,0x96,0xfb,0x34,0xc0,0xb5,0xd1
        }
      },
      { "message digest",
        {0xab,0x4f,0x49,0x6b,0xfb,0x2a,0x53,0x0b,
         0x21,0x9f,0xf3,0x30,0x31,0xfe,0x06,0xb0
        }
      },
      { "abcdefghijklmnopqrstuvwxyz",
        {0x4e,0x8d,0xdf,0xf3,0x65,0x02,0x92,0xab,
         0x5a,0x41,0x08,0xc3,0xaa,0x47,0x94,0x0b
        }
      },
      { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
        {0xda,0x33,0xde,0xf2,0xa4,0x2d,0xf1,0x39,
         0x75,0x35,0x28,0x46,0xc3,0x03,0x38,0xcd
        }
      },
      { "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
        {0xd5,0x97,0x6f,0x79,0xd8,0x3d,0x3a,0x0d,
         0xc9,0x80,0x6c,0x3c,0x66,0xf3,0xef,0xd8
        }
      }
   };
   int i;
   md2Param mymd, * mp = &mymd;
   byte buf[16];

   for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
       md2Reset(mp);
       md2Update(mp, (byte*)tests[i].msg, strlen(tests[i].msg));
       md2Digest(mp, buf);
       if (memcmp(buf, tests[i].md, 16) != 0) {
          return 5;
       }
   }
   return 0;        
}
Ejemplo n.º 2
0
error_t md2Compute(const void *data, size_t length, uint8_t *digest)
{
   //Allocate a memory buffer to hold the MD2 context
   Md2Context *context = osAllocMem(sizeof(Md2Context));
   //Failed to allocate memory?
   if(!context) return ERROR_OUT_OF_MEMORY;

   //Initialize the MD2 context
   md2Init(context);
   //Digest the message
   md2Update(context, data, length);
   //Finalize the MD2 message digest
   md2Final(context, digest);

   //Free previously allocated memory
   osFreeMem(context);
   //Successful processing
   return NO_ERROR;
}