ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
decrypt.c
Go to the documentation of this file.
1/*****************************************************************************
2 * ugBASIC - an isomorphic BASIC language compiler for retrocomputers *
3 *****************************************************************************
4 * Copyright 2021-2025 Marco Spedaletti (asimov@mclink.it)
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *----------------------------------------------------------------------------
18 * Concesso in licenza secondo i termini della Licenza Apache, versione 2.0
19 * (la "Licenza"); è proibito usare questo file se non in conformità alla
20 * Licenza. Una copia della Licenza è disponibile all'indirizzo:
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Se non richiesto dalla legislazione vigente o concordato per iscritto,
25 * il software distribuito nei termini della Licenza è distribuito
26 * "COSÌ COM'È", SENZA GARANZIE O CONDIZIONI DI ALCUN TIPO, esplicite o
27 * implicite. Consultare la Licenza per il testo specifico che regola le
28 * autorizzazioni e le limitazioni previste dalla medesima.
29 ****************************************************************************/
30
31/****************************************************************************
32 * INCLUDE SECTION
33 ****************************************************************************/
34
35#include "../../ugbc.h"
36
37/****************************************************************************
38 * CODE SECTION
39 ****************************************************************************/
40
46/* <usermanual>
47@keyword DECRYPT
48
49@english
50
51The ''DECRYPT'' command applies a symmetric decryption algorithm to a
52structured variable or memory area, checking for security checksum
53present at the end, to prevent data corruption or accidental tampering.
54
55When the command is executed, the destination buffer is filled with the
56original data decrypted by the encryption operation. A computed value
57a checksum is recalculated and checked with the one on the end of the
58buffer, to serve as a trust seal.
59
60Note that the size of the key must be the same as the size of the data.
61
62@italian
63
64Il comando ''DECRYPT'' applica un algoritmo di decrittazione simmetrico a una
65variabile strutturata o a un'area di memoria, verificando la presenza di un checksum di sicurezza
66alla fine, per prevenire il danneggiamento dei dati o manomissioni accidentali.
67
68Quando il comando viene eseguito, il buffer di destinazione viene riempito con i
69dati originali decrittografati dall'operazione di crittografia. Un valore calcolato
70un checksum viene ricalcolato e confrontato con quello alla fine del
71buffer, per fungere da sigillo di attendibilità.
72
73Si noti che la dimensione della chiave deve essere uguale alla dimensione dei dati.
74
75@syntax = DECRYPT( data, key TO var )
76
77@example DIM params(2) AS INTEGER
78@example IF DECRYPT( ENCRYPT( params, "OKOK" ), "OKOK" TO params) THEN: PRINT "OK!": ENDIF
79
80</usermanual> */
81Variable * decrypt( Environment * _environment, char * _data, char * _key, char * _var ) {
82
83 Variable * data = variable_retrieve( _environment, _data );
84 Variable * key = variable_retrieve( _environment, _key );
85 Variable * output = variable_retrieve( _environment, _var );
86 Variable * result = variable_temporary( _environment, VT_SBYTE, "(result)");
87
88 Variable * dataAddress = variable_temporary( _environment, VT_ADDRESS, "(address of data)");
89 Variable * dataSize = variable_temporary( _environment, VT_BYTE, "(size of data)");
90
91 Variable * keyAddress = variable_temporary( _environment, VT_ADDRESS, "(address of data)");
92 Variable * keySize = variable_temporary( _environment, VT_BYTE, "(size of data)");
93
94 Variable * outputAddress = variable_temporary( _environment, VT_ADDRESS, "(address of output)");
95
96 switch( data->type ) {
97 case VT_STRING: {
98 cpu_move_8bit( _environment, data->realName, dataSize->realName );
99 cpu_addressof_16bit( _environment, data->realName, dataAddress->realName );
100 cpu_inc_16bit( _environment, dataAddress->realName );
101 break;
102 };
103 case VT_DSTRING: {
104 cpu_dsdescriptor( _environment, data->realName, dataAddress->realName, dataSize->realName );
105 break;
106 };
107 case VT_TYPE:
108 case VT_TARRAY:
109 case VT_BUFFER: {
110 cpu_store_8bit( _environment, dataSize->realName, data->size );
111 cpu_addressof_16bit( _environment, data->realName, dataAddress->realName );
112 break;
113 };
114 }
115
116 switch( key->type ) {
117 case VT_STRING: {
118 cpu_move_8bit( _environment, key->realName, keySize->realName );
119 cpu_addressof_16bit( _environment, key->realName, keyAddress->realName );
120 cpu_inc_16bit( _environment, keyAddress->realName );
121 break;
122 };
123 case VT_TARRAY:
124 case VT_TYPE:
125 case VT_BUFFER: {
126 cpu_store_8bit( _environment, key->realName, key->size );
127 cpu_addressof_16bit( _environment, key->realName, keyAddress->realName );
128 break;
129 };
130 case VT_DSTRING: {
131 cpu_dsdescriptor( _environment, key->realName, keyAddress->realName, keySize->realName );
132 break;
133 };
134 }
135
136 if ( VT_BITWIDTH( output->type ) ) {
137 cpu_store_8bit( _environment, dataSize->realName, output->size );
138 cpu_addressof_16bit( _environment, output->realName, outputAddress->realName );
139 } else {
140 switch( output->type ) {
141 case VT_TARRAY:
142 case VT_TYPE:
143 case VT_BUFFER: {
144 cpu_store_8bit( _environment, dataSize->realName, output->size );
145 cpu_addressof_16bit( _environment, output->realName, outputAddress->realName );
146 break;
147 };
148 case VT_DSTRING: {
149 cpu_dsfree( _environment, output->realName );
150 cpu_dsalloc( _environment, keySize->realName, output->realName );
151 cpu_dsdescriptor( _environment, output->realName, outputAddress->realName, NULL );
152 break;
153 };
154 default:
156 }
157 }
158
159 cpu_decrypt( _environment, dataAddress->realName, dataSize->realName, keyAddress->realName, keySize->realName, outputAddress->realName, result->realName );
160
161 return result;
162
163}
164
void cpu_dsfree(Environment *_environment, char *_index)
Definition 6309.c:5917
void cpu_addressof_16bit(Environment *_environment, char *_source, char *_destination)
Definition 6309.c:1485
void cpu_decrypt(Environment *_environment, char *_data, char *_data_size, char *_key, char *_key_size, char *_output, char *_result)
Definition 6309.c:7582
void cpu_dsalloc(Environment *_environment, char *_size, char *_index)
Definition 6309.c:5895
void cpu_store_8bit(Environment *_environment, char *_destination, int _value)
CPU 6309: emit code to store 8 bit
Definition 6309.c:761
void cpu_inc_16bit(Environment *_environment, char *_variable)
Definition 6309.c:4565
void cpu_move_8bit(Environment *_environment, char *_source, char *_destination)
CPU 6309: emit code to move 8 bit
Definition 6309.c:743
void cpu_dsdescriptor(Environment *_environment, char *_index, char *_address, char *_size)
Definition 6309.c:5977
Variable * variable_retrieve(Environment *_environment, char *_name)
Variable * variable_temporary(Environment *_environment, VariableType _type, char *_meaning)
Define a temporary variable.
Variable * decrypt(Environment *_environment, char *_data, char *_key, char *_var)
Emit code for DECRYPT.
Definition decrypt.c:81
int size
Definition ugbc.h:1077
VariableType type
Definition ugbc.h:988
char * realName
Definition ugbc.h:982
struct _Variable Variable
Structure of a single variable.
struct _Environment Environment
Structure of compilation environment.
@ VT_TARRAY
Definition ugbc.h:480
@ VT_STRING
Definition ugbc.h:474
@ VT_BYTE
Definition ugbc.h:450
@ VT_BUFFER
Definition ugbc.h:477
@ VT_SBYTE
Definition ugbc.h:452
@ VT_TYPE
Definition ugbc.h:546
@ VT_ADDRESS
Definition ugbc.h:465
@ VT_DSTRING
Definition ugbc.h:483
#define CRITICAL_CANNOT_DECRYPT_TO_DATATYPE(s)
Definition ugbc.h:3857
#define VT_BITWIDTH(t)
Definition ugbc.h:595