Advertisement
Guest User

OpenSSL GMAC example

a guest
Sep 24th, 2014
1,588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.12 KB | None | 0 0
  1. /* OpenSSL GMAC example.         */
  2. /* Written and placed in public  */
  3. /*   domain by Jeffrey Walton    */
  4.  
  5. /* gcc -g3 -O1 -DDEBUG=1 -Wall -Wextra -I/usr/local/ssl/macosx-x64/include/ /usr/local/ssl/macosx-x64/lib/libcrypto.a t.c -o t.exe */
  6. /* gcc -g2 -Os -DNDEBUG=1 -Wall -Wextra -I/usr/local/ssl/macosx-x64/include/ /usr/local/ssl/macosx-x64/lib/libcrypto.a t.c -o t.exe */
  7.  
  8. #include <assert.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <limits.h>
  13. #include <errno.h>
  14.  
  15. #include <openssl/bn.h>
  16. #include <openssl/rsa.h>
  17. #include <openssl/pem.h>
  18. #include <openssl/evp.h>
  19. #include <openssl/rand.h>
  20.  
  21. #define UNUSED(x) ((void)x)
  22. #define ASSERT(x) assert(x)
  23.  
  24. typedef unsigned char byte;
  25.  
  26. /* Taken from NIST's GCM Test Vectors    */
  27. /* http://csrc.nist.gov/groups/STM/cavp/ */
  28.  
  29. /*
  30.  [Keylen = 128]
  31.  [IVlen = 96]
  32.  [PTlen = 0]
  33.  [AADlen = 128]
  34.  [Taglen = 128]
  35.  
  36.  Count = 0
  37.  Key = 77be63708971c4e240d1cb79e8d77feb
  38.  IV = e0e00f19fed7ba0136a797f3
  39.  PT =
  40.  AAD = 7a43ec1d9c0a5a78a0b16533a6213cab
  41.  CT =
  42.  Tag = 209fcc8d3675ed938e9c7166709dd946
  43. */
  44.  
  45. int main(int argc, char* argv[])
  46. {
  47.     UNUSED(argc), UNUSED(argv);
  48.    
  49.     int rc = 0, unused;
  50.     unsigned int i;
  51.    
  52.     byte key[] = { 0x77, 0xbe, 0x63, 0x70, 0x89, 0x71, 0xc4, 0xe2, 0x40, 0xd1, 0xcb, 0x79, 0xe8, 0xd7, 0x7f, 0xeb };
  53.     byte iv[] = { 0xe0, 0xe0, 0x0f, 0x19, 0xfe, 0xd7, 0xba, 0x01, 0x36, 0xa7, 0x97, 0xf3 };
  54.     byte aad[] = { 0x7a, 0x43, 0xec, 0x1d, 0x9c, 0x0a, 0x5a, 0x78, 0xa0, 0xb1, 0x65, 0x33, 0xa6, 0x21, 0x3c, 0xab };
  55.    
  56.     byte tag[16] = {};
  57.     byte exp[] = { 0x20, 0x9f, 0xcc, 0x8d, 0x36, 0x75, 0xed, 0x93, 0x8e, 0x9c, 0x71, 0x66, 0x70, 0x9d, 0xd9, 0x46 };
  58.  
  59.     EVP_CIPHER_CTX *ctx = NULL;
  60.    
  61.     ctx = EVP_CIPHER_CTX_new();
  62.     ASSERT(ctx != NULL);
  63.     if(ctx == NULL) {
  64.         exit (1);
  65.     }
  66.    
  67.     rc = EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
  68.     ASSERT(rc == 1);
  69.     if(rc != 1) {
  70.         exit (1);
  71.     }
  72.    
  73.     rc = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, sizeof(iv), NULL);
  74.     ASSERT(rc == 1);
  75.     if(rc != 1) {
  76.         exit (1);
  77.     }
  78.    
  79.     rc = EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv);
  80.     ASSERT(rc == 1);
  81.     if(rc != 1) {
  82.         exit (1);
  83.     }
  84.    
  85.     rc = EVP_EncryptUpdate(ctx, NULL, &unused, aad, sizeof(aad));
  86.     ASSERT(rc == 1);
  87.     if(rc != 1) {
  88.         exit (1);
  89.     }
  90.    
  91.     rc = EVP_EncryptFinal_ex(ctx, NULL, &unused);
  92.     ASSERT(rc == 1);
  93.     if(rc != 1) {
  94.         exit (1);
  95.     }
  96.    
  97.     rc = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, sizeof(tag), tag);
  98.     ASSERT(rc == 1);
  99.     if(rc != 1) {
  100.         exit (1);
  101.     }
  102.    
  103.     printf("Calculated tag:\n  ");
  104.     for(i = 0; i < sizeof(tag); i++)
  105.     {
  106.         printf("%02x", tag[i]);
  107.        
  108.         if(i == sizeof(tag) - 1) {
  109.             printf("\n");
  110.         }
  111.     }
  112.    
  113.     printf("Expected tag:\n  ");
  114.     for(i = 0; i < sizeof(exp); i++)
  115.     {
  116.         printf("%02x", exp[i]);
  117.        
  118.         if(i == sizeof(exp) - 1) {
  119.             printf("\n");
  120.         }
  121.     }
  122.    
  123.     if(ctx) {
  124.         EVP_CIPHER_CTX_free(ctx);
  125.     }
  126.    
  127.     return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement