Wednesday, April 20, 2022

Security Concepts

 Security Concepts


1. data flow diagram, tracking data flow and protecting the path using encryption - Confidentiality

2. root of trust/ chain of trust - Integrity

3. Multi-layer security - defense in depth to keep in mind before designing an APP. Supplementary protection.

4. Data Injection checks for Injection attack. Input/Output Validation/ Authentication

5. Protect the ROM Image using Chain of trust

6. Create security zone - memory / process data  --  Public, private or regulated data

7. Fail safe / Server Error response from Applications.

8. Proper Non-verbose error and error response.

9. Data residency spread across different zones.

10. TOC TOU , this is time of copy and time of use.

C programming - Remove Nth Node From End of Singly Link List

Remove Nth Node From End of List

/**
 * Definition for singly-linked list.
*/
struct ListNode {
   int val;
   struct ListNode *next;
};

struct ListNode* removeNthFromEnd(struct ListNode* head, int n)
{
    struct ListNode *f0 = head;
    struct ListNode *f1 = head;
    struct ListNode *f2 = head;
    struct ListNode *temp = head;
    int flag = 1;

    if((head ==  NULL) || (n < 0))
        return NULL;

    if(n == 0)
    {
        return head;
    }    
    else if(n == 1)
    {
        // if there is only one node in list
        if(head->next == NULL)
        {   
            free(head);
            return NULL;
        }    
        else
        {    
           while(f1->next != NULL)
           {
                f0 = f1;    
                f1 = f1->next;
           }    
        }
        free(f1);
        f0->next = NULL;
        return head;
    }
    
    n--;

    while(f2->next != NULL)
    {          
        if(n != 0)
        {        
           f2 = f2->next; 
           n--;
        }
        else
        {   
            f0 = f1; 
            f1 = f1->next;
            f2 = f2->next; 
            flag = 0;
        }
    } 
    if(flag != 0)
    {    
        temp = head->next;
        free(head);
    }
    else
    {
        f0->next = f1->next;
        temp = head;
        free(f1);
    }    
    return temp;
}

Sunday, April 17, 2022

C programming - count number of bits that are set in an integer ( without using loops )


#define bit(val, p)  ((val & (1 << p)) >> p)

int main()

{

    unsigned int val   = 0x12344;

    int count = 0;

    count = bit(val,0) +  bit(val,1) + bit(val,2) + bit(val,3)

            + bit(val,4) + bit(val,5) + bit(val,6) + bit(val,7)

            + bit(val,8) + bit(val,9) + bit(val,10) + bit(val,11)

            + bit(val,12) + bit(val,13) + bit(val,14) + bit(val,15)

            + bit(val,16) + bit(val,17) + bit(val,18) + bit(val,19)

            + bit(val,20) + bit(val,21) + bit(val,22) + bit(val,23)

            + bit(val,24) + bit(val,25) + bit(val,26) + bit(val,27)

            + bit(val,28) + bit(val,29) + bit(val,30) + bit(val,31);

    printf("\ncount  = %d\n", count);

    return 0;

}