EE445M RTOS
Taken at the University of Texas Spring 2015
des.c
Go to the documentation of this file.
1 //*****************************************************************************
2 //
3 // des.c - Driver for the DES data transformation.
4 //
5 // Copyright (c) 2012-2014 Texas Instruments Incorporated. All rights reserved.
6 // Software License Agreement
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 //
12 // Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright
16 // notice, this list of conditions and the following disclaimer in the
17 // documentation and/or other materials provided with the
18 // distribution.
19 //
20 // Neither the name of Texas Instruments Incorporated nor the names of
21 // its contributors may be used to endorse or promote products derived
22 // from this software without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 //
36 // This is part of revision 2.1.0.12573 of the Tiva Peripheral Driver Library.
37 //
38 //*****************************************************************************
39 
40 //*****************************************************************************
41 //
44 //
45 //*****************************************************************************
46 
47 #include <stdbool.h>
48 #include <stdint.h>
49 #include "inc/hw_des.h"
50 #include "inc/hw_ints.h"
51 #include "inc/hw_memmap.h"
52 #include "inc/hw_types.h"
53 #include "driverlib/debug.h"
54 #include "driverlib/des.h"
55 #include "driverlib/interrupt.h"
56 
57 //*****************************************************************************
58 //
66 //
67 //*****************************************************************************
68 void
69 DESReset(uint32_t ui32Base)
70 {
71  //
72  // Check the arguments.
73  //
74  ASSERT(ui32Base == DES_BASE);
75 
76  //
77  // Trigger the soft reset.
78  //
80 
81  //
82  // Wait for the reset to finish.
83  //
84  while((HWREG(ui32Base + DES_O_SYSSTATUS) &
86  {
87  }
88 }
89 
90 //*****************************************************************************
91 //
122 //
123 //*****************************************************************************
124 void
125 DESConfigSet(uint32_t ui32Base, uint32_t ui32Config)
126 {
127  //
128  // Check the arguments.
129  //
130  ASSERT(ui32Base == DES_BASE);
131 
132  //
133  // Backup the save context field.
134  //
135  ui32Config |= (HWREG(ui32Base + DES_O_CTRL) & DES_CTRL_CONTEXT);
136 
137  //
138  // Write the control register.
139  //
140  HWREG(ui32Base + DES_O_CTRL) = ui32Config;
141 }
142 
143 //*****************************************************************************
144 //
156 //
157 //*****************************************************************************
158 void
159 DESKeySet(uint32_t ui32Base, uint32_t *pui32Key)
160 {
161  //
162  // Check the arguments.
163  //
164  ASSERT(ui32Base == DES_BASE);
165 
166  //
167  // Write the first part of the key.
168  //
169  HWREG(ui32Base + DES_O_KEY1_L) = pui32Key[0];
170  HWREG(ui32Base + DES_O_KEY1_H) = pui32Key[1];
171 
172  //
173  // If we are performing tripe DES, then write the key registers for
174  // the second and third rounds.
175  //
176  if(HWREG(ui32Base + DES_O_CTRL) & DES_CFG_TRIPLE)
177  {
178  HWREG(ui32Base + DES_O_KEY2_L) = pui32Key[2];
179  HWREG(ui32Base + DES_O_KEY2_H) = pui32Key[3];
180  HWREG(ui32Base + DES_O_KEY3_L) = pui32Key[4];
181  HWREG(ui32Base + DES_O_KEY3_H) = pui32Key[5];
182  }
183 }
184 
185 //*****************************************************************************
186 //
199 //
200 //*****************************************************************************
201 bool
202 DESIVSet(uint32_t ui32Base, uint32_t *pui32IVdata)
203 {
204  //
205  // Check the arguments.
206  //
207  ASSERT(ui32Base == DES_BASE);
208 
209  //
210  // Check to see if context registers can be overwritten. If not, return
211  // false.
212  //
213  if((HWREG(ui32Base + DES_O_CTRL) & DES_CTRL_CONTEXT) == 0)
214  {
215  return(false);
216  }
217 
218  //
219  // Write the initialization vector registers.
220  //
221  HWREG(ui32Base + DES_O_IV_L) = pui32IVdata[0];
222  HWREG(ui32Base + DES_O_IV_H) = pui32IVdata[1];
223 
224  //
225  // Return true to indicate the write was successful.
226  //
227  return(true);
228 }
229 
230 //*****************************************************************************
231 //
244 //
245 //*****************************************************************************
246 void
247 DESLengthSet(uint32_t ui32Base, uint32_t ui32Length)
248 {
249  //
250  // Check the arguments.
251  //
252  ASSERT(ui32Base == DES_BASE);
253 
254  //
255  // Write the length register.
256  //
257  HWREG(ui32Base + DES_O_LENGTH) = ui32Length;
258 }
259 
260 //*****************************************************************************
261 //
271 //
272 //*****************************************************************************
273 bool
274 DESDataReadNonBlocking(uint32_t ui32Base, uint32_t *pui32Dest)
275 {
276  //
277  // Check the arguments.
278  //
279  ASSERT(ui32Base == DES_BASE);
280 
281  //
282  // Check to see if the data is ready to be read.
283  //
284  if((DES_CTRL_OUTPUT_READY & (HWREG(ui32Base + DES_O_CTRL))) == 0)
285  {
286  return(false);
287  }
288 
289  //
290  // Read two words of data from the data registers.
291  //
292  pui32Dest[0] = HWREG(DES_BASE + DES_O_DATA_L);
293  pui32Dest[1] = HWREG(DES_BASE + DES_O_DATA_H);
294 
295  //
296  // Return true to indicate a successful write.
297  //
298  return(true);
299 }
300 
301 //*****************************************************************************
302 //
313 //
314 //*****************************************************************************
315 void
316 DESDataRead(uint32_t ui32Base, uint32_t *pui32Dest)
317 {
318  //
319  // Check the arguments.
320  //
321  ASSERT(ui32Base == DES_BASE);
322 
323  //
324  // Wait for data output to be ready.
325  //
326  while((HWREG(ui32Base + DES_O_CTRL) & DES_CTRL_OUTPUT_READY) == 0)
327  {
328  }
329 
330  //
331  // Read two words of data from the data registers.
332  //
333  pui32Dest[0] = HWREG(DES_BASE + DES_O_DATA_L);
334  pui32Dest[1] = HWREG(DES_BASE + DES_O_DATA_H);
335 }
336 
337 //*****************************************************************************
338 //
348 //
349 //*****************************************************************************
350 bool
351 DESDataWriteNonBlocking(uint32_t ui32Base, uint32_t *pui32Src)
352 {
353  //
354  // Check the arguments.
355  //
356  ASSERT(ui32Base == DES_BASE);
357 
358  //
359  // Check if the DES module is ready to encrypt or decrypt data. If it
360  // is not, return false.
361  //
362  if(!(DES_CTRL_INPUT_READY & (HWREG(ui32Base + DES_O_CTRL))))
363  {
364  return(false);
365  }
366 
367  //
368  // Write the data.
369  //
370  HWREG(DES_BASE + DES_O_DATA_L) = pui32Src[0];
371  HWREG(DES_BASE + DES_O_DATA_H) = pui32Src[1];
372 
373  //
374  // Return true to indicate a successful write.
375  //
376  return(true);
377 }
378 
379 //*****************************************************************************
380 //
390 //
391 //*****************************************************************************
392 void
393 DESDataWrite(uint32_t ui32Base, uint32_t *pui32Src)
394 {
395  //
396  // Check the arguments.
397  //
398  ASSERT(ui32Base == DES_BASE);
399 
400  //
401  // Wait for the input ready bit to go high.
402  //
403  while(((HWREG(ui32Base + DES_O_CTRL) & DES_CTRL_INPUT_READY)) == 0)
404  {
405  }
406 
407  //
408  // Write the data.
409  //
410  HWREG(DES_BASE + DES_O_DATA_L) = pui32Src[0];
411  HWREG(DES_BASE + DES_O_DATA_H) = pui32Src[1];
412 }
413 
414 //*****************************************************************************
415 //
435 //
436 //*****************************************************************************
437 bool
438 DESDataProcess(uint32_t ui32Base, uint32_t *pui32Src, uint32_t *pui32Dest,
439  uint32_t ui32Length)
440 {
441  uint32_t ui32Count;
442 
443  //
444  // Check the arguments.
445  //
446  ASSERT(ui32Base == DES_BASE);
447  ASSERT((ui32Length % 8) == 0);
448 
449  //
450  // Write the length register first. This triggers the engine to start
451  // using this context.
452  //
453  HWREG(ui32Base + DES_O_LENGTH) = ui32Length;
454 
455  //
456  // Now loop until the blocks are written.
457  //
458  for(ui32Count = 0; ui32Count < (ui32Length / 4); ui32Count += 2)
459  {
460  //
461  // Check if the input ready is fine
462  //
463  while((DES_CTRL_INPUT_READY & (HWREG(ui32Base + DES_O_CTRL))) == 0)
464  {
465  }
466 
467  //
468  // Write the block data.
469  //
470  DESDataWriteNonBlocking(ui32Base, pui32Src + ui32Count);
471 
472  //
473  // Wait for the output ready
474  //
475  while((DES_CTRL_OUTPUT_READY & (HWREG(ui32Base + DES_O_CTRL))) == 0)
476  {
477  }
478 
479  //
480  // Read the processed data block.
481  //
482  DESDataReadNonBlocking(ui32Base, pui32Dest + ui32Count);
483  }
484 
485  //
486  // Return true to indicate the process was successful.
487  //
488  return(true);
489 }
490 
491 //*****************************************************************************
492 //
510 //
511 //*****************************************************************************
512 uint32_t
513 DESIntStatus(uint32_t ui32Base, bool bMasked)
514 {
515  uint32_t ui32Status, ui32Enable;
516 
517  //
518  // Check the arguments.
519  //
520  ASSERT(ui32Base == DES_BASE);
521 
522  //
523  // Read the status register and return the value.
524  //
525  ui32Status = HWREG(ui32Base + DES_O_IRQSTATUS);
526  if(bMasked)
527  {
528  ui32Enable = HWREG(ui32Base + DES_O_IRQENABLE);
529  return((ui32Status & ui32Enable) |
530  (HWREG(ui32Base + DES_O_DMAMIS) << 16));
531  }
532  else
533  {
534  return(ui32Status | (HWREG(ui32Base + DES_O_DMARIS) << 16));
535  }
536 }
537 
538 //*****************************************************************************
539 //
556 //
557 //*****************************************************************************
558 void
559 DESIntEnable(uint32_t ui32Base, uint32_t ui32IntFlags)
560 {
561  //
562  // Check the arguments.
563  //
564  ASSERT(ui32Base == DES_BASE);
565  ASSERT((ui32IntFlags & DES_INT_CONTEXT_IN) ||
566  (ui32IntFlags & DES_INT_DATA_IN) ||
567  (ui32IntFlags & DES_INT_DATA_OUT) ||
568  (ui32IntFlags & DES_INT_DMA_CONTEXT_IN) ||
569  (ui32IntFlags & DES_INT_DMA_DATA_IN) ||
570  (ui32IntFlags & DES_INT_DMA_DATA_OUT));
571 
572  //
573  // Enable the interrupts from the flags.
574  //
575  HWREG(ui32Base + DES_O_DMAIM) |= (ui32IntFlags & 0x00070000) >> 16;
576  HWREG(ui32Base + DES_O_IRQENABLE) |= ui32IntFlags & 0x0000ffff;
577 }
578 
579 //*****************************************************************************
580 //
598 //
599 //*****************************************************************************
600 void
601 DESIntDisable(uint32_t ui32Base, uint32_t ui32IntFlags)
602 {
603  //
604  // Check the arguments.
605  //
606  ASSERT(ui32Base == DES_BASE);
607  ASSERT((ui32IntFlags & DES_INT_CONTEXT_IN) ||
608  (ui32IntFlags & DES_INT_DATA_IN) ||
609  (ui32IntFlags & DES_INT_DATA_OUT) ||
610  (ui32IntFlags & DES_INT_DMA_CONTEXT_IN) ||
611  (ui32IntFlags & DES_INT_DMA_DATA_IN) ||
612  (ui32IntFlags & DES_INT_DMA_DATA_OUT));
613 
614  //
615  // Clear the interrupts from the flags.
616  //
617  HWREG(ui32Base + DES_O_DMAIM) &= ~((ui32IntFlags & 0x00070000) >> 16);
618  HWREG(ui32Base + DES_O_IRQENABLE) &= ~(ui32IntFlags & 0x0000ffff);
619 }
620 
621 //*****************************************************************************
622 //
640 //
641 //*****************************************************************************
642 void
643 DESIntClear(uint32_t ui32Base, uint32_t ui32IntFlags)
644 {
645  //
646  // Check the arguments.
647  //
648  ASSERT(ui32Base == DES_BASE);
649  ASSERT((ui32IntFlags & DES_INT_DMA_CONTEXT_IN) ||
650  (ui32IntFlags & DES_INT_DMA_DATA_IN) ||
651  (ui32IntFlags & DES_INT_DMA_DATA_OUT));
652 
653  HWREG(ui32Base + DES_O_DMAIC) = (ui32IntFlags & 0x00070000) >> 16;
654 }
655 
656 //*****************************************************************************
657 //
679 //
680 //*****************************************************************************
681 void
682 DESIntRegister(uint32_t ui32Base, void (*pfnHandler)(void))
683 {
684  //
685  // Check the arguments.
686  //
687  ASSERT(ui32Base == DES_BASE);
688 
689  //
690  // Register the interrupt handler.
691  //
692  IntRegister(INT_DES0_TM4C129, pfnHandler);
693 
694  //
695  // Enable the interrupt.
696  //
698 }
699 
700 //*****************************************************************************
701 //
713 //
714 //*****************************************************************************
715 void
716 DESIntUnregister(uint32_t ui32Base)
717 {
718  //
719  // Check the arguments.
720  //
721  ASSERT(ui32Base == DES_BASE);
722 
723  //
724  // Disable the interrupt.
725  //
727 
728  //
729  // Unregister the interrupt handler.
730  //
732 }
733 
734 //*****************************************************************************
735 //
749 //
750 //*****************************************************************************
751 void
752 DESDMAEnable(uint32_t ui32Base, uint32_t ui32Flags)
753 {
754  //
755  // Check the arguments.
756  //
757  ASSERT(ui32Base == DES_BASE);
758  ASSERT((ui32Flags & DES_DMA_CONTEXT_IN) ||
759  (ui32Flags & DES_DMA_DATA_OUT) ||
760  (ui32Flags & DES_DMA_DATA_IN));
761 
762  //
763  // Set the data in and data out DMA request enable bits.
764  //
765  HWREG(ui32Base + DES_O_SYSCONFIG) |= ui32Flags;
766 }
767 
768 //*****************************************************************************
769 //
783 //
784 //*****************************************************************************
785 void
786 DESDMADisable(uint32_t ui32Base, uint32_t ui32Flags)
787 {
788  //
789  // Check the arguments.
790  //
791  ASSERT(ui32Base == DES_BASE);
792  ASSERT((ui32Flags & DES_DMA_CONTEXT_IN) ||
793  (ui32Flags & DES_DMA_DATA_OUT) ||
794  (ui32Flags & DES_DMA_DATA_IN));
795 
796  //
797  // Disable the DMA sources.
798  //
799  HWREG(ui32Base + DES_O_SYSCONFIG) &= ~ui32Flags;
800 }
801 
802 //*****************************************************************************
803 //
804 // Close the Doxygen group.
806 //
807 //*****************************************************************************
void DESConfigSet(uint32_t ui32Base, uint32_t ui32Config)
Definition: des.c:125
#define INT_DES0_TM4C129
Definition: hw_ints.h:270
#define DES_DMA_DATA_OUT
Definition: des.h:103
#define DES_CTRL_INPUT_READY
Definition: hw_des.h:155
uint32_t DESIntStatus(uint32_t ui32Base, bool bMasked)
Definition: des.c:513
#define DES_INT_DMA_DATA_OUT
Definition: des.h:94
#define HWREG(x)
Definition: hw_types.h:48
bool DESDataReadNonBlocking(uint32_t ui32Base, uint32_t *pui32Dest)
Definition: des.c:274
#define DES_CFG_TRIPLE
Definition: des.h:81
#define DES_O_DATA_L
Definition: hw_des.h:58
void DESLengthSet(uint32_t ui32Base, uint32_t ui32Length)
Definition: des.c:247
void DESDMADisable(uint32_t ui32Base, uint32_t ui32Flags)
Definition: des.c:786
#define DES_DMA_CONTEXT_IN
Definition: des.h:102
#define ASSERT(expr)
Definition: debug.h:67
#define DES_INT_DATA_OUT
Definition: des.h:91
bool DESDataProcess(uint32_t ui32Base, uint32_t *pui32Src, uint32_t *pui32Dest, uint32_t ui32Length)
Definition: des.c:438
#define DES_O_LENGTH
Definition: hw_des.h:57
#define DES_O_DMAMIS
Definition: hw_des.h:68
#define DES_O_KEY1_L
Definition: hw_des.h:52
#define DES_O_DMAIM
Definition: hw_des.h:66
#define DES_SYSSTATUS_RESETDONE
Definition: hw_des.h:219
void DESIntRegister(uint32_t ui32Base, void(*pfnHandler)(void))
Definition: des.c:682
#define DES_O_SYSSTATUS
Definition: hw_des.h:62
#define DES_O_KEY1_H
Definition: hw_des.h:53
void DESKeySet(uint32_t ui32Base, uint32_t *pui32Key)
Definition: des.c:159
#define DES_O_DMAIC
Definition: hw_des.h:69
bool DESIVSet(uint32_t ui32Base, uint32_t *pui32IVdata)
Definition: des.c:202
#define DES_CTRL_OUTPUT_READY
Definition: hw_des.h:157
#define DES_INT_DMA_DATA_IN
Definition: des.h:93
#define DES_O_KEY2_L
Definition: hw_des.h:50
#define DES_INT_DATA_IN
Definition: des.h:90
#define DES_O_IV_L
Definition: hw_des.h:54
void DESIntEnable(uint32_t ui32Base, uint32_t ui32IntFlags)
Definition: des.c:559
#define DES_O_KEY2_H
Definition: hw_des.h:51
#define DES_O_IV_H
Definition: hw_des.h:55
void DESReset(uint32_t ui32Base)
Definition: des.c:69
void DESDataWrite(uint32_t ui32Base, uint32_t *pui32Src)
Definition: des.c:393
void DESDMAEnable(uint32_t ui32Base, uint32_t ui32Flags)
Definition: des.c:752
void IntUnregister(uint32_t ui32Interrupt)
Definition: interrupt.c:381
#define DES_CTRL_CONTEXT
Definition: hw_des.h:142
#define DES_DMA_DATA_IN
Definition: des.h:104
void DESIntUnregister(uint32_t ui32Base)
Definition: des.c:716
#define DES_BASE
Definition: hw_memmap.h:142
#define DES_SYSCONFIG_SOFTRESET
Definition: hw_des.h:211
#define DES_O_SYSCONFIG
Definition: hw_des.h:61
void DESIntClear(uint32_t ui32Base, uint32_t ui32IntFlags)
Definition: des.c:643
void DESDataRead(uint32_t ui32Base, uint32_t *pui32Dest)
Definition: des.c:316
#define DES_O_KEY3_H
Definition: hw_des.h:49
#define DES_O_IRQSTATUS
Definition: hw_des.h:63
#define DES_INT_CONTEXT_IN
Definition: des.h:89
#define DES_O_DMARIS
Definition: hw_des.h:67
void DESIntDisable(uint32_t ui32Base, uint32_t ui32IntFlags)
Definition: des.c:601
#define DES_O_DATA_H
Definition: hw_des.h:59
#define DES_INT_DMA_CONTEXT_IN
Definition: des.h:92
#define DES_O_IRQENABLE
Definition: hw_des.h:64
#define DES_O_KEY3_L
Definition: hw_des.h:48
void IntDisable(uint32_t ui32Interrupt)
Definition: interrupt.c:684
void IntRegister(uint32_t ui32Interrupt, void(*pfnHandler)(void))
Definition: interrupt.c:309
#define DES_O_CTRL
Definition: hw_des.h:56
void IntEnable(uint32_t ui32Interrupt)
Definition: interrupt.c:610
bool DESDataWriteNonBlocking(uint32_t ui32Base, uint32_t *pui32Src)
Definition: des.c:351