顯示具有 分析了再做 標籤的文章。 顯示所有文章
顯示具有 分析了再做 標籤的文章。 顯示所有文章

2016年1月1日 星期五

公鑰加密及數位簽章

key work: public key cryptography
A.shared secret
以共同祕密(shared secret)對資訊做加減的運算.其中Shared secret中key的長度,
在現在一般在講的128bit encryption,就是指其shared secret的長度(128bit 約38數字, log10 2=0.3).而除此外,就如古早的替換加密,也可能以統計來反解譯.因此, 現代的會以[分塊加密,block ciphers].
  一'將長訊息細分為10~15 byte的字元,二'不光只是加上shared secret而是根據固定的規則將每塊訊息經多次(十次)轉換. ex,將公鑰的前半部加到訊息的後半部, 再將結果反轉 ,最後再把公鑰的後半部加到訊息的前半部.
ex: AES(advanced Encryption Standard)
B.Diffie-Hellman Key exchange(paint –mixing trick)
而其中對於shared secret中key要放在那才能不被其它人得知.
因此,在真實實務上用上一個"離散指數(discrete exponentiation)及還原則是離散對數(discrete logarithm). . 由於並沒有已知方法可以讓電腦有效地計算離散對數.
1.A及B 選個人數字.
   A= 8 ; B =9
2. A 及 B 針對二個公開數字達成共識. 分別,時鐘尺寸(11)及基數(2)
3. A及B 各自利用次方記號 及 時鐘算法, 將個人數字和公共數字混合. 創造一個公共個人數字(PPN,public -private number)
PPN=基數^個人數字(時鐘尺寸)
A=2^8 (11) =3. B=2^ 9(11) =6.
4.A和B 個拿對方的PPN,再與自己個人數混合.
   shared secret = another PPN ^個人數(時間吋尺)
   A=6^8 (11) 4 ; B=3^9(11)=4

數位簽章:
任何數位內容理應可被拷貝,但是數位簽章卻可以做到.
這個矛盾在於數位簽章依賴一個只有簽章者知道的祕密及被簽章的訊息,
這個簽章者是用一個祕密(所稱的鎖)來簽章他的每個訊自,但是不同的訊息會有不同的簽章. 因此, 雖然任何人可以拷貝簽章,但是卻無法被移轉到另一個訊息上.
因此,無法完成偽造.
1.首先利用乘法來簽名(multiplicative padlock trick)
  A.準備一把鎖及鑰匙,
     鎖,是由小於時鐘尺吋(11)的任一數位做為數字鎖.
     鑰匙,則由....時尺吋及鎖所算出.(為2)
  B, 5 (訊息) x 6(鎖) mod(時鐘尺吋) = 8
  C.  8 (簽名) x 2(鑰匙) mod (時鐘尺吋) = 5
  *因為個人鎖是不會讓別人知的,但對於時鐘尺吋及鑰匙會公開.
2.取冪次
ex:RSA

2015年5月9日 星期六

about linux driver resource

在撰寫linux driver時,若需要用到其資料(map(iomap,memmap), interrupt ,memory page, and symbol)

這些都可以用來觀察目系統中,對不同的driver所使用的資源有多少.並且可用來看是否有衝突.

keyword:

udev,procfs,interrupt hook,mapping,memory,symbol

1.註冊device,

無論是用register_chrdev() *1, alloc_chrdev_region(),udev(class_create,class_device and class_device_create)*2

均會在出現在 /proc/devices,用來去建立其device inode

Character devices:
  1 mem
  4 /dev/vc/0
  4 tty
  5 /dev/tty
  5 /dev/console
  5 /dev/ptmx
  5 ttyprintk
  7 vcs
 10 misc
Block devices:
  1 ramdisk
259 blkext
  7 loop
  8 sd
 65 sd
 66 sd

其中若是以udev,則需額外有/sys/class/driver class/driver name


提供其驅動所需的規則檔等..



