openssl学习之ccmgcm模式Word文件下载.docx
- 文档编号:4061036
- 上传时间:2023-05-02
- 格式:DOCX
- 页数:22
- 大小:18.93KB
openssl学习之ccmgcm模式Word文件下载.docx
《openssl学习之ccmgcm模式Word文件下载.docx》由会员分享,可在线阅读,更多相关《openssl学习之ccmgcm模式Word文件下载.docx(22页珍藏版)》请在冰点文库上搜索。
Simple
AES
CCM
test
program,
uses
the
same
NIST
data
used
for
FIPS
2.
*
self
but
application
level
EVP
APIs.
3.
*/
4.#include
<
stdio.h>
5.#include
openssl/bio.h>
6.#include
openssl/evp.h>
7.
8./*
AES-CCM
from
public
vectors
9.
10.static
const
unsigned
char
ccm_key[]
=
{
11.
0xce,0xb0,0x09,0xae,0xa4,0x45,0x44,0x51,0xfe,0xad,0xf0,0xe6,
12.
0xb3,0x6f,0x45,0x55,0x5d,0xd0,0x47,0x23,0xba,0xa4,0x48,0xe8
13.};
14.//
随机数,每次加密针对相同的KEY使用不同的NONCE。
否则会破坏CCM模式的安全性(RFC3610)
15.static
ccm_nonce[]
16.
0x76,0x40,0x43,0xc4,0x94,0x60,0xb7
17.};
18.//附加数据
19.static
ccm_adata[]
20.
0x6e,0x80,0xdd,0x7f,0x1b,0xad,0xf3,0xa1,0xc9,0xab,0x25,0xc7,
21.
0x5f,0x10,0xbd,0xe7,0x8c,0x23,0xfa,0x0e,0xb8,0xf9,0xaa,0xa5,
22.
0x3a,0xde,0xfb,0xf4,0xcb,0xf7,0x8f,0xe4
23.};
24.//plaintext
表示明文
25.static
ccm_pt[]
26.
0xc8,0xd2,0x75,0xf9,0x19,0xe1,0x7d,0x7f,0xe6,0x9c,0x2a,0x1f,
27.
0x58,0x93,0x9d,0xfe,0x4d,0x40,0x37,0x91,0xb5,0xdf,0x13,0x10
28.};
29.//ciphertext
表示密文
30.static
ccm_ct[]
31.
0x8a,0x0f,0x3d,0x82,0x29,0xe4,0x8e,0x74,0x87,0xfd,0x95,0xa2,
32.
0x8a,0xd3,0x92,0xc8,0x0b,0x36,0x81,0xd4,0xfb,0xc7,0xbb,0xfd
33.};
34.//tag
表示tag数据
35.static
ccm_tag[]
36.
0x2d,0xd6,0xef,0x1c,0x45,0xd4,0xcc,0xb7,0x23,0xdc,0x07,0x44,
37.
0x14,0xdb,0x50,0x6d
38.};
39.
40.void
aes_ccm_encrypt(void)
41.{
42.
EVP_CIPHER_CTX
*ctx;
43.
int
outlen,
tmplen;
44.
outbuf[1024];
45.
printf("
Encrypt:
\n"
);
46.
Plaintext:
47.
BIO_dump_fp(stdout,
ccm_pt,
sizeof(ccm_pt));
48.
ctx
EVP_CIPHER_CTX_new();
49.
/*
Set
cipher
type
and
mode
50.
EVP_EncryptInit_ex(ctx,
EVP_aes_192_ccm(),
NULL,
NULL);
51.
nonce
length
if
default
96
bits
is
not
appropriate
52.
EVP_CIPHER_CTX_ctrl(ctx,
EVP_CTRL_CCM_SET_IVLEN,
sizeof(ccm_nonce),
53.
tag
54.
EVP_CTRL_CCM_SET_TAG,
sizeof(ccm_tag),
55.
Initialise
key
IV
56.
ccm_key,
ccm_nonce);
57.
plaintext
length:
only
needed
AAD
used*/
58.
//输入输出需设置为NULL
59.
EVP_EncryptUpdate(ctx,
&
60.
Zero
or
one
call
to
specify
any
61.
//设置AAD,out参数需设置为NULL
62.
ccm_adata,
sizeof(ccm_adata));
63.
Encrypt
plaintext:
can
be
called
once
64.
outbuf,
65.
Output
encrypted
block
66.
Ciphertext:
67.
outlen);
68.
Finalise:
note
get
no
output
69.
EVP_EncryptFinal_ex(ctx,
70.
Get
71.
EVP_CTRL_CCM_GET_TAG,
16,
outbuf);
72.
73.
Tag:
74.
16);
75.
EVP_CIPHER_CTX_free(ctx);
76.}
77.
78.void
aes_ccm_decrypt(void)
79.{
80.
81.
tmplen,
rv;
82.
83.
Derypt:
84.
85.
ccm_ct,
sizeof(ccm_ct));
86.
87.
Select
88.
EVP_DecryptInit_ex(ctx,
89.
length,
omit
90.
91.
expected
value
92.
93.
(void
*)ccm_tag);
94.
Specify
95.
96.
ciphertext
we
have
97.
EVP_DecryptUpdate(ctx,
98.
99.
100.
Decrypt
plaintext,
verify
tag:
101.
rv
102.
decrypted
block:
failed
nothing
103.
(rv
>
0)
104.
105.
106.
107.
}
108.
else
109.
Plaintext
available:
failed.\n"
110.
111.}
112.
113.int
main(int
argc,
**argv)
114.{
115.
aes_ccm_encrypt();
116.
aes_ccm_decrypt();
117.}
/
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- openssl 学习 ccmgcm 模式