Search code examples
c++cryptographycrypto++

Example of LUC algorithm with Crypto++


I am looking for an example of LUC algorithm, but I can't find anything. I know that it is in Crypto++, but I don't know C++ too well to use it.


Solution

  • I look for an example of algorithm of LUC...

    It kind of depends on what you want to do. You might want to browse luc.h to see some of the things Crypto++ offers for LUC. There's a LUCES, a LUCSS and a LUC_IES. The *ES is encryption scheme, the *SS is a signature scheme, and the *IES is an integrated encryption scheme (which includes a key agreement algorithm and mask function).

    Generally speaking, LUC is a public key encryption system. Using it is like using any other public key encryption system offered by Crypto++. That's because all the public key encryption systems inherit from the same classes (more correctly, base interfaces). You can see the design in the comments for file pubkey.h.

    $ grep -R LUCES *
    ...
    typedef LUCES<OAEP<SHA> >::Decryptor LUCES_OAEP_SHA_Decryptor;
    typedef LUCES<OAEP<SHA> >::Encryptor LUCES_OAEP_SHA_Encryptor;
    

    And that's pretty much all you need, though you may not know it.

    Here's the easier problem to solve. How do you perform RSA encryption in Crypto++?

    $ grep -R RSAES *
    ...
    typedef RSAES<PKCS1v15>::Decryptor RSAES_PKCS1v15_Decryptor;
    typedef RSAES<PKCS1v15>::Encryptor RSAES_PKCS1v15_Encryptor;
    
    typedef RSAES<OAEP<SHA> >::Decryptor RSAES_OAEP_SHA_Decryptor;
    typedef RSAES<OAEP<SHA> >::Encryptor RSAES_OAEP_SHA_Encryptor;
    

    If you find an RSAES_PKCS1v15_Decryptor or RSAES_OAEP_SHA_Decryptor example, you just copy/replace with LUCES_OAEP_SHA_Decryptor and it will work just fine. And if you find an RSAES_PKCS1v15_Encryptor or RSAES_OAEP_SHA_Encryptor example, you just copy/replace with LUCES_OAEP_SHA_Encryptor and it will work just fine.

    You can find the examples of using RSAES_OAEP_SHA_Encryptor and RSAES_OAEP_SHA_Decryptor on the Crypto++ wiki page for RSA Encryption Schemes. Or you can use the ECIES examples at Elliptic Curve Integrated Encryption Scheme (remember, all the public key systems inherit from the same base interfaces, so they all have the same methods and you use them the same way).


    This should get you started. It creates a private key, saves it, then creates a public key, and saves it.

    try
    {
        AutoSeededRandomPool prng;
    
        FileSink fs1("lucs-private.der", true);
        FileSink fs2("lucs-public.der", true);
    
        InvertibleLUCFunction params;
        params.GenerateRandomWithKeySize(prng, 2048);
    
        LUC::PrivateKey privateKey(params);
        privateKey.DEREncode(fs1);
    
        LUCES_OAEP_SHA_Decryptor decryptor(privateKey);
        // ...
    
        LUC::PublicKey publicKey(params);
        publicKey.DEREncode(fs2);
    
        LUCES_OAEP_SHA_Encryptor encryptor(publicKey);
        // ...      
    }
    catch(CryptoPP::Exception& ex)
    {
        cerr << ex.what() << endl;
    }
    

    If you don't want to use InvertibleLUCFunction, the do something like this to generate the key. Note: RSA has an InvertibleRSAFunction.

    LUC::PrivateKey privateKey;
    privateKey.Initialize(prng, 2048);
    ...
    
    LUC::PublicKey publicKey(privateKey);
    ...
    

    An here's yet another way to do it:

    FileSink fs1("lucs-private.der", true);
    FileSink fs2("lucs-public.der", true);
    
    LUCES_OAEP_SHA_Decryptor decryptor;
    decryptor.AccessKey().Initialize(prng, 2048);
    decryptor.AccessKey().DEREncode(fs1);
    ...
    
    LUCES_OAEP_SHA_Encryptor encryptor(decryptor);
    encryptor.AccessKey().DEREncode(fs2);
    ...
    

    And here's a dump of the private key created by the test program:

    $ dumpasn1 lucs-private.der 
      0 662: SEQUENCE {
      4   1:   INTEGER 0
      7 257:   INTEGER
           :     00 B8 7A CA 6A 61 D9 CF 2F D8 89 5C A4 7D 74 7B
           :     AC F5 10 4C 3D 95 BF DD 2E F5 4E E5 F4 20 CF CD
           :     44 7F C7 27 41 48 6B 83 E0 7C D9 66 16 8D 54 36
           :     97 B9 CE 2D 80 A6 F6 E5 25 87 83 6E B9 41 45 DC
           :     2A EB EC 4E EC D9 C0 17 B4 E0 04 F0 58 61 60 F8
           :     87 18 27 16 58 BA 56 4E DD 9B C8 CD 18 46 28 38
           :     A2 6A A6 14 36 D0 A6 FF 9C B8 A8 B5 0F 3A 11 B5
           :     00 08 44 B3 31 58 AF 01 F8 57 17 E8 FC 68 B2 5F
           :             [ Another 129 bytes skipped ]
    268   1:   INTEGER 17
    271 129:   INTEGER
           :     00 C8 DF 47 D0 B2 6F C2 1A E4 B7 E8 3D 12 BB FF
           :     04 F7 34 40 A0 0E ED DC F7 24 7B D9 46 EE 10 C4
           :     D5 E2 9C 93 05 CF 13 53 40 F4 50 EC 1F 6D D7 33
           :     FF FF 46 42 88 8D FC F4 EE 7F 0C 8B 71 71 51 D2
           :     3C 32 E3 9A 11 B7 D8 CF EA 10 B2 07 49 3F 93 CD
           :     A0 3F 71 A9 23 27 35 1F 6A C9 1D FE CE 24 75 33
           :     8F 53 71 B9 0B DE BC 05 93 98 A3 EA 94 8E 04 B1
           :     29 A1 4F 4C 82 34 7A 08 3A 0E 07 98 8B 00 30 D7
           :     5B
    403 129:   INTEGER
           :     00 EB 1B D0 EF 5C 0F FC FC B7 56 A7 70 8C AA B7
           :     A6 90 C8 1F AA AD A0 0B 66 E5 33 75 F2 BE 68 35
           :     29 2E 57 AC E0 E0 C8 04 A7 C4 13 1D 10 30 8B 50
           :     20 17 0C 83 A7 14 4A 7D 25 31 77 50 66 08 36 13
           :     BE 9D C0 4E F4 44 74 7A BB D2 92 D0 F7 AE 7C EB
           :     8E 84 5C 27 61 2C C9 7A D1 D0 C5 A0 13 98 96 E3
           :     76 CD B0 E7 E8 7E 4E 0A 2D 00 86 07 57 DB 8A 51
           :     1E 59 76 EA 88 44 4D DA F3 D6 AB 75 CB A6 45 F3
           :     F3
    535 128:   INTEGER
           :     2E 6A AA BA B4 E8 DD 11 2D 31 A4 D5 F7 08 AB E3
           :     1A 9A 15 58 AE C8 59 BE C4 75 85 90 6D 5D A4 18
           :     39 27 8F FF 1C 9A FD 0F 0C 29 05 98 9C 16 FE 84
           :     A4 5C 85 15 F7 98 E6 D5 5B 23 CA 2F A2 27 8A 00
           :     6E B1 BB 02 6E 93 53 85 30 30 61 F5 1C 49 5D 19
           :     EF DF CD 6F 11 7C 6D DC AE F6 A2 06 53 BB 7E 03
           :     C3 E5 4E E9 59 E0 D8 5F C3 28 0E E0 17 5C 63 6E
           :     8E A6 18 FC AD A5 9B 08 D1 8B 7B 28 9D E2 CF E2
           :   }
    
    0 warnings, 0 errors.