pi@raspberrypi ~/test/driver/gpio_io $ sudo ls /sys/class/
bcm2708_vcio  dma          input             iscsi_session    mmc_host      rtc           sound       uio     vc-sm
bdi           gpio         iscsi_connection  iscsi_transport  net           scsi_device   spidev      vc      vtconsole
block         graphics     iscsi_endpoint    leds             power_supply  scsi_disk     spi_master  vc-cma
bluetooth     hidraw       iscsi_host        mem              raw           scsi_generic  thermal     vchiq
bsg           i2c-adapter  iscsi_iface       misc             rfkill        scsi_host     tty         vc-mem

2. Interrupt Hook


因在嵌入式系統,會用到許多來自內/外的中斷來進行狀態的轉換及操作.


request_irq(),向kernel 去註冊interrup



CPU0
9437   ARMCTRL   3  BCM2708 Timer Tick
   0   ARMCTRL  16  bcm2708_fb dma
4694   ARMCTRL  24  DMA IRQ
0585   ARMCTRL  25  DMA IRQ
7362   ARMCTRL  32  dwc_otg, dwc_otg_pcd, dwc_otg_hcd:usb1
   0   ARMCTRL  49  20200000.gpio:bank0
   0   ARMCTRL  50  20200000.gpio:bank1
2864   ARMCTRL  65  ARM Mailbox IRQ
   2   ARMCTRL  66  VCHIQ doorbell
   1   ARMCTRL  75
 524   ARMCTRL  83  uart-pl011
4012   ARMCTRL  84  mmc0
       usb_fiq
   0

 


3.mapping


又分為io memory mapping and memory mapping.*3


/proc/ioports and /proc/iomem *4



$sudo less /proc/iomem
 
 
00000000-1bffffff : System RAM
  00008000-0078ebe7 : Kernel code
  007e4000-008f27a7 : Kernel data
20000000-20000fff : bcm2708_vcio
20003000-20003fff : bcm2708_systemtimer
20006000-20006fff : bcm2708_usb
  20006000-20006fff : dwc_otg
20007000-20007fff : bcm2708_dma.0
  20007000-20007fff : bcm2708_dma
20100000-201000ff : bcm2708_powerman.0
20200000-202000b3 : /soc/gpio
20201000-20201fff : dev:f1
  20201000-20201fff : uart-pl011
20300000-203000ff : mmc-bcm2835.0
  20300000-203000ff : mmc0
20980000-2099ffff : bcm2708_usb
  20980000-2099ffff : dwc_otg

4.memory (page)


在kernel中,所使用全域變數等也會在kernel中去檢查.像是cache,及page)*5



