当前位置:   article > 正文

openssl学习之ccm,gcm 模式_openssl gcm

openssl gcm

openssl中添加了对AES ccm 和gcm模式的支持。下面的内容主要是对这两个模式相关资料的收集以及整理。

一,CCM

CCM (counter with CBC-MAC)定义在分组长度为128位的加密算法中,如,AES 的分组长度为128。组成AES-CCM算法的关键组成是CTR工作模式以及CMAC认证算法。Wifi 的WPE协议中使用了AES-CCM。在HMAC中我们介绍CCM是属于一种E&M(认证并且加密),首先我们来看一下AES-CCM模式的输入输出。

首先介绍两个参数设置:

L:长度域,取值为2~8 ,openssl中缺省的为8。

M:tag的长度,合法的值为:4,6,8,10,12,14 和16。openssl中缺省的为12

key16,24,32
None15-L
Message to authenticate and encryptlen(Msg)
Additional authenticated datalen(AAD)
其中对消息长度有:0<= len(Msg)<= 2^(8L);

对附加数据长度有:0<= len(AAD)< 2^64;

  1. /* Simple AES CCM test program, uses the same NIST data used for the FIPS
  2. * self test but uses the application level EVP APIs.
  3. */
  4. #include <stdio.h>
  5. #include <openssl/bio.h>
  6. #include <openssl/evp.h>
  7. /* AES-CCM test data from NIST public test vectors */
  8. static const unsigned char ccm_key[] = {
  9. 0xce,0xb0,0x09,0xae,0xa4,0x45,0x44,0x51,0xfe,0xad,0xf0,0xe6,
  10. 0xb3,0x6f,0x45,0x55,0x5d,0xd0,0x47,0x23,0xba,0xa4,0x48,0xe8
  11. };
  12. // 随机数,每次加密针对相同的KEY使用不同的NONCE。否则会破坏CCM模式的安全性(RFC3610)
  13. static const unsigned char ccm_nonce[] = {
  14. 0x76,0x40,0x43,0xc4,0x94,0x60,0xb7
  15. };
  16. //附加数据
  17. static const unsigned char ccm_adata[] = {
  18. 0x6e,0x80,0xdd,0x7f,0x1b,0xad,0xf3,0xa1,0xc9,0xab,0x25,0xc7,
  19. 0x5f,0x10,0xbd,0xe7,0x8c,0x23,0xfa,0x0e,0xb8,0xf9,0xaa,0xa5,
  20. 0x3a,0xde,0xfb,0xf4,0xcb,0xf7,0x8f,0xe4
  21. };
  22. //plaintext 表示明文
  23. static const unsigned char ccm_pt[] = {
  24. 0xc8,0xd2,0x75,0xf9,0x19,0xe1,0x7d,0x7f,0xe6,0x9c,0x2a,0x1f,
  25. 0x58,0x93,0x9d,0xfe,0x4d,0x40,0x37,0x91,0xb5,0xdf,0x13,0x10
  26. };
  27. //ciphertext 表示密文
  28. static const unsigned char ccm_ct[] = {
  29. 0x8a,0x0f,0x3d,0x82,0x29,0xe4,0x8e,0x74,0x87,0xfd,0x95,0xa2,
  30. 0x8a,0xd3,0x92,0xc8,0x0b,0x36,0x81,0xd4,0xfb,0xc7,0xbb,0xfd
  31. };
  32. //tag 表示tag数据
  33. static const unsigned char ccm_tag[] = {
  34. 0x2d,0xd6,0xef,0x1c,0x45,0xd4,0xcc,0xb7,0x23,0xdc,0x07,0x44,
  35. 0x14,0xdb,0x50,0x6d
  36. };
  37. void aes_ccm_encrypt(void)
  38. {
  39. EVP_CIPHER_CTX *ctx;
  40. int outlen, tmplen;
  41. unsigned char outbuf[1024];
  42. printf("AES CCM Encrypt:\n");
  43. printf("Plaintext:\n");
  44. BIO_dump_fp(stdout, ccm_pt, sizeof(ccm_pt));
  45. ctx = EVP_CIPHER_CTX_new();
  46. /* Set cipher type and mode */
  47. EVP_EncryptInit_ex(ctx, EVP_aes_192_ccm(), NULL, NULL, NULL);
  48. /* Set nonce length if default 96 bits is not appropriate */
  49. EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_IVLEN, sizeof(ccm_nonce), NULL);
  50. /* Set tag length */
  51. EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG, sizeof(ccm_tag), NULL);
  52. /* Initialise key and IV */
  53. EVP_EncryptInit_ex(ctx, NULL, NULL, ccm_key, ccm_nonce);
  54. /* Set plaintext length: only needed if AAD is used*/
  55. //输入输出需设置为NULL
  56. EVP_EncryptUpdate(ctx, NULL, &outlen, NULL, sizeof(ccm_pt));
  57. /* Zero or one call to specify any AAD */
  58. //设置AAD,out参数需设置为NULL
  59. EVP_EncryptUpdate(ctx, NULL, &outlen, ccm_adata, sizeof(ccm_adata));
  60. /* Encrypt plaintext: can only be called once */
  61. EVP_EncryptUpdate(ctx, outbuf, &outlen, ccm_pt, sizeof(ccm_pt));
  62. /* Output encrypted block */
  63. printf("Ciphertext:\n");
  64. BIO_dump_fp(stdout, outbuf, outlen);
  65. /* Finalise: note get no output for CCM */
  66. EVP_EncryptFinal_ex(ctx, outbuf, &outlen);
  67. /* Get tag */
  68. EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_GET_TAG, 16, outbuf);
  69. /* Output tag */
  70. printf("Tag:\n");
  71. BIO_dump_fp(stdout, outbuf, 16);
  72. EVP_CIPHER_CTX_free(ctx);
  73. }
  74. void aes_ccm_decrypt(void)
  75. {
  76. EVP_CIPHER_CTX *ctx;
  77. int outlen, tmplen, rv;
  78. unsigned char outbuf[1024];
  79. printf("AES CCM Derypt:\n");
  80. printf("Ciphertext:\n");
  81. BIO_dump_fp(stdout, ccm_ct, sizeof(ccm_ct));
  82. ctx = EVP_CIPHER_CTX_new();
  83. /* Select cipher */
  84. EVP_DecryptInit_ex(ctx, EVP_aes_192_ccm(), NULL, NULL, NULL);
  85. /* Set nonce length, omit for 96 bits */
  86. EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_IVLEN, sizeof(ccm_nonce), NULL);
  87. /* Set expected tag value */
  88. EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG,
  89. sizeof(ccm_tag), (void *)ccm_tag);
  90. /* Specify key and IV */
  91. EVP_DecryptInit_ex(ctx, NULL, NULL, ccm_key, ccm_nonce);
  92. /* Set ciphertext length: only needed if we have AAD */
  93. EVP_DecryptUpdate(ctx, NULL, &outlen, NULL, sizeof(ccm_ct));
  94. /* Zero or one call to specify any AAD */
  95. EVP_DecryptUpdate(ctx, NULL, &outlen, ccm_adata, sizeof(ccm_adata));
  96. /* Decrypt plaintext, verify tag: can only be called once */
  97. rv = EVP_DecryptUpdate(ctx, outbuf, &outlen, ccm_ct, sizeof(ccm_ct));
  98. /* Output decrypted block: if tag verify failed we get nothing */
  99. if (rv > 0)
  100. {
  101. printf("Plaintext:\n");
  102. BIO_dump_fp(stdout, outbuf, outlen);
  103. }
  104. else
  105. printf("Plaintext not available: tag verify failed.\n");
  106. EVP_CIPHER_CTX_free(ctx);
  107. }
  108. int main(int argc, char **argv)
  109. {
  110. aes_ccm_encrypt();
  111. aes_ccm_decrypt();
  112. }


