@@ -79,3 +79,62 @@ void aes128_cbc_enc_finish(aes_context ctx){
79
79
bcal_cbc_free ((bcal_cbc_ctx_t * )ctx );
80
80
free (ctx );
81
81
}
82
+
83
+
84
+
85
+ // decrypt multiple blocks of 128bit data, data_len but be mod 16
86
+ // key and iv are assumed to be both 128bit thus 16 uint8_t's
87
+ void aes128_cbc_dec (uint8_t * key , uint8_t * iv , void * data , uint16_t data_len ){
88
+ if (data_len % 16 != 0 ) {
89
+ return ;
90
+ }
91
+ bcal_cbc_ctx_t ctx ;
92
+ uint8_t r ;
93
+ r = bcal_cbc_init (& aes128_desc , key , 128 , & ctx );
94
+ if (r ) {
95
+ return ;
96
+ }
97
+ bcal_cbc_decMsg (iv , data , data_len / 16 , & ctx );
98
+ bcal_cbc_free (& ctx );
99
+ }
100
+
101
+ // decrypt single 128bit block. data is assumed to be 16 uint8_t's
102
+ // key and iv are assumed to be both 128bit thus 16 uint8_t's
103
+ void aes128_dec_single (uint8_t * key , void * data ){
104
+ aes128_ctx_t ctx ;
105
+ aes128_init (key , & ctx );
106
+ aes128_dec (data , & ctx );
107
+ }
108
+
109
+ // prepare an decrypted to use for decrypting multiple blocks lateron.
110
+ // key and iv are assumed to be both 128bit thus 16 uint8_t's
111
+ aes_context aes128_cbc_dec_start (uint8_t * key , void * iv ){
112
+ bcal_cbc_ctx_t * ctx = (bcal_cbc_ctx_t * )malloc (sizeof (bcal_cbc_ctx_t ));
113
+ uint8_t r = bcal_cbc_init (& aes128_desc , key , 128 , ctx );
114
+ if (r ) {
115
+ free (ctx );
116
+ return NULL ;
117
+ }
118
+ bcal_cbc_loadIV (iv , ctx );
119
+ return (aes_context )ctx ;
120
+ }
121
+
122
+ // decrypt one or more blocks of 128bit data
123
+ // data_len should be mod 16
124
+ void aes128_cbc_dec_continue (aes_context ctx , void * data , uint16_t data_len ){
125
+ if (data_len % 16 != 0 ) {
126
+ return ;
127
+ }
128
+ bcal_cbc_ctx_t * _ctx = (bcal_cbc_ctx_t * )ctx ;
129
+ uint16_t msg_blocks = data_len / 16 ;
130
+ while (msg_blocks -- ){
131
+ bcal_cbc_decNext (data , _ctx );
132
+ data = (uint8_t * )data + _ctx -> blocksize_B ;
133
+ }
134
+ }
135
+
136
+ // cleanup decryption context
137
+ void aes128_cbc_dec_finish (aes_context ctx ){
138
+ bcal_cbc_free ((bcal_cbc_ctx_t * )ctx );
139
+ free (ctx );
140
+ }
0 commit comments