slabinfo - version: 2.1
# name            <;active_objs> <num_objs> <objsize> <objperslab> <pagesperslab> : tunables <limit> <batchcount> <sharedfactor> : slabdata <active_slabs> <num_slabs> <sharedavail>
isofs_inode_cache      0      0    344   23    2 : tunables    0    0    0 : slabdata      0      0      0
udf_inode_cache        0      0    408   10    1 : tunables    0    0    0 : slabdata      0      0      0
nf_conntrack_c082c640     20     80    248   16    1 : tunables    0    0    0 : slabdata      5      5      0
nf_conntrack_expect      0      0    184   22    1 : tunables    0    0    0 : slabdata      0      0      0
cfq_queue              0      0    152   26    1 : tunables    0    0    0 : slabdata      0      0      0
bsg_cmd                0      0    288   14    1 : tunables    0    0    0 : slabdata      0      0      0
mqueue_inode_cache      8      8    512    8    1 : tunables    0    0    0 : slabdata      1      1      0
nat_entry_set          0      0     24  170    1 : tunables    0    0    0 : slabdata      0      0      0
f2fs_inode_cache       0      0    408   10    1 : tunables    0    0    0 : slabdata      0      0      0
nfs_direct_cache       0      0     96   42    1 : tunables    0    0    0 : slabdata      0      0      0
nfs_commit_data        9      9    448    9    1 : tunables    0    0    0 : slabdata      1      1      0
nfs_read_data         42     42    576   14    2 : tunables    0    0    0 : slabdata      3      3      0
nfs_inode_cache        0      0    616   13    2 : tunables    0    0    0 : slabdata      0      0      0
fat_inode_cache       17     20    392   10    1 : tunables    0    0    0 : slabdata      2      2      0
fat_cache              0      0     24  170    1 : tunables    0    0    0 : slabdata      0      0      0
jbd2_revoke_table_s    256    256     16  256    1 : tunables    0    0    0 : slabdata      1      1      0
ext4_inode_cache   22359  22372    584   14    2 : tunables    0    0    0 : slabdata   1598   1598      0
ext4_allocation_context    156    156    104   39    1 : tunables    0    0    0 : slabdata      4      4      0
ext4_io_end          102    204     40  102    1 : tunables    0    0    0 : slabdata      2      2      0
ext4_extent_status   8064   8064     32  128    1 : tunables    0    0    0 : slabdata     63     63      0
configfs_dir_cache      0      0     56   73    1 : tunables    0    0    0 : slabdata      0      0      0
dquot                 25     50    160   25    1 : tunables    0    0    0 : slabdata      2      2      0
kioctx                 0      0    224   18    1 : tunables    0    0    0 : slabdata      0      0      0
posix_timers_cache     23     23    176   23    1 : tunables    0    0    0 : slabdata      1      1      0
rpc_inode_cache        0      0    352   11    1 : tunables    0    0    0 : slabdata      0      0      0
UNIX                  43     45    544   15    2 : tunables    0    0    0 : slabdata      3      3      0
UDP-Lite               0      0    640   12    2 : tunables    0    0    0 : slabdata      0      0      0
RAW                   13     13    608   13    2 : tunables    0    0    0 : slabdata      1      1      0
UDP                   36     36    640   12    2 : tunables    0    0    0 : slabdata      3      3      0
tw_sock_TCP           25    125    160   25    1 : tunables    0    0    0 : slabdata      5      5      0
TCP                   33     72   1344   12    4 : tunables    0    0    0 : slabdata      6      6      0
cachefiles_object_jar      0      0    192   21    1 : tunables    0    0    0 : slabdata      0      0      0
fscache_cookie_jar     85     85     48   85    1 : tunables    0    0    0 : slabdata      1      1      0
eventpoll_pwq        135    306     40  102    1 : tunables    0    0    0 : slabdata      3      3      0
blkdev_queue          32     32   1016    8    2 : tunables    0    0    0 : slabdata      4      4      0
blkdev_requests       29     85    232   17    1 : tunables    0    0    0 : slabdata      5      5      0
biovec-256            10     10   3072   10    8 : tunables    0    0    0 : slabdata      1      1      0
biovec-128            10     50   1536   10    4 : tunables    0    0    0 : slabdata      5      5      0
biovec-64             10     20    768   10    2 : tunables    0    0    0 : slabdata      2      2      0


Node 0, zone   Normal    506     77      2     27      7      3      2      0      0      0      2

5.symbol


當使用全域變數時,*6



$sudo less /proc/kallsyms
c0008000 T stext
c0008000 T _text
c0008078 t __create_page_tables
c0008124 t __turn_mmu_on_loc
c0008130 t __enable_mmu
c0008160 t __vet_atags
c00081c0 T do_undefinstr
c00081c0 T _stext
c00081c0 T __exception_text_start
c00083bc T do_DataAbort
c0008464 T do_PrefetchAbort
c000850c T __exception_text_end
c0008510 T asm_do_IRQ
c0008510 T __irqentry_text_start
c0008540 T handle_fiq_as_nmi
c00085cc T __irqentry_text_end
c00085d0 t __do_fixup_smp_on_up
c00085e4 T fixup_smp
c00085fc t __lookup_processor_type
c0008634 t __lookup_processor_type_data
c0008640 t __error_lpae
c0008644 t __error
c0008644 t __error_p
c000864c t run_init_process
c0008684 t try_to_run_init_process
c00086d0 T do_one_initcall
c00088b4 t match_dev_by_uuid
c00088f0 t rootfs_mount
c0008970 T name_to_dev_t
c0008d20 t init_linuxrc
c0008de4 T calibrate_delay
c00093a4 t vfp_emulate_instruction.isra.0
c00093f8 t vfp_raise_sigfpe.isra.1
c0009478 t vfp_enable
c00094cc t vfp_raise_exceptions
c0009628 T VFP_bounce
c0009760 T vfp_sync_hwstate
c00097e8 t vfp_notifier
c0009948 T vfp_flush_hwstate
c00099d0 T vfp_preserve_user_clear_hwstate
c0009a6c T vfp_restore_user_hwstate
c0009af0 T vfp_kmode_exception

 


 


