/src/gpac/src/crypto/g_crypt.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * GPAC - Multimedia Framework C SDK |
3 | | * |
4 | | * Authors: Jean Le Feuvre |
5 | | * Copyright (c) Telecom ParisTech 2000-2018 |
6 | | * All rights reserved |
7 | | * |
8 | | * This file is part of GPAC / crypto lib sub-project |
9 | | * |
10 | | * GPAC is free software; you can redistribute it and/or modify |
11 | | * it under the terms of the GNU Lesser General Public License as published by |
12 | | * the Free Software Foundation; either version 2, or (at your option) |
13 | | * any later version. |
14 | | * |
15 | | * GPAC is distributed in the hope that it will be useful, |
16 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | | * GNU Lesser General Public License for more details. |
19 | | * |
20 | | * You should have received a copy of the GNU Lesser General Public |
21 | | * License along with this library; see the file COPYING. If not, write to |
22 | | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
23 | | * |
24 | | */ |
25 | | |
26 | | #include <gpac/internal/crypt_dev.h> |
27 | | |
28 | | GF_EXPORT |
29 | | GF_Crypt *gf_crypt_open(GF_CRYPTO_ALGO algorithm, GF_CRYPTO_MODE mode) |
30 | 0 | { |
31 | 0 | GF_Crypt *td; |
32 | 0 | GF_Err e; |
33 | |
|
34 | 0 | GF_SAFEALLOC(td, GF_Crypt); |
35 | 0 | if (td == NULL) return NULL; |
36 | | |
37 | 0 | #ifdef GPAC_HAS_SSL |
38 | 0 | e = gf_crypt_open_open_openssl(td, mode); |
39 | | #else |
40 | | e = gf_crypt_open_open_tinyaes(td, mode); |
41 | | #endif |
42 | |
|
43 | 0 | if (e != GF_OK) { |
44 | 0 | gf_free(td); |
45 | 0 | return NULL; |
46 | 0 | } |
47 | 0 | return td; |
48 | 0 | } |
49 | | |
50 | | GF_EXPORT |
51 | | void gf_crypt_close(GF_Crypt *td) |
52 | 0 | { |
53 | 0 | if (!td) return; |
54 | 0 | td->_deinit_crypt(td); |
55 | 0 | gf_free(td->context); |
56 | 0 | gf_free(td); |
57 | 0 | } |
58 | | |
59 | | GF_EXPORT |
60 | | GF_Err gf_crypt_set_key(GF_Crypt *td, void *key) |
61 | 0 | { |
62 | 0 | td->_set_key(td, key); |
63 | 0 | return GF_OK; |
64 | 0 | } |
65 | | |
66 | | GF_EXPORT |
67 | | GF_Err gf_crypt_set_IV(GF_Crypt *td, const void *iv, u32 size) |
68 | 256 | { |
69 | 256 | if (!td) return GF_BAD_PARAM; |
70 | 0 | return td->_set_state(td, (void *)iv, size); |
71 | 256 | } |
72 | | |
73 | | GF_EXPORT |
74 | | GF_Err gf_crypt_get_IV(GF_Crypt *td, void *iv, u32 *size) |
75 | 0 | { |
76 | 0 | if (!td) return GF_BAD_PARAM; |
77 | 0 | return td->_get_state(td, iv, size); |
78 | 0 | } |
79 | | |
80 | | |
81 | | GF_EXPORT |
82 | | GF_Err gf_crypt_init(GF_Crypt *td, void *key, const void *IV) |
83 | 0 | { |
84 | 0 | GF_Err e; |
85 | |
|
86 | 0 | e = td->_init_crypt(td, key, IV); |
87 | 0 | if (e != GF_OK) gf_crypt_close(td); |
88 | | //need for openssl we have 2 passes init |
89 | 0 | td->_set_key(td, key); |
90 | 0 | return e; |
91 | 0 | } |
92 | | |
93 | | GF_EXPORT |
94 | | GF_Err gf_crypt_encrypt(GF_Crypt *td, void *plaintext, u32 len) |
95 | 0 | { |
96 | 0 | if (!td) return GF_BAD_PARAM; |
97 | 0 | return td->_crypt(td, plaintext, len); |
98 | 0 | } |
99 | | |
100 | | GF_EXPORT |
101 | | GF_Err gf_crypt_decrypt(GF_Crypt *td, void *ciphertext, u32 len) |
102 | 256 | { |
103 | 256 | if (!td) return GF_BAD_PARAM; |
104 | 0 | if (!len) return GF_OK; |
105 | 0 | return td->_decrypt(td, ciphertext, len); |
106 | 0 | } |