]> pd.if.org Git - zpackage/blob - ctr_test.c
878d425564aa81014deeb8616c2db4b2e83e14ce
[zpackage] / ctr_test.c
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2  *
3  * LibTomCrypt is a library that provides various cryptographic
4  * algorithms in a highly modular and flexible manner.
5  *
6  * The library is free for all purposes without any express
7  * guarantee it works.
8  */
9 #include "tomcrypt.h"
10
11 /**
12   @file ctr_test.c
13   CTR implementation, Tests again RFC 3686, Tom St Denis
14 */
15
16 #ifdef LTC_CTR_MODE
17
18 int ctr_test(void)
19 {
20 #ifdef LTC_NO_TEST
21    return CRYPT_NOP;
22 #else
23    static const struct {
24       int keylen, msglen;
25       unsigned char key[32], IV[16], pt[64], ct[64];
26    } tests[] = {
27 /* 128-bit key, 16-byte pt */
28 {
29    16, 16,
30    {0xAE,0x68,0x52,0xF8,0x12,0x10,0x67,0xCC,0x4B,0xF7,0xA5,0x76,0x55,0x77,0xF3,0x9E },
31    {0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
32    {0x53,0x69,0x6E,0x67,0x6C,0x65,0x20,0x62,0x6C,0x6F,0x63,0x6B,0x20,0x6D,0x73,0x67 },
33    {0xE4,0x09,0x5D,0x4F,0xB7,0xA7,0xB3,0x79,0x2D,0x61,0x75,0xA3,0x26,0x13,0x11,0xB8 },
34 },
35
36 /* 128-bit key, 36-byte pt */
37 {
38    16, 36,
39    {0x76,0x91,0xBE,0x03,0x5E,0x50,0x20,0xA8,0xAC,0x6E,0x61,0x85,0x29,0xF9,0xA0,0xDC },
40    {0x00,0xE0,0x01,0x7B,0x27,0x77,0x7F,0x3F,0x4A,0x17,0x86,0xF0,0x00,0x00,0x00,0x00 },
41    {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,
42     0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,
43     0x20,0x21,0x22,0x23},
44    {0xC1,0xCF,0x48,0xA8,0x9F,0x2F,0xFD,0xD9,0xCF,0x46,0x52,0xE9,0xEF,0xDB,0x72,0xD7,
45     0x45,0x40,0xA4,0x2B,0xDE,0x6D,0x78,0x36,0xD5,0x9A,0x5C,0xEA,0xAE,0xF3,0x10,0x53,
46     0x25,0xB2,0x07,0x2F },
47 },
48 };
49   int idx, err, x;
50   unsigned char buf[64];
51   symmetric_CTR ctr;
52
53   /* AES can be under rijndael or aes... try to find it */
54   if ((idx = find_cipher("aes")) == -1) {
55      if ((idx = find_cipher("rijndael")) == -1) {
56         return CRYPT_NOP;
57      }
58   }
59
60   for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
61      if ((err = ctr_start(idx, tests[x].IV, tests[x].key, tests[x].keylen, 0, CTR_COUNTER_BIG_ENDIAN|LTC_CTR_RFC3686, &ctr)) != CRYPT_OK) {
62         return err;
63      }
64      if ((err = ctr_encrypt(tests[x].pt, buf, tests[x].msglen, &ctr)) != CRYPT_OK) {
65         return err;
66      }
67      ctr_done(&ctr);
68      if (compare_testvector(buf, tests[x].msglen, tests[x].ct, tests[x].msglen, "CTR", x)) {
69         return CRYPT_FAIL_TESTVECTOR;
70      }
71   }
72   return CRYPT_OK;
73 #endif
74 }
75
76 #endif
77
78 /* ref:         $Format:%D$ */
79 /* git commit:  $Format:%H$ */
80 /* commit time: $Format:%ai$ */
81
82
83