*1.should be on linux kernel 2.4.x,not commond for 2.6.x and other


*2.udev




*3.io mapping及memory mapping簡單就是分別為將io視為特殊的位置及存取指令或一則無.


*4.若要使用io/memory mapping時,也需去檢查是否已經有被其它driver申請,否則也會出現衝突.


*5.若由,kmalloc,kfree等為Slab allocator 則為cache為單位.故其實是呼叫kmem_cache_create()'kmem_cache_destory())


而vmalloc(),ioremap()等則是以Buddy System,是以page為單位.





*6:全域變數會在整個驅動之內分享


使用EXPORT_SYMBOL來對外公開

2014年12月14日 星期日

enum container always int

 

keyword:

compiler options,gcc,static lib and linker

在使用外部協力商所給的lib(static lib)出現了..

Cannot link object as its attributes are incompatible with the image attributes

原本,以為是那邊給錯了..所以試著去查了一下它的內容.用gnu Binutils(ar,nm)

看看是那邊有問題,依著Linker Error 可能原因有

a.alignment

b.endian

c.different --fpu

但,看起來這幾項都不應該出現.

除了去信要求協力商再次檢查其lib 外.

而其中,原本是懷疑是c ,即它可能用到float point運算.

A.

但,我還是得先以gcc Binutils(ar and nm)去看一下是不是它原本就有問題.像是給錯或檔案毀壞. 其結果如下.

image

fig . readelf . to check the ABI versiion and endian

image

fig. ar . static lib to check the conatin .

image

fig . nm ; to show the symbol

由上可得出,能排及b (endian)及obj file 毀壞.

而我還是不能得到它的原因.因此

請其它同事以不同的案子去試著linker. 卻是正常的

所以看了一下它的環境,才得出這個答案.enum container always int

B.

在disable 原本在compiler對於enum container always int 設定後.

C陷阱篇之enum默认长度 以這篇所敘述的內容.

"enum是一种数据类型,使用时会检查类型匹配".因此

    enum长度不确定会带来可移植性问题,如果第三方库API接口使用enum类型,编译和调用库时一旦有关enum长度的编译器设置不一致,API接口层对数值的解析就不匹配。比如上层应用编译时没有用-fshort-enums,默认用4字节空间来存储使用enum变量,而编译库时设置了fshort-enums,则库内部此enum size可能为1。当把enum变量地址传进API时,内部只修改变量最低字节,高3字节值无变化(内容随机),API返回时,上层使用的4字节enum变量值就可能随机。(潜规则篇之API接口)

"因此内部代码使用enum类型优于define,但对外API接口尽量避免用enum型。"

C.

那麻,這個問題要如何由gcc Binutils 去檢查?

它是由對外的API 所宣告的enum所造成的,因此首先就是對所有對於的API (nm 中的T)

但我自己建立lib後,卻不論是否有enable enum container always int 其程式也是可以動作??而且以objdump 也沒看到它有去檢查??

D.

最後,在未來建立自己的Lib及使用它人的Lib這些都需要特別注意

C潜规则篇之API接口

http://blog.csdn.net/ipmux/article/details/17309277

ref:

Linker Error: L6242E: Cannot link object <objname> as its attributes are incompatible with the image attributes.

http://infocenter.arm.com/help/topic/com.arm.doc.faqs/ka4223.html

gnu gcc basic

https://docs.google.com/document/d/1rKNprDiocE094tU3xq_waHIN-tHpY_Tuum84pSoTEZE/edit?usp=sharing

http://infocenter.arm.com/help/topic/com.arm.doc.dui0491f/Babjddhe.html

