diff demos/tv_gen.c @ 380:d5faf4814ddb libtomcrypt-orig libtomcrypt-1.16

Update to LibTomCrypt 1.16
author Matt Johnston <matt@ucc.asn.au>
date Thu, 11 Jan 2007 02:22:00 +0000
parents 59400faa4b44
children
line wrap: on
line diff
--- a/demos/tv_gen.c	Wed Mar 08 12:58:00 2006 +0000
+++ b/demos/tv_gen.c	Thu Jan 11 02:22:00 2007 +0000
@@ -98,6 +98,18 @@
   }
 #endif
 
+#ifdef USE_LTM
+   ltc_mp = ltm_desc;
+#elif defined(USE_TFM)
+   ltc_mp = tfm_desc;
+#elif defined(USE_GMP)
+   ltc_mp = gmp_desc;
+#else
+   extern ltc_math_descriptor EXT_MATH_LIB;
+   ltc_mp = EXT_MATH_LIB;
+#endif
+
+
 }
 
 void hash_gen(void)
@@ -541,7 +553,7 @@
             plaintext[z] = (unsigned char)(z & 255);
          }
          len = sizeof(tag);
-         if ((err = ccm_memory(x, key, kl, nonce, 13, plaintext, y1, plaintext, y1, plaintext, tag, &len, CCM_ENCRYPT)) != CRYPT_OK) {
+         if ((err = ccm_memory(x, key, kl, NULL, nonce, 13, plaintext, y1, plaintext, y1, plaintext, tag, &len, CCM_ENCRYPT)) != CRYPT_OK) {
             printf("Error CCM'ing: %s\n", error_to_string(err));
             exit(EXIT_FAILURE);
          }
@@ -641,30 +653,134 @@
    fclose(out);
 }
 
