• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the License); you may
5  *  not use this file except in compliance with the License.
6  *
7  *  http://www.apache.org/licenses/LICENSE-2.0
8  */
9 
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <gmssl/sm4.h>
14 #include <gmssl/rand.h>
15 
16 
main(void)17 int main(void)
18 {
19 	SM4_CTR_CTX cbc_ctx;
20 	unsigned char key[16] = {
21 		0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,
22 		0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,
23 	};
24 	unsigned char ctr[16] = {
25 		0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,
26 		0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,
27 	};
28 	unsigned char inbuf[1024];
29 	unsigned char outbuf[1024 + 32];
30 	ssize_t inlen;
31 	size_t outlen;
32 
33 	if (sm4_ctr_encrypt_init(&cbc_ctx, key, ctr) != 1) {
34 		fprintf(stderr, "%s %d: error\n", __FILE__, __LINE__);
35 		return 1;
36 	}
37 	while ((inlen = fread(inbuf, 1, sizeof(inbuf), stdin)) > 0) {
38 		if (sm4_ctr_encrypt_update(&cbc_ctx, inbuf, inlen, outbuf, &outlen) != 1) {
39 			fprintf(stderr, "%s %d: error\n", __FILE__, __LINE__);
40 			return 1;
41 		}
42 		fwrite(outbuf, 1, outlen, stdout);
43 	}
44 	if (sm4_ctr_encrypt_finish(&cbc_ctx, outbuf, &outlen) != 1) {
45 		fprintf(stderr, "%s %d: error\n", __FILE__, __LINE__);
46 		return 1;
47 	}
48 	fwrite(outbuf, 1, outlen, stdout);
49 
50 	return 0;
51 }
52