https://answers.launchpad.net/gcc-arm-embedded/+question/224724

http://infocenter.arm.com/help/topic/com.arm.doc.dui0348c/CHDIBFAB.html

http://blog.csdn.net/ipmux/article/details/17334099

2014年10月4日 星期六

Use BTLE Soc with other Vendor BTLE Compatibility issue

在之前介紹該vendor ble SoC 時, 曾提到感覺它的相容性問題.

那時,指的是它在連接時卡卡的(有時連不上)

而事實上,它的連接方式的確有相容性問題.

AMICCOM -BLE SOC A8105

http://stevenlin08book.blogspot.tw/2014/04/amiccom-ble-soc-a8105.html

 

keywork :

BLE SoC, Central mode and LL(link layer)

 

之前,有個案子原本是想要以這顆 加上 另外可支援Central Mode的SoC 應用.

但那時,實際動作後. 發現有問題(可連接.但不能找到A8105的服務)

因為我那時還沒有ble sniffer的設備,查看它通訊內容

雖然有發信給原廠人員詢問但沒得到回應.

在這次在作Central的前期開發時,隨便將該issue列入.並得到初步的結論.

 

1. normal v.s Abnormal

image

Fig 1. Normal , iphone and vendor soc

image

Fig 2.Abnormal, My Central and vendor Ble

 

在上面二MSC(message sequence char)中的不同,

1.告訴對方LL version

2.兩端交換MTU

3.Find By Type value Request

前二項,應不是必要的要求, 但在主要在第3點上的內容

可以看出一方,

find by type value request , device name , handle …..

而另一方則是以

find by type value request ,GATT primary service

 

這是因在Central Mode Ble Soc 定義中,定義了GATT primary service來提供資訊;

而在vendor ble soc, 則並不是由此來提供.

因此, 當Central Mode Ble soc 在連線後, 相vendor ble soc 提出 find the type value request 時,

因它不認得, 所以它也不會去反應. 導致整個系統沒有反應.

 

而另外,我也以第三家(TI CC2541)做為測試

image

Fig 3.the sendor vendor ble soc with iphone .

由這三家的系統中,可它們在第一個 read by (group) type request , 各有些不同

A. type, name

B.type. value ,primary service

C.group type,primary service

所以,在未來對於在整合不同家的系統時.可能需注意到這點.

2014年10月3日 星期五

BLE central throughput

因為下個案子(ble remoter and multi remoter)會用到central mode,所以我分析central mode timing and state

如下(對於peripheral 也可用相似方式,做分析它的BLE data throughput)

 

keyword :

BLE, central, throughput and behavior

 

Central & peripheral,every radio event trigger gpio .

default parameter:( by the central device)

#define SCAN_INTERVAL 0x00A0 /**< Determines scan interval in units of 0.625 millisecond. */
#define SCAN_WINDOW 0x0050 /**< Determines scan window in units of 0.625 millisecond. */

#define MIN_CONNECTION_INTERVAL MSEC_TO_UNITS(30, UNIT_1_25_MS) /**< Determines maximum connection interval in millisecond. */
#define MAX_CONNECTION_INTERVAL MSEC_TO_UNITS(60, UNIT_1_25_MS) /**< Determines maximum connection interval in millisecond. */
#define SLAVE_LATENCY 0 /**< Determines slave latency in counts of connection events. */
#define SUPERVISION_TIMEOUT MSEC_TO_UNITS(4000, UNIT_10_MS) /**< Determines supervision time-out in units of 10 millisecond. */

clip_image002

Fig 1. default parameter radio event(0 – peripheral; 1 – Central,one pulse for notification; Square for Scan windows)

drop package(when peripheral pulse & Central Square )

clip_image004

Fig 2. One Second for Measure drop package(when peripheral pulse & Central Square )

clip_image006

Fig 3. Sniffer @ default parameter

clip_image008

Fig 4.Case 1,Measure the timing(60ms) between package .(reference the Fig 3)

clip_image010

Fig 5.Case 2,Measure the timing(0.11) between package .(reference the Fig 3)

clip_image012

Fig 4.Case 3,Measure the timing(0.12s) between package .(reference the Fig 3)

How to configure the parameters which operation without dropped packets .

1. The minimum Central connectionInterval should be ≥ 18.75 ms + scanWindow.
2. All Central connections should have a connectionInterval which can be factored by the smallest
connection interval. For example [50 ms, 100 ms, 150 ms, 200 ms…] or [75 ms, 150 ms, 225 ms] etc.
3. Scanner, Observer, and Initiator roles should haveintervals which can be factored by the smallest
connection interval (as with the Central role) and the window should be ≤ connectionInterval – 20
ms

According to list . re-configure the central parameters:

#define SCAN_INTERVAL 0x004C /**< Determines scan interval in units of 0.625 millisecond. */
//
#define SCAN_WINDOW 0x6 /**< Determines scan window in units of 0.625 millisecond. */
//10
#define MIN_CONNECTION_INTERVAL MSEC_TO_UNITS(10, UNIT_1_25_MS) /**< Determines maximum connection interval in millisecond. */
#define MAX_CONNECTION_INTERVAL MSEC_TO_UNITS(24, UNIT_1_25_MS) /**< Determines maximum connection interval in millisecond. */
#define SLAVE_LATENCY 0 /**< Determines slave latency in counts of connection events. */
#define SUPERVISION_TIMEOUT MSEC_TO_UNITS(4000, UNIT_10_MS) /**< Determines supervision time-out in units of 10 millisecond. */

clip_image014

Fig 5. no drop package , connect interval ,24ms ; scan interval 48ms(factor connect interval) ; scan size (connect interval – 20ms)

clip_image016

Fig 6. multi peripheral(2)

Ref:

S120 SoftDevice Specification v1.1

Connections action and L2CAP 分析

因在實作BLE Central mode時的筆記, 對於Connection state動作及 L2CAP

並計畫以其它的的RF chip(GFSK) 對實作該動作.(connect interval 及 channel )

(即以master – L2CAP layer)

對於FHSS 無線通訊系統重要參數 如下:

1. connect interval

2. channel increame /number

我藉此來做為初步的分析.

 

Keyword:

Controll , LL(link layer), HCI(host controller interface),

connection parameter and L2CAP(logical layer and Adaptation protocol)

 

A.Init Conection State

剛開始連線時, 它所初始值及所交換的資訊內容. 如下

image

Fig 1. initial connect

一開始,所交換的資訊有Link layer的version,兩端可接收的MTU 及提供att handle的資訊(由master端發出詢間)

可以看得出來,一開始master 所初始化的connect interval 為30ms , 因此它以每隔30ms為一周期通訊.

 

B. Slave setting connect

接下來是去改變其connect interval的參數, 它是由Slave發起. 並由master 反應

(accept 並反應master所設定的connect interval ; reject ,若它不接收時,則會斷線)

image

Fig 2, slave send “Connect parameter update request”,這裡設定其min/max connect interval time.(0.5/1.0 s)

 

image

Fig 3. master 在接收其connect parameter update request後,回應給slave它是由接收及對於connect interval的設定

(需以master 為主).

這裡可以看出它並不是直接反應. 而是在某段時間(由slave設定)後才真正動作.

image

Fig 4.由slave 所設定的時間後動作.

 

C.

Channel setting

它是由master端去設定該連線的channel map及interval .

除了一開始連線時,設定外. master也會去偵測環境等去update channel map

image

Fig 5. init channel map

image

Fig 6. master偵測環境後,可能會去對slave 發出LL_CHANNEL_MPA_REQ

 

D. L2CAP 通訊

在BLE 中, 它的通訊方式可以由 一般經由ATT(GATT)(20 Bytes) 來交換資訊外;

事實上,也可以由L2CAP Layer 來進行. 尤其是大量的資料時(23 Bbytes)

image

Fig 7. BLE stack

image

Fig 8.

由Slave 經由L2CAP 來傳送資料.需注意的是它的通訊時間仍然是要依講好的時間去通訊

同等於ATT 的notification .