+void math_gen(void)
+{
+}
+
+void ecc_gen(void)
+{
+   FILE         *out;
+   unsigned char str[512];
+   void          *k, *order, *modulus;
+   ecc_point    *G, *R;
+   int           x;
+
+   out = fopen("ecc_tv.txt", "w");
+   fprintf(out, "ecc vectors.  These are for kG for k=1,3,9,27,...,3**n until k > order of the curve outputs are <k,x,y> triplets\n\n");
+   G = ltc_ecc_new_point();
+   R = ltc_ecc_new_point();
+   mp_init(&k);
+   mp_init(&order);
+   mp_init(&modulus);
+
+   for (x = 0; ltc_ecc_sets[x].size != 0; x++) {
+        fprintf(out, "ECC-%d\n", ltc_ecc_sets[x].size*8);
+        mp_set(k, 1);
+
+        mp_read_radix(order,   (char *)ltc_ecc_sets[x].order, 16);
+        mp_read_radix(modulus, (char *)ltc_ecc_sets[x].prime, 16);
+        mp_read_radix(G->x,    (char *)ltc_ecc_sets[x].Gx,    16);
+        mp_read_radix(G->y,    (char *)ltc_ecc_sets[x].Gy,    16);
+        mp_set(G->z, 1);  
+
+        while (mp_cmp(k, order) == LTC_MP_LT) {
+            ltc_mp.ecc_ptmul(k, G, R, modulus, 1);
+            mp_tohex(k,    (char*)str); fprintf(out, "%s, ", (char*)str);
+            mp_tohex(R->x, (char*)str); fprintf(out, "%s, ", (char*)str);
+            mp_tohex(R->y, (char*)str); fprintf(out, "%s\n", (char*)str);
+            mp_mul_d(k, 3, k);
+        }
+   }
+   mp_clear_multi(k, order, modulus, NULL);
+   ltc_ecc_del_point(G);
+   ltc_ecc_del_point(R);
+   fclose(out);
+}
+
+void lrw_gen(void)
+{
+   FILE *out;
+   unsigned char tweak[16], key[16], iv[16], buf[1024];
+   int x, y, err;
+   symmetric_LRW lrw;
+   
+   /* initialize default key and tweak */
+   for (x = 0; x < 16; x++) {
+      tweak[x] = key[x] = iv[x] = x;
+   }
+
+   out = fopen("lrw_tv.txt", "w");
+   for (x = 16; x < (int)(sizeof(buf)); x += 16) {
+       if ((err = lrw_start(find_cipher("aes"), iv, key, 16, tweak, 0, &lrw)) != CRYPT_OK) {
+          fprintf(stderr, "Error starting LRW-AES: %s\n", error_to_string(err));
+          exit(EXIT_FAILURE);
+       }
+
+       /* encrypt incremental */
+       for (y = 0; y < x; y++) {
+           buf[y] = y & 255;
+       }
+
+       if ((err = lrw_encrypt(buf, buf, x, &lrw)) != CRYPT_OK) {
+          fprintf(stderr, "Error encrypting with LRW-AES: %s\n", error_to_string(err));
+          exit(EXIT_FAILURE);
+       }
+
+       /* display it */
+       fprintf(out, "%d:", x);
+       for (y = 0; y < x; y++) {
+          fprintf(out, "%02x", buf[y]);
+       }
+       fprintf(out, "\n");
+
+       /* reset IV */
+       if ((err = lrw_setiv(iv, 16, &lrw)) != CRYPT_OK) {
+          fprintf(stderr, "Error setting IV: %s\n", error_to_string(err));
+          exit(EXIT_FAILURE);
+       }
+
+       /* copy new tweak, iv and key */
+       for (y = 0; y < 16; y++) {
+          key[y]   = buf[y];
+          iv[y]    = buf[(y+16)%x];
+          tweak[y] = buf[(y+32)%x];
+       }
+
+       if ((err = lrw_decrypt(buf, buf, x, &lrw)) != CRYPT_OK) {
+          fprintf(stderr, "Error decrypting with LRW-AES: %s\n", error_to_string(err));
+          exit(EXIT_FAILURE);
+       }
+
+       /* display it */
+       fprintf(out, "%d:", x);
+       for (y = 0; y < x; y++) {
+          fprintf(out, "%02x", buf[y]);
+       }
+       fprintf(out, "\n");
+       lrw_done(&lrw);
+   }
+   fclose(out);
+}      
+
 int main(void)
 {
    reg_algs();
-   printf("Generating hash   vectors..."); fflush(stdout); hash_gen(); printf("done\n");
+   printf("Generating hash   vectors..."); fflush(stdout); hash_gen();   printf("done\n");
    printf("Generating cipher vectors..."); fflush(stdout); cipher_gen(); printf("done\n");
-   printf("Generating HMAC   vectors..."); fflush(stdout); hmac_gen(); printf("done\n");
-   printf("Generating OMAC   vectors..."); fflush(stdout); omac_gen(); printf("done\n");
-   printf("Generating PMAC   vectors..."); fflush(stdout); pmac_gen(); printf("done\n");
-   printf("Generating EAX    vectors..."); fflush(stdout); eax_gen(); printf("done\n");
-   printf("Generating OCB    vectors..."); fflush(stdout); ocb_gen(); printf("done\n");
-   printf("Generating CCM    vectors..."); fflush(stdout); ccm_gen(); printf("done\n");
-   printf("Generating GCM    vectors..."); fflush(stdout); gcm_gen(); printf("done\n");
+   printf("Generating HMAC   vectors..."); fflush(stdout); hmac_gen();   printf("done\n");
+   printf("Generating OMAC   vectors..."); fflush(stdout); omac_gen();   printf("done\n");
+   printf("Generating PMAC   vectors..."); fflush(stdout); pmac_gen();   printf("done\n");
+   printf("Generating EAX    vectors..."); fflush(stdout); eax_gen();    printf("done\n");
+   printf("Generating OCB    vectors..."); fflush(stdout); ocb_gen();    printf("done\n");
+   printf("Generating CCM    vectors..."); fflush(stdout); ccm_gen();    printf("done\n");
+   printf("Generating GCM    vectors..."); fflush(stdout); gcm_gen();    printf("done\n");
    printf("Generating BASE64 vectors..."); fflush(stdout); base64_gen(); printf("done\n");
+   printf("Generating MATH   vectors..."); fflush(stdout); math_gen();   printf("done\n");
+   printf("Generating ECC    vectors..."); fflush(stdout); ecc_gen();    printf("done\n");
+   printf("Generating LRW    vectors..."); fflush(stdout); lrw_gen();    printf("done\n");
    return 0;
 }
 
-
-         
-      
-      
-      
-    
-   
-
 /* $Source: /cvs/libtom/libtomcrypt/demos/tv_gen.c,v $ */
-/* $Revision: 1.4 $ */
-/* $Date: 2005/05/05 14:35:56 $ */
+/* $Revision: 1.15 $ */
+/* $Date: 2006/06/09 22:10:27 $ */