2,GCM 

GCM基于并行化设计,因此可以提供高效的吞吐率和低成本、低时延。本质是消息在变形的CTR模式下加密,密文结果与密钥以及消息长度在GF(2^128)域上相乘,计算流程如下所示。其输入输出和CCM基本一致。CCM和GCM的具体计算过程可以参看《密码学与网络安全》第五版,书中有详细的介绍。FRC5288 中介绍了TLS1.2 中的GCM应用。下面贴出openssl中AES-GCM的实例。


  1. /* Simple AES GCM test program, uses the same NIST data used for the FIPS
  2. * self test but uses the application level EVP APIs.
  3. */
  4. #include <stdio.h>
  5. #include <openssl/bio.h>
  6. #include <openssl/evp.h>
  7. /* AES-GCM test data from NIST public test vectors */
  8. static const unsigned char gcm_key[] = {
  9. 0xee,0xbc,0x1f,0x57,0x48,0x7f,0x51,0x92,0x1c,0x04,0x65,0x66,
  10. 0x5f,0x8a,0xe6,0xd1,0x65,0x8b,0xb2,0x6d,0xe6,0xf8,0xa0,0x69,
  11. 0xa3,0x52,0x02,0x93,0xa5,0x72,0x07,0x8f
  12. };
  13. static const unsigned char gcm_iv[] = {
  14. 0x99,0xaa,0x3e,0x68,0xed,0x81,0x73,0xa0,0xee,0xd0,0x66,0x84
  15. };
  16. static const unsigned char gcm_pt[] = {
  17. 0xf5,0x6e,0x87,0x05,0x5b,0xc3,0x2d,0x0e,0xeb,0x31,0xb2,0xea,
  18. 0xcc,0x2b,0xf2,0xa5
  19. };
  20. static const unsigned char gcm_aad[] = {
  21. 0x4d,0x23,0xc3,0xce,0xc3,0x34,0xb4,0x9b,0xdb,0x37,0x0c,0x43,
  22. 0x7f,0xec,0x78,0xde
  23. };
  24. static const unsigned char gcm_ct[] = {
  25. 0xf7,0x26,0x44,0x13,0xa8,0x4c,0x0e,0x7c,0xd5,0x36,0x86,0x7e,
  26. 0xb9,0xf2,0x17,0x36
  27. };
  28. static const unsigned char gcm_tag[] = {
  29. 0x67,0xba,0x05,0x10,0x26,0x2a,0xe4,0x87,0xd7,0x37,0xee,0x62,
  30. 0x98,0xf7,0x7e,0x0c
  31. };
  32. void aes_gcm_encrypt(void)
  33. {
  34. EVP_CIPHER_CTX *ctx;
  35. int outlen, tmplen;
  36. unsigned char outbuf[1024];
  37. printf("AES GCM Encrypt:\n");
  38. printf("Plaintext:\n");
  39. BIO_dump_fp(stdout, gcm_pt, sizeof(gcm_pt));
  40. ctx = EVP_CIPHER_CTX_new();
  41. /* Set cipher type and mode */
  42. EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
  43. /* Set IV length if default 96 bits is not appropriate */
  44. EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, sizeof(gcm_iv), NULL);
  45. /* Initialise key and IV */
  46. EVP_EncryptInit_ex(ctx, NULL, NULL, gcm_key, gcm_iv);
  47. /* Zero or more calls to specify any AAD */
  48. EVP_EncryptUpdate(ctx, NULL, &outlen, gcm_aad, sizeof(gcm_aad));
  49. /* Encrypt plaintext */
  50. EVP_EncryptUpdate(ctx, outbuf, &outlen, gcm_pt, sizeof(gcm_pt));
  51. /* Output encrypted block */
  52. printf("Ciphertext:\n");
  53. BIO_dump_fp(stdout, outbuf, outlen);
  54. /* Finalise: note get no output for GCM */
  55. EVP_EncryptFinal_ex(ctx, outbuf, &outlen);
  56. /* Get tag */
  57. EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, outbuf);
  58. /* Output tag */
  59. printf("Tag:\n");
  60. BIO_dump_fp(stdout, outbuf, 16);
  61. EVP_CIPHER_CTX_free(ctx);
  62. }
  63. void aes_gcm_decrypt(void)
  64. {
  65. EVP_CIPHER_CTX *ctx;
  66. int outlen, tmplen, rv;
  67. unsigned char outbuf[1024];
  68. printf("AES GCM Derypt:\n");
  69. printf("Ciphertext:\n");
  70. BIO_dump_fp(stdout, gcm_ct, sizeof(gcm_ct));
  71. ctx = EVP_CIPHER_CTX_new();
  72. /* Select cipher */
  73. EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
  74. /* Set IV length, omit for 96 bits */
  75. EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, sizeof(gcm_iv), NULL);
  76. /* Specify key and IV */
  77. EVP_DecryptInit_ex(ctx, NULL, NULL, gcm_key, gcm_iv);
  78. #if 0
  79. /* Set expected tag value. A restriction in OpenSSL 1.0.1c and earlier
  80. * required the tag before any AAD or ciphertext */
  81. EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, sizeof(gcm_tag), gcm_tag);
  82. #endif
  83. /* Zero or more calls to specify any AAD */
  84. EVP_DecryptUpdate(ctx, NULL, &outlen, gcm_aad, sizeof(gcm_aad));
  85. /* Decrypt plaintext */
  86. EVP_DecryptUpdate(ctx, outbuf, &outlen, gcm_ct, sizeof(gcm_ct));
  87. /* Output decrypted block */
  88. printf("Plaintext:\n");
  89. BIO_dump_fp(stdout, outbuf, outlen);
  90. /* Set expected tag value. Works in OpenSSL 1.0.1d and later */
  91. EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, sizeof(gcm_tag), gcm_tag);
  92. /* Finalise: note get no output for GCM */
  93. rv = EVP_DecryptFinal_ex(ctx, outbuf, &outlen);
  94. /* Print out return value. If this is not successful authentication
  95. * failed and plaintext is not trustworthy.
  96. */
  97. printf("Tag Verify %s\n", rv > 0 ? "Successful!" : "Failed!");
  98. EVP_CIPHER_CTX_free(ctx);
  99. }
  100. int main(int argc, char **argv)
  101. {
  102. aes_gcm_encrypt();
  103. aes_gcm_decrypt();
  104. }



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/51945
推荐阅读
相关标签
  

闽ICP备14008679号